Skip to content

Commit

Permalink
适配暗色模式
Browse files Browse the repository at this point in the history
  • Loading branch information
goweii committed Oct 4, 2019
1 parent e3d4e60 commit cb896e2
Show file tree
Hide file tree
Showing 124 changed files with 756 additions and 473 deletions.
17 changes: 17 additions & 0 deletions app/src/main/java/per/goweii/wanandroid/common/WanApp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package per.goweii.wanandroid.common;

import android.app.Activity;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatDelegate;

import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
Expand All @@ -14,6 +16,7 @@
import per.goweii.rxhttp.core.RxHttp;
import per.goweii.wanandroid.http.RxHttpRequestSetting;
import per.goweii.wanandroid.http.WanCache;
import per.goweii.wanandroid.utils.SettingUtils;
import per.goweii.wanandroid.utils.UserUtils;

/**
Expand All @@ -30,6 +33,7 @@ public class WanApp extends BaseApp {
@Override
public void onCreate() {
super.onCreate();
setDarkModeStatus();
RxHttp.init(this);
RxHttp.initRequest(new RxHttpRequestSetting(getCookieJar()));
WanCache.init();
Expand All @@ -43,6 +47,19 @@ public void onResult(Activity data) {
Realm.init(this);
}

public static boolean getDarkModeStatus() {
int mode = getAppContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return mode == Configuration.UI_MODE_NIGHT_YES;
}

public static void setDarkModeStatus() {
if (SettingUtils.getInstance().isDarkTheme()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}

public static PersistentCookieJar getCookieJar() {
if (mCookieJar == null) {
mCookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getAppContext()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,16 @@ public void getTopArticleListFailed(int code, String msg) {
public void getConfigSuccess(ConfigBean configBean) {
if (configBean.getHomeTitle() != null) {
if (mConfigBean == null || !TextUtils.equals(mConfigBean.getHomeTitle(), configBean.getHomeTitle())) {
abc.getTitleTextView().setText(configBean.getHomeTitle());
if (configBean.getHomeTitle() == null) {
abc.getTitleTextView().setText("首页");
} else {
abc.getTitleTextView().setText(configBean.getHomeTitle());
}
}
}
if (TextUtils.isEmpty(configBean.getActionBarBgImageUrl())) {
if (TextUtils.isEmpty(configBean.getActionBarBgColor())) {
abc.setBackgroundResource(R.color.main);
abc.setBackgroundResource(R.color.basic_ui_action_bar_bg);
} else {
if (mConfigBean == null || !TextUtils.equals(mConfigBean.getActionBarBgColor(), configBean.getActionBarBgColor())) {
try {
Expand All @@ -605,7 +609,7 @@ public void getConfigSuccess(ConfigBean configBean) {
}
} else {
if (TextUtils.isEmpty(configBean.getActionBarBgColor())) {
abc.setBackgroundResource(R.color.main);
abc.setBackgroundResource(R.color.basic_ui_action_bar_bg);
} else {
if (mConfigBean == null || !TextUtils.equals(mConfigBean.getActionBarBgColor(), configBean.getActionBarBgColor())) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -50,6 +51,11 @@ public static void start(Context context) {
Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
if (context instanceof Activity) {
Activity activity = (Activity) context;
activity.overridePendingTransition(R.anim.swipeback_activity_open_bottom_in,
R.anim.swipeback_activity_open_top_out);
}
}

@Override
Expand Down Expand Up @@ -95,6 +101,18 @@ protected void onStop() {
super.onStop();
}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.swipeback_activity_close_top_in,
R.anim.swipeback_activity_close_bottom_out);
}

public RelativeLayout getRl_input() {
return rl_input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
Expand Down Expand Up @@ -166,28 +167,28 @@ public void onPageScrolled(int i, float v, int i1) {

@Override
public void onPageSelected(int i) {
iv_bb_home.setImageResource(R.drawable.ic_home_normal);
iv_bb_knowledge.setImageResource(R.drawable.ic_book_normal);
iv_bb_wechat.setImageResource(R.drawable.ic_wechat_normal);
iv_bb_project.setImageResource(R.drawable.ic_project_normal);
iv_bb_mine.setImageResource(R.drawable.ic_mine_normal);
iv_bb_home.setColorFilter(ContextCompat.getColor(getContext(), R.color.third));
iv_bb_knowledge.setColorFilter(ContextCompat.getColor(getContext(), R.color.third));
iv_bb_wechat.setColorFilter(ContextCompat.getColor(getContext(), R.color.third));
iv_bb_project.setColorFilter(ContextCompat.getColor(getContext(), R.color.third));
iv_bb_mine.setColorFilter(ContextCompat.getColor(getContext(), R.color.third));
switch (i) {
default:
break;
case 0:
iv_bb_home.setImageResource(R.drawable.ic_home_selected);
iv_bb_home.setColorFilter(ContextCompat.getColor(getContext(), R.color.main));
break;
case 1:
iv_bb_knowledge.setImageResource(R.drawable.ic_book_selected);
iv_bb_knowledge.setColorFilter(ContextCompat.getColor(getContext(), R.color.main));
break;
case 2:
iv_bb_wechat.setImageResource(R.drawable.ic_wechat_selected);
iv_bb_wechat.setColorFilter(ContextCompat.getColor(getContext(), R.color.main));
break;
case 3:
iv_bb_project.setImageResource(R.drawable.ic_project_selected);
iv_bb_project.setColorFilter(ContextCompat.getColor(getContext(), R.color.main));
break;
case 4:
iv_bb_mine.setImageResource(R.drawable.ic_mine_selected);
iv_bb_mine.setColorFilter(ContextCompat.getColor(getContext(), R.color.main));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import per.goweii.basic.utils.listener.SimpleCallback;
import per.goweii.rxhttp.request.base.BaseBean;
import per.goweii.wanandroid.R;
import per.goweii.wanandroid.common.WanApp;
import per.goweii.wanandroid.event.LoginEvent;
import per.goweii.wanandroid.event.SettingChangeEvent;
import per.goweii.wanandroid.module.main.dialog.DownloadDialog;
Expand All @@ -51,6 +52,8 @@ public class SettingActivity extends BaseActivity<SettingPresenter> implements S

private static final int REQ_CODE_PERMISSION = 1;

@BindView(R.id.sc_dark_theme)
SwitchCompat sc_dark_theme;
@BindView(R.id.sc_show_read_later)
SwitchCompat sc_show_read_later;
@BindView(R.id.sc_show_top)
Expand Down Expand Up @@ -78,6 +81,7 @@ public class SettingActivity extends BaseActivity<SettingPresenter> implements S

private RuntimeRequester mRuntimeRequester;
private UpdateUtils mUpdateUtils;
private boolean mDarkTheme;
private boolean mShowTop;
private boolean mShowBanner;
private boolean mShowReadLater;
Expand Down Expand Up @@ -106,6 +110,8 @@ protected SettingPresenter initPresenter() {
protected void initView() {
tv_has_update.setText("");
tv_curr_version.setText("当前版本" + AppInfoUtils.getVersionName());
mDarkTheme = SettingUtils.getInstance().isDarkTheme();
sc_dark_theme.setChecked(mDarkTheme);
mShowTop = SettingUtils.getInstance().isShowTop();
sc_show_top.setChecked(mShowTop);
mShowBanner = SettingUtils.getInstance().isShowBanner();
Expand All @@ -121,6 +127,18 @@ protected void initView() {
tv_rv_anim.setText(RvAnimUtils.getName(mRvAnim));
mUrlIntercept = SettingUtils.getInstance().getUrlInterceptType();
tv_intercept_host.setText(WebUrlInterceptUtils.getName(mUrlIntercept));
sc_dark_theme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
SettingUtils.getInstance().setDarkTheme(isChecked);
v.postDelayed(new Runnable() {
@Override
public void run() {
WanApp.killProcess();
}
}, 300);
}
});
sc_show_top.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
Expand Down Expand Up @@ -295,7 +313,7 @@ public void updateSuccess(int code, UpdateBean data, boolean click) {
tv_has_update.setTextColor(ResUtils.getColor(R.color.text_main));
tv_has_update.setText("发现新版本");
} else {
tv_has_update.setTextColor(ResUtils.getColor(R.color.text_gray_light));
tv_has_update.setTextColor(ResUtils.getColor(R.color.text_third));
tv_has_update.setText("已是最新版");
}
if (!click) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ protected void convert(BaseViewHolder helper, CoinInfoBean item) {
TextView tv_index = helper.getView(R.id.tv_index);
if (index == 1) {
iv_index.setImageResource(R.drawable.ic_rank_1);
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_white_alpha));
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_surface_alpha));
tv_index.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv_index.getContext().getResources().getDimension(R.dimen.text_auxiliary));
} else if (index == 2) {
iv_index.setImageResource(R.drawable.ic_rank_2);
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_white_alpha));
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_surface_alpha));
tv_index.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv_index.getContext().getResources().getDimension(R.dimen.text_auxiliary));
} else if (index == 3) {
iv_index.setImageResource(R.drawable.ic_rank_3);
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_white_alpha));
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_surface_alpha));
tv_index.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv_index.getContext().getResources().getDimension(R.dimen.text_auxiliary));
} else {
iv_index.setImageResource(R.color.transparent);
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_gray_light));
tv_index.setTextColor(ContextCompat.getColor(tv_index.getContext(), R.color.text_second));
tv_index.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv_index.getContext().getResources().getDimension(R.dimen.text_content));
}
}
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/per/goweii/wanandroid/utils/AgentWebCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
Expand All @@ -28,6 +29,7 @@
import per.goweii.basic.utils.ResUtils;
import per.goweii.wanandroid.R;
import per.goweii.wanandroid.common.WanApp;
import per.goweii.wanandroid.widget.WebContainer;

/**
* @author CuiZhen
Expand All @@ -39,11 +41,20 @@
public class AgentWebCreator {

public static AgentWeb create(Activity activity,
FrameLayout container,
WebContainer container,
String url,
final ClientCallback clientCallback) {
FrameLayout webContainer = container;
if (SettingUtils.getInstance().isDarkTheme()) {
webContainer = new FrameLayout(container.getContext());
container.addView(webContainer, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
View view = new View(container.getContext());
view.setBackgroundResource(R.color.background);
view.setAlpha(0.6F);
container.addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
AgentWeb agentWeb = AgentWeb.with(activity)
.setAgentWebParent(container, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
.setAgentWebParent(webContainer, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
.useDefaultIndicator(ResUtils.getColor(R.color.assist), 1)
.interceptUnkownUrl()
.setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
Expand All @@ -66,7 +77,7 @@ public static AgentWeb create(Activity activity,
return agentWeb;
}

public static AgentWeb create(Activity activity, FrameLayout container, String url) {
public static AgentWeb create(Activity activity, WebContainer container, String url) {
return create(activity, container, url, null);
}

Expand Down Expand Up @@ -104,6 +115,7 @@ private static void syncCookiesForWanAndroid(String url) {
}
}


public interface ClientCallback {
void onReceivedUrl(String url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public IPagerTitleView getTitleView(Context context, final int index) {
SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
simplePagerTitleView.setText(Html.fromHtml(adapter.getPageTitle(index).toString()));
simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtils.getDimens(R.dimen.title_bar_text_size_def));
simplePagerTitleView.setSelectedColor(ResUtils.getColor(R.color.text_white));
simplePagerTitleView.setNormalColor(ResUtils.getColor(R.color.text_white_alpha));
simplePagerTitleView.setSelectedColor(ResUtils.getColor(R.color.basic_ui_action_bar_text));
simplePagerTitleView.setNormalColor(ResUtils.getColor(R.color.basic_ui_action_bar_text_alpha));
int paddingH = (int) DisplayInfoUtils.getInstance().dp2px(8);
int paddingV = (int) DisplayInfoUtils.getInstance().dp2px(5);
simplePagerTitleView.setPadding(paddingH, paddingV, paddingH, paddingV);
Expand All @@ -69,11 +69,6 @@ public void onClick(View v) {

@Override
public IPagerIndicator getIndicator(Context context) {
// WrapPagerIndicator indicator = new WrapPagerIndicator(context);
// indicator.setFillColor(ResUtils.getColor(R.color.black_alpha));
// indicator.setVerticalPadding((int) DisplayInfoUtils.getInstance().dp2px(5));
// indicator.setHorizontalPadding((int) DisplayInfoUtils.getInstance().dp2px(15));
// return indicator;
return null;
}
});
Expand Down Expand Up @@ -101,8 +96,8 @@ public IPagerTitleView getTitleView(Context context, final int index) {
SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
simplePagerTitleView.setText(Html.fromHtml(adapter.getPageTitle(index).toString()));
simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtils.getDimens(R.dimen.title_bar_title_text_size_def));
simplePagerTitleView.setSelectedColor(ResUtils.getColor(R.color.text_white));
simplePagerTitleView.setNormalColor(ResUtils.getColor(R.color.text_white_alpha));
simplePagerTitleView.setSelectedColor(ResUtils.getColor(R.color.basic_ui_action_bar_text));
simplePagerTitleView.setNormalColor(ResUtils.getColor(R.color.basic_ui_action_bar_text_alpha));
int paddingH = (int) DisplayInfoUtils.getInstance().dp2px(8);
int paddingV = (int) DisplayInfoUtils.getInstance().dp2px(5);
simplePagerTitleView.setPadding(paddingH, paddingV, paddingH, paddingV);
Expand All @@ -120,15 +115,6 @@ public void onClick(View v) {

@Override
public IPagerIndicator getIndicator(Context context) {
// LinePagerIndicator indicator = new LinePagerIndicator(context);
// indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
// indicator.setLineHeight(UIUtil.dip2px(context, 3));
// indicator.setLineWidth(UIUtil.dip2px(context, 16));
// indicator.setRoundRadius(UIUtil.dip2px(context, 1.5));
// indicator.setStartInterpolator(new AccelerateInterpolator());
// indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
// indicator.setColors(ResUtils.getColor(R.color.white));
// return indicator;
return null;
}
});
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/per/goweii/wanandroid/utils/SettingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class SettingUtils {

private static final String SP_NAME = "setting";
private static final String KEY_DARK_THEME = "KEY_DARK_THEME";
private static final String KEY_SHOW_READ_LATER = "KEY_SHOW_READ_LATER";
private static final String KEY_SHOW_TOP = "KEY_SHOW_TOP";
private static final String KEY_SHOW_BANNER = "KEY_SHOW_BANNER";
Expand All @@ -25,6 +26,7 @@ public class SettingUtils {

private final SPUtils mSPUtils = SPUtils.newInstance(SP_NAME);

private boolean mDarkTheme = false;
private boolean mShowReadLater = true;
private boolean mShowTop = true;
private boolean mShowBanner = true;
Expand All @@ -45,6 +47,7 @@ public static SettingUtils getInstance() {
}

private SettingUtils() {
mDarkTheme = mSPUtils.get(KEY_DARK_THEME, mDarkTheme);
mShowReadLater = mSPUtils.get(KEY_SHOW_READ_LATER, mShowReadLater);
mShowTop = mSPUtils.get(KEY_SHOW_TOP, mShowTop);
mShowBanner = mSPUtils.get(KEY_SHOW_BANNER, mShowBanner);
Expand All @@ -57,6 +60,15 @@ private SettingUtils() {
mUpdateIgnoreDuration = mSPUtils.get(KEY_UPDATE_IGNORE_DURATION, mUpdateIgnoreDuration);
}

public void setDarkTheme(boolean darkTheme) {
mDarkTheme = darkTheme;
mSPUtils.save(KEY_DARK_THEME, darkTheme);
}

public boolean isDarkTheme() {
return mDarkTheme;
}

public void setShowReadLater(boolean showReadLater) {
mShowReadLater = showReadLater;
mSPUtils.save(KEY_SHOW_READ_LATER, showReadLater);
Expand Down
Loading

0 comments on commit cb896e2

Please sign in to comment.