This repository has been archived by the owner on Dec 8, 2019. It is now read-only.
/
Aesthetic.kt
844 lines (747 loc) · 25 KB
/
Aesthetic.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
* Licensed under Apache-2.0
*
* Designed and developed by Aidan Follestad (@afollestad)
*/
@file:Suppress("unused")
package com.afollestad.aesthetic
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.support.annotation.CheckResult
import android.support.annotation.ColorInt
import android.support.annotation.ColorRes
import android.support.annotation.StyleRes
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.drawerlayout.widget.DrawerLayout
import com.afollestad.aesthetic.actions.ViewBackgroundSubscriber
import com.afollestad.aesthetic.internal.PrefNames.KEY_ACCENT_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_ACTIVITY_THEME
import com.afollestad.aesthetic.internal.PrefNames.KEY_BOTTOM_NAV_BG_MODE
import com.afollestad.aesthetic.internal.PrefNames.KEY_BOTTOM_NAV_ICONTEXT_MODE
import com.afollestad.aesthetic.internal.PrefNames.KEY_CARD_VIEW_BG_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_FIRST_TIME
import com.afollestad.aesthetic.internal.PrefNames.KEY_ICON_TITLE_ACTIVE_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_ICON_TITLE_INACTIVE_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_IS_DARK
import com.afollestad.aesthetic.internal.PrefNames.KEY_LIGHT_STATUS_MODE
import com.afollestad.aesthetic.internal.PrefNames.KEY_NAV_BAR_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_NAV_VIEW_MODE
import com.afollestad.aesthetic.internal.PrefNames.KEY_PRIMARY_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_PRIMARY_DARK_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_PRIMARY_TEXT_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_PRIMARY_TEXT_INVERSE_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_SECONDARY_TEXT_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_SECONDARY_TEXT_INVERSE_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_SNACKBAR_ACTION_TEXT
import com.afollestad.aesthetic.internal.PrefNames.KEY_SNACKBAR_TEXT
import com.afollestad.aesthetic.internal.PrefNames.KEY_STATUS_BAR_COLOR
import com.afollestad.aesthetic.internal.PrefNames.KEY_SWIPEREFRESH_COLORS
import com.afollestad.aesthetic.internal.PrefNames.KEY_TAB_LAYOUT_BG_MODE
import com.afollestad.aesthetic.internal.PrefNames.KEY_TAB_LAYOUT_INDICATOR_MODE
import com.afollestad.aesthetic.internal.PrefNames.KEY_WINDOW_BG_COLOR
import com.afollestad.aesthetic.internal.PrefNames.PREFS_NAME
import com.afollestad.aesthetic.utils.color
import com.afollestad.aesthetic.utils.colorAttr
import com.afollestad.aesthetic.utils.darkenColor
import com.afollestad.aesthetic.utils.distinctToMainThread
import com.afollestad.aesthetic.utils.getRootView
import com.afollestad.aesthetic.utils.isColorLight
import com.afollestad.aesthetic.utils.mutableArrayMapOf
import com.afollestad.aesthetic.utils.onErrorLogAndRethrow
import com.afollestad.aesthetic.utils.plusAssign
import com.afollestad.aesthetic.utils.setInflaterFactory
import com.afollestad.aesthetic.utils.setLightStatusBarCompat
import com.afollestad.aesthetic.utils.setNavBarColorCompat
import com.afollestad.aesthetic.utils.setStatusBarColorCompat
import com.afollestad.aesthetic.utils.setTaskDescriptionColor
import com.afollestad.aesthetic.utils.splitToInts
import com.afollestad.aesthetic.utils.unsubscribeOnDetach
import com.afollestad.rxkprefs.RxkPrefs
import io.reactivex.Observable
import io.reactivex.Observable.combineLatest
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Consumer
import io.reactivex.subjects.BehaviorSubject
import java.lang.String.format
/** @author Aidan Follestad (afollestad) */
class Aesthetic private constructor(private var ctxt: Context?) {
private val lastActivityThemes = mutableArrayMapOf<String, Int>(2)
private val onAttached = BehaviorSubject.create<Boolean>()
private var subs: CompositeDisposable? = null
private var prefs: SharedPreferences? = null
private var editor: SharedPreferences.Editor? = null
private var rxkPrefs: RxkPrefs? = null
private var isResumed: Boolean = false
init {
initPrefs()
}
@SuppressLint("CommitPrefEdits")
private fun initPrefs() {
rxkPrefs = RxkPrefs(context, PREFS_NAME)
prefs = rxkPrefs!!.getSharedPrefs()
editor = safePrefs.edit()
onAttached.onNext(true)
}
private fun deInitPrefs() {
onAttached.onNext(false)
prefs = null
editor = null
rxkPrefs = null
}
private fun waitForAttach() = onAttached.filter { it }.map { rxkPrefs!! }
internal val context
@CheckResult
get() = ctxt ?: throw IllegalStateException("Not attached")
private val safePrefs
@CheckResult
get() = prefs ?: throw IllegalStateException("Not attached")
private val safePrefsEditor
@CheckResult
get() = editor ?: throw IllegalStateException("Not attached")
val isDark: Observable<Boolean>
@CheckResult
get() = waitForAttach().flatMap {
it.boolean(KEY_IS_DARK)
.asObservable()
}
fun addBackgroundSubscriber(
view: View,
colorObservable: Observable<Int>
) {
colorObservable
.distinctToMainThread()
.subscribeWith(ViewBackgroundSubscriber(view))
.unsubscribeOnDetach(view)
}
@CheckResult
fun activityTheme(@StyleRes theme: Int): Aesthetic {
val key = format(KEY_ACTIVITY_THEME, key(context))
safePrefsEditor.putInt(key, theme)
return this
}
@CheckResult
fun activityTheme(): Observable<Int> {
val key = format(KEY_ACTIVITY_THEME, key(context))
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(key, 0)
.asObservable()
.filter { it != 0 && it != getLastActivityTheme(context) }
}
}
@CheckResult
fun isDark(isDark: Boolean): Aesthetic {
safePrefsEditor.putBoolean(KEY_IS_DARK, isDark)
.commit()
return this
}
@CheckResult
fun colorPrimary(@ColorInt color: Int): Aesthetic {
// needs to be committed immediately so that for statusBarColorAuto() and other auto methods
safePrefsEditor.putInt(KEY_PRIMARY_COLOR, color)
.commit()
return this
}
@CheckResult
fun colorPrimaryRes(@ColorRes color: Int): Aesthetic {
return colorPrimary(context.color(color))
}
@CheckResult
fun colorPrimary(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(KEY_PRIMARY_COLOR, context.colorAttr(R.attr.colorPrimary))
.asObservable()
}
}
@CheckResult
fun colorPrimaryDark(@ColorInt color: Int): Aesthetic {
// needs to be committed immediately so that for statusBarColorAuto() and other auto methods
safePrefsEditor.putInt(KEY_PRIMARY_DARK_COLOR, color)
.commit()
return this
}
@CheckResult
fun colorPrimaryDarkRes(@ColorRes color: Int): Aesthetic {
return colorPrimaryDark(context.color(color))
}
@CheckResult
fun colorPrimaryDark(): Observable<Int> {
return colorPrimary().flatMap { primary ->
rxkPrefs!!
.integer(
KEY_PRIMARY_DARK_COLOR, primary.darkenColor()
)
.asObservable()
}
}
@CheckResult
fun colorAccent(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_ACCENT_COLOR, color)
.commit()
return this
}
@CheckResult
fun colorAccentRes(@ColorRes color: Int): Aesthetic {
return colorAccent(context.color(color))
}
@CheckResult
fun colorAccent(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(KEY_ACCENT_COLOR, context.colorAttr(R.attr.colorAccent))
.asObservable()
}
}
@CheckResult
fun textColorPrimary(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_PRIMARY_TEXT_COLOR, color)
return this
}
@CheckResult
fun textColorPrimaryRes(@ColorRes color: Int): Aesthetic {
return textColorPrimary(context.color(color))
}
@CheckResult
fun textColorPrimary(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(
KEY_PRIMARY_TEXT_COLOR,
context.colorAttr(android.R.attr.textColorPrimary)
)
.asObservable()
}
}
@CheckResult
fun textColorSecondary(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_SECONDARY_TEXT_COLOR, color)
return this
}
@CheckResult
fun textColorSecondaryRes(@ColorRes color: Int): Aesthetic {
return textColorSecondary(context.color(color))
}
@CheckResult
fun textColorSecondary(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(
KEY_SECONDARY_TEXT_COLOR,
context.colorAttr(android.R.attr.textColorSecondary)
)
.asObservable()
}
}
@CheckResult
fun textColorPrimaryInverse(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_PRIMARY_TEXT_INVERSE_COLOR, color)
return this
}
@CheckResult
fun textColorPrimaryInverseRes(@ColorRes color: Int): Aesthetic {
return textColorPrimaryInverse(context.color(color))
}
@CheckResult
fun textColorPrimaryInverse(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(
KEY_PRIMARY_TEXT_INVERSE_COLOR,
context.colorAttr(android.R.attr.textColorPrimaryInverse)
)
.asObservable()
}
}
@CheckResult
fun textColorSecondaryInverse(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_SECONDARY_TEXT_INVERSE_COLOR, color)
return this
}
@CheckResult
fun textColorSecondaryInverseRes(@ColorRes color: Int): Aesthetic {
return textColorSecondaryInverse(context.color(color))
}
@CheckResult
fun textColorSecondaryInverse(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(
KEY_SECONDARY_TEXT_INVERSE_COLOR,
context.colorAttr(android.R.attr.textColorSecondaryInverse)
)
.asObservable()
}
}
@CheckResult
fun colorWindowBackground(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_WINDOW_BG_COLOR, color)
.commit()
return this
}
@CheckResult
fun colorWindowBackgroundRes(@ColorRes color: Int): Aesthetic {
return colorWindowBackground(context.color(color))
}
@CheckResult
fun colorWindowBackground(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(
KEY_WINDOW_BG_COLOR,
context.colorAttr(android.R.attr.windowBackground)
)
.asObservable()
}
}
@CheckResult
fun colorStatusBar(@ColorInt color: Int): Aesthetic {
val key = format(KEY_STATUS_BAR_COLOR, key(context))
safePrefsEditor.putInt(key, color)
return this
}
@CheckResult
fun colorStatusBarRes(@ColorRes color: Int): Aesthetic {
return colorStatusBar(context.color(color))
}
@CheckResult
fun colorStatusBarAuto(): Aesthetic {
val key = format(KEY_STATUS_BAR_COLOR, key(context))
safePrefsEditor.putInt(
key,
safePrefs.getInt(
KEY_PRIMARY_COLOR, context.colorAttr(R.attr.colorPrimary)
).darkenColor()
)
return this
}
@CheckResult
fun colorStatusBar(): Observable<Int> {
return colorPrimaryDark().flatMap {
val key = format(KEY_STATUS_BAR_COLOR, key(context))
rxkPrefs!!.integer(key, it)
.asObservable()
}
}
@CheckResult
fun colorNavigationBar(@ColorInt color: Int): Aesthetic {
val key = format(KEY_NAV_BAR_COLOR, key(context))
safePrefsEditor.putInt(key, color)
return this
}
@CheckResult
fun colorNavigationBarRes(@ColorRes color: Int): Aesthetic {
return colorNavigationBar(context.color(color))
}
@CheckResult
fun colorNavigationBarAuto(): Aesthetic {
val color =
safePrefs.getInt(KEY_PRIMARY_COLOR, context.colorAttr(R.attr.colorPrimary))
val key = format(KEY_NAV_BAR_COLOR, key(context))
safePrefsEditor.putInt(key, if (color.isColorLight()) Color.BLACK else color)
return this
}
@CheckResult
fun colorNavigationBar(): Observable<Int> {
val key = format(KEY_NAV_BAR_COLOR, key(context))
return waitForAttach().flatMap { rxPrefs ->
rxPrefs.integer(key, Color.BLACK)
.asObservable()
}
}
@CheckResult
fun lightStatusBarMode(mode: AutoSwitchMode): Aesthetic {
safePrefsEditor.putInt(KEY_LIGHT_STATUS_MODE, mode.value)
return this
}
@CheckResult
fun lightStatusBarMode(): Observable<Int> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(KEY_LIGHT_STATUS_MODE, AutoSwitchMode.AUTO.value)
.asObservable()
}
}
@CheckResult
fun tabLayoutIndicatorMode(mode: TabLayoutIndicatorMode): Aesthetic {
safePrefsEditor.putInt(KEY_TAB_LAYOUT_INDICATOR_MODE, mode.value)
.commit()
return this
}
@CheckResult
fun tabLayoutIndicatorMode(): Observable<TabLayoutIndicatorMode> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(
KEY_TAB_LAYOUT_INDICATOR_MODE, TabLayoutIndicatorMode.ACCENT.value
)
.asObservable()
.map { TabLayoutIndicatorMode.fromInt(it) }
}
}
@CheckResult
fun tabLayoutBackgroundMode(mode: TabLayoutBgMode): Aesthetic {
safePrefsEditor.putInt(KEY_TAB_LAYOUT_BG_MODE, mode.value)
.commit()
return this
}
@CheckResult
fun tabLayoutBackgroundMode(): Observable<TabLayoutBgMode> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(KEY_TAB_LAYOUT_BG_MODE, TabLayoutBgMode.PRIMARY.value)
.asObservable()
.map { TabLayoutBgMode.fromInt(it) }
}
}
@CheckResult
fun navigationViewMode(mode: NavigationViewMode): Aesthetic {
safePrefsEditor.putInt(KEY_NAV_VIEW_MODE, mode.value)
.commit()
return this
}
@CheckResult
fun navigationViewMode(): Observable<NavigationViewMode> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(KEY_NAV_VIEW_MODE, NavigationViewMode.SELECTED_PRIMARY.value)
.asObservable()
.map { NavigationViewMode.fromInt(it) }
}
}
@CheckResult
fun bottomNavigationBackgroundMode(mode: BottomNavBgMode): Aesthetic {
safePrefsEditor.putInt(KEY_BOTTOM_NAV_BG_MODE, mode.value)
.commit()
return this
}
@CheckResult
fun bottomNavigationBackgroundMode(): Observable<BottomNavBgMode> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(KEY_BOTTOM_NAV_BG_MODE, BottomNavBgMode.BLACK_WHITE_AUTO.value)
.asObservable()
.map { BottomNavBgMode.fromInt(it) }
}
}
@CheckResult
fun bottomNavigationIconTextMode(mode: BottomNavIconTextMode): Aesthetic {
safePrefsEditor.putInt(KEY_BOTTOM_NAV_ICONTEXT_MODE, mode.value)
.commit()
return this
}
@CheckResult
fun bottomNavigationIconTextMode(): Observable<BottomNavIconTextMode> {
return waitForAttach().flatMap { rxPrefs ->
rxPrefs
.integer(
KEY_BOTTOM_NAV_ICONTEXT_MODE,
BottomNavIconTextMode.SELECTED_ACCENT.value
)
.asObservable()
.map { BottomNavIconTextMode.fromInt(it) }
}
}
@CheckResult
fun colorCardViewBackground(): Observable<Int> {
return isDark.flatMap {
rxkPrefs!!
.integer(
KEY_CARD_VIEW_BG_COLOR,
context.color(
if (it)
R.color.ate_cardview_bg_dark
else
R.color.ate_cardview_bg_light
)
)
.asObservable()
}
}
@CheckResult
fun colorCardViewBackground(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_CARD_VIEW_BG_COLOR, color)
return this
}
@CheckResult
fun colorCardViewBackgroundRes(@ColorRes color: Int): Aesthetic {
return colorCardViewBackground(context.color(color))
}
@CheckResult
fun colorIconTitle(
requestedBackgroundObservable: Observable<Int>?
): Observable<ActiveInactiveColors> {
var backgroundObservable = requestedBackgroundObservable
if (backgroundObservable == null) {
backgroundObservable = Aesthetic.get()
.colorPrimary()
}
val iconTitleObs = backgroundObservable.flatMap {
val isDark = !it.isColorLight()
Observable.zip(
rxkPrefs!!
.integer(
KEY_ICON_TITLE_ACTIVE_COLOR,
context.color(
if (isDark) R.color.ate_icon_dark else R.color.ate_icon_light
)
)
.asObservable(),
rxkPrefs!!
.integer(
KEY_ICON_TITLE_INACTIVE_COLOR,
context.color(
if (isDark)
R.color.ate_icon_dark_inactive
else
R.color.ate_icon_light_inactive
)
)
.asObservable(),
BiFunction<Int, Int, ActiveInactiveColors> { active, inactive ->
ActiveInactiveColors(
active, inactive
)
})
}
return waitForAttach().flatMap { iconTitleObs }
}
@CheckResult
fun colorIconTitleActive(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_ICON_TITLE_ACTIVE_COLOR, color)
return this
}
@CheckResult
fun colorIconTitleActiveRes(@ColorRes color: Int): Aesthetic {
return colorIconTitleActive(context.color(color))
}
@CheckResult
fun colorIconTitleInactive(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_ICON_TITLE_INACTIVE_COLOR, color)
return this
}
@CheckResult
fun colorIconTitleInactiveRes(@ColorRes color: Int): Aesthetic {
return colorIconTitleActive(context.color(color))
}
@CheckResult
fun snackbarTextColor(): Observable<Int> {
return isDark
.flatMap { isDark ->
if (isDark) {
textColorPrimary()
} else {
textColorPrimaryInverse()
.flatMap { defaultTextColor ->
rxkPrefs!!.integer(
KEY_SNACKBAR_TEXT, defaultTextColor
)
.asObservable()
}
}
}
}
@CheckResult
fun snackbarTextColor(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_SNACKBAR_TEXT, color)
return this
}
@CheckResult
fun snackbarTextColorRes(@ColorRes color: Int): Aesthetic {
return colorCardViewBackground(context.color(color))
}
@CheckResult
fun snackbarActionTextColor(): Observable<Int> {
return colorAccent().flatMap {
rxkPrefs!!
.integer(KEY_SNACKBAR_ACTION_TEXT, it)
.asObservable()
}
}
@CheckResult
fun snackbarActionTextColor(@ColorInt color: Int): Aesthetic {
safePrefsEditor.putInt(KEY_SNACKBAR_ACTION_TEXT, color)
return this
}
@CheckResult
fun snackbarActionTextColorRes(@ColorRes color: Int): Aesthetic {
return colorCardViewBackground(context.color(color))
}
@CheckResult
fun swipeRefreshLayoutColors(): Observable<IntArray> {
return colorAccent().flatMap { accent ->
rxkPrefs!!
.string(KEY_SWIPEREFRESH_COLORS, "$accent")
.asObservable()
.map { it.splitToInts() }
}
}
@CheckResult
fun swipeRefreshLayoutColors(@ColorInt vararg colors: Int): Aesthetic {
safePrefsEditor.putString(KEY_SWIPEREFRESH_COLORS, colors.joinToString(","))
return this
}
@CheckResult
fun swipeRefreshLayoutColorsRes(@ColorRes vararg colorsRes: Int): Aesthetic {
safePrefsEditor.putString(
KEY_SWIPEREFRESH_COLORS, colorsRes.map { context.color(it) }.joinToString(",")
)
return this
}
/** Notifies all listening views that theme properties have been updated. */
fun apply() {
safePrefsEditor.commit()
}
private fun invalidateStatusBar() {
with(context as? Activity ?: return) {
val key = format(KEY_STATUS_BAR_COLOR, key(context))
val color = safePrefs.getInt(key, context.colorAttr(R.attr.colorPrimaryDark))
val rootView = getRootView()
if (rootView is DrawerLayout) {
// Color is set to DrawerLayout, Activity gets transparent status bar
setLightStatusBarCompat(false)
setStatusBarColorCompat(Color.TRANSPARENT)
rootView.setStatusBarBackgroundColor(color)
} else {
setStatusBarColorCompat(color)
}
val mode = AutoSwitchMode.fromInt(
safePrefs.getInt(KEY_LIGHT_STATUS_MODE, AutoSwitchMode.AUTO.value)
)
when (mode) {
AutoSwitchMode.OFF -> setLightStatusBarCompat(false)
AutoSwitchMode.ON -> setLightStatusBarCompat(true)
else -> setLightStatusBarCompat(color.isColorLight())
}
}
}
companion object {
@SuppressLint("StaticFieldLeak")
private var instance: Aesthetic? = null
private fun key(whereAmI: Context?): String {
var key: String?
key = if (whereAmI is AestheticKeyProvider) {
(whereAmI as AestheticKeyProvider).key()
} else {
"default"
}
if (key == null) {
key = "default"
}
return key
}
/** Should be called before super.onCreate() in each Activity. */
fun attach(whereAmI: Context): Aesthetic {
if (instance == null) {
instance = Aesthetic(whereAmI)
}
with(instance!!) {
isResumed = false
ctxt = whereAmI
initPrefs()
with(whereAmI as? Activity ?: return this) {
val li = layoutInflater
(this as? AppCompatActivity)?.setInflaterFactory(li)
val activityThemeKey = format(KEY_ACTIVITY_THEME, key(this))
val latestActivityTheme = safePrefs.getInt(activityThemeKey, 0)
lastActivityThemes[context.javaClass.name] = latestActivityTheme
if (latestActivityTheme != 0) {
setTheme(latestActivityTheme)
}
}
return this
}
}
private fun getLastActivityTheme(forContext: Context?): Int {
return if (forContext == null || instance == null) {
0
} else instance!!.lastActivityThemes[forContext.javaClass.name] ?: return 0
}
@CheckResult
fun get() = instance ?: throw IllegalStateException("Not attached")
inline fun config(func: Aesthetic.() -> Unit) {
val instance = get()
instance.func()
instance.apply()
}
/** Should be called in onPause() of each Activity or Service. */
fun pause(whereAmI: Context) {
with(instance ?: return) {
isResumed = false
subs?.clear()
if (whereAmI is Activity && whereAmI.isFinishing &&
context.javaClass.name == whereAmI.javaClass.name
) {
ctxt = null
deInitPrefs()
}
}
}
/** Should be called in onResume() of each Activity. */
fun resume(whereAmI: Context) {
with(instance ?: throw IllegalStateException("Not attached")) {
if (isResumed)
throw IllegalStateException("Already resumed")
ctxt = whereAmI
initPrefs()
isResumed = true
subs = CompositeDisposable()
if (context is Activity) {
subs += colorPrimary()
.distinctToMainThread()
.subscribe(
Consumer { (context as Activity).setTaskDescriptionColor(it) },
onErrorLogAndRethrow()
)
subs += activityTheme()
.distinctToMainThread()
.subscribe(
Consumer {
lastActivityThemes[context.javaClass.name] = it
(context as Activity).recreate()
},
onErrorLogAndRethrow()
)
subs += combineLatest<Int, Int, Pair<Int, Int>>(
colorStatusBar(), lightStatusBarMode(),
BiFunction<Int, Int, Pair<Int, Int>> { a, b -> Pair(a, b) })
.distinctToMainThread()
.subscribe(
Consumer { invalidateStatusBar() },
onErrorLogAndRethrow()
)
subs += colorNavigationBar()
.distinctToMainThread()
.subscribe(
Consumer { (context as Activity).setNavBarColorCompat(it) },
onErrorLogAndRethrow()
)
subs += colorWindowBackground()
.distinctToMainThread()
.subscribe(
Consumer {
(context as Activity).window?.setBackgroundDrawable(ColorDrawable(it))
},
onErrorLogAndRethrow()
)
}
}
}
/** Returns true if this field has never been accessed before. */
val isFirstTime: Boolean
get() {
with(instance ?: throw IllegalStateException("Not attached")) {
val firstTime = safePrefs.getBoolean(KEY_FIRST_TIME, true)
safePrefsEditor.putBoolean(KEY_FIRST_TIME, false)
.apply()
return firstTime
}
}
}
}