improve readability of toolbar/searchbar handling code

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2021-06-01 11:46:08 +02:00
parent 15d2c94d19
commit ebc3dbdf79
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B

View File

@ -23,7 +23,9 @@
package com.nextcloud.talk.controllers.base package com.nextcloud.talk.controllers.base
import android.animation.AnimatorInflater import android.animation.AnimatorInflater
import android.app.Activity
import android.content.Context import android.content.Context
import android.content.res.Resources
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -52,6 +54,7 @@ import com.nextcloud.talk.controllers.ServerSelectionController
import com.nextcloud.talk.controllers.SwitchAccountController import com.nextcloud.talk.controllers.SwitchAccountController
import com.nextcloud.talk.controllers.WebViewLoginController import com.nextcloud.talk.controllers.WebViewLoginController
import com.nextcloud.talk.controllers.base.providers.ActionBarProvider import com.nextcloud.talk.controllers.base.providers.ActionBarProvider
import com.nextcloud.talk.databinding.ActivityMainBinding
import com.nextcloud.talk.utils.DisplayUtils import com.nextcloud.talk.utils.DisplayUtils
import com.nextcloud.talk.utils.preferences.AppPreferences import com.nextcloud.talk.utils.preferences.AppPreferences
import java.util.ArrayList import java.util.ArrayList
@ -126,63 +129,89 @@ abstract class NewBaseController(@LayoutRes var layoutRes: Int, args: Bundle? =
} }
protected fun showSearchOrToolbar() { protected fun showSearchOrToolbar() {
if (activity != null && activity is MainActivity) { if (isValidActivity(activity)) {
val showSearchBar = appBarLayoutType == AppBarLayoutType.SEARCH_BAR val showSearchBar = appBarLayoutType == AppBarLayoutType.SEARCH_BAR
val activity = activity as MainActivity? val activity = activity as MainActivity
if (appBarLayoutType == AppBarLayoutType.EMPTY) { if (appBarLayoutType == AppBarLayoutType.EMPTY) {
activity!!.binding.toolbar.visibility = View.GONE hideBars(activity.binding)
activity.binding.searchToolbar.visibility = View.GONE
} else { } else {
val layoutParams = activity!!.binding.searchToolbar.layoutParams as AppBarLayout.LayoutParams
if (showSearchBar) { if (showSearchBar) {
activity.binding.searchToolbar.visibility = View.VISIBLE showSearchBar(activity.binding)
activity.binding.searchText.hint = searchHint } else {
activity.binding.toolbar.visibility = View.GONE showToolbar(activity.binding)
}
colorizeStatusBar(showSearchBar, activity, resources)
}
colorizeNavigationBar(activity, resources)
}
}
private fun isValidActivity(activity: Activity?): Boolean {
return activity != null && activity is MainActivity
}
private fun showSearchBar(binding: ActivityMainBinding) {
val layoutParams = binding.searchToolbar.layoutParams as AppBarLayout.LayoutParams
binding.searchToolbar.visibility = View.VISIBLE
binding.searchText.hint = searchHint
binding.toolbar.visibility = View.GONE
// layoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout // layoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout
// .LayoutParams.SCROLL_FLAG_SNAP | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); // .LayoutParams.SCROLL_FLAG_SNAP | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
layoutParams.scrollFlags = 0 layoutParams.scrollFlags = 0
activity.binding.appBar.stateListAnimator = AnimatorInflater.loadStateListAnimator( binding.appBar.stateListAnimator = AnimatorInflater.loadStateListAnimator(
activity.binding.appBar.context, binding.appBar.context,
R.animator.appbar_elevation_off R.animator.appbar_elevation_off
) )
} else { binding.searchToolbar.layoutParams = layoutParams
activity.binding.searchToolbar.visibility = View.GONE }
activity.binding.toolbar.visibility = View.VISIBLE
private fun showToolbar(binding: ActivityMainBinding) {
val layoutParams = binding.searchToolbar.layoutParams as AppBarLayout.LayoutParams
binding.searchToolbar.visibility = View.GONE
binding.toolbar.visibility = View.VISIBLE
layoutParams.scrollFlags = 0 layoutParams.scrollFlags = 0
activity.binding.appBar.stateListAnimator = AnimatorInflater.loadStateListAnimator( binding.appBar.stateListAnimator = AnimatorInflater.loadStateListAnimator(
activity.binding.appBar.context, binding.appBar.context,
R.animator.appbar_elevation_on R.animator.appbar_elevation_on
) )
binding.searchToolbar.layoutParams = layoutParams
} }
activity.binding.searchToolbar.layoutParams = layoutParams
if (resources != null) { private fun hideBars(binding: ActivityMainBinding) {
binding.toolbar.visibility = View.GONE
binding.searchToolbar.visibility = View.GONE
}
private fun colorizeStatusBar(showSearchBar: Boolean, activity: Activity?, resources: Resources?) {
if (activity != null && resources != null) {
if (showSearchBar) { if (showSearchBar) {
DisplayUtils.applyColorToStatusBar( DisplayUtils.applyColorToStatusBar(
activity, activity,
ResourcesCompat.getColor( ResourcesCompat.getColor(
resources!!, resources, R.color.bg_default, null
R.color.bg_default, null
) )
) )
} else { } else {
DisplayUtils.applyColorToStatusBar( DisplayUtils.applyColorToStatusBar(
activity, activity,
ResourcesCompat.getColor( ResourcesCompat.getColor(
resources!!, resources, R.color.appbar, null
R.color.appbar, null
) )
) )
} }
} }
} }
if (resources != null) {
private fun colorizeNavigationBar(activity: Activity?, resources: Resources?) {
if (activity != null && resources != null) {
DisplayUtils.applyColorToNavigationBar( DisplayUtils.applyColorToNavigationBar(
activity.window, activity.window,
ResourcesCompat.getColor(resources!!, R.color.bg_default, null) ResourcesCompat.getColor(resources, R.color.bg_default, null)
) )
} }
} }
}
override fun onDetach(view: View) { override fun onDetach(view: View) {
super.onDetach(view) super.onDetach(view)