Skip to content

Commit

Permalink
[RFR-780] Add synchronous DataStore implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 committed Sep 6, 2023
1 parent 1bc6aa9 commit 7f923f8
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/
package de.cyface.energy_settings

import android.content.Context
import androidx.core.content.edit
import de.cyface.energy_settings.Constants.PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
* Custom settings used by this library.
Expand All @@ -33,20 +32,29 @@ import de.cyface.energy_settings.Constants.PREFERENCES_MANUFACTURER_WARNING_SHOW
*
* @author Armin Schnabel
* @version 2.0.0
* @since 3.4.0
* @since 3.3.4
*/
class CustomPreferences(context: Context) {
class CustomSettings {

fun saveWarningShown(warningShown: Boolean) {
//FIXME
/*preferences.edit {
putBoolean(PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY, warningShown)
apply()
}*/
/**
* Saves whether the user marked the manufacturer-specific warning as "don't show again".
*
* @param value The boolean value to save.
*/
@Suppress("unused") // Part of the API
suspend fun setManufacturerWarningShown(value: Boolean) {
TrackingSettings.dataStore.updateData { currentSettings ->
currentSettings.toBuilder()
.setManufacturerWarningShown(value)
.build()
}
}

fun getWarningShown(): Boolean {
return true // FIXME
//return preferences.getBoolean(PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY, false)
}
/**
* @return Whether user marked the manufacturer-specific warning as "don't show again".
*/
val manufacturerWarningShownFlow: Flow<Boolean> = TrackingSettings.dataStore.data
.map { settings ->
settings.manufacturerWarningShown
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import com.afollestad.materialdialogs.MaterialDialog
import de.cyface.energy_settings.Constants.TAG
import de.cyface.energy_settings.GnssDisabledWarningDialog.Companion.create
import de.cyface.energy_settings.ProblematicManufacturerWarningDialog.Companion.create
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.Locale

/**
Expand Down Expand Up @@ -67,7 +69,9 @@ internal class ProblematicManufacturerWarningDialog(private val recipientEmail:

// Allow the user to express its preference to disable auto-popup of this dialog
builder.setNegativeButton(negativeButtonRes) { _, _ ->
onNegativeButtonCall(context)
GlobalScope.launch { // FIXME
onNegativeButtonCall(context)
}
}

// Show Sony STAMINA specific dialog (no manufacturer specific intent name known yet)
Expand Down Expand Up @@ -166,8 +170,8 @@ internal class ProblematicManufacturerWarningDialog(private val recipientEmail:
/**
* Saves the user's preference to disable auto-popup of this dialog
*/
private fun onNegativeButtonCall(context: Context?) {
CustomPreferences(context!!).saveWarningShown(true)
private suspend fun onNegativeButtonCall(context: Context?) {
CustomSettings().setManufacturerWarningShown(true)
}

/**
Expand All @@ -183,7 +187,9 @@ internal class ProblematicManufacturerWarningDialog(private val recipientEmail:

// Allow the user to express its preference to disable auto-popup of this dialog
dialog.negativeButton(negativeButtonRes) {
onNegativeButtonCall(activity.applicationContext)
GlobalScope.launch { // FIXME
onNegativeButtonCall(activity.applicationContext)
}
}

// Show Sony STAMINA specific dialog (no manufacturer specific intent name known yet)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* Copyright 2023 Cyface GmbH
*
* This file is part of the Cyface App for Android.
* This file is part of the Cyface Energy Settings for Android.
*
* The Cyface App for Android is free software: you can redistribute it and/or modify
* The Cyface Energy Settings for Android is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface App for Android is distributed in the hope that it will be useful,
* The Cyface Energy Settings for Android is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface App for Android. If not, see <http://www.gnu.org/licenses/>.
* along with the Cyface Energy Settings for Android. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.energy_settings

Expand All @@ -25,12 +25,12 @@ import java.io.InputStream
import java.io.OutputStream

/**
* The serializer for the Proto DataStore of the preferences stored in the [Settings] file.
* The serializer for the Proto DataStore of the settings stored in the [Settings] file.
*
* For details: https://developer.android.com/topic/libraries/architecture/datastore#proto-datastore
*
* @author Armin Schnabel
* @since 3.7.0
* @since 3.4.0
* @version 1.0.0
*/
object SettingsSerializer : Serializer<Settings> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,48 @@ import android.os.Build
import android.os.PowerManager
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.dataStoreFile
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import de.cyface.utils.Validate
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import java.util.Locale

/**
* Holds the API for this energy setting library.
*
* Offers checks and dialogs for energy settings required for background tracking.
*
* Attention: You need to call [initialize] before you use this object, e.g. in Activity.onCreate.
*
* @author Armin Schnabel
* @version 2.0.3
* @since 1.0.0
*/
object TrackingSettings {

/**
* Custom settings used by this library.
*/
private val settings: CustomSettings = CustomSettings()

/**
* The data store with single-process support.
*/
lateinit var dataStore: DataStore<Settings>

@JvmStatic
fun initialize(context: Context) {
val dataStoreFile = context.dataStoreFile("energy_settings.pb")
dataStore = DataStoreFactory.create(
serializer = SettingsSerializer,
produceFile = { dataStoreFile }
)
}

/**
* Checks whether the energy safer mode is active *at this moment*.
*
Expand Down Expand Up @@ -311,9 +337,12 @@ object TrackingSettings {
force: Boolean,
recipientEmail: String
): Boolean {

val preferences = CustomPreferences(context)
if (isProblematicManufacturer && (force || !preferences.getWarningShown())) {
// FIXME: consider async-preloading data at least, see:
// https://developer.android.com/topic/libraries/architecture/datastore#synchronous
val warningShown = runBlocking { // FIXME
settings.manufacturerWarningShownFlow.first() // FIXME
}
if (isProblematicManufacturer && (force || !warningShown)) {
val fragmentManager = fragment.fragmentManager
Validate.notNull(fragmentManager)
val dialog = ProblematicManufacturerWarningDialog(recipientEmail)
Expand Down Expand Up @@ -351,8 +380,11 @@ object TrackingSettings {
return false
}

val preferences = CustomPreferences(activity.applicationContext)
if (isProblematicManufacturer && (force || !preferences.getWarningShown())) {
val warningShown = runBlocking { // FIXME
settings.manufacturerWarningShownFlow.first() // FIXME
}

if (isProblematicManufacturer && (force || !warningShown)) {
ProblematicManufacturerWarningDialog.create(activity, recipientEmail).show()
return true
}
Expand Down
38 changes: 38 additions & 0 deletions energy_settings/src/main/proto/energy_settings.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 Cyface GmbH
*
* This file is part of the Cyface Energy Settings for Android.
*
* The Cyface Energy Settings for Android is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Cyface Energy Settings for Android is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Cyface Energy Settings for Android. If not, see <http://www.gnu.org/licenses/>.
*/
syntax = "proto3";

option java_package = "de.cyface.energy_settings";
option java_multiple_files = true;

/**
* The data types for the values stored in the `EnergySettings`.
*
* See https://protobuf.dev/programming-guides/proto3/
*
* Attention: The classes are generated from the file at compile time. Don't forget to rebuild.
*
* @author: Armin Schnabel
* @since: 3.4.0
* @version: 1.0.0
*/
message Settings {
// Whether the user marked the manufacturer-specific warning as "don't show again".
bool manufacturer_warning_shown = 1;
}
53 changes: 0 additions & 53 deletions energy_settings/src/main/proto/settings.proto

This file was deleted.

0 comments on commit 7f923f8

Please sign in to comment.