Skip to content

Commit

Permalink
Merge pull request #106 from walkingice/issue_92_make_menus_be_expand…
Browse files Browse the repository at this point in the history
…able

Issue 92 make menus be expandable
  • Loading branch information
walkingice authored Mar 29, 2022
2 parents fdf9463 + a539106 commit 169b8d1
Show file tree
Hide file tree
Showing 11 changed files with 749 additions and 803 deletions.
67 changes: 41 additions & 26 deletions app/src/main/java/org/mozilla/focus/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.localbroadcastmanager.content.LocalBroadcastManager
Expand Down Expand Up @@ -83,8 +85,8 @@ import org.mozilla.rocket.landing.NavigationModel
import org.mozilla.rocket.landing.OrientationState
import org.mozilla.rocket.landing.PortraitComponent
import org.mozilla.rocket.landing.PortraitStateModel
import org.mozilla.rocket.menu.BrowserMenuDialog
import org.mozilla.rocket.menu.HomeMenuDialog
import org.mozilla.rocket.menu.BottomSheetBrowserMenuFragment
import org.mozilla.rocket.menu.BottomSheetHomeMenuFragment
import org.mozilla.rocket.periodic.FirstLaunchWorker
import org.mozilla.rocket.periodic.PeriodicReceiver
import org.mozilla.rocket.privately.PrivateMode
Expand Down Expand Up @@ -131,8 +133,6 @@ class MainActivity :
private lateinit var downloadIndicatorViewModel: DownloadIndicatorViewModel
private var promotionModel: PromotionModel? = null

private lateinit var homeMenu: HomeMenuDialog
private lateinit var browserMenu: BrowserMenuDialog
private var myshotOnBoardingDialog: Dialog? = null

private lateinit var screenNavigator: ScreenNavigator
Expand Down Expand Up @@ -231,22 +231,10 @@ class MainActivity :
}

private fun setUpMenu() {
if (::homeMenu.isInitialized) {
homeMenu.release()
}
homeMenu = HomeMenuDialog(this, R.style.BottomSheetTheme).apply {
setCanceledOnTouchOutside(true)
setOnShowListener { portraitStateModel.request(PortraitComponent.BottomMenu) }
setOnDismissListener { portraitStateModel.cancelRequest(PortraitComponent.BottomMenu) }
}
if (::browserMenu.isInitialized) {
browserMenu.release()
}
browserMenu = BrowserMenuDialog(this, R.style.BottomSheetTheme).apply {
setCanceledOnTouchOutside(true)
setOnShowListener { portraitStateModel.request(PortraitComponent.BottomMenu) }
setOnDismissListener { portraitStateModel.cancelRequest(PortraitComponent.BottomMenu) }
}
supportFragmentManager.registerFragmentLifecycleCallbacks(
FragmentStateListener(portraitStateModel),
false
)
}

private fun initBroadcastReceivers() {
Expand Down Expand Up @@ -355,8 +343,12 @@ class MainActivity :
TabTray.show(supportFragmentManager)
}
)
showHomeMenu.observe(this@MainActivity, Observer { homeMenu.show() })
showBrowserMenu.observe(this@MainActivity, Observer { browserMenu.show() })
showHomeMenu.observe(this@MainActivity) {
BottomSheetHomeMenuFragment.show(supportFragmentManager)
}
showBrowserMenu.observe(this@MainActivity) {
BottomSheetBrowserMenuFragment.show(supportFragmentManager)
}
showNewTab.observe(
this@MainActivity,
Observer {
Expand Down Expand Up @@ -698,8 +690,8 @@ class MainActivity :
}

private fun dismissAllMenus() {
homeMenu.dismiss()
browserMenu.dismiss()
BottomSheetHomeMenuFragment.dismiss(supportFragmentManager)
BottomSheetBrowserMenuFragment.dismiss(supportFragmentManager)
visibleBrowserFragment?.run { dismissAllMenus() }
getListPanelFragment()?.dismissAllowingStateLoss()
myshotOnBoardingDialog?.run {
Expand Down Expand Up @@ -862,7 +854,7 @@ class MainActivity :
@VisibleForTesting
@UiThread
fun showMyShotOnBoarding() {
val view = browserMenu.findViewById<View>(R.id.menu_screenshots)
val view = BottomSheetBrowserMenuFragment.getScreenshotMenuButton(supportFragmentManager)
view?.post {
myshotOnBoardingDialog = DialogUtils.showMyShotOnBoarding(
this@MainActivity,
Expand All @@ -878,7 +870,6 @@ class MainActivity :
)
chromeViewModel.onMyShotOnBoardingDisplayed()
}
browserMenu.show()
}

private fun checkInAppUpdate() {
Expand Down Expand Up @@ -1011,6 +1002,30 @@ class MainActivity :
}
}

private class FragmentStateListener(
private val portraitStateModel: PortraitStateModel
) : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentAttached(
fm: FragmentManager,
f: Fragment,
context: Context
) {
when (f.tag) {
BottomSheetBrowserMenuFragment.TAG,
BottomSheetHomeMenuFragment.TAG ->
portraitStateModel.request(PortraitComponent.BottomMenu)
}
}

override fun onFragmentDetached(fm: FragmentManager, f: Fragment) {
when (f.tag) {
BottomSheetBrowserMenuFragment.TAG,
BottomSheetHomeMenuFragment.TAG ->
portraitStateModel.cancelRequest(PortraitComponent.BottomMenu)
}
}
}

companion object {
const val REQUEST_CODE_IN_APP_UPDATE = 1024
const val ACTION_INSTALL_IN_APP_UPDATE = "action_install_in_app_update"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class ListPanelDialog : DialogFragment() {
this.dismissAllowingStateLoss()
}

ScrollableBottomSheetHelper.setRoundedCorner(it.container)

setTopButtonsClickListener(it)
enableLoadMore(it.mainContent)
}.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ object ScrollableBottomSheetHelper {
menuBottomMargin: Float = context.resources.getDimension(R.dimen.menu_bottom_margin),
dismissListener: () -> Unit
) {
setRoundedCorner(binding.container, cornerRadius)

binding.container.setOnClickListener { dismissListener.invoke() }

Expand All @@ -35,7 +34,10 @@ object ScrollableBottomSheetHelper {
)
}

private fun setRoundedCorner(container: CoordinatorLayout, cornerRadius: Float) {
fun setRoundedCorner(
container: ViewGroup,
cornerRadius: Float = container.resources.getDimension(R.dimen.menu_corner_radius)
) {
container.outlineProvider = RoundedCornerOutlineProvider(cornerRadius)
container.clipToOutline = true
}
Expand All @@ -55,7 +57,7 @@ object ScrollableBottomSheetHelper {

val callback = BottomSheetCallback(
bottomSheetBehavior,
rootView,
bottomSheet,
menuBottomMargin,
cornerRadius,
dismissListener
Expand All @@ -74,7 +76,7 @@ object ScrollableBottomSheetHelper {

private class BottomSheetCallback(
val bottomSheetBehavior: BottomSheetBehavior<ViewGroup>,
val rootView: ViewGroup,
val movableView: ViewGroup,
menuBottomMargin: Float,
cornerRadius: Float,
val dismissListener: () -> Unit
Expand Down Expand Up @@ -106,12 +108,12 @@ object ScrollableBottomSheetHelper {
if (translationYChanged) {
this.translationY = currentTranslationY
if (abs(currentTranslationY) <= maxTranslationY) {
rootView.translationY = currentTranslationY
movableView.translationY = currentTranslationY
} else if (currentTranslationY > maxTranslationY &&
rootView.translationY < maxTranslationY
movableView.translationY < maxTranslationY
) {
// In case of fast changing
rootView.translationY = maxTranslationY
movableView.translationY = maxTranslationY
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/mozilla/rocket/di/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import org.mozilla.rocket.home.HomeFragment
import org.mozilla.rocket.home.di.HomeModule
import org.mozilla.rocket.home.topsites.domain.GetTopSitesUseCase
import org.mozilla.rocket.home.topsites.ui.AddNewTopSitesFragment
import org.mozilla.rocket.menu.BrowserMenuDialog
import org.mozilla.rocket.menu.HomeMenuDialog
import org.mozilla.rocket.menu.BottomSheetBrowserMenuFragment
import org.mozilla.rocket.menu.BottomSheetHomeMenuFragment
import org.mozilla.rocket.menu.PrivateBrowserMenuDialog
import org.mozilla.rocket.privately.PrivateModeActivity
import org.mozilla.rocket.privately.home.PrivateHomeFragment
Expand Down Expand Up @@ -91,8 +91,8 @@ interface AppComponent {
fun inject(tabTrayFragment: TabTrayFragment)
fun inject(privateHomeFragment: PrivateHomeFragment)
fun inject(urlInputFragment: UrlInputFragment)
fun inject(homeMenuDialog: HomeMenuDialog)
fun inject(browserMenuDialog: BrowserMenuDialog)
fun inject(homeMenuFragment: BottomSheetHomeMenuFragment)
fun inject(browserMenuFragment: BottomSheetBrowserMenuFragment)
fun inject(privateBrowserMenuDialog: PrivateBrowserMenuDialog)
fun inject(browsingHistoryFragment: BrowsingHistoryFragment)
fun inject(privateModeActivity: PrivateModeActivity)
Expand Down
Loading

0 comments on commit 169b8d1

Please sign in to comment.