From 85fa965a33cbb9f31ae7a3bd299659803f80825b Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 30 Aug 2021 08:34:46 +0530 Subject: [PATCH 1/3] Use WindowInsetsAnimationCompat. --- .../edithistory/EditHistoryFragment.kt | 5 +- .../streetcomplete/map/MainFragment.kt | 7 ++- .../streetcomplete/map/MapFragment.kt | 2 +- .../quests/AbstractBottomSheetFragment.kt | 12 ++-- .../streetcomplete/quests/SplitWayFragment.kt | 2 +- .../tutorial/TutorialFragment.kt | 2 +- .../ImeInsetsAnimationCallback.kt | 59 ++++++++----------- 7 files changed, 39 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/edithistory/EditHistoryFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/edithistory/EditHistoryFragment.kt index f32f142dc7..fa50c7408a 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/edithistory/EditHistoryFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/edithistory/EditHistoryFragment.kt @@ -2,6 +2,7 @@ package de.westnordost.streetcomplete.edithistory import android.os.Bundle import android.view.View +import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import de.westnordost.streetcomplete.Injector @@ -69,9 +70,7 @@ class EditHistoryFragment : Fragment(R.layout.fragment_edit_history_list) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - editHistoryList.respectSystemInsets { left, top, right, bottom -> - setPadding(left, top, 0, bottom) - } + editHistoryList.respectSystemInsets { updatePadding(left = it.left, top = it.top, bottom = it.bottom) } lifecycleScope.launch { val edits = withContext(Dispatchers.IO) { editHistorySource.getAll() } adapter.setEdits(edits) diff --git a/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt index 4416f2d837..2e1464d635 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt @@ -21,6 +21,7 @@ import androidx.annotation.DrawableRes import androidx.annotation.UiThread import androidx.appcompat.widget.PopupMenu import androidx.core.content.getSystemService +import androidx.core.graphics.Insets import androidx.core.graphics.minus import androidx.core.graphics.toPointF import androidx.core.graphics.toRectF @@ -96,7 +97,7 @@ class MainFragment : Fragment(R.layout.fragment_main), private var locationWhenOpenedQuest: Location? = null - private var windowInsets: Rect? = null + private var windowInsets: Insets? = null internal var mapFragment: QuestsMapFragment? = null internal var mainMenuButtonFragment: MainMenuButtonFragment? = null @@ -152,8 +153,8 @@ class MainFragment : Fragment(R.layout.fragment_main), override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - mapControls.respectSystemInsets(View::setMargins) - view.respectSystemInsets { l, t, r, b -> windowInsets = Rect(l, t, r, b) } + mapControls.respectSystemInsets { setMargins(it.left, it.top, it.right, it.bottom) } + view.respectSystemInsets { windowInsets = it } locationPointerPin.setOnClickListener { onClickLocationPointer() } compassView.setOnClickListener { onClickCompassButton() } diff --git a/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt index f7caf87fc4..51b87fcaf1 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt @@ -116,7 +116,7 @@ open class MapFragment : Fragment(), mapTileProviderLink.text = vectorTileProvider.copyrightText mapTileProviderLink.setOnClickListener { showOpenUrlDialog(vectorTileProvider.copyrightLink) } - attributionContainer.respectSystemInsets(View::setMargins) + attributionContainer.respectSystemInsets { setMargins(it.left, it.top, it.right, it.bottom) } lifecycleScope.launch { initMap() } } diff --git a/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.kt index 9766f325d1..05a449b0b8 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.kt @@ -50,14 +50,14 @@ abstract class AbstractBottomSheetFragment : Fragment(), IsCloseableBottomSheet closeButton.setOnClickListener { activity?.onBackPressed() } minBottomInset = Int.MAX_VALUE - view.respectSystemInsets { left, top, right, bottom -> - scrollViewChild.updatePadding(bottom = bottom) - bottomSheetContainer.updateMargins(top = top, left = left, right = right) - okButton.updateMargins(bottom = bottom + 8f.toPx(context).toInt()) + view.respectSystemInsets { + scrollViewChild.updatePadding(bottom = it.bottom) + bottomSheetContainer.updateMargins(top = it.top, left = it.left, right = it.right) + okButton.updateMargins(bottom = it.bottom + 8f.toPx(context).toInt()) // expanding bottom sheet when keyboard is opened - if (minBottomInset < bottom) expand() - minBottomInset = min(bottom, minBottomInset) + if (minBottomInset < it.bottom) expand() + minBottomInset = min(it.bottom, minBottomInset) } bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) diff --git a/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt index 62f68689e4..28729cd419 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt @@ -83,7 +83,7 @@ class SplitWayFragment : Fragment(R.layout.fragment_split_way), override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - bottomSheetContainer.respectSystemInsets(View::setMargins) + bottomSheetContainer.respectSystemInsets { setMargins(it.left, it.top, it.right, it.bottom) } splitWayRoot.setOnTouchListener { _, event -> clickPos = PointF(event.x, event.y) diff --git a/app/src/main/java/de/westnordost/streetcomplete/tutorial/TutorialFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/tutorial/TutorialFragment.kt index 84b27bd80b..c10b223a15 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/tutorial/TutorialFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/tutorial/TutorialFragment.kt @@ -38,7 +38,7 @@ class TutorialFragment : Fragment(R.layout.fragment_tutorial) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - view.respectSystemInsets(View::setPadding) + view.respectSystemInsets() updateIndicatorDots() diff --git a/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt b/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt index aeff9772aa..3db8bdec68 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt @@ -1,66 +1,55 @@ package de.westnordost.streetcomplete.view.insets_animation -import android.os.Build import android.view.View -import android.view.WindowInsets -import android.view.WindowInsetsAnimation -import androidx.annotation.RequiresApi +import androidx.core.graphics.Insets +import androidx.core.view.OnApplyWindowInsetsListener +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsAnimationCompat +import androidx.core.view.WindowInsetsCompat /** Make the keyboard appear and disappear smoothly. Must be set on both * setOnApplyWindowInsetsListener and setWindowInsetsAnimationCallback */ -@RequiresApi(Build.VERSION_CODES.R) class ImeInsetsAnimationCallback( private val view: View, - private val onNewInsets: View.(left: Int, top: Int, right: Int, bottom: Int) -> Unit -) : WindowInsetsAnimation.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE), View.OnApplyWindowInsetsListener { - + private val onNewInsets: View.(insets: Insets) -> Unit +) : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE), OnApplyWindowInsetsListener { private var isAnimating = false - private var prevInsets: WindowInsets? = null + private var prevInsets: WindowInsetsCompat? = null - override fun onApplyWindowInsets(v: View, windowInsets: WindowInsets): WindowInsets { + override fun onApplyWindowInsets(v: View, windowInsets: WindowInsetsCompat): WindowInsetsCompat { prevInsets = windowInsets if (!isAnimating) applyNewInsets(windowInsets) return windowInsets } - override fun onPrepare(animation: WindowInsetsAnimation) { - if (animation.typeMask and WindowInsets.Type.ime() != 0) { + override fun onPrepare(animation: WindowInsetsAnimationCompat) { + if (animation.typeMask and WindowInsetsCompat.Type.ime() != 0) { isAnimating = true } } - override fun onProgress(insets: WindowInsets, runningAnims: List): WindowInsets { + override fun onProgress(insets: WindowInsetsCompat, runningAnims: List): WindowInsetsCompat { applyNewInsets(insets) return insets } - override fun onEnd(animation: WindowInsetsAnimation) { - if (isAnimating && (animation.typeMask and WindowInsets.Type.ime()) != 0) { + override fun onEnd(animation: WindowInsetsAnimationCompat) { + if (isAnimating && (animation.typeMask and WindowInsetsCompat.Type.ime()) != 0) { isAnimating = false - prevInsets?.let { view.dispatchApplyWindowInsets(it) } + prevInsets?.let { view.dispatchApplyWindowInsets(it.toWindowInsets()) } } } - private fun applyNewInsets(insets: WindowInsets) { - val typeInsets = insets.getInsets(WindowInsets.Type.ime() or WindowInsets.Type.systemBars()) - onNewInsets(view, typeInsets.left, typeInsets.top, typeInsets.right, typeInsets.bottom) + private fun applyNewInsets(insets: WindowInsetsCompat) { + val typeInsets = insets.getInsets(WindowInsetsCompat.Type.ime() or WindowInsetsCompat.Type.systemBars()) + view.onNewInsets(typeInsets) } } -fun View.respectSystemInsets(onNewInsets: View.(left: Int, top: Int, right: Int, bottom: Int) -> Unit = View::setPadding) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - val imeAnimationCallback = ImeInsetsAnimationCallback(this, onNewInsets) - setOnApplyWindowInsetsListener(imeAnimationCallback) - setWindowInsetsAnimationCallback(imeAnimationCallback) - } else { - setOnApplyWindowInsetsListener { v, windowInsets -> - onNewInsets(v, - windowInsets.systemWindowInsetLeft, - windowInsets.systemWindowInsetTop, - windowInsets.systemWindowInsetRight, - windowInsets.systemWindowInsetBottom - ) - windowInsets - } - } +fun View.respectSystemInsets(onNewInsets: View.(insets: Insets) -> Unit = { + setPadding(it.left, it.top, it.right, it.bottom) +}) { + val imeAnimationCallback = ImeInsetsAnimationCallback(this, onNewInsets) + ViewCompat.setOnApplyWindowInsetsListener(this, imeAnimationCallback) + ViewCompat.setWindowInsetsAnimationCallback(this, imeAnimationCallback) } From 0b5d49aa15eca8836538087ef40d527f18f897d5 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 6 Sep 2021 06:49:39 +0530 Subject: [PATCH 2/3] Add setPadding() and setMargins() extensions. --- .../java/de/westnordost/streetcomplete/ktx/View.kt | 11 +++++++++++ .../de/westnordost/streetcomplete/map/MainFragment.kt | 2 +- .../de/westnordost/streetcomplete/map/MapFragment.kt | 2 +- .../streetcomplete/quests/SplitWayFragment.kt | 2 +- .../settings/questselection/QuestPresetsFragment.kt | 4 ---- .../insets_animation/ImeInsetsAnimationCallback.kt | 5 ++--- buildSrc/src/main/java/GetTranslatorCreditsTask.kt | 2 +- 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt b/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt index ff57bd0faa..db7db2c74b 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt @@ -7,6 +7,7 @@ import android.view.ViewGroup import android.view.ViewPropertyAnimator import android.view.animation.AccelerateInterpolator import android.view.animation.DecelerateInterpolator +import androidx.core.graphics.Insets import androidx.core.os.postDelayed import androidx.core.view.* import kotlinx.coroutines.suspendCancellableCoroutine @@ -73,6 +74,16 @@ fun View.showTapHint(initialDelay: Long = 300, pressedDelay: Long = 600) { } } +@Suppress("NOTHING_TO_INLINE") +inline fun View.setPadding(insets: Insets) { + setPadding(insets.left, insets.top, insets.right, insets.bottom) +} + +@Suppress("NOTHING_TO_INLINE") +inline fun View.setMargins(insets: Insets) { + setMargins(insets.left, insets.top, insets.right, insets.bottom) +} + fun View.setMargins(left: Int, top: Int, right: Int, bottom: Int) { updateLayoutParams { setMargins(left, top, right, bottom) } } diff --git a/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt index 2e1464d635..32b75fd700 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/map/MainFragment.kt @@ -153,7 +153,7 @@ class MainFragment : Fragment(R.layout.fragment_main), override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - mapControls.respectSystemInsets { setMargins(it.left, it.top, it.right, it.bottom) } + mapControls.respectSystemInsets(View::setMargins) view.respectSystemInsets { windowInsets = it } locationPointerPin.setOnClickListener { onClickLocationPointer() } diff --git a/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt index 51b87fcaf1..f7caf87fc4 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/map/MapFragment.kt @@ -116,7 +116,7 @@ open class MapFragment : Fragment(), mapTileProviderLink.text = vectorTileProvider.copyrightText mapTileProviderLink.setOnClickListener { showOpenUrlDialog(vectorTileProvider.copyrightLink) } - attributionContainer.respectSystemInsets { setMargins(it.left, it.top, it.right, it.bottom) } + attributionContainer.respectSystemInsets(View::setMargins) lifecycleScope.launch { initMap() } } diff --git a/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt index 28729cd419..62f68689e4 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/quests/SplitWayFragment.kt @@ -83,7 +83,7 @@ class SplitWayFragment : Fragment(R.layout.fragment_split_way), override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - bottomSheetContainer.respectSystemInsets { setMargins(it.left, it.top, it.right, it.bottom) } + bottomSheetContainer.respectSystemInsets(View::setMargins) splitWayRoot.setOnTouchListener { _, event -> clickPos = PointF(event.x, event.y) diff --git a/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt b/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt index d326663527..adb14be3b6 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/settings/questselection/QuestPresetsFragment.kt @@ -1,11 +1,8 @@ package de.westnordost.streetcomplete.settings.questselection import android.os.Bundle -import android.text.InputType import android.view.LayoutInflater import android.view.View -import android.widget.EditText -import android.widget.FrameLayout import androidx.appcompat.app.AlertDialog import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope @@ -13,7 +10,6 @@ import de.westnordost.streetcomplete.HasTitle import de.westnordost.streetcomplete.Injector import de.westnordost.streetcomplete.R import de.westnordost.streetcomplete.data.visiblequests.QuestPresetsController -import de.westnordost.streetcomplete.ktx.setMargins import kotlinx.android.synthetic.main.dialog_input_text.view.* import kotlinx.android.synthetic.main.fragment_quest_presets.view.* import kotlinx.coroutines.Dispatchers diff --git a/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt b/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt index 3db8bdec68..1fe7e92a22 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/view/insets_animation/ImeInsetsAnimationCallback.kt @@ -6,6 +6,7 @@ import androidx.core.view.OnApplyWindowInsetsListener import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsAnimationCompat import androidx.core.view.WindowInsetsCompat +import de.westnordost.streetcomplete.ktx.setPadding /** Make the keyboard appear and disappear smoothly. Must be set on both * setOnApplyWindowInsetsListener and setWindowInsetsAnimationCallback */ @@ -46,9 +47,7 @@ class ImeInsetsAnimationCallback( } } -fun View.respectSystemInsets(onNewInsets: View.(insets: Insets) -> Unit = { - setPadding(it.left, it.top, it.right, it.bottom) -}) { +fun View.respectSystemInsets(onNewInsets: View.(insets: Insets) -> Unit = View::setPadding) { val imeAnimationCallback = ImeInsetsAnimationCallback(this, onNewInsets) ViewCompat.setOnApplyWindowInsetsListener(this, imeAnimationCallback) ViewCompat.setWindowInsetsAnimationCallback(this, imeAnimationCallback) diff --git a/buildSrc/src/main/java/GetTranslatorCreditsTask.kt b/buildSrc/src/main/java/GetTranslatorCreditsTask.kt index f6e7e2b5e7..522a869148 100644 --- a/buildSrc/src/main/java/GetTranslatorCreditsTask.kt +++ b/buildSrc/src/main/java/GetTranslatorCreditsTask.kt @@ -103,7 +103,7 @@ open class GetTranslatorCreditsTask : DefaultTask() { private fun queryTranslatorStats(userId: Int): Map? { val url = URL("https://poeditor.com/contributors/contributor_stats") val connection = url.openConnection() as HttpURLConnection - val cookieEncoded = URLEncoder.encode(cookie, Charset.forName("UTF-8")) + val cookieEncoded = URLEncoder.encode(cookie, "UTF-8") val today = LocalDate.now().toString() try { connection.doOutput = true From 8ba99353f9c73a6554f8851f52cf088a95fe5763 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 6 Sep 2021 19:49:25 +0530 Subject: [PATCH 3/3] Remove 'inline' modifier. --- app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt b/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt index db7db2c74b..fabfa7c757 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/ktx/View.kt @@ -74,13 +74,11 @@ fun View.showTapHint(initialDelay: Long = 300, pressedDelay: Long = 600) { } } -@Suppress("NOTHING_TO_INLINE") -inline fun View.setPadding(insets: Insets) { +fun View.setPadding(insets: Insets) { setPadding(insets.left, insets.top, insets.right, insets.bottom) } -@Suppress("NOTHING_TO_INLINE") -inline fun View.setMargins(insets: Insets) { +fun View.setMargins(insets: Insets) { setMargins(insets.left, insets.top, insets.right, insets.bottom) }