-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate from SharedPreferences to DataStore
- Loading branch information
Showing
8 changed files
with
113 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/PrivacyDisclaimerRepository.kt
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/UserPreferencesMigration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.mullvad.mullvadvpn.repository | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataMigration | ||
import androidx.datastore.migrations.SharedPreferencesMigration | ||
import androidx.datastore.migrations.SharedPreferencesView | ||
import net.mullvad.mullvadvpn.di.APP_PREFERENCES_NAME | ||
|
||
private const val IS_PRIVACY_DISCLOSURE_ACCEPTED_KEY_SHARED_PREF_KEY = | ||
"is_privacy_disclosure_accepted" | ||
|
||
data object UserPreferencesMigration { | ||
fun migrations(context: Context): List<DataMigration<UserPreferences>> = | ||
listOf( | ||
SharedPreferencesMigration( | ||
context, | ||
sharedPreferencesName = APP_PREFERENCES_NAME, | ||
keysToMigrate = setOf(IS_PRIVACY_DISCLOSURE_ACCEPTED_KEY_SHARED_PREF_KEY), | ||
) { sharedPrefs: SharedPreferencesView, currentData: UserPreferences -> | ||
val privacyDisclosureAccepted = | ||
sharedPrefs.getBoolean( | ||
IS_PRIVACY_DISCLOSURE_ACCEPTED_KEY_SHARED_PREF_KEY, | ||
false, | ||
) | ||
currentData | ||
.toBuilder() | ||
.setIsPrivacyDisclosureAccepted(privacyDisclosureAccepted) | ||
.build() | ||
} | ||
) | ||
} |
32 changes: 32 additions & 0 deletions
32
android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/UserPreferencesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package net.mullvad.mullvadvpn.repository | ||
|
||
import androidx.datastore.core.DataStore | ||
import co.touchlab.kermit.Logger | ||
import java.io.IOException | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.first | ||
|
||
class UserPreferencesRepository(private val userPreferences: DataStore<UserPreferences>) { | ||
|
||
// Note: this should not be made into a StateFlow. See: | ||
// https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore#data() | ||
val preferencesFlow: Flow<UserPreferences> = | ||
userPreferences.data.catch { exception -> | ||
// dataStore.data throws an IOException when an error is encountered when reading data | ||
if (exception is IOException) { | ||
Logger.e("Error reading user preferences file, falling back to default.", exception) | ||
emit(UserPreferences.getDefaultInstance()) | ||
} else { | ||
throw exception | ||
} | ||
} | ||
|
||
suspend fun preferences(): UserPreferences = preferencesFlow.first() | ||
|
||
suspend fun setPrivacyDisclosureAccepted() { | ||
userPreferences.updateData { prefs -> | ||
prefs.toBuilder().setIsPrivacyDisclosureAccepted(true).build() | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/UserPreferencesSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.mullvad.mullvadvpn.repository | ||
|
||
import androidx.datastore.core.CorruptionException | ||
import androidx.datastore.core.Serializer | ||
import com.google.protobuf.InvalidProtocolBufferException | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
|
||
object UserPreferencesSerializer : Serializer<UserPreferences> { | ||
override val defaultValue: UserPreferences = UserPreferences.getDefaultInstance() | ||
|
||
override suspend fun readFrom(input: InputStream): UserPreferences { | ||
try { | ||
return UserPreferences.parseFrom(input) | ||
} catch (exception: InvalidProtocolBufferException) { | ||
throw CorruptionException("Cannot read proto", exception) | ||
} | ||
} | ||
|
||
override suspend fun writeTo(t: UserPreferences, output: OutputStream) = t.writeTo(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters