diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java index 9d0b22d44..37392fa29 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java @@ -86,6 +86,7 @@ import java.util.Date; import java.util.List; import javax.inject.Inject; import kotlin.Unit; +import kotlin.jvm.functions.Function1; import org.jetbrains.annotations.NotNull; import org.json.JSONArray; import org.kiwix.kiwixmobile.core.BuildConfig; @@ -216,6 +217,8 @@ public abstract class CoreMainActivity extends BaseActivity protected NewBookDao newBookDao; @Inject protected DialogShower alertDialogShower; + @Inject + protected NightModeViewPainter painter; private CountDownTimer hideBackToTopTimer = new CountDownTimer(1200, 1200) { @Override @@ -491,7 +494,7 @@ public abstract class CoreMainActivity extends BaseActivity } private void setupTabsAdapter() { - tabsAdapter = new TabsAdapter(this, webViewList); + tabsAdapter = new TabsAdapter(this, webViewList, painter); tabsAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { @Override public void onChanged() { @@ -1546,6 +1549,11 @@ public abstract class CoreMainActivity extends BaseActivity } } + private void updateNightMode() { + painter.update(getCurrentWebView(), kiwixWebView -> kiwixWebView.getUrl() == null + || !kiwixWebView.getUrl().equals(HOME_URL), videoView); + } + private void loadPrefs() { isBackToTopEnabled = sharedPreferenceUtil.getPrefBackToTop(); isHideToolbar = sharedPreferenceUtil.getPrefHideToolbar(); @@ -1567,14 +1575,6 @@ public abstract class CoreMainActivity extends BaseActivity updateNightMode(); } - private void updateNightMode() { - if (nightModeConfig.isNightModeActive()) { - getCurrentWebView().activateNightMode(); - } else { - getCurrentWebView().deactivateNightMode(); - } - } - private boolean isInFullScreenMode() { return sharedPreferenceUtil.getPrefFullScreen(); } @@ -1724,7 +1724,7 @@ public abstract class CoreMainActivity extends BaseActivity @Override public void setHomePage(View view) { - getCurrentWebView().deactivateNightMode(); + painter.deactivateNightMode(getCurrentWebView(), videoView); RecyclerView homeRecyclerView = view.findViewById(R.id.recycler_view); presenter.loadBooks(); homeRecyclerView.setAdapter(booksAdapter); diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java index 161c25963..c435e4c7a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java @@ -66,7 +66,6 @@ public class KiwixWebView extends VideoEnabledWebView { @Inject ZimReaderContainer zimReaderContainer; private final WebViewCallback callback; - private final Paint invertedPaint = createInvertedPaint(); @SuppressLint("SetJavaScriptEnabled") public KiwixWebView(Context context, WebViewCallback callback, AttributeSet attrs, @@ -109,26 +108,6 @@ public class KiwixWebView extends VideoEnabledWebView { } } - public void deactivateNightMode() { - setLayerType(LAYER_TYPE_NONE, null); - videoView.setLayerType(LAYER_TYPE_NONE, null); - } - - public void activateNightMode() { - if (getUrl() != null && getUrl().equals(HOME_URL)) { - return; - } - setLayerType(LAYER_TYPE_HARDWARE, invertedPaint); - videoView.setLayerType(LAYER_TYPE_HARDWARE, invertedPaint); - } - - @NotNull private Paint createInvertedPaint() { - Paint paint = new Paint(); - ColorMatrixColorFilter filterInvert = new ColorMatrixColorFilter(NIGHT_MODE_COLORS); - paint.setColorFilter(filterInvert); - return paint; - } - @Override public boolean performLongClick() { HitTestResult result = getHitTestResult(); diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/NightModeViewPainter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/NightModeViewPainter.kt new file mode 100644 index 000000000..8324ed43a --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/NightModeViewPainter.kt @@ -0,0 +1,61 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package org.kiwix.kiwixmobile.core.main + +import android.graphics.ColorMatrixColorFilter +import android.graphics.Paint +import android.view.View +import org.kiwix.kiwixmobile.core.NightModeConfig +import javax.inject.Inject + +/** + * NightModeViewPainter class is used to apply respective filters to the views + * depending whether the app is in dark mode or not + * Created by yashk2000 on 24/03/2020. + */ + +class NightModeViewPainter @Inject constructor( + private val nightModeConfig: NightModeConfig +) { + + private val invertedPaint = + Paint().apply { colorFilter = ColorMatrixColorFilter(KiwixWebView.NIGHT_MODE_COLORS) } + + @JvmOverloads + fun update( + view: T, + shouldActivateCriteria: ((T) -> Boolean) = { true }, + vararg additionalViews: View = emptyArray() + ) { + if (nightModeConfig.isNightModeActive()) { + if (shouldActivateCriteria(view)) { + activateNightMode(view, *additionalViews) + } + } else { + deactivateNightMode(view, *additionalViews) + } + } + + fun deactivateNightMode(vararg additionalViews: View) { + additionalViews.forEach { it.setLayerType(View.LAYER_TYPE_NONE, null) } + } + + private fun activateNightMode(vararg additionalViews: View) { + additionalViews.forEach { it.setLayerType(View.LAYER_TYPE_HARDWARE, invertedPaint) } + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java index 345a43763..18866272f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java @@ -45,12 +45,14 @@ import static org.kiwix.kiwixmobile.core.utils.StyleUtils.fromHtml; public class TabsAdapter extends RecyclerView.Adapter { private final List webViews; private final CoreMainActivity activity; + private final NightModeViewPainter painter; private TabClickListener listener; private int selectedPosition = 0; - TabsAdapter(CoreMainActivity activity, List webViews) { + TabsAdapter(CoreMainActivity activity, List webViews, NightModeViewPainter painter) { this.webViews = webViews; this.activity = activity; + this.painter = painter; setHasStableIds(true); } @@ -145,6 +147,9 @@ public class TabsAdapter extends RecyclerView.Adapter { listener.onSelectTab(v, selectedPosition); notifyDataSetChanged(); }); + if (!webViewTitle.equals(activity.getString(R.string.menu_home))) { + painter.update(holder.content); + } } @Override