Skip to content

Commit

Permalink
Fixed release version cannot restore database
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Jan 25, 2024
1 parent 8c22d7a commit 9e8372f
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 45 deletions.
7 changes: 7 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# Gson
-keepattributes Signature
-keep class com.google.gson.reflect.TypeToken { *; }
-keep class * extends com.google.gson.reflect.TypeToken
-keepattributes AnnotationDefault,RuntimeVisibleAnnotations

# ToDo Room
-keep class cn.super12138.todo.logic.dao.ToDoRoom { *; }
6 changes: 6 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/logic/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object Repository {
/**
* 设置数据到应用设置中
*/
fun setPreference(context: Context, key: String, value: Any) =
SPHelper.setPreference(context, key, value)

/**
* 获取应用设置里的数据
*/
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/logic/database/SPHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@ object SPHelper {
PreferenceManager.getDefaultSharedPreferences(context /* Activity context */)
return sharedPreferences.getBoolean(name, defaultValue)
}

fun setPreference(context: Context, name: String, value: Any) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val editor = sharedPreferences.edit()

when (value) {
is String -> editor.putString(name, value)
is Boolean -> editor.putBoolean(name, value)
is Int -> editor.putInt(name, value)
is Float -> editor.putFloat(name, value)
is Long -> editor.putLong(name, value)
else -> throw IllegalArgumentException("Unsupported data type")
}

editor.apply()
}
}
8 changes: 8 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/views/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.logic.Repository

open class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val springFestivalTheme =
Repository.getPreferenceBoolean(ToDoApplication.context, "spring_festival_theme", false)
if (springFestivalTheme) {
setTheme(R.style.Theme_SpringFestival)
}
// enableEdgeToEdge()
super.onCreate(savedInstanceState)
// 适配刘海屏
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.constant.Constants
import cn.super12138.todo.databinding.ActivityAboutBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.views.BaseActivity
import showToast

Expand Down Expand Up @@ -59,6 +61,24 @@ class AboutActivity : BaseActivity() {
when (clickCount) {
5 -> {
clickCount = 0
val isSpringFestivalTheme = Repository.getPreferenceBoolean(
ToDoApplication.context,
"spring_festival_theme",
false
)
if (isSpringFestivalTheme) {
Repository.setPreference(
ToDoApplication.context,
"spring_festival_theme",
false
)
} else {
Repository.setPreference(
ToDoApplication.context,
"spring_festival_theme",
true
)
}
"🧧".showToast()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import android.os.Process
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.databinding.ActivitySettingsBinding
import cn.super12138.todo.databinding.DialogBackupBinding
import cn.super12138.todo.databinding.DialogRestoreBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.logic.dao.ToDoRoom
import cn.super12138.todo.views.BaseActivity
import cn.super12138.todo.views.progress.ProgressFragmentViewModel
import cn.super12138.todo.views.todo.ToDoFragmentViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.gson.Gson
Expand Down Expand Up @@ -56,10 +54,8 @@ class SettingsActivity : BaseActivity() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
val gson = Gson()
val progressViewModel =
ViewModelProvider(requireActivity()).get(ProgressFragmentViewModel::class.java)
val todoViewModel =
ViewModelProvider(requireActivity()).get(ToDoFragmentViewModel::class.java)
val isDevMode =
Repository.getPreferenceBoolean(ToDoApplication.context, "dev_mode", false)

findPreference<ListPreference>("dark_mode")?.apply {
setOnPreferenceChangeListener { preference, newValue ->
Expand Down Expand Up @@ -161,8 +157,12 @@ class SettingsActivity : BaseActivity() {
restoreBinding.jsonInput.error =
getString(R.string.json_data_incorrect)
} catch (e: Exception) {
restoreBinding.jsonInput.error =
getString(R.string.restore_failed)
if (isDevMode) {
restoreBinding.jsonInput.error = e.toString()
} else {
restoreBinding.jsonInput.error =
getString(R.string.restore_failed)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
ViewModelProvider(viewModelStoreOwner).get(ToDoFragmentViewModel::class.java)

val view = LayoutInflater.from(parent.context)
.inflate(R.layout.todo_item, parent, false)
.inflate(R.layout.item_todo, parent, false)
val holder = ViewHolder(view)

holder.checkToDoBtn.setOnClickListener {
Expand Down
85 changes: 50 additions & 35 deletions app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cn.super12138.todo.logic.model.ToDo
import cn.super12138.todo.views.progress.ProgressFragmentViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.launch
import showToast
import java.util.UUID

class ToDoFragment : Fragment() {
Expand Down Expand Up @@ -95,44 +96,58 @@ class ToDoFragment : Fragment() {
ToDoDialogBinding.todoContent.error =
getString(R.string.content_cannot_be_empty)
} else {
val randomUUID = UUID.randomUUID().toString()
val todoSubject = when (ToDoDialogBinding.todoSubject.checkedChipId) {
R.id.subject_chinese -> getString(R.string.subject_chinese)
R.id.subject_math -> getString(R.string.subject_math)
R.id.subject_english -> getString(R.string.subject_english)
R.id.subject_biology -> getString(R.string.subject_biology)
R.id.subject_geography -> getString(R.string.subject_geography)
R.id.subject_history -> getString(R.string.subject_history)
R.id.subject_physics -> getString(R.string.subject_physics)
R.id.subject_law -> getString(R.string.subject_law)
R.id.subject_other -> getString(R.string.subject_other)
else -> getString(R.string.subject_unknown)
}

// 显示RecyclerView
if (todoList.size + 1 > 0) {
todoViewModel.emptyTipVis.value = View.GONE
}

// 添加到RecyclerView
todoList.add(
ToDo(randomUUID, todoContent, todoSubject)
)

lifecycleScope.launch {
Repository.insert(
ToDoRoom(
randomUUID,
0,
todoSubject,
todoContent
if (todoContent == "/DEV MODE") {
if (Repository.getPreferenceBoolean(
ToDoApplication.context,
"dev_mode",
true
)
) {
Repository.setPreference(ToDoApplication.context, "dev_mode", false)
} else {
Repository.setPreference(ToDoApplication.context, "dev_mode", true)
"Dev Mode".showToast()
}
dialog.dismiss()
} else {
val randomUUID = UUID.randomUUID().toString()
val todoSubject = when (ToDoDialogBinding.todoSubject.checkedChipId) {
R.id.subject_chinese -> getString(R.string.subject_chinese)
R.id.subject_math -> getString(R.string.subject_math)
R.id.subject_english -> getString(R.string.subject_english)
R.id.subject_biology -> getString(R.string.subject_biology)
R.id.subject_geography -> getString(R.string.subject_geography)
R.id.subject_history -> getString(R.string.subject_history)
R.id.subject_physics -> getString(R.string.subject_physics)
R.id.subject_law -> getString(R.string.subject_law)
R.id.subject_other -> getString(R.string.subject_other)
else -> getString(R.string.subject_unknown)
}

// 显示RecyclerView
if (todoList.size + 1 > 0) {
todoViewModel.emptyTipVis.value = View.GONE
}

// 添加到RecyclerView
todoList.add(
ToDo(randomUUID, todoContent, todoSubject)
)
progressViewModel.updateProgress()
}

binding.todoList.adapter?.notifyItemInserted(todoList.size + 1)
dialog.dismiss()
lifecycleScope.launch {
Repository.insert(
ToDoRoom(
randomUUID,
0,
todoSubject,
todoContent
)
)
progressViewModel.updateProgress()
binding.todoList.adapter?.notifyItemInserted(todoList.size + 1)
dialog.dismiss()
}
}
}
}
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 9e8372f

Please sign in to comment.