|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Ashish Yadav <mailtoashish693@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 12 | + * details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along with |
| 15 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.ichi2.anki.mediacheck |
| 19 | + |
| 20 | +import android.content.Context |
| 21 | +import android.content.Intent |
| 22 | +import android.os.Bundle |
| 23 | +import android.view.View |
| 24 | +import android.widget.TextView |
| 25 | +import androidx.appcompat.app.AlertDialog |
| 26 | +import androidx.fragment.app.Fragment |
| 27 | +import androidx.fragment.app.viewModels |
| 28 | +import androidx.lifecycle.lifecycleScope |
| 29 | +import androidx.recyclerview.widget.RecyclerView |
| 30 | +import com.google.android.material.appbar.MaterialToolbar |
| 31 | +import com.google.android.material.button.MaterialButton |
| 32 | +import com.ichi2.anki.CollectionManager.TR |
| 33 | +import com.ichi2.anki.R |
| 34 | +import com.ichi2.anki.SingleFragmentActivity |
| 35 | +import com.ichi2.anki.launchCatchingTask |
| 36 | +import com.ichi2.anki.ui.internationalization.toSentenceCase |
| 37 | +import com.ichi2.anki.withProgress |
| 38 | +import com.ichi2.libanki.MediaCheckResult |
| 39 | +import com.ichi2.utils.cancelable |
| 40 | +import com.ichi2.utils.message |
| 41 | +import com.ichi2.utils.negativeButton |
| 42 | +import com.ichi2.utils.positiveButton |
| 43 | +import com.ichi2.utils.show |
| 44 | +import com.ichi2.utils.title |
| 45 | +import kotlinx.coroutines.flow.collectLatest |
| 46 | +import kotlinx.coroutines.launch |
| 47 | + |
| 48 | +/** |
| 49 | + * MediaCheckFragment for displaying a list of media files that are either unused or missing. |
| 50 | + * It allows users to tag missing media files or delete unused ones. |
| 51 | + **/ |
| 52 | +class MediaCheckFragment : Fragment(R.layout.fragment_media_check) { |
| 53 | + private val viewModel: MediaCheckViewModel by viewModels() |
| 54 | + private lateinit var adapter: MediaCheckAdapter |
| 55 | + |
| 56 | + private lateinit var deleteMediaButton: MaterialButton |
| 57 | + private lateinit var tagMissingButton: MaterialButton |
| 58 | + |
| 59 | + override fun onViewCreated( |
| 60 | + view: View, |
| 61 | + savedInstanceState: Bundle?, |
| 62 | + ) { |
| 63 | + super.onViewCreated(view, savedInstanceState) |
| 64 | + |
| 65 | + view.findViewById<MaterialToolbar>(R.id.toolbar).apply { |
| 66 | + setTitle(TR.mediaCheckCheckMediaAction().toSentenceCase(requireContext(), R.string.check_media)) |
| 67 | + setNavigationOnClickListener { |
| 68 | + requireActivity().onBackPressedDispatcher.onBackPressed() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + deleteMediaButton = view.findViewById(R.id.delete_used_media_button) |
| 73 | + tagMissingButton = view.findViewById(R.id.tag_missing_media_button) |
| 74 | + |
| 75 | + val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView) |
| 76 | + |
| 77 | + adapter = MediaCheckAdapter(requireContext()) |
| 78 | + recyclerView.adapter = adapter |
| 79 | + |
| 80 | + launchCatchingTask { |
| 81 | + withProgress(R.string.check_media_message) { |
| 82 | + viewModel.checkMedia().join() |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + lifecycleScope.launch { |
| 87 | + viewModel.mediaCheckResult.collectLatest { result -> |
| 88 | + view.findViewById<TextView>(R.id.unused_media_count)?.apply { |
| 89 | + text = (TR.mediaCheckUnusedCount(result?.unusedFileNames?.size ?: 0)) |
| 90 | + } |
| 91 | + |
| 92 | + view.findViewById<TextView>(R.id.missing_media_count)?.apply { |
| 93 | + text = (TR.mediaCheckMissingCount(result?.missingMediaNotes?.size ?: 0)) |
| 94 | + } |
| 95 | + |
| 96 | + result?.let { files -> |
| 97 | + handleMediaResult(files) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + setupButtonListeners() |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Processes media check results and updates the UI. |
| 107 | + * |
| 108 | + * @param mediaCheckResult The result containing missing and unused media file names. |
| 109 | + */ |
| 110 | + private fun handleMediaResult(mediaCheckResult: MediaCheckResult) { |
| 111 | + val fileList = |
| 112 | + buildList { |
| 113 | + if (mediaCheckResult.missingFileNames.isNotEmpty()) { |
| 114 | + tagMissingButton.visibility = View.VISIBLE |
| 115 | + add(TR.mediaCheckMissingHeader()) |
| 116 | + addAll(mediaCheckResult.missingFileNames.map(TR::mediaCheckMissingFile)) |
| 117 | + } |
| 118 | + if (mediaCheckResult.unusedFileNames.isNotEmpty()) { |
| 119 | + deleteMediaButton.visibility = View.VISIBLE |
| 120 | + if (isNotEmpty()) add("\n") |
| 121 | + add(TR.mediaCheckUnusedHeader()) |
| 122 | + addAll(mediaCheckResult.unusedFileNames.map(TR::mediaCheckUnusedFile)) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + adapter.submitList(fileList) |
| 127 | + } |
| 128 | + |
| 129 | + private fun setupButtonListeners() { |
| 130 | + tagMissingButton.apply { |
| 131 | + // mediaCheckAddTag => "Tag Missing" |
| 132 | + text = TR.mediaCheckAddTag().toSentenceCase(requireContext(), R.string.tag_missing) |
| 133 | + |
| 134 | + setOnClickListener { |
| 135 | + launchCatchingTask { |
| 136 | + withProgress(getString(R.string.check_media_adding_missing_tag)) { |
| 137 | + viewModel.tagMissing(TR.mediaCheckMissingMediaTag()).join() |
| 138 | + showResultDialog( |
| 139 | + R.string.check_media_tags_added, |
| 140 | + TR.browsingNotesUpdated(viewModel.taggedFiles), |
| 141 | + ) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + deleteMediaButton.apply { |
| 148 | + text = |
| 149 | + TR.mediaCheckDeleteUnused().toSentenceCase( |
| 150 | + requireContext(), |
| 151 | + R.string.check_media_delete_unused, |
| 152 | + ) |
| 153 | + |
| 154 | + setOnClickListener { |
| 155 | + deleteConfirmationDialog() |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private fun deleteConfirmationDialog() { |
| 161 | + AlertDialog.Builder(requireContext()).show { |
| 162 | + message(text = TR.mediaCheckDeleteUnusedConfirm()) |
| 163 | + positiveButton(R.string.dialog_ok) { handleDeleteConfirmation() } |
| 164 | + negativeButton(R.string.dialog_cancel) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private fun handleDeleteConfirmation() { |
| 169 | + launchCatchingTask { |
| 170 | + withProgress(resources.getString(R.string.delete_media_message)) { |
| 171 | + viewModel.deleteUnusedMedia().join() |
| 172 | + showDeletionResult() |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private fun showDeletionResult() { |
| 178 | + showResultDialog( |
| 179 | + R.string.delete_media_result_title, |
| 180 | + resources.getQuantityString( |
| 181 | + R.plurals.delete_media_result_message, |
| 182 | + viewModel.deletedFiles, |
| 183 | + viewModel.deletedFiles, |
| 184 | + ), |
| 185 | + ) |
| 186 | + } |
| 187 | + |
| 188 | + private fun showResultDialog( |
| 189 | + titleRes: Int, |
| 190 | + message: String, |
| 191 | + ) { |
| 192 | + AlertDialog.Builder(requireContext()).show { |
| 193 | + title(titleRes) |
| 194 | + message(text = message) |
| 195 | + positiveButton(R.string.dialog_ok) { |
| 196 | + requireActivity().finish() |
| 197 | + } |
| 198 | + cancelable(false) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + companion object { |
| 203 | + fun getIntent(context: Context): Intent = SingleFragmentActivity.getIntent(context, MediaCheckFragment::class) |
| 204 | + } |
| 205 | +} |
0 commit comments