Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions utils/src/main/kotlin/de/cyface/utils/settings/AppSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,29 @@ import java.io.File
* If this changes, consider using the standard Android Architecture, see `MeasurementRepository`.
*
* @author Armin Schnabel
* @version 2.1.0
* @version 3.0.0
* @since 3.4.0
* @param context The context to access the preferences from.
*/
@Suppress("unused") // Part of the API
class AppSettings(context: Context) {
class AppSettings private constructor(context: Context) {

/**
* Use Singleton to ensure only one instance per process is created. [LEIP-294]
*
* It should be okay to use a Singleton as this is also suggested in the documentation:
* https://developer.android.com/topic/libraries/architecture/datastore#multiprocess
*/
companion object {
@Volatile
private var instance: AppSettings? = null

fun getInstance(context: Context): AppSettings {
return instance ?: synchronized(this) {
instance ?: AppSettings(context.applicationContext).also { instance = it }
}
}
}

/**
* This avoids leaking the context when this object outlives the Activity of Fragment.
Expand Down
Loading