1 package com.folioreader.view;
3 import android.animation.Animator;
4 import android.animation.ArgbEvaluator;
5 import android.animation.ValueAnimator;
6 import android.app.Activity;
7 import android.graphics.drawable.Drawable;
8 import android.os.Bundle;
9 import android.support.annotation.Nullable;
10 import android.support.design.widget.BottomSheetBehavior;
11 import android.support.design.widget.BottomSheetDialog;
12 import android.support.design.widget.BottomSheetDialogFragment;
13 import android.support.design.widget.CoordinatorLayout;
14 import android.support.v4.content.ContextCompat;
15 import android.support.v7.widget.Toolbar;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.view.ViewTreeObserver;
20 import android.widget.FrameLayout;
21 import android.widget.ImageView;
22 import android.widget.RelativeLayout;
23 import android.widget.SeekBar;
25 import com.folioreader.Config;
26 import com.folioreader.Constants;
27 import com.folioreader.R;
28 import com.folioreader.model.event.BusOwner;
29 import com.folioreader.model.event.ReloadDataEvent;
30 import com.folioreader.ui.folio.activity.ToolbarUtils;
31 import com.folioreader.util.AppUtil;
32 import com.folioreader.util.UiUtil;
36 * Created by mobisys2 on 11/16/2016.
39 public class ConfigBottomSheetDialogFragment extends BottomSheetDialogFragment implements View.OnClickListener {
41 public static final int DAY_BUTTON = 30;
42 public static final int NIGHT_BUTTON = 31;
43 private static final int FADE_DAY_NIGHT_MODE = 500;
45 private CoordinatorLayout.Behavior mBehavior;
46 private boolean mIsNightMode = false;
49 private RelativeLayout mContainer;
50 private ImageView mDayButton;
51 private ImageView mNightButton;
52 private SeekBar mFontSizeSeekBar;
53 private SeekBar mMarginSizeSeekBar;
54 private SeekBar mInterlineSizeSeekBar;
55 private View mDialogView;
56 private ConfigDialogCallback mConfigDialogCallback;
57 private Config mConfig;
59 public interface ConfigDialogCallback {
60 void onOrientationChange(int orentation);
65 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
66 return inflater.inflate(R.layout.view_config, container);
70 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
71 super.onViewCreated(view, savedInstanceState);
72 view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
74 public void onGlobalLayout() {
75 BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
76 FrameLayout bottomSheet = (FrameLayout)
77 dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
78 BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
79 behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
80 behavior.setPeekHeight(0);
85 mConfig = AppUtil.getSavedConfig(getActivity());
90 public void onDestroy() {
92 mDialogView.getViewTreeObserver().addOnGlobalLayoutListener(null);
95 private void initViews() {
98 mFontSizeSeekBar.setProgress(mConfig.getFontSize());
99 mMarginSizeSeekBar.setProgress(mConfig.getMarginSize());
100 mInterlineSizeSeekBar.setProgress(mConfig.getInterlineSize());
102 selectFont(mConfig.getFont(), false);
103 mIsNightMode = mConfig.isNightMode();
106 mContainer.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.night));
107 mDayButton.setSelected(false);
108 mNightButton.setSelected(true);
109 UiUtil.setColorToImage(getActivity(), mConfig.getThemeColor(), mNightButton.getDrawable());
110 UiUtil.setColorToImage(getActivity(), R.color.app_gray, mDayButton.getDrawable());
112 mContainer.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));
113 mDayButton.setSelected(true);
114 mNightButton.setSelected(false);
115 UiUtil.setColorToImage(getActivity(), mConfig.getThemeColor(), mDayButton.getDrawable());
116 UiUtil.setColorToImage(getActivity(), R.color.app_gray, mNightButton.getDrawable());
119 mConfigDialogCallback = (ConfigDialogCallback) getActivity();
122 private void inflateView() {
123 mContainer = (RelativeLayout) mDialogView.findViewById(R.id.container);
124 mFontSizeSeekBar = (SeekBar) mDialogView.findViewById(R.id.seekbar_font_size);
125 mMarginSizeSeekBar = mDialogView.findViewById(R.id.seekbar_margin_size);
126 mInterlineSizeSeekBar = mDialogView.findViewById(R.id.seekbar_interline_size);
127 mDayButton = (ImageView) mDialogView.findViewById(R.id.day_button);
128 mNightButton = (ImageView) mDialogView.findViewById(R.id.night_button);
129 mDayButton.setTag(DAY_BUTTON);
130 mNightButton.setTag(NIGHT_BUTTON);
131 mDayButton.setOnClickListener(this);
132 mNightButton.setOnClickListener(this);
136 private void configFonts() {
137 ((StyleableTextView) mDialogView.findViewById(R.id.btn_font_ebgaramond)).setTextColor(UiUtil.getColorList(getActivity(), mConfig.getThemeColor(), R.color.grey_color));
138 ((StyleableTextView) mDialogView.findViewById(R.id.btn_font_lato)).setTextColor(UiUtil.getColorList(getActivity(), mConfig.getThemeColor(), R.color.grey_color));
139 ((StyleableTextView) mDialogView.findViewById(R.id.btn_font_lora)).setTextColor(UiUtil.getColorList(getActivity(), mConfig.getThemeColor(), R.color.grey_color));
140 ((StyleableTextView) mDialogView.findViewById(R.id.btn_font_raleway)).setTextColor(UiUtil.getColorList(getActivity(), mConfig.getThemeColor(), R.color.grey_color));
141 mDialogView.findViewById(R.id.btn_font_ebgaramond).setOnClickListener(new View.OnClickListener() {
143 public void onClick(View v) {
144 selectFont(Constants.FONT_EBGARAMOND, true);
148 mDialogView.findViewById(R.id.btn_font_lato).setOnClickListener(new View.OnClickListener() {
150 public void onClick(View v) {
151 selectFont(Constants.FONT_LATO, true);
155 mDialogView.findViewById(R.id.btn_font_lora).setOnClickListener(new View.OnClickListener() {
157 public void onClick(View v) {
158 selectFont(Constants.FONT_LORA, true);
162 mDialogView.findViewById(R.id.btn_font_raleway).setOnClickListener(new View.OnClickListener() {
164 public void onClick(View v) {
165 selectFont(Constants.FONT_RALEWAY, true);
170 private void selectFont(int selectedFont, boolean isReloadNeeded) {
171 if (selectedFont == Constants.FONT_EBGARAMOND) {
172 mDialogView.findViewById(R.id.btn_font_ebgaramond).setSelected(true);
173 mDialogView.findViewById(R.id.btn_font_lato).setSelected(false);
174 mDialogView.findViewById(R.id.btn_font_lora).setSelected(false);
175 mDialogView.findViewById(R.id.btn_font_raleway).setSelected(false);
176 } else if (selectedFont == Constants.FONT_LATO) {
177 mDialogView.findViewById(R.id.btn_font_ebgaramond).setSelected(false);
178 mDialogView.findViewById(R.id.btn_font_lato).setSelected(true);
179 mDialogView.findViewById(R.id.btn_font_lora).setSelected(false);
180 mDialogView.findViewById(R.id.btn_font_raleway).setSelected(false);
181 } else if (selectedFont == Constants.FONT_LORA) {
182 mDialogView.findViewById(R.id.btn_font_ebgaramond).setSelected(false);
183 mDialogView.findViewById(R.id.btn_font_lato).setSelected(false);
184 mDialogView.findViewById(R.id.btn_font_lora).setSelected(true);
185 mDialogView.findViewById(R.id.btn_font_raleway).setSelected(false);
186 } else if (selectedFont == Constants.FONT_RALEWAY) {
187 mDialogView.findViewById(R.id.btn_font_ebgaramond).setSelected(false);
188 mDialogView.findViewById(R.id.btn_font_lato).setSelected(false);
189 mDialogView.findViewById(R.id.btn_font_lora).setSelected(false);
190 mDialogView.findViewById(R.id.btn_font_raleway).setSelected(true);
193 mConfig.setFont(selectedFont);
194 //if (mConfigDialogCallback != null) mConfigDialogCallback.onConfigChange();
195 if (isAdded() && isReloadNeeded) {
196 AppUtil.saveConfig(getActivity(),mConfig);
198 Activity activity = getActivity();
199 if (activity instanceof BusOwner)
200 ((BusOwner) activity).getBus().post(new ReloadDataEvent());
204 private void toggleBlackTheme() {
206 int day = getResources().getColor(R.color.white);
207 int night = getResources().getColor(R.color.night);
208 int darkNight = getResources().getColor(R.color.dark_night);
209 final int diffNightDark = night - darkNight;
211 ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(),
212 mIsNightMode ? day : night, mIsNightMode ? night : day);
213 colorAnimation.setDuration(FADE_DAY_NIGHT_MODE);
214 colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
217 public void onAnimationUpdate(ValueAnimator animator) {
218 int value = (int) animator.getAnimatedValue();
219 mContainer.setBackgroundColor(value);
223 colorAnimation.addListener(new Animator.AnimatorListener() {
225 public void onAnimationStart(Animator animator) {
229 public void onAnimationEnd(Animator animator) {
230 // mIsNightMode = !mIsNightMode;
231 mConfig.setNightMode(mIsNightMode);
232 AppUtil.saveConfig(getActivity(),mConfig);
234 Activity activity = getActivity();
235 if (activity instanceof BusOwner)
236 ((BusOwner) activity).getBus().post(new ReloadDataEvent());
238 ///mConfigDialogCallback.onConfigChange();
242 public void onAnimationCancel(Animator animator) {
246 public void onAnimationRepeat(Animator animator) {
250 colorAnimation.setDuration(FADE_DAY_NIGHT_MODE);
251 colorAnimation.start();
254 private void configSeekBars() {
255 setupSeekBarThumb(mFontSizeSeekBar);
256 setupSeekBarThumb(mMarginSizeSeekBar);
257 setupSeekBarThumb(mInterlineSizeSeekBar);
259 mFontSizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
261 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
262 mConfig.setFontSize(progress);
263 saveConfigAndNotifyBusOwner();
267 public void onStartTrackingTouch(SeekBar seekBar) {
271 public void onStopTrackingTouch(SeekBar seekBar) {
274 mMarginSizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
276 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
277 mConfig.setMarginSize(progress);
278 saveConfigAndNotifyBusOwner();
282 public void onStartTrackingTouch(SeekBar seekBar) {
286 public void onStopTrackingTouch(SeekBar seekBar) {
289 mInterlineSizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
291 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
292 mConfig.setInterlineSize(progress);
293 saveConfigAndNotifyBusOwner();
297 public void onStartTrackingTouch(SeekBar seekBar) {
301 public void onStopTrackingTouch(SeekBar seekBar) {
306 private void setupSeekBarThumb(SeekBar seekBar) {
307 Drawable thumbDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.seekbar_thumb);
308 UiUtil.setColorToImage(getActivity(), mConfig.getThemeColor(), (thumbDrawable));
309 UiUtil.setColorToImage(getActivity(), mConfig.getThemeColor(), seekBar.getProgressDrawable());
310 seekBar.setThumb(thumbDrawable);
313 private void saveConfigAndNotifyBusOwner() {
314 AppUtil.saveConfig(getActivity(),mConfig);
315 Activity activity = getActivity();
316 if (activity instanceof BusOwner) {
317 ((BusOwner) activity).getBus().post(new ReloadDataEvent());
323 public void onClick(View v) {
324 switch (((Integer) v.getTag())) {
326 mIsNightMode = false;
328 mDayButton.setSelected(true);
329 mNightButton.setSelected(false);
331 setAudioPlayerBackground();
332 UiUtil.setColorToImage(getActivity(), R.color.app_gray, mNightButton.getDrawable());
333 UiUtil.setColorToImage(getActivity(), mConfig.getThemeColor(), mDayButton.getDrawable());
338 mDayButton.setSelected(false);
339 mNightButton.setSelected(true);
340 UiUtil.setColorToImage(getActivity(), mConfig.getThemeColor(), mNightButton.getDrawable());
341 UiUtil.setColorToImage(getActivity(), R.color.app_gray, mDayButton.getDrawable());
343 setAudioPlayerBackground();
350 private void setToolBarColor() {
351 Toolbar toolbar = ((Activity) getContext()).findViewById(R.id.toolbar);
352 ToolbarUtils.updateToolbarColors(getContext(), toolbar, mConfig, mIsNightMode);
355 private void setAudioPlayerBackground() {
357 ((Activity) getContext()).
358 findViewById(R.id.container).
359 setBackgroundColor(ContextCompat.getColor(getContext(), R.color.night));
361 ((Activity) getContext()).
362 findViewById(R.id.container).
363 setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));