Skip to content

Commit

Permalink
Remove date data fetched for LocalRepository. Update OnlineRepository…
Browse files Browse the repository at this point in the history
… to not update data fetched time if fetch function fails.
  • Loading branch information
levibostian committed May 14, 2018
1 parent 536407d commit 124ecea
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 129 deletions.
121 changes: 64 additions & 57 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import java.util.*
*/
class LocalDataState<DATA> private constructor(val isEmpty: Boolean = false,
val data: DATA? = null,
val dataFetched: Date? = null,
val latestError: Throwable? = null) {

companion object {
Expand All @@ -32,8 +31,8 @@ class LocalDataState<DATA> private constructor(val isEmpty: Boolean = false,
return LocalDataState(isEmpty = true)
}

fun <T> data(data: T, dataFetched: Date): LocalDataState<T> {
return LocalDataState(data = data, dataFetched = dataFetched)
fun <T> data(data: T): LocalDataState<T> {
return LocalDataState(data = data)
}
}

Expand All @@ -55,7 +54,7 @@ class LocalDataState<DATA> private constructor(val isEmpty: Boolean = false,
*/
fun deliver(listener: LocalDataStateListener<DATA>) {
if (isEmpty) listener.isEmpty()
data?.let { listener.data(it, dataFetched!!) }
data?.let { listener.data(it) }
latestError?.let { listener.error(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import java.util.*

interface LocalDataStateListener<in DATA> {
fun isEmpty()
fun data(data: DATA, fetched: Date)
fun data(data: DATA)
fun error(error: Throwable)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,6 @@ abstract class LocalRepository<DATA: Any> {
private var stateOfDate: LocalDataStateCompoundBehaviorSubject<DATA>? = null
private var compositeDisposable = CompositeDisposable()

/**
* If your repository has some special ID or something to make it unique compared to other instances of your repository, set this tag.
*
* This tag is used to keep track of the [Date] that data was last saved to determine how old your data is. If you do not set this [tag] property, all instances of your [LocalRepository] subclass will have the same [Date].
*/
open val tag: String = "(none)"

private val dataLastSavedKey by lazy {
"${ConstantsUtil.PREFIX}_${this::class.java.simpleName}_${tag}_LAST_SAVED_KEY"
}

/**
* Keeps track of how old your data is.
*/
private var timeSavedData: Date
get() {
return Date(Teller.shared.sharedPreferences.getLong(dataLastSavedKey, Date().time))
}
@SuppressLint("ApplySharedPref")
set(value) {
Teller.shared.sharedPreferences.edit().putLong(dataLastSavedKey, value.time).commit()
}

/**
* Get an observable that gets the current state of data and all future states.
*/
Expand All @@ -53,7 +30,7 @@ abstract class LocalRepository<DATA: Any> {
if (cachedData == null || isDataEmpty(cachedData)) {
stateOfDate!!.onNextEmpty()
} else {
stateOfDate!!.onNextData(cachedData, timeSavedData)
stateOfDate!!.onNextData(cachedData)
}
}, { error ->
stateOfDate!!.onNextError(error)
Expand All @@ -68,13 +45,8 @@ abstract class LocalRepository<DATA: Any> {
*
* It is up to you to call [saveData] when you have new data to save. A good place to do this is in a ViewModel.
*/
fun save(data: DATA?): Completable {
return Completable.fromAction { timeSavedData = Date() }
.andThen(saveData(data))
.subscribeOn(Schedulers.io())
}

protected abstract fun saveData(data: DATA?): Completable
abstract fun saveData(data: DATA?): Completable

/**
* This function should be setup to trigger anytime there is a data change. So if you were to call [saveData], anyone observing the [Observable] returned here will get notified of a new update.
Expand Down

0 comments on commit 124ecea

Please sign in to comment.