|
| 1 | +/**************************************************************************************** |
| 2 | + * Copyright (c) 2025 lukstbit <[email protected]> * |
| 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 FOR A * |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 | + * * |
| 13 | + * You should have received a copy of the GNU General Public License along with * |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 | + ****************************************************************************************/ |
| 16 | + |
| 17 | +package com.ichi2.anki.notetype |
| 18 | + |
| 19 | +import androidx.lifecycle.ViewModel |
| 20 | +import androidx.lifecycle.viewModelScope |
| 21 | +import anki.collection.OpChanges |
| 22 | +import anki.notetypes.Notetype |
| 23 | +import anki.notetypes.copy |
| 24 | +import com.ichi2.anki.CollectionManager.withCol |
| 25 | +import com.ichi2.anki.libanki.Collection |
| 26 | +import com.ichi2.anki.libanki.NoteTypeId |
| 27 | +import com.ichi2.anki.libanki.getNotetype |
| 28 | +import com.ichi2.anki.libanki.getNotetypeNameIdUseCount |
| 29 | +import com.ichi2.anki.libanki.removeNotetype |
| 30 | +import com.ichi2.anki.libanki.updateNotetype |
| 31 | +import com.ichi2.anki.notetype.ManageNoteTypesState.CardEditor |
| 32 | +import com.ichi2.anki.notetype.ManageNoteTypesState.FieldsEditor |
| 33 | +import com.ichi2.anki.notetype.ManageNoteTypesState.ReportableException |
| 34 | +import com.ichi2.anki.notetype.ManageNoteTypesState.UserMessage |
| 35 | +import com.ichi2.anki.notetype.NoteTypeItemState.Companion.asModel |
| 36 | +import com.ichi2.anki.observability.undoableOp |
| 37 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 38 | +import kotlinx.coroutines.flow.StateFlow |
| 39 | +import kotlinx.coroutines.flow.asStateFlow |
| 40 | +import kotlinx.coroutines.flow.update |
| 41 | +import kotlinx.coroutines.launch |
| 42 | +import net.ankiweb.rsdroid.BackendException |
| 43 | + |
| 44 | +class ManageNoteTypesViewModel : ViewModel() { |
| 45 | + private val _state = MutableStateFlow(ManageNoteTypesState()) |
| 46 | + val state: StateFlow<ManageNoteTypesState> = _state.asStateFlow() |
| 47 | + private lateinit var initialNoteTypes: List<NoteTypeItemState> |
| 48 | + |
| 49 | + init { |
| 50 | + refreshNoteTypes() |
| 51 | + } |
| 52 | + |
| 53 | + fun refreshNoteTypes() { |
| 54 | + _state.update { oldState -> oldState.copy(isLoading = true) } |
| 55 | + viewModelScope.launch { |
| 56 | + withCol { safeGetNotetypeNameIdUseCount() } |
| 57 | + .onFailure { |
| 58 | + _state.update { oldState -> |
| 59 | + oldState.copy(isLoading = false, error = ReportableException(it)) |
| 60 | + } |
| 61 | + }.onSuccess { |
| 62 | + initialNoteTypes = it |
| 63 | + _state.update { oldState -> |
| 64 | + oldState.copy(isLoading = false, noteTypes = it) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fun filter(query: String) { |
| 71 | + val matchedNoteTypes = |
| 72 | + initialNoteTypes.filter { entry -> |
| 73 | + entry.name.contains(query) |
| 74 | + } |
| 75 | + _state.update { oldState -> |
| 76 | + oldState.copy(isLoading = false, noteTypes = matchedNoteTypes, searchQuery = query) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fun rename( |
| 81 | + nid: NoteTypeId, |
| 82 | + name: String, |
| 83 | + ) { |
| 84 | + _state.update { oldState -> oldState.copy(isLoading = true) } |
| 85 | + viewModelScope.launch { |
| 86 | + undoableOp<OpChanges> { |
| 87 | + safeRenameNoteType(nid, name) |
| 88 | + .onSuccess { changes -> |
| 89 | + _state.update { oldState -> |
| 90 | + val updatedNoteTypes = |
| 91 | + oldState.noteTypes |
| 92 | + .map { noteTypeState -> |
| 93 | + if (noteTypeState.id == nid) { |
| 94 | + noteTypeState.copy(name = name) |
| 95 | + } else { |
| 96 | + noteTypeState |
| 97 | + } |
| 98 | + }.also { initialNoteTypes = it } |
| 99 | + oldState.copy(isLoading = false, noteTypes = updatedNoteTypes) |
| 100 | + } |
| 101 | + return@undoableOp changes |
| 102 | + }.onFailure { |
| 103 | + // TODO: Change to CardTypeException: https://github.com/ankidroid/Anki-Android-Backend/issues/537 |
| 104 | + // Card template 1 in note type 'character' has a problem. |
| 105 | + // Expected to find a field replacement on the front of the card template. |
| 106 | + _state.update { oldState -> |
| 107 | + oldState.copy( |
| 108 | + isLoading = false, |
| 109 | + error = ReportableException(it, it !is BackendException), |
| 110 | + ) |
| 111 | + } |
| 112 | + OpChanges.getDefaultInstance() |
| 113 | + } |
| 114 | + OpChanges.getDefaultInstance() |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fun delete(nid: NoteTypeId) { |
| 120 | + _state.update { oldState -> oldState.copy(isLoading = true) } |
| 121 | + val noteTypesCount = _state.value.noteTypes.size |
| 122 | + viewModelScope.launch { |
| 123 | + undoableOp<OpChanges> { |
| 124 | + if (noteTypesCount <= 1) { |
| 125 | + _state.update { oldState -> |
| 126 | + oldState.copy(isLoading = false, message = UserMessage.DeletingLastModel) |
| 127 | + } |
| 128 | + OpChanges.getDefaultInstance() |
| 129 | + } |
| 130 | + safeRemoveNoteType(nid) |
| 131 | + .onSuccess { changes -> |
| 132 | + _state.update { oldState -> |
| 133 | + val updatedNoteTypes = |
| 134 | + oldState.noteTypes |
| 135 | + .filter { it.id != nid } |
| 136 | + .also { initialNoteTypes = it } |
| 137 | + oldState.copy(isLoading = false, noteTypes = updatedNoteTypes) |
| 138 | + } |
| 139 | + return@undoableOp changes |
| 140 | + }.onFailure { |
| 141 | + _state.update { oldState -> |
| 142 | + oldState.copy(isLoading = false, error = ReportableException(it)) |
| 143 | + } |
| 144 | + return@undoableOp OpChanges.getDefaultInstance() |
| 145 | + } |
| 146 | + OpChanges.getDefaultInstance() |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + fun onItemClick(entry: NoteTypeItemState) { |
| 152 | + _state.update { oldState -> |
| 153 | + oldState.copy(destination = FieldsEditor(entry.id, entry.name)) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + fun onCardEditorRequested(entry: NoteTypeItemState) { |
| 158 | + _state.update { oldState -> |
| 159 | + oldState.copy(destination = CardEditor(entry.id)) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + fun clearMessage() { |
| 164 | + _state.update { oldState -> oldState.copy(message = null) } |
| 165 | + } |
| 166 | + |
| 167 | + fun clearError() { |
| 168 | + _state.update { oldState -> oldState.copy(error = null) } |
| 169 | + } |
| 170 | + |
| 171 | + fun clearDestination() { |
| 172 | + _state.update { oldState -> oldState.copy(destination = null) } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +private fun Collection.safeRenameNoteType( |
| 177 | + nid: NoteTypeId, |
| 178 | + newName: String, |
| 179 | +): Result<OpChanges> = |
| 180 | + try { |
| 181 | + val currentNoteType: Notetype = getNotetype(nid) |
| 182 | + val renamedNotetype = currentNoteType.copy { this.name = newName } |
| 183 | + Result.success(updateNotetype(renamedNotetype)) |
| 184 | + } catch (exception: Exception) { |
| 185 | + Result.failure(exception) |
| 186 | + } |
| 187 | + |
| 188 | +private fun Collection.safeRemoveNoteType(nid: NoteTypeId): Result<OpChanges> = |
| 189 | + try { |
| 190 | + Result.success(removeNotetype(nid)) |
| 191 | + } catch (exception: Exception) { |
| 192 | + Result.failure(exception) |
| 193 | + } |
| 194 | + |
| 195 | +private fun Collection.safeGetNotetypeNameIdUseCount(): Result<List<NoteTypeItemState>> = |
| 196 | + try { |
| 197 | + Result.success(getNotetypeNameIdUseCount().map(::asModel)) |
| 198 | + } catch (exception: Exception) { |
| 199 | + Result.failure(exception) |
| 200 | + } |
0 commit comments