mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-09 07:16:04 -04:00
Merge pull request #1944 from yashk2000/feature/yashk2000/1823-update-tab-color-in-night-mode
Fix #1823 Change tab theme in light and dark mode
This commit is contained in:
commit
740c460406
@ -86,6 +86,7 @@ import java.util.Date;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import kotlin.Unit;
|
import kotlin.Unit;
|
||||||
|
import kotlin.jvm.functions.Function1;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.kiwix.kiwixmobile.core.BuildConfig;
|
import org.kiwix.kiwixmobile.core.BuildConfig;
|
||||||
@ -216,6 +217,8 @@ public abstract class CoreMainActivity extends BaseActivity
|
|||||||
protected NewBookDao newBookDao;
|
protected NewBookDao newBookDao;
|
||||||
@Inject
|
@Inject
|
||||||
protected DialogShower alertDialogShower;
|
protected DialogShower alertDialogShower;
|
||||||
|
@Inject
|
||||||
|
protected NightModeViewPainter painter;
|
||||||
|
|
||||||
private CountDownTimer hideBackToTopTimer = new CountDownTimer(1200, 1200) {
|
private CountDownTimer hideBackToTopTimer = new CountDownTimer(1200, 1200) {
|
||||||
@Override
|
@Override
|
||||||
@ -491,7 +494,7 @@ public abstract class CoreMainActivity extends BaseActivity
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setupTabsAdapter() {
|
private void setupTabsAdapter() {
|
||||||
tabsAdapter = new TabsAdapter(this, webViewList);
|
tabsAdapter = new TabsAdapter(this, webViewList, painter);
|
||||||
tabsAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
|
tabsAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
|
||||||
@Override
|
@Override
|
||||||
public void onChanged() {
|
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() {
|
private void loadPrefs() {
|
||||||
isBackToTopEnabled = sharedPreferenceUtil.getPrefBackToTop();
|
isBackToTopEnabled = sharedPreferenceUtil.getPrefBackToTop();
|
||||||
isHideToolbar = sharedPreferenceUtil.getPrefHideToolbar();
|
isHideToolbar = sharedPreferenceUtil.getPrefHideToolbar();
|
||||||
@ -1567,14 +1575,6 @@ public abstract class CoreMainActivity extends BaseActivity
|
|||||||
updateNightMode();
|
updateNightMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateNightMode() {
|
|
||||||
if (nightModeConfig.isNightModeActive()) {
|
|
||||||
getCurrentWebView().activateNightMode();
|
|
||||||
} else {
|
|
||||||
getCurrentWebView().deactivateNightMode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isInFullScreenMode() {
|
private boolean isInFullScreenMode() {
|
||||||
return sharedPreferenceUtil.getPrefFullScreen();
|
return sharedPreferenceUtil.getPrefFullScreen();
|
||||||
}
|
}
|
||||||
@ -1724,7 +1724,7 @@ public abstract class CoreMainActivity extends BaseActivity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setHomePage(View view) {
|
public void setHomePage(View view) {
|
||||||
getCurrentWebView().deactivateNightMode();
|
painter.deactivateNightMode(getCurrentWebView(), videoView);
|
||||||
RecyclerView homeRecyclerView = view.findViewById(R.id.recycler_view);
|
RecyclerView homeRecyclerView = view.findViewById(R.id.recycler_view);
|
||||||
presenter.loadBooks();
|
presenter.loadBooks();
|
||||||
homeRecyclerView.setAdapter(booksAdapter);
|
homeRecyclerView.setAdapter(booksAdapter);
|
||||||
|
@ -66,7 +66,6 @@ public class KiwixWebView extends VideoEnabledWebView {
|
|||||||
@Inject
|
@Inject
|
||||||
ZimReaderContainer zimReaderContainer;
|
ZimReaderContainer zimReaderContainer;
|
||||||
private final WebViewCallback callback;
|
private final WebViewCallback callback;
|
||||||
private final Paint invertedPaint = createInvertedPaint();
|
|
||||||
|
|
||||||
@SuppressLint("SetJavaScriptEnabled")
|
@SuppressLint("SetJavaScriptEnabled")
|
||||||
public KiwixWebView(Context context, WebViewCallback callback, AttributeSet attrs,
|
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
|
@Override
|
||||||
public boolean performLongClick() {
|
public boolean performLongClick() {
|
||||||
HitTestResult result = getHitTestResult();
|
HitTestResult result = getHitTestResult();
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2020 Kiwix <android.kiwix.org>
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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 <T : View> 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) }
|
||||||
|
}
|
||||||
|
}
|
@ -45,12 +45,14 @@ import static org.kiwix.kiwixmobile.core.utils.StyleUtils.fromHtml;
|
|||||||
public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
|
public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
|
||||||
private final List<KiwixWebView> webViews;
|
private final List<KiwixWebView> webViews;
|
||||||
private final CoreMainActivity activity;
|
private final CoreMainActivity activity;
|
||||||
|
private final NightModeViewPainter painter;
|
||||||
private TabClickListener listener;
|
private TabClickListener listener;
|
||||||
private int selectedPosition = 0;
|
private int selectedPosition = 0;
|
||||||
|
|
||||||
TabsAdapter(CoreMainActivity activity, List<KiwixWebView> webViews) {
|
TabsAdapter(CoreMainActivity activity, List<KiwixWebView> webViews, NightModeViewPainter painter) {
|
||||||
this.webViews = webViews;
|
this.webViews = webViews;
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
|
this.painter = painter;
|
||||||
setHasStableIds(true);
|
setHasStableIds(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,6 +147,9 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
|
|||||||
listener.onSelectTab(v, selectedPosition);
|
listener.onSelectTab(v, selectedPosition);
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
});
|
});
|
||||||
|
if (!webViewTitle.equals(activity.getString(R.string.menu_home))) {
|
||||||
|
painter.update(holder.content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user