taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

UserPreferencesSerializer.kt (1788B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2024 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.settings
     18 
     19 import android.content.Context
     20 import androidx.datastore.core.CorruptionException
     21 import androidx.datastore.core.DataStore
     22 import androidx.datastore.core.Serializer
     23 import androidx.datastore.dataStore
     24 import com.google.protobuf.InvalidProtocolBufferException
     25 import net.taler.wallet.UserPreferences
     26 import java.io.InputStream
     27 import java.io.OutputStream
     28 
     29 object UserPreferencesSerializer: Serializer<UserPreferences> {
     30     override val defaultValue = UserPreferences.getDefaultInstance()
     31 
     32     override suspend fun readFrom(input: InputStream): UserPreferences {
     33         try {
     34             return UserPreferences.parseFrom(input)
     35         } catch (exception: InvalidProtocolBufferException) {
     36             throw CorruptionException("Cannot read proto.", exception)
     37         }
     38     }
     39 
     40     override suspend fun writeTo(t: UserPreferences, output: OutputStream) =
     41         t.writeTo(output)
     42 
     43 }
     44 
     45 val Context.userPreferencesDataStore: DataStore<UserPreferences> by dataStore(
     46     fileName = "settings.pb",
     47     serializer = UserPreferencesSerializer,
     48 )