-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac16a1a
commit 2b864e1
Showing
8 changed files
with
121 additions
and
96 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
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/sample/android/tmdb/domain/repository/BaseRepository.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,30 @@ | ||
package com.sample.android.tmdb.domain.repository | ||
|
||
import android.content.Context | ||
import com.sample.android.tmdb.R | ||
import com.sample.android.tmdb.util.Resource | ||
import com.sample.android.tmdb.util.isNetworkAvailable | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
|
||
abstract class BaseRepository<T>( | ||
private val context: Context, | ||
ioDispatcher: CoroutineDispatcher | ||
) { | ||
protected abstract suspend fun getSuccessResult(): T | ||
|
||
val result: Flow<Resource<T>> = flow { | ||
emit(Resource.Loading) | ||
if (context.isNetworkAvailable()) { | ||
try { | ||
emit(Resource.Success(getSuccessResult())) | ||
} catch (t: Throwable) { | ||
emit(Resource.Error(context.getString(R.string.failed_loading_msg))) | ||
} | ||
} else { | ||
emit(Resource.Error(context.getString(R.string.failed_network_msg))) | ||
} | ||
}.flowOn(ioDispatcher) | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/sample/android/tmdb/ui/BaseViewModel.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,31 @@ | ||
package com.sample.android.tmdb.ui | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.sample.android.tmdb.domain.repository.BaseRepository | ||
import com.sample.android.tmdb.util.Resource | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
open class BaseViewModel<T>( | ||
private val repository: BaseRepository<T> | ||
) : ViewModel() { | ||
|
||
private val _stateFlow = MutableStateFlow<Resource<T>>(Resource.Loading) | ||
val stateFlow: StateFlow<Resource<T>> | ||
get() = _stateFlow.asStateFlow() | ||
|
||
init { | ||
refresh() | ||
} | ||
|
||
fun refresh() { | ||
viewModelScope.launch { | ||
repository.result.collect { | ||
_stateFlow.tryEmit(it) | ||
} | ||
} | ||
} | ||
} |
31 changes: 4 additions & 27 deletions
31
app/src/main/java/com/sample/android/tmdb/ui/feed/FeedViewModel.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,33 +1,10 @@ | ||
package com.sample.android.tmdb.ui.feed | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.sample.android.tmdb.domain.model.FeedWrapper | ||
import com.sample.android.tmdb.domain.model.TmdbItem | ||
import com.sample.android.tmdb.domain.repository.BaseFeedRepository | ||
import com.sample.android.tmdb.domain.model.FeedWrapper | ||
import com.sample.android.tmdb.util.Resource | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import com.sample.android.tmdb.ui.BaseViewModel | ||
|
||
open class FeedViewModel<T : TmdbItem>( | ||
private val repository: BaseFeedRepository<T> | ||
) : ViewModel() { | ||
|
||
private val _stateFlow = MutableStateFlow<Resource<List<FeedWrapper<T>>>>(Resource.Loading) | ||
val stateFlow: StateFlow<Resource<List<FeedWrapper<T>>>> | ||
get() = _stateFlow.asStateFlow() | ||
|
||
init { | ||
refresh() | ||
} | ||
|
||
fun refresh() { | ||
viewModelScope.launch { | ||
repository.result.collect { | ||
_stateFlow.tryEmit(it) | ||
} | ||
} | ||
} | ||
} | ||
repository: BaseFeedRepository<T>, | ||
) : BaseViewModel<List<FeedWrapper<T>>>(repository) |
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