-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,038 additions
and
360 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package fr.geonature.commons.input | ||
|
||
import android.app.Application | ||
import android.preference.PreferenceManager | ||
import androidx.preference.PreferenceManager | ||
import fr.geonature.commons.input.io.InputJsonReader | ||
import fr.geonature.commons.input.io.InputJsonWriter | ||
import fr.geonature.commons.util.FileUtils | ||
import fr.geonature.commons.util.StringUtils | ||
import fr.geonature.commons.util.StringUtils.isEmpty | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Dispatchers.IO | ||
import kotlinx.coroutines.coroutineScope | ||
|
@@ -24,13 +24,24 @@ import java.io.Writer | |
* | ||
* @author [S. Grimault](mailto:[email protected]) | ||
*/ | ||
class InputManager(private val application: Application, | ||
inputJsonReaderListener: InputJsonReader.OnInputJsonReaderListener, | ||
inputJsonWriterListener: InputJsonWriter.OnInputJsonWriterListener) { | ||
class InputManager<T : AbstractInput>(private val application: Application, | ||
inputJsonReaderListener: InputJsonReader.OnInputJsonReaderListener<T>, | ||
inputJsonWriterListener: InputJsonWriter.OnInputJsonWriterListener<T>) { | ||
|
||
private val preferenceManager = PreferenceManager.getDefaultSharedPreferences(application) | ||
private val inputJsonReader: InputJsonReader = InputJsonReader(inputJsonReaderListener) | ||
private val inputJsonWriter: InputJsonWriter = InputJsonWriter(inputJsonWriterListener) | ||
private val inputJsonReader: InputJsonReader<T> = InputJsonReader(inputJsonReaderListener) | ||
private val inputJsonWriter: InputJsonWriter<T> = InputJsonWriter(inputJsonWriterListener) | ||
|
||
/** | ||
* Reads all [AbstractInput]s. | ||
* | ||
* @return A list of [AbstractInput]s | ||
*/ | ||
suspend fun readInputs(): List<T> = withContext(IO) { | ||
preferenceManager.all.filterKeys { it.startsWith("${KEY_PREFERENCE_INPUT}_") } | ||
.values.mapNotNull { if (it is String && !isEmpty(it)) inputJsonReader.read(it) else null } | ||
.sortedBy { it.id } | ||
} | ||
|
||
/** | ||
* Reads [AbstractInput] from given ID. | ||
|
@@ -39,14 +50,14 @@ class InputManager(private val application: Application, | |
* | ||
* @return [AbstractInput] or `null` if not found | ||
*/ | ||
suspend fun readInput(id: Long? = null): AbstractInput? = withContext(IO) { | ||
suspend fun readInput(id: Long? = null): T? = withContext(IO) { | ||
val inputPreferenceKey = | ||
buildInputPreferenceKey(id ?: preferenceManager.getLong(KEY_PREFERENCE_CURRENT_INPUT, | ||
0)) | ||
val inputAsJson = preferenceManager.getString(inputPreferenceKey, | ||
null) | ||
|
||
if (StringUtils.isEmpty(inputAsJson)) { | ||
if (isEmpty(inputAsJson)) { | ||
return@withContext null | ||
} | ||
|
||
|
@@ -58,7 +69,7 @@ class InputManager(private val application: Application, | |
* | ||
* @return [AbstractInput] or `null` if not found | ||
*/ | ||
suspend fun readCurrentInput(): AbstractInput? { | ||
suspend fun readCurrentInput(): T? { | ||
return readInput() | ||
} | ||
|
||
|
@@ -67,10 +78,10 @@ class InputManager(private val application: Application, | |
* | ||
* @return `true` if the given [AbstractInput] has been successfully saved, `false` otherwise | ||
*/ | ||
suspend fun saveInput(input: AbstractInput): Boolean = withContext(IO) { | ||
suspend fun saveInput(input: T): Boolean = withContext(IO) { | ||
val inputAsJson = inputJsonWriter.write(input) | ||
|
||
if (StringUtils.isEmpty(inputAsJson)) return@withContext false | ||
if (isEmpty(inputAsJson)) return@withContext false | ||
|
||
preferenceManager.edit() | ||
.putString(buildInputPreferenceKey(input.id), | ||
|
@@ -111,7 +122,8 @@ class InputManager(private val application: Application, | |
* @return `true` if the given [AbstractInput] has been successfully exported, `false` otherwise | ||
*/ | ||
suspend fun exportInput(id: Long): Boolean = coroutineScope { | ||
val inputToExport = withContext(Dispatchers.Default) { readInput(id) } ?: return@coroutineScope false | ||
val inputToExport = | ||
withContext(Dispatchers.Default) { readInput(id) } ?: return@coroutineScope false | ||
|
||
val exported = withContext(IO) { | ||
inputJsonWriter.write(getInputExportWriter(inputToExport), | ||
|
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ import java.io.StringReader | |
* | ||
* @see InputJsonWriter | ||
*/ | ||
class InputJsonReader(private val onInputJsonReaderListener: OnInputJsonReaderListener) { | ||
class InputJsonReader<T : AbstractInput>(private val onInputJsonReaderListener: OnInputJsonReaderListener<T>) { | ||
|
||
/** | ||
* parse a `JSON` string to convert as [AbstractInput]. | ||
|
@@ -24,7 +24,7 @@ class InputJsonReader(private val onInputJsonReaderListener: OnInputJsonReaderLi | |
* @return a [AbstractInput] instance from the `JSON` string or `null` if something goes wrong | ||
* @see .read | ||
*/ | ||
fun read(json: String?): AbstractInput? { | ||
fun read(json: String?): T? { | ||
if (StringUtils.isEmpty(json)) { | ||
return null | ||
} | ||
|
@@ -48,7 +48,7 @@ class InputJsonReader(private val onInputJsonReaderListener: OnInputJsonReaderLi | |
* @throws IOException if something goes wrong | ||
*/ | ||
@Throws(IOException::class) | ||
fun read(reader: Reader): AbstractInput { | ||
fun read(reader: Reader): T { | ||
val jsonReader = JsonReader(reader) | ||
val input = readInput(jsonReader) | ||
jsonReader.close() | ||
|
@@ -57,7 +57,7 @@ class InputJsonReader(private val onInputJsonReaderListener: OnInputJsonReaderLi | |
} | ||
|
||
@Throws(IOException::class) | ||
private fun readInput(reader: JsonReader): AbstractInput { | ||
private fun readInput(reader: JsonReader): T { | ||
val input = onInputJsonReaderListener.createInput() | ||
|
||
reader.beginObject() | ||
|
@@ -77,20 +77,19 @@ class InputJsonReader(private val onInputJsonReaderListener: OnInputJsonReaderLi | |
return input | ||
} | ||
|
||
|
||
/** | ||
* Callback used by [InputJsonReader]. | ||
* | ||
* @author [S. Grimault](mailto:[email protected]) | ||
*/ | ||
interface OnInputJsonReaderListener { | ||
interface OnInputJsonReaderListener<T : AbstractInput> { | ||
|
||
/** | ||
* Returns a new instance of [AbstractInput]. | ||
* | ||
* @return new instance of [AbstractInput] | ||
*/ | ||
fun createInput(): AbstractInput | ||
fun createInput(): T | ||
|
||
/** | ||
* Reading some additional data to set to the given [AbstractInput]. | ||
|
@@ -103,7 +102,7 @@ class InputJsonReader(private val onInputJsonReaderListener: OnInputJsonReaderLi | |
@Throws(IOException::class) | ||
fun readAdditionalInputData(reader: JsonReader, | ||
keyName: String, | ||
input: AbstractInput) | ||
input: T) | ||
} | ||
|
||
companion object { | ||
|
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 |
---|---|---|
|
@@ -13,7 +13,7 @@ import java.io.Writer | |
* @author [S. Grimault](mailto:[email protected]) | ||
* @see InputJsonReader | ||
*/ | ||
class InputJsonWriter(private val onInputJsonWriterListener: OnInputJsonWriterListener) { | ||
class InputJsonWriter<T : AbstractInput>(private val onInputJsonWriterListener: OnInputJsonWriterListener<T>) { | ||
|
||
private var indent: String = "" | ||
|
||
|
@@ -26,7 +26,7 @@ class InputJsonWriter(private val onInputJsonWriterListener: OnInputJsonWriterLi | |
* | ||
* @return InputJsonWriter fluent interface | ||
*/ | ||
fun setIndent(indent: String): InputJsonWriter { | ||
fun setIndent(indent: String): InputJsonWriter<T> { | ||
this.indent = indent | ||
|
||
return this | ||
|
@@ -39,7 +39,7 @@ class InputJsonWriter(private val onInputJsonWriterListener: OnInputJsonWriterLi | |
* @return a `JSON` string representation of the given [AbstractInput] or `null` if something goes wrong | ||
* @see .write | ||
*/ | ||
fun write(input: AbstractInput?): String? { | ||
fun write(input: T?): String? { | ||
if (input == null) { | ||
return null | ||
} | ||
|
@@ -69,7 +69,7 @@ class InputJsonWriter(private val onInputJsonWriterListener: OnInputJsonWriterLi | |
*/ | ||
@Throws(IOException::class) | ||
fun write(out: Writer, | ||
input: AbstractInput) { | ||
input: T) { | ||
val writer = JsonWriter(out) | ||
writer.setIndent(this.indent) | ||
writeInput(writer, | ||
|
@@ -80,7 +80,7 @@ class InputJsonWriter(private val onInputJsonWriterListener: OnInputJsonWriterLi | |
|
||
@Throws(IOException::class) | ||
private fun writeInput(writer: JsonWriter, | ||
input: AbstractInput) { | ||
input: T) { | ||
writer.beginObject() | ||
|
||
writer.name("id") | ||
|
@@ -99,7 +99,7 @@ class InputJsonWriter(private val onInputJsonWriterListener: OnInputJsonWriterLi | |
* | ||
* @author [S. Grimault](mailto:[email protected]) | ||
*/ | ||
interface OnInputJsonWriterListener { | ||
interface OnInputJsonWriterListener<T : AbstractInput> { | ||
|
||
/** | ||
* Adding some additional data to write from the current [AbstractInput]. | ||
|
@@ -110,7 +110,7 @@ class InputJsonWriter(private val onInputJsonWriterListener: OnInputJsonWriterLi | |
*/ | ||
@Throws(IOException::class) | ||
fun writeAdditionalInputData(writer: JsonWriter, | ||
input: AbstractInput) | ||
input: T) | ||
} | ||
|
||
companion object { | ||
|
80 changes: 80 additions & 0 deletions
80
commons/src/main/java/fr/geonature/commons/settings/AppSettingsManager.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,80 @@ | ||
package fr.geonature.commons.settings | ||
|
||
import android.app.Application | ||
import android.util.Log | ||
import fr.geonature.commons.model.MountPoint.StorageType.INTERNAL | ||
import fr.geonature.commons.settings.io.AppSettingsJsonReader | ||
import fr.geonature.commons.util.FileUtils.getFile | ||
import fr.geonature.commons.util.FileUtils.getRootFolder | ||
import kotlinx.coroutines.Dispatchers.IO | ||
import kotlinx.coroutines.Dispatchers.Main | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import java.io.File | ||
import java.io.FileReader | ||
import java.io.IOException | ||
|
||
/** | ||
* Manage [IAppSettings]. | ||
* - Read [IAppSettings] from `JSON` file | ||
* | ||
* @author [S. Grimault](mailto:[email protected]) | ||
*/ | ||
class AppSettingsManager<T : IAppSettings>(private val application: Application, | ||
onAppSettingsJsonJsonReaderListener: AppSettingsJsonReader.OnAppSettingsJsonReaderListener<T>) { | ||
|
||
private val appSettingsJsonReader: AppSettingsJsonReader<T> = | ||
AppSettingsJsonReader(onAppSettingsJsonJsonReaderListener) | ||
|
||
init { | ||
GlobalScope.launch(Main) { | ||
withContext(IO) { | ||
getRootFolder(application, | ||
INTERNAL).mkdirs() | ||
} | ||
} | ||
} | ||
|
||
fun getAppSettingsFilename(): String { | ||
val packageName = application.packageName | ||
|
||
return "settings_${packageName.substring(packageName.lastIndexOf('.') + 1)}.json" | ||
} | ||
|
||
/** | ||
* Loads [IAppSettings] from `JSON` file. | ||
* | ||
* @return [IAppSettings] or `null` if not found | ||
*/ | ||
suspend fun loadAppSettings(): T? = withContext(IO) { | ||
val settingsJsonFile = getAppSettingsAsFile() | ||
|
||
if (!settingsJsonFile.exists()) { | ||
Log.w(TAG, | ||
"'${settingsJsonFile.absolutePath}' not found") | ||
null | ||
} | ||
else { | ||
try { | ||
appSettingsJsonReader.read(FileReader(settingsJsonFile)) | ||
} | ||
catch (e: IOException) { | ||
Log.w(TAG, | ||
"Failed to load '${settingsJsonFile.name}'") | ||
|
||
null | ||
} | ||
} | ||
} | ||
|
||
internal fun getAppSettingsAsFile(): File { | ||
return getFile(getRootFolder(application, | ||
INTERNAL), | ||
getAppSettingsFilename()) | ||
} | ||
|
||
companion object { | ||
private val TAG = AppSettingsManager::class.java.name | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
commons/src/main/java/fr/geonature/commons/settings/IAppSettings.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,10 @@ | ||
package fr.geonature.commons.settings | ||
|
||
import android.os.Parcelable | ||
|
||
/** | ||
* Global internal settings. | ||
* | ||
* @author [S. Grimault](mailto:[email protected]) | ||
*/ | ||
interface IAppSettings : Parcelable |
Oops, something went wrong.