ViewMode.kt (2483B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2025 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler 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 11 * A 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 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.wallet.main 18 19 import net.taler.common.CurrencySpecification 20 import net.taler.wallet.PrefsViewMode 21 import net.taler.wallet.ViewModeType 22 import net.taler.wallet.balances.ScopeInfo 23 import net.taler.wallet.transactions.TransactionStateFilter 24 25 sealed class ViewMode { 26 abstract fun toPrefs(): PrefsViewMode 27 28 data object Assets: ViewMode() { 29 override fun toPrefs(): PrefsViewMode = PrefsViewMode 30 .newBuilder() 31 .setType(ViewModeType.ASSETS) 32 .build() 33 } 34 35 data class Transactions( 36 val selectedScope: ScopeInfo, 37 val selectedSpec: CurrencySpecification? = null, 38 val searchQuery: String? = null, 39 val stateFilter: TransactionStateFilter? = null, 40 ): ViewMode() { 41 override fun toPrefs(): PrefsViewMode = PrefsViewMode 42 .newBuilder() 43 .setType(ViewModeType.TRANSACTIONS) 44 .setSelectedScope(selectedScope.toPrefs()) 45 .apply { 46 if (this@Transactions.searchQuery != null) 47 setSearchQuery(this@Transactions.searchQuery) 48 if (this@Transactions.stateFilter != null) 49 setStateFilter(this@Transactions.stateFilter.toPrefs()) 50 }.build() 51 } 52 53 companion object { 54 fun fromPrefs(prefs: PrefsViewMode): ViewMode = when (prefs.type) { 55 ViewModeType.ASSETS, ViewModeType.UNRECOGNIZED -> Assets 56 57 ViewModeType.TRANSACTIONS -> Transactions( 58 // TODO: better null handling 59 selectedScope = ScopeInfo.fromPrefs(prefs.selectedScope)!!, 60 searchQuery = prefs.searchQuery, 61 stateFilter = TransactionStateFilter.fromPrefs(prefs.stateFilter), 62 ) 63 } 64 } 65 }