Skip to content

Commit

Permalink
简化回调写法
Browse files Browse the repository at this point in the history
  • Loading branch information
Reginer committed Dec 15, 2021
1 parent 6347212 commit 88f4745
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 43 deletions.
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,41 @@ maven { url 'https://jitpack.io' }
```

```
implementation 'com.github.Reginer:MVVMHub:2.0.6'
implementation 'com.github.Reginer:MVVMHub:2.1.1'
```

登录:

```
class LoginActivity : BaseVmActivity() {
// private val mViewModel: FragmentViewModel by activityViewModels() use in fragment
private val mViewModel by viewModels<LoginViewModel>()
private val mViewBinding by viewBinding(ActivityLoginBinding::bind)
override val layoutId: Int get() = R.layout.activity_login
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
btnLogin.setOnClickListener { mViewModel.login(etUsername.text(), etPwd.text()) }
mViewBinding.login.setOnClickListener { mViewModel.login(mViewBinding.username.text(), mViewBinding.pwd.text()) }
}
override fun createObserver() {
mViewModel.loginResult.vmObserver(this) {
onAppLoading = { showProgress() }
onAppSuccess = { mViewModel.saveUser(it);finish() }
onAppError = { Logger.e(it.errorMsg) }
onAppComplete = { dismissProgress() }
onAppLoading { showProgress() }
onAppSuccess { mViewModel.saveUser(it);finish() }
onAppError { Logger.e(it.errorMsg) }
onAppComplete { dismissProgress() }
}
//不管那一套,直接取成功就完事了
// mViewModel.loginResult.vmObserver(this) {
// onAppSuccess = { mViewModel.saveUser(it);finish() }
// onAppSuccess { mViewModel.saveUser(it);finish() }
// }
}
}
```





```
class LoginViewModel : BaseViewModel() {
private val loginRepository by lazy { LoginRepository() }
Expand All @@ -59,9 +57,8 @@ class LoginViewModel : BaseViewModel() {
}
```



## License

```text
MIT License
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ dependencies {
androidTestImplementation(MvvmExt.espresso)
implementation project(path: ':base')
implementation(MvvmExt.viewBinding)
// releaseImplementation 'com.github.Reginer:MVVMHub:2.0.6'
// releaseImplementation 'com.github.Reginer:MVVMHub:2.1.1'
}
6 changes: 4 additions & 2 deletions app/objectbox-models/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@
{
"id": "2:2458359200209838021",
"name": "username",
"type": 9
"indexId": "2:3098815379399554163",
"type": 9,
"flags": 34848
}
],
"relations": []
}
],
"lastEntityId": "2:4503351864145115806",
"lastIndexId": "1:2016177599447269887",
"lastIndexId": "2:3098815379399554163",
"lastRelationId": "0:0",
"lastSequenceId": "0:0",
"modelVersion": 5,
Expand Down
38 changes: 33 additions & 5 deletions app/src/main/java/win/regin/mvvm/data/BoxOptions.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
/*
* Copyright (c) 2021, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.mvvm.data

import io.objectbox.query.Query

/**
* @author :Reginer in 19-6-20 下午1:41.
* @author :Reginer 19-6-20 下午1:41.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:数据库操作类
*/
object BoxOptions {

Expand All @@ -19,9 +50,6 @@ object BoxOptions {
BoxManager.instance.boxStore.boxFor(UserEntity::class.java).put(userEntity)
}

fun deleteAllUser() {
BoxManager.instance.boxStore.boxFor(UserEntity::class.java).removeAll()
}

fun insertArticle(articleEntity: ArticleEntity) {
BoxManager.instance.boxStore.boxFor(ArticleEntity::class.java).put(articleEntity)
Expand Down
36 changes: 34 additions & 2 deletions app/src/main/java/win/regin/mvvm/data/Database.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
/*
* Copyright (c) 2021, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.mvvm.data


Expand All @@ -9,16 +40,17 @@ import kotlinx.parcelize.Parcelize
import win.regin.common.toJsonString

/**
* @author :Reginer in 19-6-19 下午4:42.
* @author :Reginer 19-6-19 下午4:42.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:数据库列表
*/
@Keep
@Entity
@Parcelize
data class UserEntity(
@Id
var dbId: Long = 0,
@Unique(onConflict = ConflictStrategy.REPLACE)
val username: String? = ""
) : Parcelable {
override fun toString(): String {
Expand Down
36 changes: 33 additions & 3 deletions app/src/main/java/win/regin/mvvm/repository/LoginRepository.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
/*
* Copyright (c) 2021, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.mvvm.repository

import win.regin.mvvm.api.NetworkApi
Expand All @@ -6,17 +37,16 @@ import win.regin.mvvm.data.BoxOptions
import win.regin.mvvm.data.UserEntity

/**
* @author :Reginer in 2019/6/18 21:33.
* @author :Reginer 2019/6/18 21:33.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:登录操作类
*/
class LoginRepository {
suspend fun login(username: String, password: String): AppBaseEntity<UserEntity> {
return NetworkApi.getApi().login(username, password)
}

fun insertUser(userEntity: UserEntity) {
BoxOptions.deleteAllUser()
BoxOptions.insertUser(userEntity)
}
}
45 changes: 38 additions & 7 deletions app/src/main/java/win/regin/mvvm/ui/LoginActivity.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
/*
* Copyright (c) 2021, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.mvvm.ui

import android.os.Bundle
Expand All @@ -12,9 +43,9 @@ import win.regin.mvvm.text
import win.regin.mvvm.viewmodel.LoginViewModel

/**
* @author :Reginer in 2019/6/18 21:23.
* @author :Reginer 2019/6/18 21:23.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:登录页面
*/
class LoginActivity : BaseVmActivity() {

Expand All @@ -32,14 +63,14 @@ class LoginActivity : BaseVmActivity() {
override fun createObserver() {

mViewModel.loginResult.vmObserver(this) {
onAppLoading = { showProgress() }
onAppSuccess = { mViewModel.saveUser(it);finish() }
onAppError = { Logger.e(it.errorMsg) }
onAppComplete = { dismissProgress() }
onAppLoading { showProgress() }
onAppSuccess { mViewModel.saveUser(it);finish() }
onAppError { Logger.e(it.errorMsg) }
onAppComplete { dismissProgress() }
}
//不管那一套,直接取成功就完事了
// mViewModel.loginResult.vmObserver(this) {
// onAppSuccess = { mViewModel.saveUser(it);finish() }
// onAppSuccess { mViewModel.saveUser(it);finish() }
// }
}
}
35 changes: 33 additions & 2 deletions app/src/main/java/win/regin/mvvm/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
/*
* Copyright (c) 2021, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.mvvm.ui

import android.content.Intent
Expand Down Expand Up @@ -45,7 +76,7 @@ class MainActivity : BaseVmActivity() {
it?.let { mViewModel.getArticle(0) }
}
mViewModel.articleResult.vmObserver(this) {
onAppSuccess = { mViewModel.parseArticleData(it) }
onAppSuccess { mViewModel.parseArticleData(it) }
}
mViewModel.articleLiveData.observe(this) {
mViewBinding.content.text = it.toJsonString()
Expand All @@ -59,7 +90,7 @@ class MainActivity : BaseVmActivity() {
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
startActivity(Intent(this,LoginActivity::class.java))
startActivity(Intent(this, LoginActivity::class.java))
return super.onOptionsItemSelected(item)
}
}
Loading

0 comments on commit 88f4745

Please sign in to comment.