Skip to content

Commit

Permalink
apply insets per screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Aug 17, 2024
1 parent e0f7cc4 commit 33711dc
Show file tree
Hide file tree
Showing 39 changed files with 147 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.viewbinding.ViewBinding
import com.example.util.simpletimetracker.core.extension.allowDiskWrite
import com.example.util.simpletimetracker.core.manager.ThemeManager
import com.example.util.simpletimetracker.core.provider.ContextProvider
import com.example.util.simpletimetracker.core.utils.applyStatusBarInsets
import com.example.util.simpletimetracker.core.utils.applySystemBarInsets

abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
Expand All @@ -31,7 +32,7 @@ abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
themeManager.setTheme(this)
_binding = inflater(layoutInflater)
setContentView(binding.root)
binding.root.applySystemBarInsets()
binding.root.applyStatusBarInsets()
initUi()
initUx()
initViewModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import android.view.ViewTreeObserver.OnPreDrawListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.viewbinding.ViewBinding
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.applyNavBarInsets
import kotlinx.coroutines.Job

abstract class BaseFragment<T : ViewBinding> : Fragment(), Throttler {

abstract val inflater: (LayoutInflater, ViewGroup?, Boolean) -> T
abstract val insetConfiguration: InsetConfiguration
override var throttleJob: Job? = null
protected val binding: T get() = _binding!!
private var _binding: T? = null
Expand Down Expand Up @@ -42,6 +45,7 @@ abstract class BaseFragment<T : ViewBinding> : Fragment(), Throttler {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (!initialized) {
initInsets()
initUi()
initUx()
}
Expand Down Expand Up @@ -88,6 +92,17 @@ abstract class BaseFragment<T : ViewBinding> : Fragment(), Throttler {
view?.viewTreeObserver?.addOnPreDrawListener(listener)
}

private fun initInsets() {
when (val config = insetConfiguration) {
is InsetConfiguration.DoNotApply -> {
// Do nothing
}
is InsetConfiguration.ApplyToView -> {
config.view().applyNavBarInsets()
}
}
}

inline fun <T> LiveData<T>.observe(
crossinline onChanged: (T) -> Unit,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package com.example.util.simpletimetracker.core.utils

import android.view.View
Expand Down Expand Up @@ -40,3 +42,8 @@ fun WindowInsetsCompat.getNavBarInsets(): Insets {
fun WindowInsetsCompat.getSystemBarInsets(): Insets {
return getInsets(WindowInsetsCompat.Type.systemBars())
}

sealed interface InsetConfiguration {
object DoNotApply : InsetConfiguration
data class ApplyToView(val view: () -> View) : InsetConfiguration
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.android.flexbox.JustifyContent
import dagger.hilt.android.AndroidEntryPoint
import com.example.util.simpletimetracker.feature_archive.databinding.ArchiveFragmentBinding as Binding
import androidx.core.view.isVisible
import com.example.util.simpletimetracker.core.utils.InsetConfiguration

@AndroidEntryPoint
class ArchiveFragment :
Expand All @@ -31,6 +32,9 @@ class ArchiveFragment :
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.rvArchiveList }

private val viewModel: ArchiveViewModel by viewModels()

private val archiveAdapter: BaseRecyclerAdapter by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
android:id="@+id/rvArchiveList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/dividerArchive"
tools:listitem="@layout/item_record_type_layout" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.feature_base_adapter.category.createCategoryAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.category.createCategoryAddAdapterDelegate
Expand All @@ -25,6 +26,9 @@ class CategoriesFragment : BaseFragment<Binding>() {
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.rvCategoriesList }

private val viewModel: CategoriesViewModel by viewModels()

private val categoriesAdapter: BaseRecyclerAdapter by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
android:id="@+id/rvCategoriesList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/dividerCategories"
tools:listitem="@layout/item_category_layout" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.example.util.simpletimetracker.core.extension.hideKeyboard
import com.example.util.simpletimetracker.core.extension.observeOnce
import com.example.util.simpletimetracker.core.extension.setSharedTransitions
import com.example.util.simpletimetracker.core.extension.showKeyboard
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.core.view.UpdateViewChooserState
import com.example.util.simpletimetracker.domain.extension.orFalse
Expand Down Expand Up @@ -51,7 +52,11 @@ class ChangeActivityFilterFragment :
BaseFragment<Binding>(),
ColorSelectionDialogListener {

override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding = Binding::inflate
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

private val viewModel: ChangeActivityFilterViewModel by viewModels()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.example.util.simpletimetracker.core.extension.hideKeyboard
import com.example.util.simpletimetracker.core.extension.observeOnce
import com.example.util.simpletimetracker.core.extension.setSharedTransitions
import com.example.util.simpletimetracker.core.extension.showKeyboard
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.core.view.UpdateViewChooserState
import com.example.util.simpletimetracker.domain.extension.orFalse
Expand Down Expand Up @@ -57,7 +58,11 @@ class ChangeCategoryFragment :
ColorSelectionDialogListener,
DurationDialogListener {

override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding = Binding::inflate
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

private val viewModel: ChangeCategoryViewModel by viewModels()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.dialog.TypesSelectionDialogListener
import com.example.util.simpletimetracker.core.extension.addOnBackPressedListener
import com.example.util.simpletimetracker.core.extension.observeOnce
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.core.view.UpdateViewChooserState
import com.example.util.simpletimetracker.domain.extension.orFalse
Expand Down Expand Up @@ -48,7 +49,11 @@ class ChangeComplexRuleFragment :
BaseFragment<Binding>(),
TypesSelectionDialogListener {

override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding = Binding::inflate
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

private val viewModel: ChangeComplexRuleViewModel by viewModels()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.example.util.simpletimetracker.core.extension.observeOnce
import com.example.util.simpletimetracker.core.extension.setSharedTransitions
import com.example.util.simpletimetracker.core.extension.toViewData
import com.example.util.simpletimetracker.core.sharedViewModel.RemoveRecordViewModel
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordViewData
Expand All @@ -33,6 +34,9 @@ class ChangeRecordFragment :
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

@Inject
lateinit var removeRecordViewModelFactory: BaseViewModelFactory<RemoveRecordViewModel>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.example.util.simpletimetracker.core.extension.setSharedTransitions
import com.example.util.simpletimetracker.core.extension.showKeyboard
import com.example.util.simpletimetracker.core.extension.toViewData
import com.example.util.simpletimetracker.core.repo.DeviceRepo
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.core.view.UpdateViewChooserState
import com.example.util.simpletimetracker.domain.extension.orFalse
Expand Down Expand Up @@ -78,6 +79,9 @@ class ChangeRecordTagFragment :
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

@Inject
lateinit var deviceRepo: DeviceRepo

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.example.util.simpletimetracker.core.extension.setSharedTransitions
import com.example.util.simpletimetracker.core.extension.showKeyboard
import com.example.util.simpletimetracker.core.extension.toViewData
import com.example.util.simpletimetracker.core.repo.DeviceRepo
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.core.view.UpdateViewChooserState
import com.example.util.simpletimetracker.domain.extension.orFalse
Expand Down Expand Up @@ -81,6 +82,9 @@ class ChangeRecordTypeFragment :
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

@Inject
lateinit var deviceRepo: DeviceRepo

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.example.util.simpletimetracker.core.dialog.DateTimeDialogListener
import com.example.util.simpletimetracker.core.extension.observeOnce
import com.example.util.simpletimetracker.core.extension.setSharedTransitions
import com.example.util.simpletimetracker.core.extension.toViewData
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate
import com.example.util.simpletimetracker.feature_base_adapter.runningRecord.RunningRecordViewData
import com.example.util.simpletimetracker.feature_change_record.view.ChangeRecordCore
Expand All @@ -33,6 +34,9 @@ class ChangeRunningRecordFragment :
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

@Inject
lateinit var router: Router

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.feature_base_adapter.hint.createHintAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.loader.createLoaderAdapterDelegate
Expand All @@ -20,6 +21,9 @@ class ComplexRulesFragment : BaseFragment<Binding>() {
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.rvComplexRulesList }

private val viewModel: ComplexRulesViewModel by viewModels()

private val rulesAdapter: BaseRecyclerAdapter by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
android:id="@+id/rvComplexRulesList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/dividerComplexRules"
tools:listitem="@layout/item_complex_rule_layout" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.example.util.simpletimetracker.core.dialog.RecordsFilterListener
import com.example.util.simpletimetracker.core.dialog.StandardDialogListener
import com.example.util.simpletimetracker.core.extension.hideKeyboard
import com.example.util.simpletimetracker.core.extension.showKeyboard
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.feature_base_adapter.category.createCategoryAdapterDelegate
import com.example.util.simpletimetracker.feature_data_edit.dialog.DataEditTagSelectionDialogListener
Expand Down Expand Up @@ -43,6 +44,9 @@ class DataEditFragment :
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }

private val viewModel: DataEditViewModel by viewModels()

private val addTagsPreviewAdapter: BaseRecyclerAdapter by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.utils.BuildVersions
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.domain.extension.orZero
import java.util.Calendar
import com.example.util.simpletimetracker.feature_dialogs.databinding.DateDialogFragmentBinding as Binding
Expand All @@ -14,6 +15,9 @@ class DateDialogFragment : BaseFragment<Binding>() {
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.DoNotApply

interface OnDateSetListener {
fun onDateSet(year: Int, monthOfYear: Int, dayOfMonth: Int)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.domain.extension.orFalse
import com.example.util.simpletimetracker.domain.extension.orZero
import java.util.Calendar
Expand All @@ -18,6 +19,9 @@ class TimeDialogFragment : BaseFragment<Binding>() {
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.DoNotApply

interface OnTimeSetListener {
fun onTimeSet(hourOfDay: Int, minute: Int, seconds: Int)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.di.BaseViewModelFactory
import com.example.util.simpletimetracker.core.sharedViewModel.MainTabsViewModel
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.feature_base_adapter.empty.createEmptyAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.hint.createHintAdapterDelegate
Expand All @@ -24,6 +25,9 @@ class GoalsFragment : BaseFragment<Binding>() {
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.rvGoalsList }

@Inject
lateinit var mainTabsViewModelFactory: BaseViewModelFactory<MainTabsViewModel>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
android:id="@+id/rvGoalsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:itemCount="15"
tools:listitem="@layout/item_statistics_goal_layout" />
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.example.util.simpletimetracker.core.di.BaseViewModelFactory
import com.example.util.simpletimetracker.core.extension.addOnBackPressedListener
import com.example.util.simpletimetracker.core.extension.addOnPageChangeCallback
import com.example.util.simpletimetracker.core.sharedViewModel.MainTabsViewModel
import com.example.util.simpletimetracker.core.utils.InsetConfiguration
import com.example.util.simpletimetracker.core.utils.SHORTCUT_NAVIGATION_KEY
import com.example.util.simpletimetracker.core.view.SafeFragmentStateAdapter
import com.example.util.simpletimetracker.domain.extension.orZero
Expand All @@ -36,6 +37,9 @@ class MainFragment : BaseFragment<Binding>() {
override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Binding::inflate

override val insetConfiguration: InsetConfiguration =
InsetConfiguration.DoNotApply

@Inject
lateinit var mainTabsViewModelFactory: BaseViewModelFactory<MainTabsViewModel>

Expand Down
Loading

0 comments on commit 33711dc

Please sign in to comment.