-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stream Episode 17: Initial SharedPreferences storage implementation (#81
) * Scaffold sharedpreferences module - Attach to store implemetnation - Mock data currently being returned Some housekeeping. * Small configurations for CI Still not working, wanted to make mock work with debug but variants still compain. * Test removing ViewModelModule * Ci why you no work? * Fix lints on CI * Working stuff for CI * housekeeping mobile-ui/build.gradle Had to make some changtes while debuging CI fails. This change still makes sense IMO. #69 * Rename data package to include project name #69 * Remove unecessary .gitignore from other packages Maintenece commit, not really related to this branch feature. * Manifest cleanup and format * Scaffold stuff for stream episode 17 Some basic ideas just so I know what I'm supposed to continue, a lot of TODOs so lint warns me. For #17 Ref #69 * Housekeeping from previous commit * Fix typo in readme Changed the copy on the stream log section * Include kotlinx.serialization experimental library for sharedPrefs This will be included in the stblib from Kotlin 1.3. #17, #69 * Make SharedPrefTransport model @kotlinx.serialization.Serializable Also, fixes and issue where the String was default to which made the serialization fail: - Kotlin/kotlinx.serialization#115 #17, #69 * SharedPrefMapper tests - Created necessary factory/mock data classes for the SharedPref module Named the mock classes with the SharedPref prefix for #66 #17 #69 * Initial tests for FrameworkLocalStorageImpl Lacking SharedPreferences test setup - Will probably need to move all the Android test libs to androidx variant. #17 #69 * Include stream episode PR to README
- Loading branch information
1 parent
dfb2163
commit 2d10258
Showing
21 changed files
with
530 additions
and
63 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
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
74 changes: 67 additions & 7 deletions
74
.../src/main/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageImpl.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 |
---|---|---|
@@ -1,31 +1,91 @@ | ||
package com.joaquimley.transporteta.sharedpreferences | ||
|
||
import android.content.SharedPreferences | ||
import com.joaquimley.transporteta.data.model.TransportEntity | ||
import com.joaquimley.transporteta.data.source.FrameworkLocalStorage | ||
import com.joaquimley.transporteta.sharedpreferences.mapper.SharedPrefTransportMapper | ||
import com.joaquimley.transporteta.sharedpreferences.model.SharedPrefTransport | ||
import io.reactivex.Completable | ||
import io.reactivex.Single | ||
import io.reactivex.subjects.PublishSubject | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class FrameworkLocalStorageImpl @Inject constructor(private val sharedPreferences: SharedPreferences, | ||
private val mapper: SharedPrefTransportMapper) : FrameworkLocalStorage { | ||
|
||
private val sharedPreferencesObservable: PublishSubject<List<TransportEntity>> = PublishSubject.create() | ||
|
||
init { | ||
loadAll() | ||
observeSharedPreferencesChanges() | ||
} | ||
|
||
class FrameworkLocalStorageImpl: FrameworkLocalStorage { | ||
override fun saveTransport(transportEntity: TransportEntity): Completable { | ||
return Completable.complete() | ||
return Completable.fromAction { | ||
saveToSharedPrefs(mapper.toSharedPref(transportEntity)) | ||
} | ||
} | ||
|
||
override fun deleteTransport(transportEntityId: String): Completable { | ||
return Completable.complete() | ||
} | ||
|
||
override fun getTransport(transportEntityId: String): Single<TransportEntity> { | ||
return Single.just(TransportEntity("hi", "mock",2, "el", true,"bus")) | ||
return Single.just(TransportEntity("hi", "mock", 2, "el", true, "bus")) | ||
} | ||
|
||
override fun getAll(): Single<List<TransportEntity>> { | ||
val list = mutableListOf<TransportEntity>() | ||
list.add(TransportEntity("hi", "mock",2, "latestEta 12324", true,"bus")) | ||
list.add(TransportEntity("there", "mock",23, "latestEta 123", true,"bus")) | ||
list.add(TransportEntity("hi", "mock", 2, "latestEta 12324", true, "bus")) | ||
list.add(TransportEntity("there", "mock", 23, "latestEta 123", true, "bus")) | ||
list.add(TransportEntity("world", "mock", 25, "latestEta 12454", true, "bus")) | ||
list.add(TransportEntity("sup", "mock", 29, "latestEta 675", true, "bus")) | ||
return Single.just(list) | ||
} | ||
|
||
override fun clearAll(): Completable { | ||
return Completable.complete() | ||
return Completable.fromAction { | ||
|
||
} | ||
} | ||
|
||
private fun observeSharedPreferencesChanges() { | ||
sharedPreferences.registerOnSharedPreferenceChangeListener { _, key -> | ||
if (key != SHARED_PREFERENCES_LAST_UPDATED) { | ||
loadAll() | ||
} | ||
} | ||
} | ||
|
||
private fun loadAll() { | ||
val data = mutableListOf<TransportEntity>() | ||
getFromSharedPrefs(Slot.ONE)?.let { data.add(mapper.toEntity(it)) } | ||
getFromSharedPrefs(Slot.TWO)?.let { data.add(mapper.toEntity(it)) } | ||
getFromSharedPrefs(Slot.THREE)?.let { data.add(mapper.toEntity(it)) } | ||
sharedPreferencesObservable.onNext(data) | ||
} | ||
|
||
private fun saveToSharedPrefs(sharedPrefTransport: SharedPrefTransport) { | ||
sharedPreferences.edit() | ||
.putString(Slot.ONE.name, mapper.toCacheString(sharedPrefTransport)) | ||
.apply() | ||
} | ||
|
||
private fun getFromSharedPrefs(slot: Slot): SharedPrefTransport? { | ||
sharedPreferences.getString(slot.name, null)?.let { | ||
return mapper.fromCacheString(it) | ||
} ?: return null | ||
} | ||
|
||
companion object { | ||
private const val SHARED_PREFERENCES_LAST_UPDATED = "sharedpreferences.last_updated" | ||
} | ||
|
||
enum class Slot(name: String) { | ||
ONE("transport_eta_fav_1"), | ||
TWO("transport_eta_fav_2"), | ||
THREE("transport_eta_fav_3"), | ||
} | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
...es/src/main/java/com/joaquimley/transporteta/sharedpreferences/mapper/SharedPrefMapper.kt
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
...in/java/com/joaquimley/transporteta/sharedpreferences/mapper/SharedPrefTransportMapper.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 com.joaquimley.transporteta.sharedpreferences.mapper | ||
|
||
import com.joaquimley.transporteta.data.model.TransportEntity | ||
import com.joaquimley.transporteta.sharedpreferences.model.SharedPrefTransport | ||
import kotlinx.serialization.json.JSON | ||
|
||
class SharedPrefTransportMapper { | ||
|
||
fun toCacheString(from: SharedPrefTransport): String { | ||
return JSON.stringify(from) | ||
} | ||
|
||
fun fromCacheString(from: String): SharedPrefTransport { | ||
return JSON.parse(from) | ||
} | ||
|
||
fun toSharedPref(from: List<TransportEntity>): List<SharedPrefTransport> { | ||
return from.map { toSharedPref(it) } | ||
} | ||
|
||
fun toSharedPref(from: TransportEntity): SharedPrefTransport { | ||
return SharedPrefTransport(from.id, from.name, from.code, from.latestEta, from.isFavorite, from.type, 1312, "") | ||
} | ||
|
||
fun toEntity(from: List<SharedPrefTransport>): List<TransportEntity> { | ||
return from.map { toEntity(it) } | ||
} | ||
|
||
fun toEntity(from: SharedPrefTransport): TransportEntity { | ||
return TransportEntity(from.id, from.name, from.code, from.latestEta, from.isFavorite, from.type) | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
.../src/main/java/com/joaquimley/transporteta/sharedpreferences/model/SharedPrefTransport.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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.joaquimley.transporteta.sharedpreferences.model | ||
|
||
class SharedPrefTransport { | ||
|
||
// TODO | ||
} | ||
@kotlinx.serialization.Serializable | ||
data class SharedPrefTransport(val id: String, val name: String, val code: Int, val latestEta: String, | ||
val isFavorite: Boolean = false, val type: String, val lastUpdated: Long, | ||
val slot: String) |
17 changes: 0 additions & 17 deletions
17
...ferences/src/test/java/com/joaquimley/transporteta/sharedpreferences/ExampleUnitTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.