This file contains hidden or 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
| sealed class Resource<out T> { | |
| data class Success<out T>(val data: T) : Resource<T>() | |
| data class Loading<out T>(val partialData: T? = null) : Resource<T>() | |
| data class Failure<out T>(val throwable: Throwable? = null) : Resource<T>() | |
| val extractData: T? get() = when (this) { | |
| is Success -> data | |
| is Loading -> partialData | |
| is Failure -> null |
This file contains hidden or 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
| inline fun <reified T> DataSnapshot.getTypedValue(): T { | |
| val genericTypeIndicator = object : GenericTypeIndicator<T>() {} | |
| return getValue(genericTypeIndicator)!! | |
| } |
This file contains hidden or 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
| class UsersViewModel : ViewModel() { | |
| private val usersSnapLiveData: LiveData<Resource<DataSnapshot>> by lazy { | |
| UsersSnapLiveData() | |
| } | |
| val usersLiveData: LiveData<Resource<HashMap<String, User>>> by lazy { | |
| usersSnapLiveData.mapLiveDataResource< | |
| DataSnapshot, // from | |
| HashMap<String, User> // to |
This file contains hidden or 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
| inline fun <reified T> LiveData<Resource<DataSnapshot>>.mapGetValue(): LiveData<Resource<T>> = map { | |
| it.getTypedValueResource<T>() | |
| } | |
| inline fun <reified T> Resource<DataSnapshot>.getTypedValueResource(): Resource<T> = mapResource { | |
| it.getTypedValue<T>() | |
| } |
This file contains hidden or 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
| class UsersViewModel : ViewModel() { | |
| private val usersSnapLiveData: LiveData<Resource<DataSnapshot>> by lazy { | |
| UsersSnapLiveData() | |
| } | |
| val usersLiveData: LiveData<Resource<HashMap<String, User>>> by lazy { | |
| usersSnapLiveData.mapGetValue<HashMap<String, User>>() | |
| } | |
This file contains hidden or 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
| class UserFragment : Fragment() { | |
| private var usersViewModel: UserViewModel? = null | |
| override fun onCreateView( | |
| inflater: LayoutInflater, | |
| container: ViewGroup?, | |
| savedInstanceState: Bundle? | |
| ): View? = inflater.inflate(R.layout.fragment_users, container, false) |
This file contains hidden or 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
| fun <T> Resource<T>.onSuccess( | |
| onSuccess: (data: T) -> Unit | |
| ): Resource<T> { | |
| if (this is Resource.Success<T>) | |
| onSuccess(data) | |
| return this | |
| } | |
| fun <T> Resource<T>.onLoading( |
This file contains hidden or 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
| inline fun <T> Resource<List<T>>.filterResource(predicate: (T) -> Boolean): Resource<List<T>> = try { | |
| when (this) { | |
| is Resource.Success<List<T>> -> Resource.Success<List<T>>(data.filter(predicate)) | |
| is Resource.Loading<List<T>> -> Resource.Loading<List<T>>(partialData?.filter(predicate)) | |
| is Resource.Failure<List<T>> -> Resource.Failure<List<T>>(throwable) | |
| } | |
| } catch (e: Throwable) { | |
| Resource.Failure<List<T>>(e) | |
| } |
This file contains hidden or 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
| sealed class Resource<out T> { | |
| data class Success<out T>(val data: T) : Resource<T>() | |
| data class Loading<out T>(val partialData: T? = null) : Resource<T>() | |
| data class Failure<out T>(val throwable: Throwable? = null) : Resource<T>() | |
| val extractData: T? get() = when (this) { | |
| is Success -> data | |
| is Loading -> partialData | |
| is Failure -> null |
This file contains hidden or 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
| open class FirebaseResourceLiveData(ref: DatabaseReference) : LiveData<Resource<DataSnapshot>> { | |
| init { | |
| value = Resource.Loading() // setValue() of LiveData | |
| } | |
| private val listener = MyValueEventListener() | |
| override fun onActive() { // we have observers! | |
| value = Resource.Loading(value?.extractData) | |
| query.addValueEventListener(listener) |