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:
Seán Mac Gillicuddy 2020-04-02 12:08:52 +01:00 committed by GitHub
commit 740c460406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 32 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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) }
}
}

View File

@ -45,12 +45,14 @@ import static org.kiwix.kiwixmobile.core.utils.StyleUtils.fromHtml;
public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
private final List<KiwixWebView> webViews;
private final CoreMainActivity activity;
private final NightModeViewPainter painter;
private TabClickListener listener;
private int selectedPosition = 0;
TabsAdapter(CoreMainActivity activity, List<KiwixWebView> webViews) {
TabsAdapter(CoreMainActivity activity, List<KiwixWebView> webViews, NightModeViewPainter painter) {
this.webViews = webViews;
this.activity = activity;
this.painter = painter;
setHasStableIds(true);
}
@ -145,6 +147,9 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
listener.onSelectTab(v, selectedPosition);
notifyDataSetChanged();
});
if (!webViewTitle.equals(activity.getString(R.string.menu_home))) {
painter.update(holder.content);
}
}
@Override