Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. package com.foodient.whisk.features.main
  2.  
  3. import android.content.ClipboardManager
  4. import android.os.Bundle
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import androidx.coordinatorlayout.widget.CoordinatorLayout
  8. import androidx.core.content.getSystemService
  9. import androidx.core.text.buildSpannedString
  10. import androidx.core.view.forEach
  11. import androidx.lifecycle.Lifecycle
  12. import com.foodient.whisk.BuildConfig
  13. import com.foodient.whisk.R
  14. import com.foodient.whisk.core.extension.color
  15. import com.foodient.whisk.core.extension.gone
  16. import com.foodient.whisk.core.extension.injectPresenter
  17. import com.foodient.whisk.core.extension.installModule
  18. import com.foodient.whisk.core.extension.postSelf
  19. import com.foodient.whisk.core.extension.visible
  20. import com.foodient.whisk.core.notification.NotificationHelper
  21. import com.foodient.whisk.core.notification.SystemMessage
  22. import com.foodient.whisk.core.notification.SystemMessage.Notification.Duration
  23. import com.foodient.whisk.core.ui.fragment.BaseFragment
  24. import com.foodient.whisk.data.user.UserInfoProvider
  25. import com.foodient.whisk.data.user.UserInfoProviderImpl
  26. import com.foodient.whisk.features.main.nativeshare.notarecipe.NotARecipeBottomSheetDialogFragment
  27. import com.foodient.whisk.navigation.main.MainFlowScreens
  28. import com.wootric.androidsdk.Wootric
  29. import kotlinx.android.synthetic.main.fragment_main.*
  30. import ru.terrakok.cicerone.android.support.SupportAppScreen
  31. import toothpick.Scope
  32. import toothpick.ktp.binding.bind
  33.  
  34. class MainFlowFragment : BaseFragment(), MainFlowView {
  35. override val layoutRes = R.layout.fragment_main
  36.  
  37. private val currentTabFragment: BaseFragment?
  38. get() = childFragmentManager.fragments.firstOrNull { !it.isHidden } as? BaseFragment
  39.  
  40. private val presenter: MainFlowPresenter by injectPresenter()
  41.  
  42. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  43. super.onViewCreated(view, savedInstanceState)
  44. configureBottomTabs()
  45. selectTab(
  46. when (currentTabFragment?.tag) {
  47. shoppingListTab.screenKey -> shoppingListTab
  48. recipesListTab.screenKey -> recipesListTab
  49. settingsTab.screenKey -> settingsTab
  50. debugTab.screenKey -> debugTab
  51. else -> shoppingListTab
  52. }
  53. )
  54. view.setOnApplyWindowInsetsListener { _, insets ->
  55. presenter.showHideNavBar(insets.systemWindowInsetBottom == 0)
  56. insets
  57. }
  58. }
  59.  
  60. override fun installModules(scope: Scope) {
  61. super.installModules(scope)
  62. scope.installModule {
  63. val wootric = Wootric.init(requireActivity(), BuildConfig.WOOTRIC_CLIENT_ID, BuildConfig.WOOTRIC_TOKEN)
  64. bind<Wootric>().toInstance(wootric)
  65. bind<UserInfoProvider>().toClass<UserInfoProviderImpl>().singleton()
  66. }
  67. }
  68.  
  69. override fun onResume() {
  70. super.onResume()
  71. requireView().postSelf {
  72. context.getSystemService<ClipboardManager>()?.run {
  73. val clip = primaryClip?.apply {
  74. if (itemCount > 0) {
  75. presenter.getDataFromClipboard(getItemAt(0).text)
  76. }
  77. }
  78. if (clip != null) {
  79. primaryClip = clip
  80. }
  81. }
  82. }
  83. presenter.onResume()
  84. }
  85.  
  86. override fun showNotARecipeUrl() {
  87. NotARecipeBottomSheetDialogFragment.showNow(this)
  88. }
  89.  
  90. override fun showSaveRecipeNotification(possibleUrl: String, duration: Int) {
  91. val text = buildSpannedString {
  92. append(getString(R.string.recipes_save_link_from_clipboard))
  93. append("\n")
  94. color(requireContext(), R.color.gray82) {
  95. append(possibleUrl)
  96. }
  97. }
  98. NotificationHelper.showSnackBarNotification(
  99. view = findInnerCoordinator() ?: container,
  100. message = SystemMessage.Notification(
  101. text = text,
  102. duration = Duration.CUSTOM(duration),
  103. actionText = getString(R.string.btn_save),
  104. actionListener = presenter::openRecipeBuilder
  105. )
  106. )
  107. }
  108.  
  109. private fun findInnerCoordinator(): CoordinatorLayout? {
  110. return (currentTabFragment?.requireView() as? ViewGroup)?.run {
  111. findInnerCoordinatorRecursively(this)
  112. }
  113. }
  114.  
  115. private fun findInnerCoordinatorRecursively(viewGroup: ViewGroup): CoordinatorLayout? {
  116. var coordinator: CoordinatorLayout? = null
  117. viewGroup.forEach {
  118. if (it is ViewGroup) {
  119. if (it is CoordinatorLayout) {
  120. coordinator = it
  121. }
  122. val coordinatorLayout = findInnerCoordinatorRecursively(it)
  123. if (coordinatorLayout != null) {
  124. coordinator = coordinatorLayout
  125. }
  126. }
  127. }
  128. return coordinator
  129. }
  130.  
  131. override fun onBackPressed() {
  132. currentTabFragment?.onBackPressed()
  133. }
  134.  
  135. private fun configureBottomTabs() {
  136. bottomNavigationView.menu.findItem(R.id.tab_debug).isVisible = BuildConfig.DEBUG
  137. bottomNavigationView.setOnNavigationItemSelectedListener {
  138. when (it.itemId) {
  139. R.id.tab_shopping_lists -> presenter.openShoppingLists()
  140. R.id.tab_recipes_list -> presenter.openRecipes()
  141. R.id.tab_settings -> presenter.openSettings()
  142. R.id.tab_debug -> selectTab(debugTab)
  143. }
  144. true
  145. }
  146. }
  147.  
  148. private fun selectTab(tab: SupportAppScreen) {
  149. val currentFragment = currentTabFragment
  150. val newFragment = childFragmentManager.findFragmentByTag(tab.screenKey)
  151.  
  152. if (currentFragment != null && newFragment != null && currentFragment == newFragment) {
  153. return
  154. }
  155.  
  156. childFragmentManager.beginTransaction().apply {
  157. if (newFragment == null) {
  158. add(R.id.container, createTabFragment(tab), tab.screenKey)
  159. }
  160.  
  161. currentFragment?.let {
  162. hide(it)
  163. setMaxLifecycle(it, Lifecycle.State.STARTED)
  164. }
  165. newFragment?.let {
  166. show(it)
  167. setMaxLifecycle(it, Lifecycle.State.RESUMED)
  168. }
  169. }.commitNow()
  170. }
  171.  
  172. private fun createTabFragment(tab: SupportAppScreen) = tab.fragment
  173.  
  174. override fun openShoppingList() {
  175. selectTab(shoppingListTab)
  176. reselectTab(R.id.tab_shopping_lists)
  177. }
  178.  
  179. override fun openRecipeBox() {
  180. selectTab(recipesListTab)
  181. reselectTab(R.id.tab_recipes_list)
  182. }
  183.  
  184. override fun openSettings() {
  185. selectTab(settingsTab)
  186. reselectTab(R.id.tab_settings)
  187. }
  188.  
  189. private fun reselectTab(id: Int) {
  190. val item = bottomNavigationView.menu.findItem(id)
  191. if (!item.isChecked) {
  192. item.isChecked = true
  193. }
  194. }
  195.  
  196. override fun showNavBar() {
  197. bottomNavigationView.visible()
  198. }
  199.  
  200. override fun hideNavBar() {
  201. bottomNavigationView.gone()
  202. }
  203.  
  204. companion object {
  205. private val shoppingListTab = MainFlowScreens.ShoppingList
  206. private val recipesListTab = MainFlowScreens.RecipesList
  207. private val settingsTab = MainFlowScreens.Settings
  208. private val debugTab = MainFlowScreens.Debug
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement