-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2、移除冗余方法
- Loading branch information
Showing
3 changed files
with
118 additions
and
24 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
96 changes: 96 additions & 0 deletions
96
reactivehttp/src/main/java/github/leavesc/reactivehttp/base/ReactiveFragment.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,96 @@ | ||
package github.leavesc.reactivehttp.base | ||
|
||
import android.app.ProgressDialog | ||
import android.widget.Toast | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.lifecycleScope | ||
import github.leavesc.reactivehttp.viewmodel.IUIAction | ||
import github.leavesc.reactivehttp.viewmodel.IUIActionEventObserver | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
/** | ||
* @Author: leavesC | ||
* @Date: 2021/12/1 23:16 | ||
* @Desc: | ||
* @Github:https://github.com/leavesC | ||
*/ | ||
class ReactiveFragment : Fragment(), IUIActionEventObserver { | ||
|
||
protected inline fun <reified VM> getViewModel( | ||
lifecycleOwner: LifecycleOwner = viewLifecycleOwner, | ||
factory: ViewModelProvider.Factory? = null, | ||
noinline initializer: (VM.(lifecycleOwner: LifecycleOwner) -> Unit)? = null | ||
): Lazy<VM> where VM : ViewModel, VM : IUIAction { | ||
return lazy { | ||
getViewModelFast( | ||
viewModelStoreOwner = this, | ||
lifecycleOwner = lifecycleOwner, | ||
viewModelClass = VM::class.java, | ||
factory = factory, | ||
initializer = initializer | ||
) | ||
} | ||
} | ||
|
||
protected inline fun <reified VM> getViewModelInstance( | ||
lifecycleOwner: LifecycleOwner = viewLifecycleOwner, | ||
crossinline create: () -> VM, | ||
noinline initializer: (VM.(lifecycleOwner: LifecycleOwner) -> Unit)? = null | ||
): Lazy<VM> where VM : ViewModel, VM : IUIAction { | ||
return lazy { | ||
getViewModelFast( | ||
viewModelStoreOwner = this, | ||
lifecycleOwner = lifecycleOwner, | ||
viewModelClass = VM::class.java, | ||
factory = object : ViewModelProvider.NewInstanceFactory() { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return create() as T | ||
} | ||
}, | ||
initializer = initializer | ||
) | ||
} | ||
} | ||
|
||
override val lifecycleSupportedScope: CoroutineScope | ||
get() = viewLifecycleOwner.lifecycleScope | ||
|
||
private var loadDialog: ProgressDialog? = null | ||
|
||
override fun showLoading() { | ||
dismissLoading() | ||
activity?.let { act -> | ||
loadDialog = ProgressDialog(act).apply { | ||
setCancelable(true) | ||
setCanceledOnTouchOutside(false) | ||
show() | ||
} | ||
} | ||
} | ||
|
||
override fun dismissLoading() { | ||
loadDialog?.takeIf { it.isShowing }?.dismiss() | ||
loadDialog = null | ||
} | ||
|
||
override fun showToast(msg: String) { | ||
activity?.let { act -> | ||
if (msg.isNotBlank()) { | ||
Toast.makeText(act, msg, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
|
||
override fun finishView() { | ||
activity?.finish() | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
dismissLoading() | ||
} | ||
|
||
} |
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