Skip to content

Commit

Permalink
Merge pull request #209 from caarmen/issue-206-fix-horizontal-inset-l…
Browse files Browse the repository at this point in the history
…andscape

Issue #206: Fix various insets
  • Loading branch information
caarmen authored Oct 27, 2024
2 parents 014511d + f3f8758 commit 9412412
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 36 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ android {
namespace "ca.rmen.android.poetassistant"
minSdkVersion 21
targetSdkVersion 35
versionCode 113009
versionName "1.30.9"
versionCode 113010
versionName "1.30.10"
// setting vectorDrawables.useSupportLibrary = true means pngs won't be generated at
// build time: http://android-developers.blogspot.fr/2016/02/android-support-library-232.html
vectorDrawables.useSupportLibrary = true
Expand Down
31 changes: 13 additions & 18 deletions app/src/main/kotlin/ca/rmen/android/poetassistant/InsetsFix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,30 @@ package ca.rmen.android.poetassistant

import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams

fun fixInsets(view: View) {
fun getInsets(view: View, callback: (view: View, insets: Insets) -> Unit) {
// Issue #206:
// https://developer.android.com/develop/ui/views/layout/edge-to-edge#system-bars-insets
ViewCompat.setOnApplyWindowInsetsListener(view) { v, windowInsets ->
val insets = windowInsets.getInsets(
WindowInsetsCompat.Type.systemBars()
or WindowInsetsCompat.Type.displayCutout() or WindowInsetsCompat.Type.statusBars()
)
// To control the status bar color, we have to draw a view behind it.
// https://developer.android.com/reference/android/view/Window.html#setStatusBarColor(int)
// If we have this view, then make it the height of the status bar.
val statusBarView = v.findViewById<View>(R.id.status_bar_view)
statusBarView?.updateLayoutParams<ViewGroup.LayoutParams> {
height = insets.top
}
v.updateLayoutParams<ViewGroup.MarginLayoutParams> {
// If we don't have a status bar view, we need to shift the content
// of the root view down, so it's below the status bar.
if (statusBarView == null) {
topMargin = insets.top
}
leftMargin = insets.left
bottomMargin = insets.bottom
rightMargin = insets.right
}
callback(v, insets)
WindowInsetsCompat.CONSUMED
}
}

fun fixStatusBarViewForInsets(statusBarView: View, insets: Insets) {
// To control the status bar color, we have to draw a view behind it.
// Make it the height of the status bar.
// https://developer.android.com/reference/android/view/Window.html#setStatusBarColor(int)
statusBarView.updateLayoutParams<ViewGroup.LayoutParams> {
height = insets.top
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import androidx.annotation.DrawableRes
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import android.widget.TextView
import androidx.core.view.updatePadding
import ca.rmen.android.poetassistant.R
import ca.rmen.android.poetassistant.compat.VectorCompat
import ca.rmen.android.poetassistant.databinding.ActivityAboutBinding
import ca.rmen.android.poetassistant.fixInsets
import ca.rmen.android.poetassistant.fixStatusBarViewForInsets
import ca.rmen.android.poetassistant.getInsets

class AboutActivity : AppCompatActivity() {

Expand All @@ -51,7 +53,14 @@ class AboutActivity : AppCompatActivity() {
val appVersionText = getString(R.string.about_app_version, getString(R.string.app_name), versionName)
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_about)
mBinding.txtVersion.text = appVersionText
fixInsets(mBinding.root)
getInsets(mBinding.aboutContent) { view, insets ->
view.updatePadding(
left = insets.left,
right = insets.right,
bottom = insets.bottom,
)
fixStatusBarViewForInsets(mBinding.statusBarView, insets)
}
hackSetIcons()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import android.os.Bundle
import android.util.Log
import androidx.annotation.WorkerThread
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.updatePadding
import androidx.databinding.DataBindingUtil
import ca.rmen.android.poetassistant.Constants
import ca.rmen.android.poetassistant.R
import ca.rmen.android.poetassistant.dagger.DaggerHelper
import ca.rmen.android.poetassistant.databinding.ActivityLicenseBinding
import ca.rmen.android.poetassistant.fixInsets
import ca.rmen.android.poetassistant.fixStatusBarViewForInsets
import ca.rmen.android.poetassistant.getInsets
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
Expand Down Expand Up @@ -61,7 +63,14 @@ class LicenseActivity : AppCompatActivity() {
val title = intent.getStringExtra(EXTRA_TITLE)
val licenseFile = intent.getStringExtra(EXTRA_LICENSE_TEXT_ASSET_FILE)!!
mBinding.tvTitle.text = title
fixInsets(mBinding.root)
getInsets(mBinding.licenseContent) { view, insets ->
view.updatePadding(
left = insets.left,
right = insets.right,
bottom = insets.bottom,
)
fixStatusBarViewForInsets(mBinding.statusBarView, insets)
}
val threading = DaggerHelper.getMainScreenComponent(this).getThreading()
threading.execute({ readFile(licenseFile) },
{ mBinding.tvLicenseText.text = it })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import android.view.MenuItem
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.core.view.updatePadding
import ca.rmen.android.poetassistant.BuildConfig
import ca.rmen.android.poetassistant.Constants
import ca.rmen.android.poetassistant.Favorites
Expand All @@ -48,6 +49,7 @@ import ca.rmen.android.poetassistant.Threading
import ca.rmen.android.poetassistant.about.AboutActivity
import ca.rmen.android.poetassistant.dagger.DaggerHelper
import ca.rmen.android.poetassistant.databinding.ActivityMainBinding
import ca.rmen.android.poetassistant.getInsets
import ca.rmen.android.poetassistant.main.dictionaries.ResultListFragment
import ca.rmen.android.poetassistant.main.dictionaries.dictionary.Dictionary
import ca.rmen.android.poetassistant.main.dictionaries.rt.OnWordClickListener
Expand Down Expand Up @@ -119,6 +121,19 @@ class MainActivity : AppCompatActivity(), OnWordClickListener, WarningNoSpaceDia
}
})
volumeControlStream = AudioManager.STREAM_MUSIC
getInsets(mBinding.toolbar) { view, insets ->
view.updatePadding(
left = insets.left,
right = insets.right,
)
}
getInsets(mBinding.appBarLayout) { view, insets ->
view.updatePadding(
left = insets.left,
right = insets.right,
top = insets.top,
)
}
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.core.view.updateLayoutParams
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
Expand All @@ -45,7 +43,7 @@ import ca.rmen.android.poetassistant.R
import ca.rmen.android.poetassistant.compat.VectorCompat
import ca.rmen.android.poetassistant.databinding.BindingCallbackAdapter
import ca.rmen.android.poetassistant.databinding.FragmentResultListBinding
import ca.rmen.android.poetassistant.fixInsets
import ca.rmen.android.poetassistant.getInsets
import ca.rmen.android.poetassistant.main.AppBarLayoutHelper
import ca.rmen.android.poetassistant.main.Tab
import ca.rmen.android.poetassistant.settings.SettingsPrefs
Expand Down Expand Up @@ -77,7 +75,13 @@ class ResultListFragment<out T: Any> : Fragment() {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_result_list, container, false)
mBinding.recyclerView.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
mBinding.recyclerView.setHasFixedSize(true)
fixInsets(mBinding.recyclerView)
getInsets(mBinding.recyclerView) { view, insets ->
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
leftMargin = insets.left
bottomMargin = insets.bottom
rightMargin = insets.right
}
}
@Suppress("UNCHECKED_CAST")
mViewModel = ResultListFactory.createViewModel(it, this) as ResultListViewModel<T>
mBinding.viewModel = mViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
Expand All @@ -53,7 +50,8 @@ import ca.rmen.android.poetassistant.Tts
import ca.rmen.android.poetassistant.TtsState
import ca.rmen.android.poetassistant.dagger.DaggerHelper
import ca.rmen.android.poetassistant.databinding.ActivitySettingsBinding
import ca.rmen.android.poetassistant.fixInsets
import ca.rmen.android.poetassistant.getInsets
import ca.rmen.android.poetassistant.fixStatusBarViewForInsets
import ca.rmen.android.poetassistant.main.dictionaries.ConfirmDialogFragment
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch
Expand All @@ -68,7 +66,14 @@ class SettingsActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivitySettingsBinding>(this, R.layout.activity_settings)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
fixInsets(binding.root)
getInsets(binding.settingsFragment) { view, insets ->
view.updatePadding(
left = insets.left,
right = insets.right,
bottom = insets.bottom,
)
fixStatusBarViewForInsets(binding.statusBarView, insets)
}
volumeControlStream = AudioManager.STREAM_MUSIC
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_about.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
android:background="?colorPrimary" />

<FrameLayout
android:id="@+id/about_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context=".about.AboutActivity">

<ImageView
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_license.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
android:background="?colorPrimary" />

<ScrollView
android:id="@+id/license_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context=".about.LicenseActivity">


Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
android:layout_height="0dp"
android:background="?colorPrimary" />

<fragment
<androidx.fragment.app.FragmentContainerView
android:id="@+id/settings_fragment"
android:name="ca.rmen.android.poetassistant.settings.SettingsActivity$GeneralPreferenceFragment"
android:layout_width="match_parent"
Expand Down

0 comments on commit 9412412

Please sign in to comment.