This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f6f580b
Migrate MainSrlViewModel to use flow
Goooler 32823e5
Migrate DetailViewModel to use flow
Goooler 4d3f075
Reset states
Goooler f1632d3
Merge branch 'trunk' into flow
Goooler 6fae401
Remove livedata deps
Goooler b25216c
Replace livedata in BaseThemeViewModel
Goooler 03d6947
Remove rxjava from app
Goooler 863de9e
Rename functions
Goooler becef0c
Merge branch 'trunk' into flow
Goooler ed201e5
BaseViewModel no longer to extends DefaultLifecycleObserver
Goooler 7b10c65
Merge branch 'trunk' into flow
Goooler 822ef6c
Import SizeUtils
Goooler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 1 addition & 2 deletions
3
base/src/main/kotlin/io/goooler/demoapp/base/core/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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package io.goooler.demoapp.base.core | ||
|
||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.ViewModel | ||
|
||
abstract class BaseViewModel : ViewModel(), DefaultLifecycleObserver | ||
abstract class BaseViewModel : ViewModel() |
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
20 changes: 0 additions & 20 deletions
20
common/src/main/kotlin/io/goooler/demoapp/common/base/theme/BaseRxViewModel.kt
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import androidx.core.widget.doAfterTextChanged | |
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.recyclerview.widget.AsyncDifferConfig | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.viewbinding.ViewBinding | ||
|
@@ -34,24 +35,21 @@ import com.blankj.utilcode.util.StringUtils | |
import com.blankj.utilcode.util.TimeUtils | ||
import com.google.android.material.textfield.TextInputLayout | ||
import com.scwang.smart.refresh.layout.SmartRefreshLayout | ||
import io.goooler.demoapp.base.core.BaseViewModel | ||
import io.goooler.demoapp.base.util.ToastUtil | ||
import io.goooler.demoapp.common.BuildConfig | ||
import io.goooler.demoapp.common.CommonApplication | ||
import io.goooler.demoapp.common.base.theme.BaseThemeViewModel | ||
import io.goooler.demoapp.common.base.theme.ITheme | ||
import io.goooler.demoapp.common.type.SpKeys | ||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers | ||
import io.reactivex.rxjava3.core.Observable | ||
import io.reactivex.rxjava3.core.Single | ||
import io.reactivex.rxjava3.schedulers.Schedulers | ||
import java.lang.reflect.ParameterizedType | ||
import java.math.BigDecimal | ||
import java.util.Calendar | ||
import java.util.Date | ||
import kotlin.math.absoluteValue | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.asExecutor | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
|
||
typealias DimensionUtil = SizeUtils | ||
typealias SpHelper = SPUtils | ||
|
@@ -181,23 +179,6 @@ fun Number.formatMoney(isYuan: Boolean = false, trans2W: Boolean = false, scale: | |
} | ||
} | ||
|
||
// ---------------------Rx-------------------------------// | ||
|
||
fun <T : Any> Single<T>.subscribeOnIoThread(): Single<T> = subscribeOn(Schedulers.io()) | ||
|
||
fun <T : Any> Single<T>.observeOnMainThread(): Single<T> = observeOn(AndroidSchedulers.mainThread()) | ||
|
||
fun <T : Any> Single<T>.subscribeAndObserve(): Single<T> = | ||
subscribeOnIoThread().observeOnMainThread() | ||
|
||
fun <T : Any> Observable<T>.subscribeOnIoThread(): Observable<T> = subscribeOn(Schedulers.io()) | ||
|
||
fun <T : Any> Observable<T>.observeOnMainThread(): Observable<T> = | ||
observeOn(AndroidSchedulers.mainThread()) | ||
|
||
fun <T : Any> Observable<T>.subscribeAndObserve(): Observable<T> = | ||
subscribeOnIoThread().observeOnMainThread() | ||
|
||
// ---------------------Res-------------------------------// | ||
|
||
fun @receiver:DrawableRes Int.getDrawable(): Drawable? = try { | ||
|
@@ -267,19 +248,14 @@ inline fun <reified T> DiffUtil.ItemCallback<T>.asConfig(): AsyncDifferConfig<T> | |
|
||
// ---------------------Fragment-------------------------------// | ||
|
||
@MainThread | ||
inline fun <reified V, reified VM : BaseViewModel> V.baseViewModels(): Lazy<VM> | ||
where V : LifecycleOwner, V : ViewModelStoreOwner = lazy(LazyThreadSafetyMode.NONE) { | ||
ViewModelProvider(this)[VM::class.java].apply(lifecycle::addObserver) | ||
} | ||
|
||
Comment on lines
-178
to
-183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow up #75 (comment). |
||
@MainThread | ||
inline fun <reified V, reified VM : BaseThemeViewModel> V.themeViewModels(): Lazy<VM> | ||
where V : LifecycleOwner, V : ViewModelStoreOwner, V : ITheme = lazy(LazyThreadSafetyMode.NONE) { | ||
ViewModelProvider(this)[VM::class.java].also { | ||
lifecycle.addObserver(it) | ||
it.loading.observe(this) { show -> | ||
if (show) showLoading() else hideLoading() | ||
lifecycleScope.launch { | ||
it.loading.collectLatest { show -> | ||
if (show) showLoading() else hideLoading() | ||
} | ||
} | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
package io.goooler.demoapp.detail.vm | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.viewModelScope | ||
import io.goooler.demoapp.base.core.BaseViewModel | ||
import io.goooler.demoapp.base.util.MutableBooleanLiveData | ||
import io.goooler.demoapp.common.network.RetrofitHelper | ||
import io.goooler.demoapp.detail.model.RepoDetailModel | ||
import io.goooler.demoapp.detail.repository.DetailRepository | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class DetailViewModel : BaseViewModel() { | ||
|
||
private val repository = DetailRepository(RetrofitHelper.create()) | ||
private var repoDetail = RepoDetailModel() | ||
private val _repoDetailModel = MutableLiveData(repoDetail) | ||
|
||
private val _repoDetailModel = MutableStateFlow(repoDetail) | ||
private val _isRefreshing = MutableStateFlow(false) | ||
|
||
lateinit var fullName: String | ||
val repoDetailModel: LiveData<RepoDetailModel> = _repoDetailModel | ||
val isRefreshing: MutableBooleanLiveData = MutableBooleanLiveData() | ||
|
||
val repoDetailModel: StateFlow<RepoDetailModel> | ||
get() = _repoDetailModel.asStateFlow() | ||
val isRefreshing: StateFlow<Boolean> | ||
get() = _isRefreshing.asStateFlow() | ||
Comment on lines
+22
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow up #57 (comment). |
||
|
||
fun refresh() { | ||
isRefreshing.value = true | ||
viewModelScope.launch { | ||
_isRefreshing.emit(true) | ||
repository.getRepoDetail(fullName).let { | ||
repoDetail = RepoDetailModel( | ||
it.fullName, | ||
|
@@ -34,7 +39,7 @@ class DetailViewModel : BaseViewModel() { | |
) | ||
} | ||
_repoDetailModel.value = repoDetail | ||
isRefreshing.value = false | ||
_isRefreshing.emit(false) | ||
} | ||
} | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.