aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
blob: 9d16c63abeb7f4f0a740e68e7583521510f66595 (plain)
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/*
 * Copyright 2018 Allan Wang
 *
 * 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 com.pitchedapps.frost.activities

import android.annotation.SuppressLint
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.PointF
import android.graphics.drawable.Drawable
import android.graphics.drawable.RippleDrawable
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.Toolbar
import androidx.core.graphics.alpha
import androidx.core.view.updateLayoutParams
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import ca.allanwang.kau.searchview.SearchItem
import ca.allanwang.kau.searchview.SearchView
import ca.allanwang.kau.searchview.SearchViewHolder
import ca.allanwang.kau.searchview.bindSearchView
import ca.allanwang.kau.ui.ProgressAnimator
import ca.allanwang.kau.utils.blendWith
import ca.allanwang.kau.utils.colorToForeground
import ca.allanwang.kau.utils.dimenPixelSize
import ca.allanwang.kau.utils.drawable
import ca.allanwang.kau.utils.fadeScaleTransition
import ca.allanwang.kau.utils.gone
import ca.allanwang.kau.utils.invisible
import ca.allanwang.kau.utils.materialDialog
import ca.allanwang.kau.utils.restart
import ca.allanwang.kau.utils.setIcon
import ca.allanwang.kau.utils.setMenuIcons
import ca.allanwang.kau.utils.showIf
import ca.allanwang.kau.utils.string
import ca.allanwang.kau.utils.tint
import ca.allanwang.kau.utils.toDrawable
import ca.allanwang.kau.utils.toast
import ca.allanwang.kau.utils.unboundedHeight
import ca.allanwang.kau.utils.visible
import ca.allanwang.kau.utils.withAlpha
import ca.allanwang.kau.utils.withMinAlpha
import com.afollestad.materialdialogs.checkbox.checkBoxPrompt
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.tabs.TabLayout
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.pitchedapps.frost.BuildConfig
import com.pitchedapps.frost.R
import com.pitchedapps.frost.contracts.MainActivityContract
import com.pitchedapps.frost.contracts.VideoViewHolder
import com.pitchedapps.frost.contracts.WebFileChooser
import com.pitchedapps.frost.databinding.ActivityMainBinding
import com.pitchedapps.frost.databinding.ActivityMainBottomTabsBinding
import com.pitchedapps.frost.databinding.ActivityMainDrawerWrapperBinding
import com.pitchedapps.frost.databinding.ViewNavHeaderBinding
import com.pitchedapps.frost.db.CookieDao
import com.pitchedapps.frost.db.CookieEntity
import com.pitchedapps.frost.db.GenericDao
import com.pitchedapps.frost.db.currentCookie
import com.pitchedapps.frost.db.getTabs
import com.pitchedapps.frost.enums.MainActivityLayout
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.parsers.FrostSearch
import com.pitchedapps.frost.facebook.parsers.SearchParser
import com.pitchedapps.frost.facebook.profilePictureUrl
import com.pitchedapps.frost.fragments.BaseFragment
import com.pitchedapps.frost.fragments.WebFragment
import com.pitchedapps.frost.glide.FrostGlide
import com.pitchedapps.frost.glide.GlideApp
import com.pitchedapps.frost.services.scheduleNotificationsFromPrefs
import com.pitchedapps.frost.utils.ACTIVITY_SETTINGS
import com.pitchedapps.frost.utils.BiometricUtils
import com.pitchedapps.frost.utils.EXTRA_COOKIES
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.MAIN_TIMEOUT_DURATION
import com.pitchedapps.frost.utils.REQUEST_FAB
import com.pitchedapps.frost.utils.REQUEST_NAV
import com.pitchedapps.frost.utils.REQUEST_NOTIFICATION
import com.pitchedapps.frost.utils.REQUEST_REFRESH
import com.pitchedapps.frost.utils.REQUEST_RESTART
import com.pitchedapps.frost.utils.REQUEST_RESTART_APPLICATION
import com.pitchedapps.frost.utils.REQUEST_SEARCH
import com.pitchedapps.frost.utils.REQUEST_TEXT_ZOOM
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.frostChangelog
import com.pitchedapps.frost.utils.frostEvent
import com.pitchedapps.frost.utils.frostNavigationBar
import com.pitchedapps.frost.utils.launchLogin
import com.pitchedapps.frost.utils.launchNewTask
import com.pitchedapps.frost.utils.launchWebOverlay
import com.pitchedapps.frost.utils.urlEncode
import com.pitchedapps.frost.views.BadgedIcon
import com.pitchedapps.frost.views.FrostVideoViewer
import com.pitchedapps.frost.views.FrostViewPager
import com.pitchedapps.frost.widgets.NotificationWidget
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.qualifiers.ActivityContext
import dagger.hilt.android.scopes.ActivityScoped
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.math.abs

/**
 * Created by Allan Wang on 20/12/17.
 *
 * Most of the logic that is unrelated to handling fragments
 */
@AndroidEntryPoint
abstract class BaseMainActivity :
    BaseActivity(),
    MainActivityContract,
    VideoViewHolder,
    SearchViewHolder {

    /**
     * Note that tabs themselves are initialized through a coroutine during onCreate
     */
    protected val adapter: SectionsPagerAdapter = SectionsPagerAdapter()
    override val frameWrapper: FrameLayout get() = drawerWrapperBinding.mainContainer
    lateinit var drawerWrapperBinding: ActivityMainDrawerWrapperBinding
    lateinit var contentBinding: ActivityMainContentBinding

    @Inject
    lateinit var cookieDao: CookieDao

    @Inject
    lateinit var genericDao: GenericDao

    @Inject
    lateinit var webFileChooser: WebFileChooser

    interface ActivityMainContentBinding {
        val root: View
        val toolbar: Toolbar
        val viewpager: FrostViewPager
        val tabs: TabLayout
        val appbar: AppBarLayout
        val fab: FloatingActionButton
    }

    protected var lastPosition = -1

    override var videoViewer: FrostVideoViewer? = null
    private var lastAccessTime = -1L

    override var searchView: SearchView? = null
    private val searchViewCache = mutableMapOf<String, List<SearchItem>>()
    private var controlWebview: WebView? = null

    final override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val start = System.currentTimeMillis()
        drawerWrapperBinding = ActivityMainDrawerWrapperBinding.inflate(layoutInflater)
        setContentView(drawerWrapperBinding.root)
        contentBinding = when (prefs.mainActivityLayout) {
            MainActivityLayout.TOP_BAR -> {
                val binding = ActivityMainBinding.inflate(layoutInflater)
                @SuppressLint("StaticFieldLeak")
                object : ActivityMainContentBinding {
                    override val root: View = binding.root
                    override val toolbar: Toolbar = binding.toolbar
                    override val viewpager: FrostViewPager = binding.viewpager
                    override val tabs: TabLayout = binding.tabs
                    override val appbar: AppBarLayout = binding.appbar
                    override val fab: FloatingActionButton = binding.fab
                }
            }
            MainActivityLayout.BOTTOM_BAR -> {
                val binding = ActivityMainBottomTabsBinding.inflate(layoutInflater)
                @SuppressLint("StaticFieldLeak")
                object : ActivityMainContentBinding {
                    override val root: View = binding.root
                    override val toolbar: Toolbar = binding.toolbar
                    override val viewpager: FrostViewPager = binding.viewpager
                    override val tabs: TabLayout = binding.tabs
                    override val appbar: AppBarLayout = binding.appbar
                    override val fab: FloatingActionButton = binding.fab
                }
            }
        }
        drawerWrapperBinding.mainContainer.addView(contentBinding.root)
        with(contentBinding) {
            activityThemer.setFrostColors {
                toolbar(toolbar)
                themeWindow = false
                header(appbar)
                background(viewpager)
            }
            setSupportActionBar(toolbar)
            viewpager.adapter = adapter
            tabs.setBackgroundColor(prefs.mainActivityLayout.backgroundColor(themeProvider))
        }
        onNestedCreate(savedInstanceState)
        L.i { "Main finished loading UI in ${System.currentTimeMillis() - start} ms" }
        launch {
            adapter.setPages(genericDao.getTabs())
        }
        controlWebview = WebView(this)
        if (BuildConfig.VERSION_CODE > prefs.versionCode) {
            prefs.prevVersionCode = prefs.versionCode
            prefs.versionCode = BuildConfig.VERSION_CODE
            if (!BuildConfig.DEBUG) {
                frostChangelog()
                frostEvent(
                    "Version",
                    "Version code" to BuildConfig.VERSION_CODE,
                    "Prev version code" to prefs.prevVersionCode,
                    "Version name" to BuildConfig.VERSION_NAME,
                    "Build type" to BuildConfig.BUILD_TYPE,
                    "Frost id" to prefs.frostId
                )
            }
        }
        L.i { "Main started in ${System.currentTimeMillis() - start} ms" }
        drawerWrapperBinding.initDrawer()
        contentBinding.initFab()
        lastAccessTime = System.currentTimeMillis()
    }

    /**
     * Injector to handle creation for sub classes
     */
    protected abstract fun onNestedCreate(savedInstanceState: Bundle?)

    private var hasFab = false
    private var shouldShow = false

    private class FrostMenuBuilder(private val context: Context, private val menu: Menu) {
        private var order: Int = 0
        private var groupId: Int = 13
        private val items: MutableList<Menu> = mutableListOf()

        fun primaryFrostItem(fbItem: FbItem) {
            val item = menu.add(groupId, fbItem.ordinal, order++, context.string(fbItem.titleId))
            item.icon = fbItem.icon.toDrawable(context, 18)
        }

        fun divider() {
            groupId++
        }

        fun secondaryFrostItem(fbItem: FbItem) {
            menu.add(groupId, fbItem.ordinal, order++, context.string(fbItem.titleId))
        }
    }

    private fun createNavDrawable(foreground: Int, background: Int): RippleDrawable {
        val drawable = drawable(R.drawable.nav_item_background) as RippleDrawable
        drawable.setColor(
            ColorStateList(
                arrayOf(intArrayOf()),
                intArrayOf(background.blendWith(foreground.withAlpha(background.alpha), 0.35f))
            )
        )
        return drawable
    }

    private fun ActivityMainDrawerWrapperBinding.initDrawer() {

        val toggle = ActionBarDrawerToggle(
            this@BaseMainActivity, drawer, contentBinding.toolbar,
            R.string.open,
            R.string.close
        )
        toggle.isDrawerSlideAnimationEnabled = false
        drawer.addDrawerListener(toggle)
        toggle.syncState()

        val foregroundColor = ColorStateList.valueOf(themeProvider.textColor)

        with(navigation) {
            FrostMenuBuilder(this@BaseMainActivity, menu).apply {
                primaryFrostItem(FbItem.FEED_MOST_RECENT)
                primaryFrostItem(FbItem.FEED_TOP_STORIES)
                primaryFrostItem(FbItem.ACTIVITY_LOG)
                divider()
                primaryFrostItem(FbItem.PHOTOS)
                primaryFrostItem(FbItem.GROUPS)
                primaryFrostItem(FbItem.FRIENDS)
                primaryFrostItem(FbItem.CHAT)
                primaryFrostItem(FbItem.PAGES)
                divider()
                primaryFrostItem(FbItem.EVENTS)
                primaryFrostItem(FbItem.BIRTHDAYS)
                primaryFrostItem(FbItem.ON_THIS_DAY)
                divider()
                primaryFrostItem(FbItem.NOTES)
                primaryFrostItem(FbItem.SAVED)
                primaryFrostItem(FbItem.MARKETPLACE)
            }
            setNavigationItemSelectedListener {
                val item = FbItem.values[it.itemId]
                frostEvent("Drawer Tab", "name" to item.name)
                drawer.closeDrawer(navigation)
                launchWebOverlay(item.url, fbCookie, prefs)
                false
            }
            val navBg = themeProvider.bgColor.withMinAlpha(200)
            setBackgroundColor(navBg)
            itemBackground = createNavDrawable(themeProvider.accentColor, navBg)
            itemTextColor = foregroundColor
            itemIconTintList = foregroundColor

            val header = NavHeader()
            addHeaderView(header.root)
        }
    }

    private fun ActivityMainContentBinding.initFab() {
        hasFab = false
        shouldShow = false
        fab.backgroundTintList = ColorStateList.valueOf(themeProvider.headerColor.withMinAlpha(200))
        fab.hide()
        appbar.addOnOffsetChangedListener(
            AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
                if (!hasFab) return@OnOffsetChangedListener
                val percent = abs(verticalOffset.toFloat() / appBarLayout.totalScrollRange)
                val shouldShow = percent < 0.2
                if (this@BaseMainActivity.shouldShow != shouldShow) {
                    this@BaseMainActivity.shouldShow = shouldShow
                    fab.showIf(shouldShow)
                }
            }
        )
    }

    override fun showFab(iicon: IIcon, clickEvent: () -> Unit) {
        with(contentBinding) {
            hasFab = true
            fab.setOnClickListener { clickEvent() }
            if (shouldShow) {
                if (fab.isShown) {
                    fab.fadeScaleTransition {
                        setIcon(iicon, color = themeProvider.iconColor)
                    }
                    return
                }
            }
            fab.setIcon(iicon, color = themeProvider.iconColor)
            fab.showIf(shouldShow)
        }
    }

    override fun hideFab() {
        with(contentBinding) {
            hasFab = false
            fab.setOnClickListener(null)
            fab.hide()
        }
    }

    fun tabsForEachView(action: (position: Int, view: BadgedIcon) -> Unit) {
        with(contentBinding) {
            (0 until tabs.tabCount).asSequence().forEach { i ->
                action(i, tabs.getTabAt(i)!!.customView as BadgedIcon)
            }
        }
    }

    private inner class NavHeader {

        private var orderedAccounts: List<CookieEntity> = cookies()
        private var pendingUpdate: Boolean = false
        private val binding = ViewNavHeaderBinding.inflate(layoutInflater)
        val root: View get() = binding.root
        private val optionsBackground = themeProvider.bgColor.withMinAlpha(200).colorToForeground(
            0.1f
        )

        init {
            setPrimary(prefs.userId)
            binding.updateAccounts()
            with(drawerWrapperBinding) {
                drawer.addDrawerListener(object : DrawerLayout.SimpleDrawerListener() {
                    override fun onDrawerClosed(drawerView: View) {
                        if (drawerView !== navigation) return
                        if (!pendingUpdate) return
                        pendingUpdate = false
                        binding.updateAccounts()
                    }
                })
            }
            with(binding) {
                optionsContainer.setBackgroundColor(optionsBackground)
                var showOptions = false
                val animator: ProgressAnimator = ProgressAnimator.ofFloat()
                background.setOnClickListener {
                    animator.reset()
                    if (showOptions) {
                        animator.apply {
                            withAnimator(optionsContainer.height, 0) {
                                optionsContainer.updateLayoutParams {
                                    height = it
                                }
                            }
                            withAnimator(arrow.rotation, 0f) {
                                arrow.rotation = it
                            }
                            withEndAction {
                                optionsContainer.gone()
                            }
                        }
                    } else {
                        optionsContainer.visible()
                        animator.apply {
                            withAnimator(
                                optionsContainer.height,
                                optionsContainer.unboundedHeight
                            ) {
                                optionsContainer.updateLayoutParams {
                                    height = it
                                }
                            }
                            withEndAction {
                                // Sometimes, height remains the same as measured during collapse
                                // if the animations are disabled.
                                // We will resolve this by always falling back to wrap content afterwards
                                optionsContainer.updateLayoutParams {
                                    height = ViewGroup.LayoutParams.WRAP_CONTENT
                                }
                            }
                            withAnimator(arrow.rotation, 180f) {
                                arrow.rotation = it
                            }
                        }
                    }
                    showOptions = !showOptions
                    animator.start()
                }

                val textColor = themeProvider.textColor

                fun TextView.setOptionsIcon(iicon: IIcon) {
                    setCompoundDrawablesRelativeWithIntrinsicBounds(
                        iicon.toDrawable(this@BaseMainActivity, color = textColor, sizeDp = 20),
                        null,
                        null,
                        null
                    )
                    setTextColor(textColor)
                    background = createNavDrawable(themeProvider.accentColor, optionsBackground)
                }

                with(optionsLogout) {
                    setOptionsIcon(GoogleMaterial.Icon.gmd_exit_to_app)
                    setOnClickListener {
                        launch {
                            val currentCookie = cookieDao.currentCookie(prefs)
                            if (currentCookie == null) {
                                toast(R.string.account_not_found)
                                fbCookie.reset()
                                launchLogin(cookies(), true)
                            } else {
                                materialDialog {
                                    title(R.string.kau_logout)
                                    message(
                                        text =
                                        String.format(
                                            string(R.string.kau_logout_confirm_as_x),
                                            currentCookie.name ?: prefs.userId.toString()
                                        )
                                    )
                                    positiveButton(R.string.kau_yes) {
                                        this@BaseMainActivity.launch {
                                            fbCookie.logout(
                                                this@BaseMainActivity,
                                                deleteCookie = true
                                            )
                                        }
                                    }
                                    negativeButton(R.string.kau_no)
                                }
                            }
                        }
                    }
                }
                with(optionsAddAccount) {
                    setOptionsIcon(GoogleMaterial.Icon.gmd_add)
                    setOnClickListener {
                        launchNewTask<LoginActivity>(clearStack = false)
                    }
                }
                with(optionsManageAccount) {
                    setOptionsIcon(GoogleMaterial.Icon.gmd_settings)
                    setOnClickListener {
                        launchNewTask<SelectorActivity>(cookies(), false)
                    }
                }
                arrow.setImageDrawable(
                    GoogleMaterial.Icon.gmd_arrow_drop_down.toDrawable(
                        this@BaseMainActivity,
                        color = themeProvider.textColor
                    )
                )
            }
        }

        private fun setPrimary(id: Long) {
            val (primaries, others) = orderedAccounts.partition { it.id == id }
            if (primaries.size != 1) {
                L._e(null) { "Updating account primaries, could not find specified id" }
            }
            orderedAccounts = primaries + others
        }

        /**
         * Syncs UI to match [orderedAccounts].
         *
         * We keep this separate as we usually only want to update when the drawer is hidden.
         */
        private fun ViewNavHeaderBinding.updateAccounts() {
            avatarPrimary.setAccount(orderedAccounts.getOrNull(0), true)
            avatarSecondary.setAccount(orderedAccounts.getOrNull(1), false)
            avatarTertiary.setAccount(orderedAccounts.getOrNull(2), false)
            optionsAccountsContainer.removeAllViews()
            name.text = orderedAccounts.getOrNull(0)?.name
            name.setTextColor(themeProvider.textColor)
            val glide = Glide.with(root)
            val accountSize = dimenPixelSize(R.dimen.drawer_account_avatar_size)
            val textColor = themeProvider.textColor
            orderedAccounts.forEach { cookie ->
                val tv =
                    TextView(
                        this@BaseMainActivity,
                        null,
                        0,
                        R.style.Main_DrawerAccountUserOptions
                    )
                glide.load(profilePictureUrl(cookie.id)).transform(FrostGlide.circleCrop)
                    .into(object : CustomTarget<Drawable>(accountSize, accountSize) {
                        override fun onLoadCleared(placeholder: Drawable?) {
                            tv.setCompoundDrawablesRelativeWithIntrinsicBounds(
                                placeholder,
                                null,
                                null,
                                null
                            )
                        }

                        override fun onResourceReady(
                            resource: Drawable,
                            transition: Transition<in Drawable>?
                        ) {
                            tv.setCompoundDrawablesRelativeWithIntrinsicBounds(
                                resource,
                                null,
                                null,
                                null
                            )
                        }
                    })
                tv.text = cookie.name
                tv.setTextColor(textColor)
                tv.background = createNavDrawable(themeProvider.accentColor, optionsBackground)
                tv.setOnClickListener {
                    switchAccount(cookie.id)
                }
                optionsAccountsContainer.addView(tv)
            }
        }

        private fun closeDrawer() {
            with(drawerWrapperBinding) {
                drawer.closeDrawer(navigation)
            }
        }

        private fun ImageView.setAccount(
            cookie: CookieEntity?,
            primary: Boolean
        ) {
            if (cookie == null) {
                invisible()
                setOnClickListener(null)
            } else {
                visible()
                GlideApp.with(this)
                    .load(profilePictureUrl(cookie.id))
                    .transform(FrostGlide.circleCrop)
                    .into(this)
                setOnClickListener {
                    if (primary) {
                        launchWebOverlay(FbItem.PROFILE.url, fbCookie, prefs)
                    } else {
                        switchAccount(cookie.id)
                    }
                    closeDrawer()
                }
            }
        }

        private fun switchAccount(id: Long) {
            if (prefs.userId == id) return
            setPrimary(id)
            pendingUpdate = true
            closeDrawer()
            launch {
                fbCookie.switchUser(id)
                tabsForEachView { _, view -> view.badgeText = null }
                refreshAll()
            }
        }
    }

    private fun refreshAll() {
        L.d { "Refresh all" }
        fragmentEmit(REQUEST_REFRESH)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        contentBinding.toolbar.tint(themeProvider.iconColor)
        setMenuIcons(
            menu, themeProvider.iconColor,
            R.id.action_settings to GoogleMaterial.Icon.gmd_settings,
            R.id.action_search to GoogleMaterial.Icon.gmd_search
        )
        bindSearchView(menu)
        return true
    }

    private fun bindSearchView(menu: Menu) {
        searchViewBindIfNull {
            bindSearchView(menu, R.id.action_search, themeProvider.iconColor) {
                textCallback = { query, searchView ->
                    val results = searchViewCache[query]
                    if (results != null)
                        searchView.results = results
                    else {
                        val data = SearchParser.query(fbCookie.webCookie, query)?.data?.results
                        if (data != null) {
                            val items = data.mapTo(mutableListOf(), FrostSearch::toSearchItem)
                            if (items.isNotEmpty())
                                items.add(
                                    SearchItem(
                                        "${FbItem._SEARCH.url}/?q=${query.urlEncode()}",
                                        string(R.string.show_all_results),
                                        iicon = null
                                    )
                                )
                            searchViewCache[query] = items

                            searchView.results = items
                        }
                    }
                }
                textDebounceInterval = 300
                searchCallback =
                    { query, _ ->
                        launchWebOverlay(
                            "${FbItem._SEARCH.url}/?q=${query.urlEncode()}",
                            fbCookie,
                            prefs
                        ); true
                    }
                closeListener = { _ -> searchViewCache.clear() }
                foregroundColor = themeProvider.textColor
                backgroundColor = themeProvider.bgColor.withMinAlpha(200)
                onItemClick = { _, key, _, _ -> launchWebOverlay(key, fbCookie, prefs) }
            }
        }
    }

    @SuppressLint("RestrictedApi")
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_settings -> {
                val intent = Intent(this, SettingsActivity::class.java)
                intent.putParcelableArrayListExtra(EXTRA_COOKIES, cookies())
                val bundle =
                    ActivityOptions.makeCustomAnimation(
                        this,
                        R.anim.kau_slide_in_right,
                        R.anim.kau_fade_out
                    ).toBundle()
                startActivityForResult(intent, ACTIVITY_SETTINGS, bundle)
            }
            else -> return super.onOptionsItemSelected(item)
        }
        return true
    }

    @SuppressLint("NewApi")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (webFileChooser.onActivityResultWeb(requestCode, resultCode, data)) return
        super.onActivityResult(requestCode, resultCode, data)

        fun hasRequest(flag: Int) = resultCode and flag > 0

        if (requestCode == ACTIVITY_SETTINGS) {
            if (resultCode and REQUEST_RESTART_APPLICATION > 0) { // completely restart application
                L.d { "Restart Application Requested" }
                val intent = packageManager.getLaunchIntentForPackage(packageName)!!
                Intent.makeRestartActivityTask(intent.component)
                Runtime.getRuntime().exit(0)
                return
            }
            if (resultCode and REQUEST_RESTART > 0) {
                NotificationWidget.forceUpdate(this)
                restart()
                return
            }
            /*
             * These results can be stacked
             */
            if (hasRequest(REQUEST_REFRESH)) {
                fragmentEmit(REQUEST_REFRESH)
            }
            if (hasRequest(REQUEST_NAV)) {
                frostNavigationBar(prefs, themeProvider)
            }
            if (hasRequest(REQUEST_TEXT_ZOOM)) {
                fragmentEmit(REQUEST_TEXT_ZOOM)
            }
            if (hasRequest(REQUEST_SEARCH)) {
                invalidateOptionsMenu()
            }
            if (hasRequest(REQUEST_FAB)) {
                fragmentEmit(lastPosition)
            }
            if (hasRequest(REQUEST_NOTIFICATION)) {
                scheduleNotificationsFromPrefs(prefs)
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        adapter.saveInstanceState(outState)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        adapter.restoreInstanceState(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        val shouldReload = System.currentTimeMillis() - lastAccessTime > MAIN_TIMEOUT_DURATION
        lastAccessTime = System.currentTimeMillis() // precaution to avoid loops
        controlWebview?.resumeTimers()
        launch {
            val authDefer = BiometricUtils.authenticate(this@BaseMainActivity, prefs)
            fbCookie.switchBackUser()
            authDefer.await()
            if (shouldReload && prefs.autoRefreshFeed) {
                refreshAll()
            }
        }
    }

    override fun onPause() {
        controlWebview?.pauseTimers()
        L.v { "Pause main web timers" }
        lastAccessTime = System.currentTimeMillis()
        super.onPause()
    }

    override fun onDestroy() {
        controlWebview?.destroy()
        super.onDestroy()
    }

    override fun collapseAppBar() {
        with(contentBinding) {
            appbar.post { appbar.setExpanded(false) }
        }
    }

    override fun backConsumer(): Boolean {
        with(drawerWrapperBinding) {
            if (drawer.isDrawerOpen(navigation)) {
                drawer.closeDrawer(navigation)
                return true
            }
        }
        if (currentFragment?.onBackPressed() == true) return true
        if (prefs.exitConfirmation) {
            materialDialog {
                title(R.string.kau_exit)
                message(R.string.kau_exit_confirmation)
                positiveButton(R.string.kau_yes) { finish() }
                negativeButton(R.string.kau_no)
                checkBoxPrompt(R.string.kau_do_not_show_again, isCheckedDefault = false) {
                    prefs.exitConfirmation = !it
                }
            }
            return true
        }
        return false
    }

    inline val currentFragment: BaseFragment?
        get() {
            val viewpager = contentBinding.viewpager
            return supportFragmentManager.findFragmentByTag("android:switcher:${viewpager.id}:${viewpager.currentItem}") as BaseFragment?
        }

    override fun reloadFragment(fragment: BaseFragment) {
        runOnUiThread { adapter.reloadFragment(fragment) }
    }

    inner class SectionsPagerAdapter : FragmentPagerAdapter(supportFragmentManager) {

        private val pages: MutableList<FbItem> = mutableListOf()

        private val forcedFallbacks = mutableSetOf<String>()

        /**
         * Update page list and prompt reload
         */
        fun setPages(pages: List<FbItem>) {
            this.pages.clear()
            this.pages.addAll(pages)
            notifyDataSetChanged()
            with(contentBinding) {
                tabs.removeAllTabs()
                this@SectionsPagerAdapter.pages.forEachIndexed { index, fbItem ->
                    tabs.addTab(
                        tabs.newTab()
                            .setCustomView(
                                BadgedIcon(this@BaseMainActivity).apply {
                                    iicon = fbItem.icon
                                }.also {
                                    it.setAllAlpha(if (index == 0) SELECTED_TAB_ALPHA else UNSELECTED_TAB_ALPHA)
                                }
                            )
                    )
                }
                lastPosition = 0
                viewpager.setCurrentItem(0, false)
                viewpager.offscreenPageLimit = pages.size
                // todo check if post is necessary
                viewpager.post {
                    fragmentEmit(0)
                } // trigger hook so title is set
            }
        }

        fun saveInstanceState(outState: Bundle) {
            outState.putStringArrayList(STATE_FORCE_FALLBACK, ArrayList(forcedFallbacks))
        }

        fun restoreInstanceState(savedInstanceState: Bundle) {
            forcedFallbacks.clear()
            forcedFallbacks.addAll(
                savedInstanceState.getStringArrayList(STATE_FORCE_FALLBACK)
                    ?: emptyList()
            )
        }

        fun reloadFragment(fragment: BaseFragment) {
            if (fragment is WebFragment) return
            L.d { "Reload fragment ${fragment.position}: ${fragment.baseEnum.name}" }
            forcedFallbacks.add(fragment.baseEnum.name)
            supportFragmentManager.beginTransaction().remove(fragment).commitNowAllowingStateLoss()
            notifyDataSetChanged()
        }

        override fun getItem(position: Int): Fragment {
            val item = pages[position]
            return BaseFragment(
                item.fragmentCreator,
                prefs,
                forcedFallbacks.contains(item.name),
                item,
                position
            )
        }

        override fun getCount() = pages.size

        override fun getPageTitle(position: Int): CharSequence = getString(pages[position].titleId)

        override fun getItemPosition(fragment: Any) =
            when {
                fragment !is BaseFragment -> POSITION_UNCHANGED
                fragment is WebFragment || fragment.valid -> POSITION_UNCHANGED
                else -> POSITION_NONE
            }
    }

    private val lowerVideoPaddingPointF = PointF()

    override val lowerVideoPadding: PointF
        get() {
            if (prefs.mainActivityLayout == MainActivityLayout.BOTTOM_BAR)
                lowerVideoPaddingPointF.set(0f, contentBinding.toolbar.height.toFloat())
            else
                lowerVideoPaddingPointF.set(0f, 0f)
            return lowerVideoPaddingPointF
        }

    companion object {
        private const val STATE_FORCE_FALLBACK = "frost_state_force_fallback"
        const val SELECTED_TAB_ALPHA = 255f
        const val UNSELECTED_TAB_ALPHA = 128f
    }
}

@Module
@InstallIn(ActivityComponent::class)
object MainActivityModule {
    @Provides
    @ActivityScoped
    fun contract(@ActivityContext context: Context): MainActivityContract =
        (context as? BaseMainActivity)
            ?: throw IllegalArgumentException("${context::class.java.simpleName} does not implement MainActivityContract")
}