Modify: ARouter、MMKV

This commit is contained in:
Quyunshuo
2020-08-28 20:01:45 +08:00
parent 76d957cad8
commit 03960ef66c
20 changed files with 333 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package com.quyunshuo.base
import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.base.utils.SpUtils
/**
* @Author: QuYunShuo
@ -12,5 +14,21 @@ open class BaseApplication : Application() {
override fun onCreate() {
super.onCreate()
initialize()
}
/**
* 进行一些初始化的操作
*/
protected open fun initialize() {
// 腾讯 MMKV 初始化
SpUtils.initMMKV(this)
// 阿里路由 ARouter 初始化
if (BuildConfig.DEBUG) {
ARouter.openLog() // 打印日志
ARouter.openDebug() // 开启调试模式(如果在InstantRun模式下运行必须开启调试模式线上版本需要关闭,否则有安全风险)
}
ARouter.init(this)
}
}

View File

@ -0,0 +1,81 @@
package com.quyunshuo.base.ktx
import android.content.Context
import android.view.Gravity
import android.widget.Toast
import androidx.annotation.StringRes
/**
* @Author: QuYunShuo
* @Time: 2020/8/17
* @Class: ContextKtx
* @Remark: Context相关的扩展方法
*/
/**
* Toast
* @param text CharSequence 类型文本
*/
fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, text, duration).show()
}
/**
* Toast
* @param resId String 类型资源id
*/
fun Context.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, resId, duration).show()
}
/**
* 居中Toast
* @param text CharSequence 类型文本
*/
fun Context.centerToast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
val toast = Toast.makeText(this, text, duration)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
/**
* 居中Toast
* @param resId String 类型资源id
*/
fun Context.centerToast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
val toast = Toast.makeText(this, resId, duration)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
/**
* dp 转 px
*/
fun Context.dp2px(dpValue: Float): Int {
val scale = resources.displayMetrics.density
return (dpValue * scale + 0.5f).toInt()
}
/**
* px 转 dp
*/
fun Context.px2dp(pxValue: Float): Int {
val scale = resources.displayMetrics.density
return (pxValue / scale + 0.5f).toInt()
}
/**
* sp 转 px
*/
fun Context.sp2px(spValue: Float): Int {
val scale = resources.displayMetrics.scaledDensity
return (spValue * scale + 0.5f).toInt()
}
/**
* px 转 sp
*/
fun Context.px2sp(pxValue: Float): Int {
val scale = resources.displayMetrics.scaledDensity
return (pxValue / scale + 0.5f).toInt()
}

View File

@ -0,0 +1,23 @@
package com.quyunshuo.base.ktx
import android.view.View
/**
* @Author: QuYunShuo
* @Time: 2020/8/17
* @Class: ViewKtx
* @Remark: View相关的扩展方法
*/
fun View.gone() {
visibility = View.GONE
}
fun View.visible() {
visibility = View.VISIBLE
}
fun View.invisible() {
visibility = View.INVISIBLE
}

View File

@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
/**
* @Author: QuYunShuo
@ -24,6 +25,8 @@ abstract class BaseFrameActivity<VB : ViewBinding, VM : ViewModel>(private val v
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(mBinding.root)
// ARouter 依赖注入
ARouter.getInstance().inject(this)
initView()
}

View File

@ -0,0 +1,59 @@
package com.quyunshuo.base.utils
import android.content.Context
import com.tencent.mmkv.MMKV
/**
* @Author: QuYunShuo
* @Time: 2020/8/28
* @Class: SpUtils
* @Remark: MMKV使用封装
*/
object SpUtils {
/**
* 初始化
*/
fun initMMKV(context: Context): String? = MMKV.initialize(context)
/**
* 保存数据(简化)
* 根据value类型自动匹配需要执行的方法
*/
fun put(key: String, value: Any) =
when (value) {
is Int -> putInt(key, value)
is Long -> putLong(key, value)
is Float -> putFloat(key, value)
is Double -> putDouble(key, value)
is String -> putString(key, value)
is Boolean -> putBoolean(key, value)
else -> false
}
fun putString(key: String, value: String): Boolean = MMKV.defaultMMKV().encode(key, value)
fun getString(key: String, defValue: String): String = MMKV.defaultMMKV().decodeString(key, defValue)
fun putInt(key: String, value: Int): Boolean = MMKV.defaultMMKV().encode(key, value)
fun getInt(key: String, defValue: Int): Int = MMKV.defaultMMKV().decodeInt(key, defValue)
fun putLong(key: String, value: Long): Boolean = MMKV.defaultMMKV().encode(key, value)
fun getLong(key: String, defValue: Long): Long = MMKV.defaultMMKV().decodeLong(key, defValue)
fun putDouble(key: String, value: Double): Boolean = MMKV.defaultMMKV().encode(key, value)
fun getDouble(key: String, defValue: Double): Double = MMKV.defaultMMKV().decodeDouble(key, defValue)
fun putFloat(key: String, value: Float): Boolean = MMKV.defaultMMKV().encode(key, value)
fun getFloat(key: String, defValue: Float): Float = MMKV.defaultMMKV().decodeFloat(key, defValue)
fun putBoolean(key: String, value: Boolean): Boolean = MMKV.defaultMMKV().encode(key, value)
fun getBoolean(key: String, defValue: Boolean): Boolean = MMKV.defaultMMKV().decodeBool(key, defValue)
fun contains(key: String): Boolean = MMKV.defaultMMKV().contains(key)
}

View File

@ -0,0 +1,35 @@
package com.quyunshuo.base.utils
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
/**
* 以顶层函数存在的常用工具方法
* startPolling() -> 开启一个轮询
*/
/**
* 使用 Flow 做的简单的轮询
* 请使用单独的协程来进行管理该 Flow
* Flow 仍有一些操作符是实验性的 使用时需添加 @InternalCoroutinesApi 注解
* @param intervals 轮询间隔时间/毫秒
* @param block 需要执行的代码块
*/
@InternalCoroutinesApi
suspend fun startPolling(intervals: Long, block: () -> Unit) {
flow {
while (true) {
delay(intervals)
emit(0)
}
}
.catch { Log.e("flow", "startPolling: $it") }
.flowOn(Dispatchers.Main)
.collect { block.invoke() }
}

View File

@ -1,7 +1,7 @@
<resources>
<!-- Base application theme. -->
<style name="base_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="base_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/base_colorPrimary</item>
<item name="colorPrimaryDark">@color/base_colorPrimaryDark</item>