Skip to content

Commit

Permalink
Refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Apr 6, 2024
1 parent 248676e commit f7e72e2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
17 changes: 17 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/utils/VersionUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.super12138.todo.utils

import android.content.Context
import android.os.Build

object VersionUtils {
fun getAppVersion(context: Context): String {
val pkgInfo = context.packageManager.getPackageInfo(context.packageName, 0)
val verName = pkgInfo.versionName
val verCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
pkgInfo.longVersionCode.toInt()
} else {
pkgInfo.versionCode
}
return "$verName($verCode)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Build
import android.os.Bundle
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.databinding.ActivityAboutBinding
import cn.super12138.todo.utils.VersionUtils
import cn.super12138.todo.utils.showToast
import cn.super12138.todo.views.BaseActivity

Expand All @@ -19,14 +20,7 @@ class AboutActivity : BaseActivity() {
binding = ActivityAboutBinding.inflate(layoutInflater)
setContentView(binding.root)

val pkgInfo = packageManager.getPackageInfo(packageName, 0)
val verName = pkgInfo.versionName
val verCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
pkgInfo.longVersionCode.toInt()
} else {
pkgInfo.versionCode
}
binding.appVersion.text = "$verName($verCode)"
binding.appVersion.text = VersionUtils.getAppVersion(this)

binding.toolBar.setNavigationOnClickListener {
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import cn.super12138.todo.databinding.ActivityCrashBinding
import cn.super12138.todo.utils.VersionUtils
import cn.super12138.todo.views.BaseActivity
import java.text.SimpleDateFormat
import java.util.Calendar
Expand Down Expand Up @@ -42,17 +43,8 @@ class CrashActivity : BaseActivity() {
val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val formattedDateTime = formatter.format(currentDateTime)

val pkgInfo = packageManager.getPackageInfo(packageName, 0)
val verName = pkgInfo.versionName
val verCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
pkgInfo.longVersionCode.toInt()
} else {
pkgInfo.versionCode
}
val mergeVer = "$verName($verCode)"

val deviceInfo = StringBuilder().apply {
append("ToDo version: ").append(mergeVer).append('\n')
append("ToDo version: ").append(VersionUtils.getAppVersion(this@CrashActivity)).append('\n')
append("Brand: ").append("").append(deviceBrand).append('\n')
append("Model: ").append(deviceModel).append('\n')
append("Device SDK: ").append(sdkLevel).append('\n').append('\n')
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_crash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="match_parent"
tools:context=".views.crash.CrashActivity">

<ScrollView
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand Down Expand Up @@ -46,7 +46,7 @@
android:textIsSelectable="true"
android:textSize="13sp" />
</LinearLayout>
</ScrollView>
</androidx.core.widget.NestedScrollView>

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/exit_app"
Expand Down

0 comments on commit f7e72e2

Please sign in to comment.