Skip to content

Instantly share code, notes, and snippets.

@AlexGladkov
Last active December 15, 2020 08:44
Show Gist options
  • Save AlexGladkov/b818ab7047357b1f951b4aea410ffaf4 to your computer and use it in GitHub Desktop.
Save AlexGladkov/b818ab7047357b1f951b4aea410ffaf4 to your computer and use it in GitHub Desktop.
/// ConfigurationModel
data class ConfigurationModel(
val currentQuestId: Int,
val currentQuestPage: Int,
val boughtQuestIds: List<Int>
)
/// Configuration Local Data Source
interface ConfigurationLocalDataSource {
fun storeConfiguration(configurationModel: ConfigurationModel)
fun fetchConfiguration(): ConfigurationModel
companion object {
const val questIdKey = "QUEST_ID_KEY"
const val questPageKey = "QUEST_PAGE_KEY"
const val boughtQuestIdsKey = "BOUGHT_QUEST_ID_KEY"
}
}
/// Configuration Remote Data Source
interface ConfigurationRemoteDataSource {
suspend fun fetchUserConfiguration(): ConfigurationModel?
suspend fun updateConfiguration(model: ConfigurationModel)
}
/// Configuration Repository
class ConfigurationRepository(
private val localDataSource: ConfigurationLocalDataSource,
private val remoteDataSource: ConfigurationRemoteDataSource = MockConfigurationDataSource()
) {
suspend fun fetchConfiguration(): ConfigurationModel {
val remoteConfiguration = remoteDataSource.fetchUserConfiguration()
return if (remoteConfiguration == null) {
localDataSource.fetchConfiguration()
} else {
localDataSource.storeConfiguration(remoteConfiguration)
remoteConfiguration
}
}
suspend fun updateConfiguration(model: ConfigurationModel) {
remoteDataSource.updateConfiguration(model)
localDataSource.storeConfiguration(model)
}
}
/// Configuration Mock
class MockConfigurationDataSource: ConfigurationRemoteDataSource {
override suspend fun fetchUserConfiguration(): ConfigurationModel? {
runBlocking {
delay(1000)
}
return null
}
override suspend fun updateConfiguration(model: ConfigurationModel) {
runBlocking {
delay(1000)
}
}
}
/// Coroutines library
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2-native-mt")
/// Android main
class CommonConfigurationDataSource(private val context: Context): ConfigurationLocalDataSource {
private val sharedPreferences: SharedPreferences = context.getSharedPreferences("APP_KEY", 0)
override fun storeConfiguration(configurationModel: ConfigurationModel) {
sharedPreferences.edit()
.putInt(questIdKey, configurationModel.currentQuestId)
.putInt(questPageKey, configurationModel.currentQuestPage)
.putStringSet(boughtQuestIdsKey, configurationModel.boughtQuestIds.map { it.toString() }.toSet())
.apply()
}
override fun fetchConfiguration(): ConfigurationModel {
return ConfigurationModel(
currentQuestId = sharedPreferences.getInt(questIdKey, -1),
currentQuestPage = sharedPreferences.getInt(questPageKey, -1),
boughtQuestIds = sharedPreferences.getStringSet(boughtQuestIdsKey, emptySet())
?.map { it.toInt() } ?: emptyList()
)
}
}
/// iOS Main
class CommonConfigurationDataSource: ConfigurationLocalDataSource {
private val userDefaults = NSUserDefaults.standardUserDefaults()
override fun storeConfiguration(configurationModel: ConfigurationModel) {
userDefaults.setInteger(configurationModel.currentQuestId.toLong(), questIdKey)
userDefaults.setInteger(configurationModel.currentQuestPage.toLong(), questPageKey)
userDefaults.setObject(configurationModel.boughtQuestIds
.map { it.toString() }.joinToString { " " }, boughtQuestIdsKey)
}
override fun fetchConfiguration(): ConfigurationModel {
val questId = userDefaults.valueForKey(questIdKey) as? NSInteger
val questPage = userDefaults.valueForKey(questPageKey) as? NSInteger
val boughtQuests = (userDefaults.valueForKey(boughtQuestIdsKey) as? NSString ?: "").toString()
return ConfigurationModel(
currentQuestId = questId?.toInt() ?: -1,
currentQuestPage = questPage?.toInt() ?: -1,
boughtQuestIds = if (boughtQuests.isEmpty()) {
emptyList()
} else {
boughtQuests.split(" ").map { it.toInt() }
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment