最近需要实现一个状态管理类:
刚好看到大佬的文章,顺便学习和实践一下。
参考学习:就算不去火星种土豆,也请务必掌握的 Android 状态管理最佳实践
还是用大佬那个例子。
例如,存在 3 种模式,和 3个按钮,按钮不可用的时候弹出对应的 Toast。
shl(bits) – 左移位 shr(bits) – 右移位 and(bits) – 与 or(bits) – 或
private const val STATE_IDIE = 1 private const val STATUS_A = 1 shl 1 private const val STATUS_B = 1 shl 2 private const val STATUS_C = 1 shl 3
private var currentStatus = STATE_IDIE //测试代码 private fun changeStateToA(){ changeStateToA = STATUS_A }
//定义不可操作的一些行为 private const val DISABLE_BTN_1 = 1 shl 4 private const val DISABLE_BTN_2 = 1 shl 5 private const val DISABLE_BTN_3 = 1 shl 6
private const val MODE_A = STATUS_A or DISABLE_BTN_3 private const val MODE_B = STATUS_B or DISABLE_BTN_1 or DISABLE_BTN_3 private const val MODE_C = STATUS_C or DISABLE_BTN_2 or DISABLE_BTN_3 private val modeList = listOf(MODE_A, MODE_B, MODE_C)
key 为对应状态+禁用标志位的 或运算 结果。这样的计算结果,是可以保证key是唯一的,不会出现重复的情况。
value 为对应的 Toast 文案。
只需要一个 HashMap 就可以实现所有的配置关系。
从代码阅读性来说,使用这样的代码进行配置,看起来也比较通俗易懂。
比如 Pair(STATUS_A or DISABLE_BTN_3, "A3"),就是代表在状态A的时候,禁用按钮3,点击按钮的时候弹的Toast文案为 “A3”。
private val toastMap = hashMapOf( Pair(STATUS_A or DISABLE_BTN_3, "A3"), Pair(STATUS_B or DISABLE_BTN_1, "B1"), Pair(STATUS_B or DISABLE_BTN_3, "B3"), Pair(STATUS_C or DISABLE_BTN_2, "C2"), Pair(STATUS_C or DISABLE_BTN_3, "C3") )
currentStatus and action !=0
若可操作,返回 true。
若不可操作,通过 currentStatus or action 的运算结果作为key,通过上面配置的 HashMap 集合,拿到对应的 Toast 文案。
/** * 判断当前某个行为是否可操作 * * @return true 可操作;false,不可操作。 */ private fun checkEnable(action: Int): Boolean { val result = modeList.filter { (it and currentStatus) != 0 && (it and action) != 0 } if (result.isNotEmpty()) { println("result is false, toast:${toastMap[currentStatus or action]}") return false } println("result is true") return true }
object SixTeenTest { //定义状态常量 private const val STATE_IDIE = 1 private const val STATUS_A = 1 shl 1 private const val STATUS_B = 1 shl 2 private const val STATUS_C = 1 shl 3 //定义不可操作的一些行为 private const val DISABLE_BTN_1 = 1 shl 4 private const val DISABLE_BTN_2 = 1 shl 5 private const val DISABLE_BTN_3 = 1 shl 6 //定义模式状态集 private const val MODE_A = STATUS_A or DISABLE_BTN_3 private const val MODE_B = STATUS_B or DISABLE_BTN_1 or DISABLE_BTN_3 private const val MODE_C = STATUS_C or DISABLE_BTN_2 or DISABLE_BTN_3 private val modeList = listOf(MODE_A, MODE_B, MODE_C) //定义Toast映射关系 private val toastMap = hashMapOf( Pair(STATUS_A or DISABLE_BTN_3, "A3"), Pair(STATUS_B or DISABLE_BTN_1, "B1"), Pair(STATUS_B or DISABLE_BTN_3, "B3"), Pair(STATUS_C or DISABLE_BTN_2, "C2"), Pair(STATUS_C or DISABLE_BTN_3, "C3") ) //当前状态 private var currentStatus = STATE_IDIE /** * 判断当前某个行为是否可操作 * * @return true 可操作;false,不可操作。 */ private fun checkEnable(action: Int): Boolean { val result = modeList.filter { (it and currentStatus) != 0 && (it and action) != 0 } if (result.isNotEmpty()) { println("result is false, toast:${toastMap[currentStatus or action]}") return false } println("result is true") return true } }
fun main(args: Array<String>) { //测试代码 currentStatus = STATUS_A println("STATUS_A") checkEnable(DISABLE_BTN_1) checkEnable(DISABLE_BTN_2) checkEnable(DISABLE_BTN_3) currentStatus = STATUS_B println("STATUS_B") checkEnable(DISABLE_BTN_1) checkEnable(DISABLE_BTN_2) checkEnable(DISABLE_BTN_3) currentStatus = STATUS_C println("STATUS_C") checkEnable(DISABLE_BTN_1) checkEnable(DISABLE_BTN_2) checkEnable(DISABLE_BTN_3) }
输出测试结果
STATUS_A
result is true
result is true
result is false, toast:A3
STATUS_B
result is false, toast:B1
result is true
result is false, toast:B3
STATUS_C
result is true
result is false, toast:C2
result is false, toast:C3