perf: 调整 App 相关工具方法,聚合至 AppUtils.kt

This commit is contained in:
Quyunshuo
2023-02-13 23:27:50 +08:00
parent a29e353d4f
commit c8418aaa37
2 changed files with 45 additions and 30 deletions

View File

@ -0,0 +1,45 @@
package com.quyunshuo.androidbaseframemvvm.base.utils
import android.content.pm.PackageInfo
import android.os.Build
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
/**
* App 相关工具类
*
* @author Qu Yunshuo
* @sine 2023/2/13 23:15
*/
class AppUtils {
/**
* 获取当前 App 版本号
*
* @return Long
*/
fun getAppVersionCode(): Long {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getAppPackageInfo().longVersionCode
} else {
getAppPackageInfo().versionCode.toLong()
}
}
/**
* 获取当前 App 版本名
*
* @return String
*/
fun getAppVersionName(): String = getAppPackageInfo().versionName
/**
* 获取当前 App 的 [PackageInfo]
*
* @return PackageInfo
*/
fun getAppPackageInfo(): PackageInfo {
return BaseApplication.context
.packageManager
.getPackageInfo(BaseApplication.context.packageName, 0)
}
}

View File

@ -1,14 +1,11 @@
package com.quyunshuo.androidbaseframemvvm.base.utils
import android.os.Build
import android.util.Log
import android.widget.Toast
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
@ -63,31 +60,4 @@ fun toast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
*/
fun toast(msgId: Int, duration: Int = Toast.LENGTH_SHORT) {
ToastUtils.showToast(msgId, duration)
}
/**************************************************************************************************/
/**
* 获取App版本号
* @return Long App版本号
*/
fun getVersionCode(): Long {
val packageInfo = BaseApplication.context
.packageManager
.getPackageInfo(BaseApplication.context.packageName, 0)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo.longVersionCode
} else {
packageInfo.versionCode.toLong()
}
}
/**************************************************************************************************/
/**
* 获取App版本名
* @return String App版本名
*/
fun getVersionName(): String {
return BaseApplication.context
.packageManager
.getPackageInfo(BaseApplication.context.packageName, 0)
.versionName
}