|
|
@@ -1,11 +1,14 @@
|
|
|
package com.example.modifier.ui.backup
|
|
|
|
|
|
import android.content.Context
|
|
|
+import android.content.DialogInterface
|
|
|
import android.os.Bundle
|
|
|
import android.util.Log
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
+import android.widget.Toast
|
|
|
+import androidx.appcompat.app.AlertDialog
|
|
|
import androidx.core.view.ViewCompat
|
|
|
import androidx.core.view.WindowInsetsCompat
|
|
|
import androidx.core.widget.doAfterTextChanged
|
|
|
@@ -14,32 +17,32 @@ import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.asFlow
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
-import com.example.modifier.baseTag
|
|
|
import com.example.modifier.adapter.BackupAdapter
|
|
|
+import com.example.modifier.baseTag
|
|
|
import com.example.modifier.data.AppDatabase
|
|
|
import com.example.modifier.data.BackupItem
|
|
|
import com.example.modifier.databinding.FragmentBackupBinding
|
|
|
-import com.example.modifier.model.SpoofedSimInfo
|
|
|
import com.example.modifier.repo.BackupRepository
|
|
|
-import com.example.modifier.repo.SpoofedSimInfoRepo
|
|
|
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.FlowPreview
|
|
|
-import kotlinx.coroutines.flow.StateFlow
|
|
|
import kotlinx.coroutines.flow.debounce
|
|
|
import kotlinx.coroutines.launch
|
|
|
import kotlinx.coroutines.withContext
|
|
|
+import java.io.File
|
|
|
|
|
|
class BackupFragment : Fragment() {
|
|
|
private val tag = "$baseTag/BackupFragment"
|
|
|
private lateinit var binding: FragmentBackupBinding
|
|
|
private val list = mutableListOf<BackupItem>()
|
|
|
private lateinit var adapter: BackupAdapter
|
|
|
- private val spoofedSimInfoRepo = SpoofedSimInfoRepo.instance
|
|
|
+ private lateinit var onItemClickListener: BackupAdapter.OnItemClickListener
|
|
|
private val backupItemDao by lazy {
|
|
|
AppDatabase.getDatabase(requireContext()).itemDao()
|
|
|
}
|
|
|
private val backupRepository by lazy {
|
|
|
- BackupRepository(requireContext(), backupItemDao, spoofedSimInfoRepo)
|
|
|
+ BackupRepository(requireContext(), backupItemDao)
|
|
|
}
|
|
|
private var sort: Boolean
|
|
|
get() {
|
|
|
@@ -51,6 +54,7 @@ class BackupFragment : Fragment() {
|
|
|
.putBoolean("sort", value).apply()
|
|
|
}
|
|
|
private val searchText = MutableLiveData<String>()
|
|
|
+ private lateinit var progressDialog: AlertDialog
|
|
|
|
|
|
@OptIn(FlowPreview::class)
|
|
|
override fun onCreateView(
|
|
|
@@ -58,8 +62,72 @@ class BackupFragment : Fragment() {
|
|
|
savedInstanceState: Bundle?
|
|
|
): View {
|
|
|
if (!this::binding.isInitialized) {
|
|
|
+ progressDialog = MaterialAlertDialogBuilder(requireContext())
|
|
|
+ .setTitle("Please wait...")
|
|
|
+ .setMessage("Processing...")
|
|
|
+ .setCancelable(false)
|
|
|
+ .create()
|
|
|
binding = FragmentBackupBinding.inflate(inflater, container, false)
|
|
|
- adapter = BackupAdapter(requireContext(), list, backupRepository)
|
|
|
+ onItemClickListener = object : BackupAdapter.OnItemClickListener {
|
|
|
+ override fun onRestoreClick(backup: BackupItem, position: Int) {
|
|
|
+ MaterialAlertDialogBuilder(requireContext())
|
|
|
+ .setTitle("Restore backup")
|
|
|
+ .setMessage("Are you sure you want to restore this backup?")
|
|
|
+ .setPositiveButton("Yes") { dialog: DialogInterface?, which: Int ->
|
|
|
+ dialog?.dismiss()
|
|
|
+ progressDialog.show()
|
|
|
+ CoroutineScope(Dispatchers.IO).launch {
|
|
|
+ try {
|
|
|
+ backupRepository.restore(backup)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ e.printStackTrace()
|
|
|
+ }
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
+ progressDialog.dismiss()
|
|
|
+ Toast.makeText(
|
|
|
+ requireContext(),
|
|
|
+ "Restore completed",
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ .setNegativeButton("No", null)
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDeleteClick(backup: BackupItem, position: Int) {
|
|
|
+ progressDialog.show()
|
|
|
+ CoroutineScope(Dispatchers.IO).launch {
|
|
|
+ val file = File(backup.path)
|
|
|
+ if (file.exists()) {
|
|
|
+ try {
|
|
|
+ File(backup.path).deleteRecursively()
|
|
|
+
|
|
|
+ } catch (e: Exception) {
|
|
|
+ e.printStackTrace()
|
|
|
+ Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Log.e("BackupAdapter", "File not found")
|
|
|
+ }
|
|
|
+ list.remove(backup)
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
+ adapter.notifyDataSetChanged()
|
|
|
+ }
|
|
|
+ backupRepository.delete(backup)
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
+ progressDialog.dismiss()
|
|
|
+ Toast.makeText(
|
|
|
+ requireContext(),
|
|
|
+ "Backup deleted",
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ adapter = BackupAdapter(requireContext(), list, onItemClickListener)
|
|
|
binding.rvBackup.adapter = adapter
|
|
|
binding.rvBackup.layoutManager = LinearLayoutManager(requireContext())
|
|
|
binding.btnSort.setOnClickListener {
|
|
|
@@ -72,11 +140,18 @@ class BackupFragment : Fragment() {
|
|
|
}
|
|
|
binding.btnAdd.setOnClickListener {
|
|
|
lifecycleScope.launch {
|
|
|
+ progressDialog.show()
|
|
|
withContext(Dispatchers.IO) {
|
|
|
- val backup = backupRepository.backup(spoofedSimInfoRepo.spoofedSimInfo.value, "manual", 0)
|
|
|
+ val backup = backupRepository.backup(type = "manual", sendCount = 0)
|
|
|
list.add(0, backup)
|
|
|
}
|
|
|
adapter.notifyItemInserted(0)
|
|
|
+ progressDialog.dismiss()
|
|
|
+ Toast.makeText(
|
|
|
+ requireContext(),
|
|
|
+ "Backup created",
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
}
|
|
|
}
|
|
|
binding.etSearch.doAfterTextChanged {
|