From bde20b8135c026d13a3da77cf1c64105762a09d1 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Sun, 28 Mar 2021 22:24:52 -0400 Subject: [PATCH] Converting main activity to binding --- .../ui/base/activity/BaseActivity.kt | 4 +- .../tachiyomi/ui/library/LibraryController.kt | 43 +++---- .../kanade/tachiyomi/ui/main/MainActivity.kt | 119 +++++++++--------- .../tachiyomi/ui/main/SearchActivity.kt | 10 +- .../ui/manga/MangaDetailsController.kt | 28 +++-- .../tachiyomi/ui/recents/RecentsController.kt | 7 +- .../ui/security/BiometricActivity.kt | 3 +- .../tachiyomi/ui/source/SourceController.kt | 11 +- .../ui/webview/BaseWebViewActivity.kt | 3 +- .../util/view/ControllerExtensions.kt | 45 +++---- app/src/main/res/layout/main_activity.xml | 2 +- 11 files changed, 142 insertions(+), 133 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index 8c7023ae6d..dba53d0440 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.base.activity import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate +import androidx.viewbinding.ViewBinding import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.main.SearchActivity import eu.kanade.tachiyomi.ui.security.BiometricActivity @@ -11,9 +12,10 @@ import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.ThemeUtil import uy.kohesive.injekt.injectLazy -abstract class BaseActivity : AppCompatActivity() { +abstract class BaseActivity : AppCompatActivity() { val preferences: PreferencesHelper by injectLazy() + lateinit var binding: VB init { @Suppress("LeakingThis") diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 4caa6fd684..6f5bd68dca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -77,6 +77,7 @@ import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.getBottomGestureInsets import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.launchUI +import eu.kanade.tachiyomi.util.view.activityBinding import eu.kanade.tachiyomi.util.view.collapse import eu.kanade.tachiyomi.util.view.expand import eu.kanade.tachiyomi.util.view.getItemView @@ -96,7 +97,6 @@ import eu.kanade.tachiyomi.util.view.visible import eu.kanade.tachiyomi.util.view.visibleIf import eu.kanade.tachiyomi.util.view.withFadeTransaction import eu.kanade.tachiyomi.widget.EndAnimatorListener -import kotlinx.android.synthetic.main.main_activity.* import kotlinx.coroutines.delay import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -256,7 +256,7 @@ class LibraryController( } fun updateFilterSheetY() { - val bottomBar = activity?.bottom_nav + val bottomBar = activityBinding?.bottomNav if (bottomBar != null) { if (binding.filterBottomSheet.filterBottomSheet.sheetBehavior.isHidden()) { val pad = bottomBar.translationY - bottomBar.height @@ -292,8 +292,8 @@ class LibraryController( if (preferences.autohideHopper().get()) { // Flow same snap rules as bottom nav val closerToHopperBottom = hopperOffset > 25f.dpToPx - val halfWayBottom = activity!!.bottom_nav.height.toFloat() / 2 - val closerToBottom = activity!!.bottom_nav.translationY > halfWayBottom + val halfWayBottom = activityBinding?.bottomNav?.height?.toFloat()?.div(2) ?: 0f + val closerToBottom = (activityBinding?.bottomNav?.translationY ?: 0f) > halfWayBottom val atTop = !binding.libraryGridRecycler.recycler.canScrollVertically(-1) val closerToEdge = if (preferences.hideBottomNavOnScroll().get()) (closerToBottom && !atTop) else closerToHopperBottom val end = if (closerToEdge) 55f.dpToPx else 0f @@ -362,8 +362,9 @@ class LibraryController( private fun showFilterTip() { if (preferences.shownFilterTutorial().get() || !hasExpanded) return + val activityBinding = activityBinding ?: return val activity = activity ?: return - val icon = activity.bottom_nav.getItemView(R.id.nav_library) ?: return + val icon = activityBinding.bottomNav.getItemView(R.id.nav_library) ?: return filterTooltip = ViewTooltip.on(activity, icon).autoHide(false, 0L).align(ViewTooltip.ALIGN.START) .position(ViewTooltip.Position.TOP).text(R.string.tap_library_to_show_filters) @@ -582,7 +583,7 @@ class LibraryController( } binding.roundedCategoryHopper.categoryButton.setOnLongClickListener { - activity?.toolbar?.menu?.performIdentifierAction(R.id.action_search, 0) + activityBinding?.toolbar?.menu?.performIdentifierAction(R.id.action_search, 0) true } @@ -627,7 +628,7 @@ class LibraryController( val view = view ?: return val listOfYs = mutableListOf( binding.filterBottomSheet.filterBottomSheet.y, - activity?.bottom_nav?.y ?: binding.filterBottomSheet.filterBottomSheet.y + activityBinding?.bottomNav?.y ?: binding.filterBottomSheet.filterBottomSheet.y ) val insetBottom = view.rootWindowInsets?.systemWindowInsetBottom ?: 0 if (!preferences.autohideHopper().get()) { @@ -639,8 +640,8 @@ class LibraryController( binding.libraryGridRecycler.recycler.translationY if (view.height - insetBottom < binding.categoryHopperFrame.y) { binding.jumperCategoryText.translationY = - -(binding.categoryHopperFrame.y - (view.height - insetBottom)) - +binding.libraryGridRecycler.recycler.translationY + -(binding.categoryHopperFrame.y - (view.height - insetBottom)) + + binding.libraryGridRecycler.recycler.translationY } else { binding.jumperCategoryText.translationY = binding.libraryGridRecycler.recycler.translationY } @@ -730,7 +731,7 @@ class LibraryController( return binding.root } - private fun anchorView(): View? { + private fun anchorView(): View { return if (binding.categoryHopperFrame.isVisible()) { binding.categoryHopperFrame } else { @@ -753,7 +754,7 @@ class LibraryController( private fun setRecyclerLayout() { binding.libraryGridRecycler.recycler.post { - binding.libraryGridRecycler.recycler.updatePaddingRelative(bottom = 50.dpToPx + (activity?.bottom_nav?.height ?: 0)) + binding.libraryGridRecycler.recycler.updatePaddingRelative(bottom = 50.dpToPx + (activityBinding?.bottomNav?.height ?: 0)) } if (libraryLayout == 0) { binding.libraryGridRecycler.recycler.spanCount = 1 @@ -792,14 +793,14 @@ class LibraryController( if (binding.filterBottomSheet.filterBottomSheet.sheetBehavior.isHidden()) { binding.filterBottomSheet.filterBottomSheet.invisible() } - activity?.toolbar?.hideDropdown() + activityBinding?.toolbar?.hideDropdown() } } override fun onChangeEnded(handler: ControllerChangeHandler, type: ControllerChangeType) { super.onChangeEnded(handler, type) if (!type.isEnter) { - activity?.toolbar?.hideDropdown() + activityBinding?.toolbar?.hideDropdown() } } @@ -859,7 +860,7 @@ class LibraryController( scrollToHeader(activeCategory) } binding.libraryGridRecycler.recycler.post { - elevateAppBar(binding.libraryGridRecycler.recycler.canScrollVertically(-1) == true) + elevateAppBar(binding.libraryGridRecycler.recycler.canScrollVertically(-1)) setActiveCategory() } @@ -876,7 +877,7 @@ class LibraryController( shouldScrollToTop = false } if (onRoot) { - listOf(activity?.toolbar, binding.headerTitle).forEach { + listOf(activityBinding?.toolbar, binding.headerTitle).forEach { it?.setOnClickListener { val recycler = binding.libraryGridRecycler.recycler if (singleCategory) { @@ -896,9 +897,9 @@ class LibraryController( private fun showDropdown() { if (onRoot) { if (!singleCategory) { - activity?.toolbar?.showDropdown() + activityBinding?.toolbar?.showDropdown() } else { - activity?.toolbar?.hideDropdown() + activityBinding?.toolbar?.hideDropdown() } } } @@ -939,7 +940,7 @@ class LibraryController( val translateY = if (show) full else 0f binding.libraryGridRecycler.recycler.animate().translationY(translateY).apply { setUpdateListener { - activity?.appbar?.y = 0f + activityBinding?.appBar?.y = 0f updateHopperY() } }.start() @@ -947,13 +948,13 @@ class LibraryController( binding.recyclerCover.animate().translationY(translateY).start() binding.recyclerCover.animate().alpha(if (show) 0.75f else 0f).start() binding.libraryGridRecycler.recycler.suppressLayout(show) - activity?.toolbar?.showDropdown(!show) + activityBinding?.toolbar?.showDropdown(!show) binding.swipeRefresh.isEnabled = !show setTitle() if (show) { binding.categoryRecycler.scrollToCategory(activeCategory) binding.fastScroller.hideScrollbar() - activity?.appbar?.y = 0f + activityBinding?.appBar?.y = 0f elevateAppBar(false) binding.filterBottomSheet.filterBottomSheet.sheetBehavior?.hide() } else { @@ -972,7 +973,7 @@ class LibraryController( } val headerPosition = adapter.indexOf(pos) if (headerPosition > -1) { - val appbar = activity?.appbar + val appbar = activityBinding?.appBar binding.libraryGridRecycler.recycler.suppressLayout(true) val appbarOffset = if (appbar?.y ?: 0f > -20) 0 else ( appbar?.y?.plus( diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 1f757ccc82..0c5b8bd301 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -45,6 +45,7 @@ import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.updater.UpdateChecker import eu.kanade.tachiyomi.data.updater.UpdateResult +import eu.kanade.tachiyomi.databinding.MainActivityBinding import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.controller.BaseController @@ -73,7 +74,6 @@ import eu.kanade.tachiyomi.util.view.updatePadding import eu.kanade.tachiyomi.util.view.visibleIf import eu.kanade.tachiyomi.util.view.withFadeTransaction import eu.kanade.tachiyomi.widget.EndAnimatorListener -import kotlinx.android.synthetic.main.main_activity.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay @@ -85,7 +85,7 @@ import java.util.Date import java.util.concurrent.TimeUnit import kotlin.math.abs -open class MainActivity : BaseActivity(), DownloadServiceListener { +open class MainActivity : BaseActivity(), DownloadServiceListener { protected lateinit var router: Router @@ -119,8 +119,6 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { extraViewForUndo = extraViewToCheck } - lateinit var tabAnimator: TabsAnimator - override fun onCreate(savedInstanceState: Bundle?) { // Create a webview before extensions do or else they will break night mode theme // https://stackoverflow.com/questions/54191883 @@ -138,10 +136,10 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { return } gestureDetector = GestureDetectorCompat(this, GestureListener()) + binding = MainActivityBinding.inflate(layoutInflater) - setContentView(R.layout.main_activity) - - setSupportActionBar(toolbar) + setContentView(binding.root) + setSupportActionBar(binding.toolbar) drawerArrow = DrawerArrowDrawable(this) drawerArrow?.color = getResourceColor(R.attr.actionBarTintColor) @@ -155,11 +153,11 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { ) var continueSwitchingTabs = false - bottom_nav.getItemView(R.id.nav_library)?.setOnLongClickListener { + binding.bottomNav.getItemView(R.id.nav_library)?.setOnLongClickListener { if (!LibraryUpdateService.isRunning()) { LibraryUpdateService.start(this) - main_content.snack(R.string.updating_library) { - anchorView = bottom_nav + binding.mainContent.snack(R.string.updating_library) { + anchorView = binding.bottomNav setAction(R.string.cancel) { LibraryUpdateService.stop(context) Handler().post { NotificationReceiver.dismissNotification(context, Notifications.ID_LIBRARY_PROGRESS) } @@ -169,9 +167,9 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { true } for (id in listOf(R.id.nav_recents, R.id.nav_browse)) { - bottom_nav.getItemView(id)?.setOnLongClickListener { - bottom_nav.selectedItemId = id - bottom_nav.post { + binding.bottomNav.getItemView(id)?.setOnLongClickListener { + binding.bottomNav.selectedItemId = id + binding.bottomNav.post { val controller = router.backstack.firstOrNull()?.controller() as? BottomSheetController controller?.showSheet() @@ -179,13 +177,13 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { true } } - bottom_nav.setOnNavigationItemSelectedListener { item -> + binding.bottomNav.setOnNavigationItemSelectedListener { item -> val id = item.itemId val currentController = router.backstack.lastOrNull()?.controller() if (!continueSwitchingTabs && currentController is BottomNavBarInterface) { if (!currentController.canChangeTabs { continueSwitchingTabs = true - this@MainActivity.bottom_nav.selectedItemId = id + this@MainActivity.binding.bottomNav.selectedItemId = id } ) return@setOnNavigationItemSelectedListener false } @@ -209,9 +207,9 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { } true } - val container: ViewGroup = findViewById(R.id.controller_container) + val container: ViewGroup = binding.controllerContainer - val content: ViewGroup = findViewById(R.id.main_content) + val content: ViewGroup = binding.mainContent DownloadService.addListener(this) content.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION @@ -221,7 +219,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { supportActionBar?.setDisplayShowCustomEnabled(true) setNavBarColor(content.rootWindowInsets) - bottom_view.gone() + binding.bottomView.gone() content.doOnApplyWindowInsets { v, insets, _ -> setNavBarColor(insets) val contextView = window?.decorView?.findViewById(R.id.action_mode_bar) @@ -235,12 +233,12 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { left = insets.systemWindowInsetLeft, right = insets.systemWindowInsetRight ) - appbar.updatePadding( + binding.appBar.updatePadding( top = insets.systemWindowInsetTop ) - bottom_nav.updatePadding(bottom = insets.systemWindowInsetBottom) - bottom_view.visibleIf(insets.systemWindowInsetBottom > 0) - bottom_view.updateLayoutParams { + binding.bottomNav.updatePadding(bottom = insets.systemWindowInsetBottom) + binding.bottomView.visibleIf(insets.systemWindowInsetBottom > 0) + binding.bottomView.updateLayoutParams { height = insets.systemWindowInsetBottom } } @@ -253,16 +251,16 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { } } - toolbar.setNavigationOnClickListener { + binding.toolbar.setNavigationOnClickListener { val rootSearchController = router.backstack.lastOrNull()?.controller() if (rootSearchController is RootSearchInterface) { rootSearchController.expandSearch() } else onBackPressed() } - bottom_nav.visibleIf(!hideBottomNav) - bottom_view.visibility = if (hideBottomNav) View.GONE else bottom_view.visibility - bottom_nav.alpha = if (hideBottomNav) 0f else 1f + binding.bottomNav.visibleIf(!hideBottomNav) + binding.bottomView.visibility = if (hideBottomNav) View.GONE else binding.bottomView.visibility + binding.bottomNav.alpha = if (hideBottomNav) 0f else 1f router.addChangeListener( object : ControllerChangeHandler.ControllerChangeListener { override fun onChangeStarted( @@ -273,9 +271,9 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { handler: ControllerChangeHandler ) { syncActivityViewWithController(to, from, isPush) - appbar.y = 0f + binding.appBar.y = 0f if (!isPush || router.backstackSize == 1) { - bottom_nav.translationY = 0f + binding.bottomNav.translationY = 0f } snackBar?.dismiss() } @@ -287,8 +285,8 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { container: ViewGroup, handler: ControllerChangeHandler ) { - appbar.y = 0f - bottom_nav.translationY = 0f + binding.appBar.y = 0f + binding.bottomNav.translationY = 0f showDLQueueTutorial() } } @@ -296,7 +294,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { syncActivityViewWithController(router.backstack.lastOrNull()?.controller()) - toolbar.navigationIcon = if (router.backstackSize > 1) drawerArrow else searchDrawable + binding.toolbar.navigationIcon = if (router.backstackSize > 1) drawerArrow else searchDrawable (router.backstack.lastOrNull()?.controller() as? BaseController<*>)?.setTitle() (router.backstack.lastOrNull()?.controller() as? SettingsController)?.setTitle() @@ -317,7 +315,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { } fun setDismissIcon(enabled: Boolean) { - toolbar.navigationIcon = if (enabled) dismissDrawable else searchDrawable + binding.toolbar.navigationIcon = if (enabled) dismissDrawable else searchDrawable } private fun setNavBarColor(insets: WindowInsets?) { @@ -373,10 +371,10 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { private fun setExtensionsBadge() { val updates = preferences.extensionUpdatesCount().getOrDefault() if (updates > 0) { - val badge = bottom_nav.getOrCreateBadge(R.id.nav_browse) + val badge = binding.bottomNav.getOrCreateBadge(R.id.nav_browse) badge.number = updates } else { - bottom_nav.removeBadge(R.id.nav_browse) + binding.bottomNav.removeBadge(R.id.nav_browse) } } @@ -392,7 +390,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { if (router.backstackSize == 1 && this !is SearchActivity && downloadManager.hasQueue() && !preferences.shownDownloadQueueTutorial().get() ) { - val recentsItem = bottom_nav.getItemView(R.id.nav_recents) ?: return + val recentsItem = binding.bottomNav.getItemView(R.id.nav_recents) ?: return preferences.shownDownloadQueueTutorial().set(true) TapTargetView.showFor( this, @@ -408,7 +406,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { object : TapTargetView.Listener() { override fun onTargetClick(view: TapTargetView) { super.onTargetClick(view) - bottom_nav.selectedItemId = R.id.nav_recents + binding.bottomNav.selectedItemId = R.id.nav_recents } } ) @@ -472,20 +470,20 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { intent.getIntExtra("groupId", 0) ) when (intent.action) { - SHORTCUT_LIBRARY -> bottom_nav.selectedItemId = R.id.nav_library + SHORTCUT_LIBRARY -> binding.bottomNav.selectedItemId = R.id.nav_library SHORTCUT_RECENTLY_UPDATED, SHORTCUT_RECENTLY_READ -> { - bottom_nav.selectedItemId = R.id.nav_recents + binding.bottomNav.selectedItemId = R.id.nav_recents val controller: Controller = when (intent.action) { SHORTCUT_RECENTLY_UPDATED -> RecentChaptersController() else -> RecentlyReadController() } router.pushController(controller.withFadeTransaction()) } - SHORTCUT_BROWSE -> bottom_nav.selectedItemId = R.id.nav_browse + SHORTCUT_BROWSE -> binding.bottomNav.selectedItemId = R.id.nav_browse SHORTCUT_EXTENSIONS -> { - bottom_nav.selectedItemId = R.id.nav_browse + binding.bottomNav.selectedItemId = R.id.nav_browse router.popToRoot() - bottom_nav.post { + binding.bottomNav.post { val controller = router.backstack.firstOrNull()?.controller() as? SourceController controller?.showSheet() @@ -493,13 +491,13 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { } SHORTCUT_MANGA -> { val extras = intent.extras ?: return false - if (router.backstack.isEmpty()) bottom_nav.selectedItemId = R.id.nav_library + if (router.backstack.isEmpty()) binding.bottomNav.selectedItemId = R.id.nav_library router.pushController(MangaDetailsController(extras).withFadeTransaction()) } SHORTCUT_DOWNLOADS -> { - bottom_nav.selectedItemId = R.id.nav_recents + binding.bottomNav.selectedItemId = R.id.nav_recents router.popToRoot() - bottom_nav.post { + binding.bottomNav.post { val controller = router.backstack.firstOrNull()?.controller() as? RecentsController controller?.showSheet() @@ -513,7 +511,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { override fun onDestroy() { super.onDestroy() DownloadService.removeListener(this) - toolbar?.setNavigationOnClickListener(null) + binding.toolbar.setNavigationOnClickListener(null) } override fun onBackPressed() { @@ -522,7 +520,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { else !router.handleBack() ) { if (preferences.backReturnsToStart().get() && this !is SearchActivity && - startingTab() != bottom_nav?.selectedItemId + startingTab() != binding.bottomNav.selectedItemId ) { goToStartingTab() } else { @@ -536,12 +534,11 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { } private fun setStartingTab() { - if (bottom_nav?.selectedItemId != R.id.nav_browse && - bottom_nav?.selectedItemId != null && + if (binding.bottomNav.selectedItemId != R.id.nav_browse && preferences.startingTab().get() >= 0 ) { preferences.startingTab().set( - when (bottom_nav?.selectedItemId) { + when (binding.bottomNav.selectedItemId) { R.id.nav_library -> 0 else -> 1 } @@ -560,7 +557,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { } private fun goToStartingTab() { - bottom_nav.selectedItemId = startingTab() + binding.bottomNav.selectedItemId = startingTab() } private fun setRoot(controller: Controller, id: Int) { @@ -620,27 +617,27 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { val onRoot = router.backstackSize == 1 if (onRoot) { window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) - toolbar.navigationIcon = searchDrawable + binding.toolbar.navigationIcon = searchDrawable } else { window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) - toolbar.navigationIcon = drawerArrow + binding.toolbar.navigationIcon = drawerArrow } drawerArrow?.progress = 1f - bottom_nav.visibility = if (!hideBottomNav) View.VISIBLE else bottom_nav.visibility + binding.bottomNav.visibility = if (!hideBottomNav) View.VISIBLE else binding.bottomNav.visibility animationSet?.cancel() animationSet = AnimatorSet() val alphaAnimation = ValueAnimator.ofFloat( - bottom_nav.alpha, + binding.bottomNav.alpha, if (hideBottomNav) 0f else 1f ) alphaAnimation.addUpdateListener { valueAnimator -> - bottom_nav.alpha = valueAnimator.animatedValue as Float + binding.bottomNav.alpha = valueAnimator.animatedValue as Float } alphaAnimation.addListener( EndAnimatorListener { - bottom_nav.visibleIf(!hideBottomNav) - bottom_view.visibility = if (hideBottomNav) View.GONE else bottom_view.visibility + binding.bottomNav.visibleIf(!hideBottomNav) + binding.bottomView.visibility = if (hideBottomNav) View.GONE else binding.bottomView.visibility } ) alphaAnimation.duration = 200 @@ -653,10 +650,10 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { val hasQueue = downloading || downloadManager.hasQueue() launchUI { if (hasQueue) { - bottom_nav?.getOrCreateBadge(R.id.nav_recents) + binding.bottomNav.getOrCreateBadge(R.id.nav_recents) showDLQueueTutorial() } else { - bottom_nav?.removeBadge(R.id.nav_recents) + binding.bottomNav.removeBadge(R.id.nav_recents) } } } @@ -677,7 +674,7 @@ open class MainActivity : BaseActivity(), DownloadServiceListener { val diffX = e2.x - e1.x if (abs(diffX) <= abs(diffY)) { val sheetRect = Rect() - bottom_nav.getGlobalVisibleRect(sheetRect) + binding.bottomNav.getGlobalVisibleRect(sheetRect) if (sheetRect.contains(e1.x.toInt(), e1.y.toInt()) && abs(diffY) > Companion.SWIPE_THRESHOLD && abs(velocityY) > Companion.SWIPE_VELOCITY_THRESHOLD && @@ -720,7 +717,7 @@ interface BottomNavBarInterface { interface RootSearchInterface { fun expandSearch() { - if (this is Controller) activity?.toolbar?.menu?.findItem(R.id.action_search) + if (this is Controller) (activity as? MainActivity)?.binding?.toolbar?.menu?.findItem(R.id.action_search) ?.expandActionView() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt index 78dda395c1..4763cf6cdc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/SearchActivity.kt @@ -16,14 +16,13 @@ import eu.kanade.tachiyomi.ui.setting.SettingsReaderController import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchController import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.withFadeTransaction -import kotlinx.android.synthetic.main.main_activity.* class SearchActivity : MainActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - toolbar?.navigationIcon = drawerArrow - toolbar?.setNavigationOnClickListener { + binding.toolbar?.navigationIcon = drawerArrow + binding.toolbar?.setNavigationOnClickListener { popToRoot() } } @@ -56,10 +55,11 @@ class SearchActivity : MainActivity() { if (from is DialogController || to is DialogController) { return } - toolbar.navigationIcon = drawerArrow + binding.toolbar.navigationIcon = drawerArrow drawerArrow?.progress = 1f - bottom_nav.gone() + binding.bottomNav.gone() + binding.bottomView.gone() } override fun handleIntentAction(intent: Intent): Boolean { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt index f747cb54f3..702af1a2f5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt @@ -92,6 +92,7 @@ import eu.kanade.tachiyomi.util.system.isInNightMode import eu.kanade.tachiyomi.util.system.isOnline import eu.kanade.tachiyomi.util.system.launchUI import eu.kanade.tachiyomi.util.system.toast +import eu.kanade.tachiyomi.util.view.activityBinding import eu.kanade.tachiyomi.util.view.getText import eu.kanade.tachiyomi.util.view.requestPermissionsSafe import eu.kanade.tachiyomi.util.view.scrollViewWith @@ -199,7 +200,7 @@ class MangaDetailsController : setPaletteColor() adapter?.fastScroller = fast_scroller fast_scroller.addOnScrollStateChangeListener { - activity?.appbar?.y = 0f + activityBinding?.appBar?.y = 0f } presenter.onCreate() @@ -234,7 +235,7 @@ class MangaDetailsController : val offset = 10.dpToPx swipe_refresh.setStyle() swipe_refresh.setDistanceToTriggerSync(70.dpToPx) - activity!!.appbar.elevation = 0f + activityBinding!!.appBar.elevation = 0f scrollViewWith( recycler, @@ -365,13 +366,14 @@ class MangaDetailsController : /** Set toolbar theme for themes that are inverted (ie. light blue theme) */ private fun setActionBar(forThis: Boolean) { - val activity = activity ?: return + val activity = activity as? MainActivity ?: return + val activityBinding = activityBinding ?: return // if the theme is using inverted toolbar color if (!activity.isInNightMode() && ThemeUtil.isBlueTheme(presenter.preferences.theme())) { - if (forThis) (activity as MainActivity).appbar.context.setTheme( + if (forThis) activityBinding.appBar.context.setTheme( R.style.ThemeOverlay_AppCompat_DayNight_ActionBar ) - else (activity as MainActivity).appbar.context.setTheme( + else activityBinding.appBar.context.setTheme( R.style.Theme_ActionBar_Dark_DayNight ) @@ -399,8 +401,8 @@ class MangaDetailsController : (activity as MainActivity).toolbar.setBackgroundColor(translucentColor) translucentColor } else Color.TRANSPARENT - (activity as MainActivity).appbar.setBackgroundColor(Color.TRANSPARENT) - (activity as MainActivity).toolbar.setBackgroundColor( + activityBinding?.appBar?.setBackgroundColor(Color.TRANSPARENT) + activityBinding?.toolbar?.setBackgroundColor( activity?.window?.statusBarColor ?: Color.TRANSPARENT ) @@ -450,8 +452,8 @@ class MangaDetailsController : if (router.backstackSize > 0 && router.backstack.last().controller() !is MangaDetailsController ) { - (activity as? MainActivity)?.appbar?.setBackgroundColor(colorSecondary) - (activity as? MainActivity)?.toolbar?.setBackgroundColor(colorSecondary) + activityBinding?.appBar?.setBackgroundColor(colorSecondary) + activityBinding?.toolbar?.setBackgroundColor(colorSecondary) activity?.window?.statusBarColor = activity?.getResourceColor( android.R.attr.statusBarColor @@ -975,7 +977,7 @@ class MangaDetailsController : override fun updateScroll() { if (recycler?.canScrollVertically(-1) == false) { getHeader()?.backdrop?.translationY = 0f - activity?.appbar?.y = 0f + activityBinding?.appBar?.y = 0f colorToolbar(isColor = false, animate = false) } } @@ -1377,10 +1379,10 @@ class MangaDetailsController : expandedImageView.requestLayout() val activity = activity as? MainActivity ?: return - val currTheme = activity.appbar.context.theme + val currTheme = activityBinding!!.appBar.context.theme val currColor = activity.drawerArrow?.color if (!activity.isInNightMode()) { - activity.appbar.context.setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar) + activityBinding?.appBar?.context?.setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar) val iconPrimary = Color.WHITE activity.toolbar.setTitleTextColor(iconPrimary) @@ -1474,7 +1476,7 @@ class MangaDetailsController : interpolator = DecelerateInterpolator() if (!activity.isInNightMode()) { - activity.appbar.context.setTheme( + activityBinding?.appBar?.context?.setTheme( ThemeUtil.theme(presenter.preferences.theme()) ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt index c044c6d72a..c4e4f70c2a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt @@ -40,6 +40,7 @@ import eu.kanade.tachiyomi.ui.recently_read.RemoveHistoryDialog import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.spToPx import eu.kanade.tachiyomi.util.system.toast +import eu.kanade.tachiyomi.util.view.activityBinding import eu.kanade.tachiyomi.util.view.expand import eu.kanade.tachiyomi.util.view.isExpanded import eu.kanade.tachiyomi.util.view.requestPermissionsSafe @@ -163,12 +164,12 @@ class RecentsController(bundle: Bundle? = null) : val shadow2 = shadow2 ?: return shadow2.alpha = (1 - abs(progress)) * 0.25f shadow.alpha = (1 - abs(progress)) * 0.5f - if (progress >= 0) activity?.appbar?.elevation = max( + if (progress >= 0) activityBinding?.appBar?.elevation = max( progress * 15f, if (recycler.canScrollVertically(-1)) 15f else 0f ) sheet_layout.alpha = 1 - progress - activity?.appbar?.y = max(activity!!.appbar.y, -headerHeight * (1 - progress)) + activityBinding?.appBar?.y = max(activityBinding!!.appBar.y, -headerHeight * (1 - progress)) val oldShow = showingDownloads showingDownloads = progress > 0.92f if (oldShow != showingDownloads) { @@ -179,7 +180,7 @@ class RecentsController(bundle: Bundle? = null) : override fun onStateChanged(p0: View, state: Int) { if (this@RecentsController.view == null) return - if (state == BottomSheetBehavior.STATE_EXPANDED) activity?.appbar?.y = 0f + if (state == BottomSheetBehavior.STATE_EXPANDED) activityBinding?.appBar?.y = 0f if (state == BottomSheetBehavior.STATE_EXPANDED || state == BottomSheetBehavior.STATE_COLLAPSED) { sheet_layout.alpha = if (state == BottomSheetBehavior.STATE_COLLAPSED) 1f else 0f diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricActivity.kt index 72f221700f..9111af9e3e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/security/BiometricActivity.kt @@ -3,12 +3,13 @@ package eu.kanade.tachiyomi.ui.security import android.os.Bundle import androidx.biometric.BiometricPrompt import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.databinding.MainActivityBinding import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import java.util.Date import java.util.concurrent.ExecutorService import java.util.concurrent.Executors -class BiometricActivity : BaseActivity() { +class BiometricActivity : BaseActivity() { private val executor: ExecutorService = Executors.newSingleThreadExecutor() override fun onCreate(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt index 1cefe18c31..1fb34f6816 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/source/SourceController.kt @@ -41,6 +41,7 @@ import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.openInBrowser import eu.kanade.tachiyomi.util.system.spToPx +import eu.kanade.tachiyomi.util.view.activityBinding import eu.kanade.tachiyomi.util.view.collapse import eu.kanade.tachiyomi.util.view.expand import eu.kanade.tachiyomi.util.view.isCollapsed @@ -172,11 +173,11 @@ class SourceController : override fun onSlide(bottomSheet: View, progress: Float) { val recycler = recycler ?: return shadow2?.alpha = (1 - max(0f, progress)) * 0.25f - activity?.appbar?.elevation = min( + activityBinding?.appBar?.elevation = min( (1f - progress) * 15f, if (recycler.canScrollVertically(-1)) 15f else 0f ) - activity?.appbar?.y = max(activity!!.appbar.y, -headerHeight * (1 - progress)) + activityBinding?.appBar?.y = max(activityBinding!!.appBar.y, -headerHeight * (1 - progress)) val oldShow = showingExtensions showingExtensions = progress > 0.92f if (oldShow != showingExtensions) { @@ -189,7 +190,7 @@ class SourceController : override fun onStateChanged(p0: View, state: Int) { val extBottomSheet = ext_bottom_sheet ?: return if (state == BottomSheetBehavior.STATE_EXPANDED) { - activity?.appbar?.y = 0f + activityBinding?.appBar?.y = 0f } if (state == BottomSheetBehavior.STATE_EXPANDED || state == BottomSheetBehavior.STATE_COLLAPSED @@ -227,7 +228,7 @@ class SourceController : fun setBottomSheetTabs(progress: Float) { val bottomSheet = ext_bottom_sheet ?: return ext_bottom_sheet.tabs.updateLayoutParams { - topMargin = ((activity?.appbar?.height?.minus(9f.dpToPx) ?: 0f) * progress).toInt() + topMargin = ((activityBinding?.appBar?.height?.minus(9f.dpToPx) ?: 0f) * progress).toInt() } val selectedColor = ColorUtils.setAlphaComponent( ContextCompat.getColor(ext_bottom_sheet.tabs.context, R.color.colorAccent), @@ -320,7 +321,7 @@ class SourceController : } if (!type.isEnter) { ext_bottom_sheet.canExpand = false - activity?.appbar?.elevation = + activityBinding?.appBar?.elevation = when { ext_bottom_sheet.sheetBehavior.isExpanded() -> 0f recycler.canScrollVertically(-1) -> 15f diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt index f9a6cdc6a3..8996e68366 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt @@ -12,6 +12,7 @@ import android.webkit.WebView import android.widget.LinearLayout import androidx.core.graphics.ColorUtils import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.databinding.WebviewActivityBinding import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.getResourceColor @@ -26,7 +27,7 @@ import eu.kanade.tachiyomi.util.view.updatePadding import eu.kanade.tachiyomi.util.view.visible import kotlinx.android.synthetic.main.webview_activity.* -open class BaseWebViewActivity : BaseActivity() { +open class BaseWebViewActivity : BaseActivity() { private var bundle: Bundle? = null diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt index 7f0a0edfbf..35384bbd04 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ControllerExtensions.kt @@ -25,13 +25,14 @@ import com.bluelinelabs.conductor.RouterTransaction import com.bluelinelabs.conductor.changehandler.FadeChangeHandler import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.databinding.MainActivityBinding import eu.kanade.tachiyomi.ui.main.BottomSheetController +import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.manga.MangaDetailsController import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.toast import kotlinx.android.synthetic.main.main_activity.* -import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import kotlin.math.abs @@ -77,11 +78,11 @@ fun Controller.liftAppbarWith(recycler: RecyclerView) { elevate = el elevationAnim?.cancel() elevationAnim = ValueAnimator.ofFloat( - activity?.appbar?.elevation ?: 0f, + activityBinding?.appBar?.elevation ?: 0f, if (el) 15f else 0f ) elevationAnim?.addUpdateListener { valueAnimator -> - activity?.appbar?.elevation = valueAnimator.animatedValue as Float + activityBinding?.appBar?.elevation = valueAnimator.animatedValue as Float } elevationAnim?.start() } @@ -112,7 +113,7 @@ fun Controller.scrollViewWith( onBottomNavUpdate: (() -> Unit)? = null ): ((Boolean) -> Unit) { var statusBarHeight = -1 - activity?.appbar?.y = 0f + activityBinding?.appBar?.y = 0f val attrsArray = intArrayOf(R.attr.actionBarSize) val array = recycler.context.obtainStyledAttributes(attrsArray) var appBarHeight = if (activity!!.toolbar.height > 0) activity!!.toolbar.height @@ -166,11 +167,11 @@ fun Controller.scrollViewWith( } else { elevationAnim?.cancel() elevationAnim = ValueAnimator.ofFloat( - activity?.appbar?.elevation ?: 0f, + activityBinding?.appBar?.elevation ?: 0f, if (el) 15f else 0f ) elevationAnim?.addUpdateListener { valueAnimator -> - activity?.appbar?.elevation = valueAnimator.animatedValue as Float + activityBinding?.appBar?.elevation = valueAnimator.animatedValue as Float } elevationAnim?.start() } @@ -247,14 +248,14 @@ fun Controller.scrollViewWith( super.onScrolled(recyclerView, dx, dy) if (router?.backstack?.lastOrNull() ?.controller() == this@scrollViewWith && statusBarHeight > -1 && - activity != null && activity!!.appbar.height > 0 && + activity != null && activityBinding!!.appBar.height > 0 && recycler.translationY == 0f ) { if (!recycler.canScrollVertically(-1)) { val shortAnimationDuration = resources?.getInteger( android.R.integer.config_shortAnimTime ) ?: 0 - activity!!.appbar.animate().y(0f) + activityBinding!!.appBar.animate().y(0f) .setDuration(shortAnimationDuration.toLong()) .start() if (router.backstackSize == 1 && isInView) { @@ -270,15 +271,14 @@ fun Controller.scrollViewWith( lastY = 0f if (elevate) elevateFunc(false) } else { - activity!!.appbar.y -= dy - activity!!.appbar.y = MathUtils.clamp( - activity!!.appbar.y, - -activity!!.appbar.height.toFloat(), + activityBinding!!.appBar.y -= dy + activityBinding!!.appBar.y = MathUtils.clamp( + activityBinding!!.appBar.y, + -activityBinding!!.appBar.height.toFloat(), 0f ) val tabBar = activity!!.bottom_nav if (tabBar != null && tabBar.isVisible() && isInView) { - val preferences: PreferencesHelper = Injekt.get() if (preferences.hideBottomNavOnScroll().get()) { tabBar.translationY += dy tabBar.translationY = MathUtils.clamp( @@ -295,14 +295,14 @@ fun Controller.scrollViewWith( if (!elevate && ( dy == 0 || ( - activity!!.appbar.y <= -activity!!.appbar.height.toFloat() || - dy == 0 && activity!!.appbar.y == 0f + activityBinding!!.appBar.y <= -activityBinding!!.appBar.height.toFloat() || + dy == 0 && activityBinding!!.appBar.y == 0f ) ) ) { elevateFunc(true) } - lastY = activity!!.appbar.y + lastY = activityBinding!!.appBar.y } } } @@ -312,14 +312,14 @@ fun Controller.scrollViewWith( if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (router?.backstack?.lastOrNull() ?.controller() == this@scrollViewWith && statusBarHeight > -1 && - activity != null && activity!!.appbar.height > 0 && + activity != null && activityBinding!!.appBar.height > 0 && recycler.translationY == 0f ) { - val halfWay = activity!!.appbar.height.toFloat() / 2 + val halfWay = activityBinding!!.appBar.height.toFloat() / 2 val shortAnimationDuration = resources?.getInteger( android.R.integer.config_shortAnimTime ) ?: 0 - val closerToTop = abs(activity!!.appbar.y) > halfWay + val closerToTop = abs(activityBinding!!.appBar.y) > halfWay val halfWayBottom = activity!!.bottom_nav.height.toFloat() / 2 val closerToBottom = activity!!.bottom_nav.translationY > halfWayBottom val atTop = !recycler.canScrollVertically(-1) @@ -328,8 +328,8 @@ fun Controller.scrollViewWith( preferences.hideBottomNavOnScroll().get() ) closerToBottom else closerToTop lastY = - if (closerToEdge && !atTop) (-activity!!.appbar.height.toFloat()) else 0f - activity!!.appbar.animate().y(lastY) + if (closerToEdge && !atTop) (-activityBinding!!.appBar.height.toFloat()) else 0f + activityBinding!!.appBar.animate().y(lastY) .setDuration(shortAnimationDuration.toLong()).start() if (activity!!.bottom_nav.isVisible && isInView && preferences.hideBottomNavOnScroll().get()) { activity!!.bottom_nav?.let { @@ -376,3 +376,6 @@ fun Controller.openInBrowser(url: String) { activity?.toast(e.message) } } + +val Controller.activityBinding: MainActivityBinding? + get() = (activity as? MainActivity)?.binding diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index 5dedcb569d..426839a249 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -21,7 +21,7 @@