Skip to content

Commit

Permalink
Refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Jun 8, 2024
1 parent 24e6813 commit 552a9fc
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 65 deletions.
4 changes: 4 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/utils/VersionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import android.content.Context
import android.os.Build

object VersionUtils {
/**
* 获取应用版本号
* @return 版本名称(版本代码)
*/
fun getAppVersion(context: Context): String {
val pkgInfo = context.packageManager.getPackageInfo(context.packageName, 0)
val verName = pkgInfo.versionName
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/kotlin/cn/super12138/todo/views/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package cn.super12138.todo.views
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.viewbinding.ViewBinding
import cn.super12138.todo.constant.GlobalValues

open class BaseActivity : AppCompatActivity() {
abstract class BaseActivity<T : ViewBinding> : AppCompatActivity() {
lateinit var binding: T
override fun onCreate(savedInstanceState: Bundle?) {
/*if (GlobalValues.springFestivalTheme) {
setTheme(R.style.Theme_SpringFestival)
}*/
enableEdgeToEdge()
super.onCreate(savedInstanceState)
binding = getViewBinding()
setContentView(binding.root)

// 深色模式
when (GlobalValues.darkMode) {
Expand All @@ -34,4 +37,6 @@ open class BaseActivity : AppCompatActivity() {
}
window.attributes = lp
}

abstract fun getViewBinding(): T
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package cn.super12138.todo.views.about

import android.content.Intent
import android.net.Uri
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

class AboutActivity : BaseActivity() {
private lateinit var binding: ActivityAboutBinding
class AboutActivity : BaseActivity<ActivityAboutBinding>() {
private var clickCount = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityAboutBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.appVersion.text = VersionUtils.getAppVersion(this)

binding.toolBar.setNavigationOnClickListener {
Expand Down Expand Up @@ -54,9 +49,21 @@ class AboutActivity : BaseActivity() {
5 -> {
clickCount = 0
// GlobalValues.springFestivalTheme = !GlobalValues.springFestivalTheme
"\uD83C\uDF3C".showToast()
"\uD83C\uDF68".showToast()
/*when (java.util.Calendar.getInstance(Locale.getDefault())
.get(java.util.Calendar.MONTH) + 1) {
3, 4, 5 ->
6, 7, 8 -> SUMMER
9, 10, 11 -> AUTUMN
12, 1, 2 -> WINTER
else -> -12
}*/
}
}
}
}

override fun getViewBinding(): ActivityAboutBinding {
return ActivityAboutBinding.inflate(layoutInflater)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import cn.super12138.todo.databinding.ActivityAllTasksBinding
import cn.super12138.todo.views.BaseActivity
import me.zhanghai.android.fastscroll.FastScrollerBuilder

class AllTasksActivity : BaseActivity() {
class AllTasksActivity : BaseActivity<ActivityAllTasksBinding>() {
private val viewModel by viewModels<AllTasksViewModel>()
private lateinit var binding: ActivityAllTasksBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -27,9 +26,6 @@ class AllTasksActivity : BaseActivity() {
false -> window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

binding = ActivityAllTasksBinding.inflate(layoutInflater)
setContentView(binding.root)

val layoutManager = LinearLayoutManager(ToDoApp.context)
binding.allTasksList.layoutManager = layoutManager
val adapter = AllTasksAdapter(viewModel.todoListAll, supportFragmentManager)
Expand Down Expand Up @@ -64,4 +60,8 @@ class AllTasksActivity : BaseActivity() {
}
})
}

override fun getViewBinding(): ActivityAllTasksBinding {
return ActivityAllTasksBinding.inflate(layoutInflater)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

class CrashActivity : BaseActivity() {
private lateinit var binding: ActivityCrashBinding

class CrashActivity : BaseActivity<ActivityCrashBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)

binding = ActivityCrashBinding.inflate(layoutInflater)
setContentView(binding.root)

ViewCompat.setOnApplyWindowInsetsListener(binding.exitApp) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
Expand All @@ -44,7 +39,8 @@ class CrashActivity : BaseActivity() {
val formattedDateTime = formatter.format(currentDateTime)

val deviceInfo = StringBuilder().apply {
append("ToDo version: ").append(VersionUtils.getAppVersion(this@CrashActivity)).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 All @@ -61,4 +57,8 @@ class CrashActivity : BaseActivity() {
this.finishAffinity()
}
}

override fun getViewBinding(): ActivityCrashBinding {
return ActivityCrashBinding.inflate(layoutInflater)
}
}
26 changes: 5 additions & 21 deletions app/src/main/kotlin/cn/super12138/todo/views/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ import cn.super12138.todo.databinding.ActivityMainBinding
import cn.super12138.todo.views.BaseActivity
import cn.super12138.todo.views.settings.SettingsActivity

class MainActivity : BaseActivity() {
private lateinit var binding: ActivityMainBinding

class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.toolBar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.item_settings -> {
Expand All @@ -32,20 +26,6 @@ class MainActivity : BaseActivity() {
}

// setSupportActionBar(binding.toolbar)
/*val pref = getSharedPreferences("data", Context.MODE_PRIVATE)
val isFirstUse = pref.getBoolean("first_use", false)
if (!isFirstUse) {
MaterialAlertDialogBuilder(this)
.setTitle("欢迎使用待办")
.setMessage("本应用不需要任何权限")
.setPositiveButton("确定") { dialog, which ->
val editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit()
editor.putBoolean("first_use", true)
editor.apply()
}
.show()
}*/
when (GlobalValues.secureMode) {
true -> window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
Expand All @@ -55,4 +35,8 @@ class MainActivity : BaseActivity() {
false -> window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}

override fun getViewBinding(): ActivityMainBinding {
return ActivityMainBinding.inflate(layoutInflater)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@ import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.launch
import kotlin.system.exitProcess

class SettingsActivity : BaseActivity() {
private lateinit var binding: ActivitySettingsBinding

class SettingsActivity : BaseActivity<ActivitySettingsBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)

if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
Expand All @@ -53,6 +48,10 @@ class SettingsActivity : BaseActivity() {
}
}

override fun getViewBinding(): ActivitySettingsBinding {
return ActivitySettingsBinding.inflate(layoutInflater)
}

class SettingsFragment : PreferenceFragmentCompat() {
private lateinit var backupBinding: DialogBackupBinding
private lateinit var restoreBinding: DialogRestoreBinding
Expand Down
51 changes: 34 additions & 17 deletions app/src/main/res/layout/activity_crash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,45 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".views.crash.CrashActivity">

<androidx.core.widget.NestedScrollView
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<LinearLayout
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/tool_bar"
style="?attr/collapsingToolbarLayoutLargeStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp">
android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
app:collapsedTitleTextAppearance="?textAppearanceTitleLarge"
app:expandedTitleTextAppearance="?textAppearanceHeadlineLarge"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleCollapseMode="scale">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:text="@string/error_title"
android:textColor="?android:attr/textColorPrimary"
android:textSize="35sp" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/top_app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="0dp"
app:layout_collapseMode="pin"
app:title="@string/error_title" />

</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<me.zhanghai.android.fastscroll.FastScrollNestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
Expand All @@ -46,7 +64,7 @@
android:textIsSelectable="true"
android:textSize="13sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</me.zhanghai.android.fastscroll.FastScrollNestedScrollView>

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/exit_app"
Expand All @@ -56,5 +74,4 @@
android:layout_margin="16dp"
android:text="@string/exit_app"
app:icon="@drawable/ic_exit" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 comments on commit 552a9fc

Please sign in to comment.