BackupAdapter.kt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.example.modifier.adapter
  2. import android.annotation.SuppressLint
  3. import android.content.Context
  4. import android.content.DialogInterface
  5. import android.util.Log
  6. import android.view.LayoutInflater
  7. import android.view.View
  8. import android.view.ViewGroup
  9. import android.widget.Toast
  10. import androidx.appcompat.content.res.AppCompatResources
  11. import androidx.recyclerview.widget.RecyclerView
  12. import com.example.modifier.R
  13. import com.example.modifier.baseTag
  14. import com.example.modifier.Utils
  15. import com.example.modifier.adapter.BackupAdapter.BackupViewHolder
  16. import com.example.modifier.data.BackupItem
  17. import com.example.modifier.databinding.ItemBackupBinding
  18. import com.example.modifier.repo.BackupRepository
  19. import com.google.android.material.dialog.MaterialAlertDialogBuilder
  20. import kotlinx.coroutines.CoroutineScope
  21. import kotlinx.coroutines.Dispatchers
  22. import kotlinx.coroutines.launch
  23. import kotlinx.coroutines.withContext
  24. import java.io.File
  25. import java.text.SimpleDateFormat
  26. import java.util.Date
  27. class BackupAdapter(
  28. private val context: Context,
  29. private val backups: MutableList<BackupItem>,
  30. private val backupRepository: BackupRepository
  31. ) :
  32. RecyclerView.Adapter<BackupViewHolder>() {
  33. private val tag = "$baseTag/BackupAdapter"
  34. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BackupViewHolder {
  35. val binding = ItemBackupBinding.inflate(
  36. LayoutInflater.from(
  37. context
  38. ), parent, false
  39. )
  40. return BackupViewHolder(binding)
  41. }
  42. @SuppressLint("DefaultLocale")
  43. override fun onBindViewHolder(holder: BackupViewHolder, position: Int) {
  44. Log.i(tag, "onBindViewHolder")
  45. val backup = backups[position]
  46. holder.binding.tvNumber.text = "+${backup.code} ${backup.number}"
  47. holder.binding.tvTime.text =
  48. SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(backup.createdAt))
  49. holder.binding.tvInfo.text = String.format(
  50. "MCC: %s, MNC: %s, Country: %s, SendCount: %d, Fresh: %b",
  51. backup.mcc, backup.mnc, backup.country.uppercase(), backup.sendCount, backup.fresh
  52. )
  53. holder.binding.btnRestore.setOnClickListener { v: View? ->
  54. MaterialAlertDialogBuilder(
  55. context
  56. )
  57. .setTitle("Restore backup")
  58. .setMessage("Are you sure you want to restore this backup?")
  59. .setPositiveButton("Yes") { dialog: DialogInterface?, which: Int ->
  60. dialog?.dismiss()
  61. Utils.makeLoadingButton(context, holder.binding.btnRestore)
  62. CoroutineScope(Dispatchers.IO).launch {
  63. try {
  64. backupRepository.restore(backup)
  65. } catch (e: Exception) {
  66. e.printStackTrace()
  67. }
  68. withContext(Dispatchers.Main) {
  69. holder.binding.btnRestore.isEnabled = true
  70. holder.binding.btnRestore.icon = AppCompatResources.getDrawable(
  71. context,
  72. R.drawable.ic_settings_backup_restore
  73. )
  74. }
  75. }
  76. }
  77. .setNegativeButton("No", null)
  78. .show()
  79. }
  80. holder.binding.btnDel.setOnClickListener { v: View? ->
  81. MaterialAlertDialogBuilder(
  82. context
  83. )
  84. .setTitle("Delete backup")
  85. .setMessage("Are you sure you want to delete this backup?")
  86. .setPositiveButton("Yes") { dialog: DialogInterface?, which: Int ->
  87. CoroutineScope(Dispatchers.IO).launch {
  88. val file = File(backup.path)
  89. if (file.exists()) {
  90. try {
  91. File(backup.path).deleteRecursively()
  92. } catch (e: Exception) {
  93. e.printStackTrace()
  94. Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
  95. }
  96. } else {
  97. Log.e("BackupAdapter", "File not found")
  98. }
  99. backups.remove(backup)
  100. withContext(Dispatchers.Main) {
  101. notifyItemRemoved(position)
  102. }
  103. backupRepository.delete(backup)
  104. }
  105. }
  106. .setNegativeButton("No", null)
  107. .show()
  108. }
  109. }
  110. override fun getItemCount(): Int {
  111. return backups.size
  112. }
  113. inner class BackupViewHolder(var binding: ItemBackupBinding) : RecyclerView.ViewHolder(
  114. binding.root
  115. )
  116. }