Skip to content

Commit

Permalink
remove deprecated stuff and add helpers for view state easier handling
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyMuse committed Dec 7, 2021
1 parent 93de2ed commit 7bc35a6
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.crazylegend.retrofit.viewstate

import com.crazylegend.retrofit.apiresult.*
import com.crazylegend.retrofit.retrofitResult.*
import com.crazylegend.retrofit.viewstate.onApiError
import com.crazylegend.retrofit.apiresult.ApiResult
import com.crazylegend.retrofit.apiresult.onApiErrorSuspend
import com.crazylegend.retrofit.apiresult.onErrorSuspend
import com.crazylegend.retrofit.apiresult.onIdleSuspend
import com.crazylegend.retrofit.apiresult.onLoadingSuspend
import com.crazylegend.retrofit.apiresult.onSuccessSuspend
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -13,7 +16,7 @@ import kotlinx.coroutines.flow.receiveAsFlow
* Created by funkymuse on 11/20/21 to long live and prosper !
*/
class ViewState<T>(
capacity: Int = Channel.UNLIMITED,
capacity: Int = Channel.BUFFERED,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
onUndeliveredElement: ((ViewEvent) -> Unit)? = null,
defaultApiState : ApiResult<T> = ApiResult.Idle
Expand All @@ -30,11 +33,11 @@ class ViewState<T>(
override suspend fun emitEvent(apiResult: ApiResult<T>) {
dataState.value = apiResult
apiResult
.onLoading { viewEvents.send(ViewEvent.Loading) }
.onError { throwable -> viewEvents.send(ViewEvent.Error(throwable)) }
.onApiError { errorBody, code -> viewEvents.send(ViewEvent.ApiError(errorBody, code)) }
.onIdle { viewEvents.send(ViewEvent.Idle) }
.onSuccess { viewEvents.send(ViewEvent.Success) }
.onLoadingSuspend { viewEvents.send(ViewEvent.Loading) }
.onErrorSuspend { throwable -> viewEvents.send(ViewEvent.Error(throwable)) }
.onApiErrorSuspend { errorBody, code -> viewEvents.send(ViewEvent.ApiError(errorBody, code)) }
.onIdleSuspend { viewEvents.send(ViewEvent.Idle) }
.onSuccessSuspend { viewEvents.send(ViewEvent.Success) }
}

override val isDataLoaded get() = payload != null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.crazylegend.retrofit.viewstate

import androidx.lifecycle.SavedStateHandle
import com.crazylegend.retrofit.apiresult.ApiResult
import com.crazylegend.retrofit.apiresult.onSuccess
import com.crazylegend.retrofit.apiresult.*
import okhttp3.ResponseBody

/**
Expand Down Expand Up @@ -54,3 +53,36 @@ fun SavedStateHandle.handleApiErrorFromSavedState(errorBody: ResponseBody?): Str
}

fun handleApiError(savedStateHandle: SavedStateHandle, errorBody: ResponseBody?): String? = savedStateHandle.handleApiErrorFromSavedState(errorBody)


val <T> ViewStateContract<T>.showEmptyDataOnErrorsOrSuccess: Boolean
get() {
val retrofitResult = data.value
return isDataNotLoaded && (retrofitResult.isError or retrofitResult.isApiError or retrofitResult.isSuccess)
}

val <T> ViewStateContract<T>.showEmptyDataOnErrors: Boolean
get() {
val retrofitResult = data.value
return isDataNotLoaded && (retrofitResult.isError or retrofitResult.isApiError)
}

val <T> ViewStateContract<T>.showEmptyDataOnApiError: Boolean
get() {
val retrofitResult = data.value
return isDataNotLoaded && (retrofitResult.isApiError)
}


val <T> ViewStateContract<T>.showEmptyDataOnError: Boolean
get() {
val retrofitResult = data.value
return isDataNotLoaded && (retrofitResult.isError)
}

val <T> ViewStateContract<T>.showEmptyDataOnSuccess: Boolean
get() {
val retrofitResult = data.value
return isDataNotLoaded && retrofitResult.isSuccess
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.crazylegend.sharedpreferences

import android.content.SharedPreferences
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner


/**
Expand All @@ -12,15 +11,15 @@ import androidx.lifecycle.OnLifecycleEvent
class SharedPreferenceChangeListener(
private val sharedPreferences: SharedPreferences,
private val listener: (SharedPreferences, String) -> Unit
) : SharedPreferences.OnSharedPreferenceChangeListener, LifecycleObserver {
) : SharedPreferences.OnSharedPreferenceChangeListener, DefaultLifecycleObserver {

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,67 @@ fun SharedPreferences.putString(key: String, value: String) {
edit { putString(key, value) }
}

fun SharedPreferences.commitString(key: String, value: String) {
edit(true) { putString(key, value) }
}

fun SharedPreferences.putInt(key: String, value: Int) {
edit { putInt(key, value) }
}

fun SharedPreferences.commitInt(key: String, value: Int) {
edit(true) { putInt(key, value) }
}

fun SharedPreferences.putBoolean(key: String, value: Boolean) {
edit { putBoolean(key, value) }
}

fun SharedPreferences.commitBoolean(key: String, value: Boolean) {
edit(true) { putBoolean(key, value) }
}

fun SharedPreferences.putFloat(key: String, value: Float) {
edit { putFloat(key, value) }
}

fun SharedPreferences.commitFloat(key: String, value: Float) {
edit(true) { putFloat(key, value) }
}

fun SharedPreferences.putLong(key: String, value: Long) {
edit { putLong(key, value) }
}

fun SharedPreferences.commitLong(key: String, value: Long) {
edit(true) { putLong(key, value) }
}

fun SharedPreferences.putStringSet(key: String, value: Set<String>) {
edit { putStringSet(key, value) }
}

fun SharedPreferences.commitStringSet(key: String, value: Set<String>) {
edit(true) { putStringSet(key, value) }
}


fun SharedPreferences.clear() {
edit { clear() }
}

fun SharedPreferences.commitClear() {
edit(true) { clear() }
}

fun SharedPreferences.remove(key: String) {
edit { remove(key) }
}

fun SharedPreferences.commitRemoval(key: String) {
edit(true) { remove(key) }
}

fun <T> SharedPreferences.put(key: String, t: T) {
when (t) {
is Int -> putInt(key, t)
Expand All @@ -52,6 +84,16 @@ fun <T> SharedPreferences.put(key: String, t: T) {
}
}

fun <T> SharedPreferences.commit(key: String, t: T) {
when (t) {
is Int -> commitInt(key, t)
is Long -> commitLong(key, t)
is Float -> commitFloat(key, t)
is String -> commitString(key, t)
is Boolean -> commitBoolean(key, t)
}
}

inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T): T =
when (defaultValue) {
is Boolean -> getBoolean(key, defaultValue) as? T ?: defaultValue
Expand Down

0 comments on commit 7bc35a6

Please sign in to comment.