Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
R3.11.1
Browse files Browse the repository at this point in the history
Snackbar可能阻碍到用户操作,所以整个生命周期内仅提示一次。
  • Loading branch information
ryuunoakaihitomi committed Aug 27, 2021
1 parent 45191af commit e274caf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ android {
//targetSdkVersion compileSdkVersion
targetSdkVersion 31
// 版本号:发布日期
versionCode 20210826
versionCode 20210827
// 版本名说明: R3.x.y.z (R3:Refactoring 第三次重构,z:Quick Fix序号)
versionName 'R3.11'
versionName 'R3.11.1'
resConfigs "en", "zh-rCN"

buildConfigField 'String', 'BUILD_TIME', '\"' + new Date() + '\"'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ import timber.log.Timber
class MyApplication : MultiDexApplication() {

/**
* @see [github.ryuunoakaihitomi.powerpanel.ui.main.MainActivity.compatibilityCheck]
* @see [github.ryuunoakaihitomi.powerpanel.ui.main.MainActivity.checkCompatibility]
*/
var hasShownCompatibilityWarning = false

/**
* @see [github.ryuunoakaihitomi.powerpanel.ui.main.MainActivity.checkScrollableListView]
*/
var hasShownScrollListTip = false

override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
Timber.plant(MyLogTree())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import android.text.style.StyleSpan
import android.text.style.TypefaceSpan
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.Window
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.AdapterView
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.getSystemService
import androidx.core.content.pm.ShortcutInfoCompat
Expand Down Expand Up @@ -63,9 +66,8 @@ import java.util.*

class MainActivity : AppCompatActivity() {

private fun compatibilityCheck() {
val myApp = application as MyApplication
if (myApp.hasShownCompatibilityWarning) return
private fun checkCompatibility() {
if (myApp().hasShownCompatibilityWarning) return
val modeType = getSystemService<UiModeManager>()?.currentModeType
?: Configuration.UI_MODE_TYPE_UNDEFINED
val isCompatible =
Expand All @@ -80,14 +82,38 @@ class MainActivity : AppCompatActivity() {
if (!isCompatible) {
Timber.i("show unsupported env hint")
Toasty.error(this, R.string.toast_unsupported_env, Toasty.LENGTH_LONG).show()
myApp.hasShownCompatibilityWarning = true
myApp().hasShownCompatibilityWarning = true
}
}

/**
* 检测ListView是否可滑动,提醒用户可能有一些项目被隐藏(仅提醒一次)
*/
private fun checkScrollableListView(lv: ListView) {
if (!myApp().hasShownScrollListTip) {
lv.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// Timber.d("OnGlobalLayoutListener")
val lastVisibleChild = lv.getChildAt(lv.lastVisiblePosition)
if (lastVisibleChild != null && lastVisibleChild.bottom > lv.height) {
Timber.d("Tip: scrollable listview")
Snackbar.make(lv, R.string.toast_list_scrollable, Snackbar.LENGTH_SHORT)
.allowInfiniteLines()
.show()
myApp().hasShownScrollListTip = true
// This ViewTreeObserver is not alive, call getViewTreeObserver() again
lv.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
}
})
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.v("start!")
compatibilityCheck()
checkCompatibility()
var mainDialog = AlertDialog.Builder(this).create()
val powerViewModel = ViewModelProvider(this)[PowerViewModel::class.java]
powerViewModel.labelResId.observe(this) {
Expand Down Expand Up @@ -250,19 +276,7 @@ class MainActivity : AppCompatActivity() {
Toasty.info(this, R.string.toast_shortcut_added).show()
return@OnItemLongClickListener true
}
/* 检测ListView是否可滑动,提醒用户可能有一些项目被隐藏 */
var hasShownScrollListTip = false
lv.viewTreeObserver.addOnGlobalLayoutListener {
// Timber.d("OnGlobalLayoutListener")
val lastVisibleChild = lv.getChildAt(lv.lastVisiblePosition)
if (lastVisibleChild != null && lastVisibleChild.bottom > lv.height && !hasShownScrollListTip) {
Timber.d("Tip: scrollable listview")
Snackbar.make(lv, R.string.toast_list_scrollable, Snackbar.LENGTH_SHORT)
.allowInfiniteLines()
.show()
hasShownScrollListTip = true
}
}
checkScrollableListView(lv)
mainDialog.window?.run {
decorView.run {
alpha = 0.85f // 窗口透明度
Expand Down Expand Up @@ -370,4 +384,13 @@ private fun Window.hideSysUi() = WindowCompat.getInsetsController(this, decorVie
hide(WindowInsetsCompat.Type.systemBars())
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}

/**
* @link https://stackoverflow.com/a/59472972/16091156
*/
private fun Snackbar.allowInfiniteLines() = apply {
view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text).isSingleLine = false
}

private fun Activity.myApp() = application as MyApplication
//endregion
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import android.os.Build
import android.provider.Browser
import android.service.quicksettings.Tile
import android.text.Spannable
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.set
import com.google.android.material.R
import com.google.android.material.snackbar.Snackbar
import es.dmoral.toasty.Toasty
import timber.log.Timber

Expand All @@ -39,11 +36,4 @@ fun Tile.updateState(state: Int) {

operator fun Spannable.set(range: IntRange, spans: Array<Any>) {
for (span in spans) this[range] = span
}

/**
* @link https://stackoverflow.com/a/59472972/16091156
*/
fun Snackbar.allowInfiniteLines() = apply {
view.findViewById<TextView>(R.id.snackbar_text).isSingleLine = false
}

0 comments on commit e274caf

Please sign in to comment.