From cd8596fbc940907c46dc771582a864f0b5fa564d Mon Sep 17 00:00:00 2001 From: Thomas Wirth Date: Thu, 9 Jan 2020 23:56:43 +0100 Subject: [PATCH] Refactor code and optimize onTouchListener --- .../activities/about/AboutActivity.kt | 6 +++--- .../activities/appmanager/AppsActivity.kt | 21 ++++++++----------- .../widgetconfig/WidgetConfigActivity.kt | 4 ++-- .../systemapps/SystemAppsDataProvider.kt | 16 +++++++------- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/com/g00fy2/developerwidget/activities/about/AboutActivity.kt b/app/src/main/kotlin/com/g00fy2/developerwidget/activities/about/AboutActivity.kt index fee33b96..45156673 100644 --- a/app/src/main/kotlin/com/g00fy2/developerwidget/activities/about/AboutActivity.kt +++ b/app/src/main/kotlin/com/g00fy2/developerwidget/activities/about/AboutActivity.kt @@ -108,10 +108,10 @@ class AboutActivity : BaseActivity(), AboutContract.AboutView { action { presenter.honorClicking() } } if (VERSION.SDK_INT >= VERSION_CODES.O_MR1) { - binding.aboutRootScrollview.doOnApplyWindowInsets { view, insets, padding, _ -> - view.updatePadding(bottom = padding.bottom + insets.systemWindowInsetBottom) - } binding.aboutRootScrollview.apply { + doOnApplyWindowInsets { _, insets, padding, _ -> + updatePadding(bottom = padding.bottom + insets.systemWindowInsetBottom) + } viewTreeObserver.addOnScrollChangedListener { val scrollableRange = getChildAt(0).bottom - height + paddingBottom if (!gesturalNavigationMode()) { diff --git a/app/src/main/kotlin/com/g00fy2/developerwidget/activities/appmanager/AppsActivity.kt b/app/src/main/kotlin/com/g00fy2/developerwidget/activities/appmanager/AppsActivity.kt index bec9ee80..4bceadf7 100644 --- a/app/src/main/kotlin/com/g00fy2/developerwidget/activities/appmanager/AppsActivity.kt +++ b/app/src/main/kotlin/com/g00fy2/developerwidget/activities/appmanager/AppsActivity.kt @@ -11,7 +11,6 @@ import android.view.LayoutInflater import android.view.MotionEvent import android.view.View import android.view.inputmethod.EditorInfo -import android.widget.EditText import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.widget.TooltipCompat import androidx.core.content.res.ResourcesCompat @@ -94,14 +93,14 @@ class AppsActivity : BaseActivity(true), AppsContract.AppsView { } }) setOnEditorActionListener { _, actionId, _ -> - val filterString = binding.filterEdittext.text.toString().trim() + val filterString = text.toString().trim() if (actionId == EditorInfo.IME_ACTION_DONE && filterString.isNotEmpty() && !presenter.duplicateFilter( filterString ) ) { presenter.addAppFilter(filterString) addFilterChip(filterString) - binding.filterEdittext.text.clear() + text.clear() } true } @@ -109,17 +108,15 @@ class AppsActivity : BaseActivity(true), AppsContract.AppsView { presenter.updateFilter(it) setCompoundDrawables(null, null, if (!it.isNullOrEmpty()) clearDrawable else null, null) } - setOnTouchListener { v, event -> - var consumed = false - if (v is EditText) { - if (event.x >= v.width - v.totalPaddingRight) { - if (event.action == MotionEvent.ACTION_UP) { - binding.filterEdittext.text.clear() - } - consumed = true + setOnTouchListener { _, event -> + if (event.x >= width - totalPaddingRight) { + if (event.action == MotionEvent.ACTION_UP) { + performClick() + text.clear() } + return@setOnTouchListener true } - consumed + false } } // necessary to measure the view heights for animations diff --git a/app/src/main/kotlin/com/g00fy2/developerwidget/activities/widgetconfig/WidgetConfigActivity.kt b/app/src/main/kotlin/com/g00fy2/developerwidget/activities/widgetconfig/WidgetConfigActivity.kt index 670d11bb..f957436e 100644 --- a/app/src/main/kotlin/com/g00fy2/developerwidget/activities/widgetconfig/WidgetConfigActivity.kt +++ b/app/src/main/kotlin/com/g00fy2/developerwidget/activities/widgetconfig/WidgetConfigActivity.kt @@ -127,9 +127,9 @@ class WidgetConfigActivity : BaseActivity(), WidgetConfigContract.WidgetConfigVi toggleDeviceNameEdit(false) } } - setOnEditorActionListener { v, actionId, _ -> + setOnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_DONE) { - v.clearFocus() + clearFocus() } true } diff --git a/app/src/main/kotlin/com/g00fy2/developerwidget/data/device/systemapps/SystemAppsDataProvider.kt b/app/src/main/kotlin/com/g00fy2/developerwidget/data/device/systemapps/SystemAppsDataProvider.kt index 7991c0fa..0a3c398e 100644 --- a/app/src/main/kotlin/com/g00fy2/developerwidget/data/device/systemapps/SystemAppsDataProvider.kt +++ b/app/src/main/kotlin/com/g00fy2/developerwidget/data/device/systemapps/SystemAppsDataProvider.kt @@ -24,22 +24,24 @@ class SystemAppsDataProvider { @SuppressLint("PrivateApi", "WebViewApiAvailability") fun getWebViewImplementation(context: Context): String { return when { - VERSION.SDK_INT >= VERSION_CODES.O -> WebView.getCurrentWebViewPackage()?.let { - context.packageManager.getApplicationLabel( - it.applicationInfo - ).toString() + " " + it.versionName - } ?: "" + VERSION.SDK_INT >= VERSION_CODES.O -> { + WebView.getCurrentWebViewPackage()?.let { + context.packageManager.getApplicationLabel(it.applicationInfo).toString() + " " + it.versionName + } ?: "" + } VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP -> { try { (Class.forName("android.webkit.WebViewFactory") .getMethod("getLoadedPackageInfo") - .invoke(null) as PackageInfo?)?.let { context.packageManager.getApplicationLabel(it.applicationInfo).toString() + " " + it.versionName } + .invoke(null) as PackageInfo?)?.let { + context.packageManager.getApplicationLabel(it.applicationInfo).toString() + " " + it.versionName + } ?: "" } catch (t: Throwable) { "" } } - Version(VERSION.RELEASE.toString()).isAtLeast("4.4.3") -> "WebView v33.0.0.0" + Version(VERSION.RELEASE).isAtLeast("4.4.3") -> "WebView v33.0.0.0" VERSION.SDK_INT >= VERSION_CODES.KITKAT -> "WebView v30.0.0.0" else -> try { context.packageManager.getPackageInfo("com.google.android.webview", 0)