Skip to content

Commit

Permalink
refactor: use shared toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy-Geenexus committed Aug 27, 2024
1 parent 76007a6 commit 8529262
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 174 deletions.
101 changes: 64 additions & 37 deletions app/src/main/kotlin/com/none/tom/exiferaser/ExifEraserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ import android.os.Build
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.FrameLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.window.core.layout.WindowSizeClass
import androidx.window.core.layout.WindowWidthSizeClass
import androidx.window.layout.WindowMetricsCalculator
Expand Down Expand Up @@ -63,43 +68,63 @@ class ExifEraserActivity : AppCompatActivity() {
installSplashScreen()
enableEdgeToEdge()
super.onCreate(savedInstanceState)
val binding = ActivityExifEraserBinding.inflate(layoutInflater)
setContentView(binding.root)
computeWindowSizeClasses()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
(supportFragmentManager.findFragmentById(R.id.nav_controller) as NavHostFragment)
.navController
.addOnDestinationChangedListener { _, destination, _ ->
if (windowSizeClass.windowWidthSizeClass != WindowWidthSizeClass.EXPANDED) {
val tv = TypedValue()
if (destination.id == R.id.fragment_main) {
theme.resolveAttribute(
com.google.android.material.R.attr.colorSurfaceContainer,
tv,
true
)
} else {
theme.resolveAttribute(
com.google.android.material.R.attr.colorSurface,
tv,
true
)
}
window.navigationBarColor = tv.data
with(ActivityExifEraserBinding.inflate(layoutInflater)) {
setContentView(root)
computeWindowSizeClasses()
val navController = findNavController()
navController.addOnDestinationChangedListener { _, navDestination, _ ->
if (navDestination.id == R.id.fragment_main) {
toolbar.setLogo(R.drawable.ic_logo)
} else {
toolbar.logo = null
}
}
setSupportActionBar(toolbar)
setupActionBarWithNavController(navController)
ViewCompat.setOnApplyWindowInsetsListener(root) { _, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.statusBars())
root.setLayoutParams(
(root.layoutParams as FrameLayout.LayoutParams).apply { topMargin = insets.top }
)
windowInsetsCompat
}
root.addView(
object : View(this@ExifEraserActivity) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
computeWindowSizeClasses()
}
}
)
}
binding.layout.addView(
object : View(this) {
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
computeWindowSizeClasses()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
findNavController().addOnDestinationChangedListener { _, destination, _ ->
if (windowSizeClass.windowWidthSizeClass != WindowWidthSizeClass.EXPANDED) {
val tv = TypedValue()
if (destination.id == R.id.fragment_main) {
theme.resolveAttribute(
com.google.android.material.R.attr.colorSurfaceContainer,
tv,
true
)
} else {
theme.resolveAttribute(
com.google.android.material.R.attr.colorSurface,
tv,
true
)
}
window.navigationBarColor = tv.data
}
}
)
}
handleSupportedIntent()
}

override fun onSupportNavigateUp(): Boolean {
return findNavController().navigateUp() || super.onSupportNavigateUp()
}

private fun computeWindowSizeClasses() {
val metrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(this)
val widthDp = metrics.bounds.width() / resources.displayMetrics.density
Expand Down Expand Up @@ -128,16 +153,13 @@ class ExifEraserActivity : AppCompatActivity() {
uris.addAll(clipData.supportedImageUrisToList())
}
if (uris.isNotEmpty()) {
(supportFragmentManager.findFragmentById(R.id.nav_controller) as NavHostFragment)
.navController
.navigate(
R.id.global_to_main,
bundleOf(NAV_ARG_IMAGE_SELECTION to uris.toTypedArray())
)
findNavController().navigate(
R.id.global_to_main,
bundleOf(NAV_ARG_IMAGE_SELECTION to uris.toTypedArray())
)
}
} else if (shortcutIntentActions.contains(intent.action)) {
(supportFragmentManager.findFragmentById(R.id.nav_controller) as NavHostFragment)
.navController
findNavController()
.createDeepLink()
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.fragment_main)
Expand All @@ -146,4 +168,9 @@ class ExifEraserActivity : AppCompatActivity() {
.startActivities()
}
}

private fun findNavController(): NavController {
return (supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment)
.navController
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ package com.none.tom.exiferaser.core.ui
import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.viewbinding.ViewBinding
import com.none.tom.exiferaser.ExifEraserActivity
import com.none.tom.exiferaser.R

abstract class BaseFragment<B : ViewBinding>(
@LayoutRes layoutRes: Int
Expand All @@ -51,17 +46,4 @@ abstract class BaseFragment<B : ViewBinding>(
abstract fun bindLayout(view: View): B

protected fun getWindowSizeClass() = (requireActivity() as ExifEraserActivity).windowSizeClass

protected fun setupToolbar(toolbar: Toolbar, @StringRes titleRes: Int) {
(requireActivity() as? AppCompatActivity)?.setSupportActionBar(toolbar)
toolbar.setTitle(titleRes)
requireParentFragment()
.childFragmentManager
.backStackEntryCount
.takeIf { count -> count > 0 }
?.let {
toolbar.setNavigationIcon(R.drawable.ic_arrow_back)
toolbar.setNavigationOnClickListener { findNavController().navigateUp() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,14 @@ class HelpFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars()
)
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars())
view.setLayoutParams(
(view.layoutParams as FrameLayout.LayoutParams).apply {
topMargin = insets.top
bottomMargin = insets.bottom
}
)
WindowInsetsCompat.CONSUMED
}
setupToolbar(binding.includeToolbar.toolbar, R.string.help)
binding.helpAndFeedback.apply {
layoutManager = LinearLayoutManager(context)
adapter = HelpAdapter(listener = this@HelpFragment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,9 @@ class ImageProcessingFragment : BaseFragment<FragmentImageProcessingBinding>(
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars()
)
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars())
view.setLayoutParams(
(view.layoutParams as FrameLayout.LayoutParams).apply {
topMargin = insets.top
bottomMargin = insets.bottom
}
)
Expand Down Expand Up @@ -107,7 +104,6 @@ class ImageProcessingFragment : BaseFragment<FragmentImageProcessingBinding>(
}
}
requireActivity().addMenuProvider(menuProvider, viewLifecycleOwner)
setupToolbar(binding.toolbarInclude.toolbar, R.string.summary)
setupResponsiveLayout()
binding.details.setOnClickListener { viewModel.handleImageProcessingDetails() }
lifecycleScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
Expand Down Expand Up @@ -100,22 +99,14 @@ class ImageProcessingDetailsFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars()
)
binding.toolbar.setLayoutParams(
(binding.toolbar.layoutParams as LinearLayout.LayoutParams).apply {
topMargin = insets.top
}
)
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars())
view.setLayoutParams(
(view.layoutParams as FrameLayout.LayoutParams).apply {
bottomMargin = insets.bottom
}
)
WindowInsetsCompat.CONSUMED
}
setupToolbar(binding.toolbar, R.string.details)
binding.details.apply {
layoutManager = LinearLayoutManager(requireContext())
adapter = ImageProcessingDetailsAdapter(listener = this@ImageProcessingDetailsFragment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.MenuProvider
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.doOnLayout
import androidx.core.view.isVisible
import androidx.draganddrop.DropHelper
Expand Down Expand Up @@ -116,15 +113,6 @@ class MainFragment :

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.statusBars())
view.setLayoutParams(
(view.layoutParams as FrameLayout.LayoutParams).apply {
topMargin = insets.top
}
)
windowInsetsCompat
}
setupResponsiveAppBarLayout()
setupResponsiveTitleLayout()
setupResponsiveImageSourceLayout()
Expand Down Expand Up @@ -452,7 +440,6 @@ class MainFragment :
}

private fun setupResponsiveAppBarLayout() {
setupToolbar(binding.toolbar, R.string.app_name)
val menuProvider = object : MenuProvider {

override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,14 @@ class SettingsFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsetsCompat ->
val insets = windowInsetsCompat.getInsets(
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars()
)
val insets = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars())
view.setLayoutParams(
(view.layoutParams as FrameLayout.LayoutParams).apply {
topMargin = insets.top
bottomMargin = insets.bottom
}
)
WindowInsetsCompat.CONSUMED
}
setupToolbar(binding.toolbarInclude.toolbar, R.string.settings)
binding.preferences.apply {
layoutManager = LinearLayoutManager(context)
adapter = SettingsAdapter(listener = this@SettingsFragment)
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/res/layout-sw600dp/fragment_image_processing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
android:layout_height="match_parent"
android:transitionGroup="true">

<include
android:id="@+id/toolbar_include"
layout="@layout/toolbar" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_progress"
android:layout_width="wrap_content"
Expand Down
9 changes: 1 addition & 8 deletions app/src/main/res/layout-sw600dp/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
android:orientation="vertical"
android:transitionGroup="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:logo="@drawable/ic_logo" />

<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progress"
style="?attr/linearProgressIndicatorStyle"
Expand All @@ -23,7 +16,7 @@
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/title"
Expand Down
25 changes: 19 additions & 6 deletions app/src/main/res/layout/activity_exif_eraser.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.fragment.app.FragmentContainerView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_controller"
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
style="?attr/toolbarSurfaceStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:minHeight="?attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.fragment.app.FragmentContainerView
android:id="@+id/container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:navGraph="@navigation/nav_graph" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
17 changes: 3 additions & 14 deletions app/src/main/res/layout/fragment_help.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/help_and_feedback"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:transitionGroup="true">

<include
android:id="@+id/include_toolbar"
layout="@layout/toolbar" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/help_and_feedback"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
android:transitionGroup="true" />
Loading

0 comments on commit 8529262

Please sign in to comment.