继承登录模块到lib 移动动画文本组件到lib ScrollList.kt->ScrollViewModel.kt Api移动到libmaster
parent
e7f07a3415
commit
f15fd6588e
@ -0,0 +1,14 @@ |
|||||||
|
package com.gyf.csams |
||||||
|
|
||||||
|
import android.app.Activity |
||||||
|
import com.gyf.csams.account.ui.LoginActivity |
||||||
|
import com.gyf.csams.main.ui.MainActivity |
||||||
|
import com.gyf.lib.uikit.AbstractInitActivity |
||||||
|
import com.gyf.lib.util.AccountApi |
||||||
|
|
||||||
|
class InitActivity : AbstractInitActivity() { |
||||||
|
override val main: Class<out Activity> = MainActivity::class.java |
||||||
|
override val login: Class<out Activity> = LoginActivity::class.java |
||||||
|
|
||||||
|
override val api: AccountApi = AccountApi.BackgroundToken |
||||||
|
} |
@ -1,11 +1,31 @@ |
|||||||
package com.gyf.csams.account.model |
package com.gyf.csams.account.model |
||||||
|
|
||||||
import androidx.lifecycle.ViewModel |
import android.app.Application |
||||||
import com.gyf.lib.uikit.StringForm |
import android.os.Build |
||||||
|
import com.gyf.lib.model.AbstractLoginViewModel |
||||||
|
import com.gyf.lib.uikit.FormStatus |
||||||
|
import com.gyf.lib.uikit.ValidStringForm |
||||||
|
import com.gyf.lib.util.AccountApi |
||||||
|
|
||||||
class LoginViewModel : ViewModel() { |
data class ManagerVo(val account: String, val password: String, val device: String) |
||||||
val user = StringForm(formDesc = "管理帐号", textLength = 8) |
|
||||||
val password = StringForm(formDesc = "管理密码", textLength = 8) |
|
||||||
|
|
||||||
val login = "登录" |
class LoginViewModel(application: Application) : AbstractLoginViewModel(application) { |
||||||
|
override val id = ValidStringForm(formDesc = "管理帐号", textLength = 8) |
||||||
|
override val password: ValidStringForm = ValidStringForm(formDesc = "管理密码", textLength = 8) |
||||||
|
|
||||||
|
override val api: AccountApi = AccountApi.BackgroundLogin |
||||||
|
|
||||||
|
override fun checkForm(): Boolean { |
||||||
|
return id.statusForm.value == FormStatus.Valid && password.statusForm.value == FormStatus.Valid |
||||||
|
} |
||||||
|
|
||||||
|
override fun loginParam(): Any { |
||||||
|
val account = "${id.formValue.value}" |
||||||
|
val password = "${password.formValue.value}" |
||||||
|
return ManagerVo( |
||||||
|
account = account, |
||||||
|
password = password, |
||||||
|
device = "${Build.MANUFACTURER} ${Build.MODEL}" |
||||||
|
) |
||||||
|
} |
||||||
} |
} |
@ -1,63 +1,14 @@ |
|||||||
package com.gyf.csams |
package com.gyf.csams |
||||||
|
|
||||||
import android.content.Intent |
import android.app.Activity |
||||||
import android.os.Bundle |
|
||||||
import androidx.activity.ComponentActivity |
|
||||||
import androidx.activity.compose.setContent |
|
||||||
import androidx.compose.runtime.getValue |
|
||||||
import androidx.compose.runtime.livedata.observeAsState |
|
||||||
import androidx.compose.ui.Alignment |
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel |
|
||||||
import com.gyf.csams.account.ui.AccountActivity |
import com.gyf.csams.account.ui.AccountActivity |
||||||
import com.gyf.csams.main.ui.MainActivity |
import com.gyf.csams.main.ui.MainActivity |
||||||
import com.gyf.csams.uikit.AnimationText |
import com.gyf.lib.uikit.AbstractInitActivity |
||||||
import com.gyf.lib.uikit.Body |
import com.gyf.lib.util.AccountApi |
||||||
import com.gyf.lib.uikit.MainBoxFrame |
|
||||||
import com.orhanobut.logger.Logger |
|
||||||
import kotlinx.coroutines.GlobalScope |
|
||||||
import kotlinx.coroutines.delay |
|
||||||
import kotlinx.coroutines.launch |
|
||||||
|
|
||||||
class InitActivity : ComponentActivity() { |
class InitActivity : AbstractInitActivity() { |
||||||
|
override val main: Class<out Activity> = MainActivity::class.java |
||||||
|
override val login: Class<out Activity> = AccountActivity::class.java |
||||||
override fun onCreate(savedInstanceState: Bundle?) { |
|
||||||
super.onCreate(savedInstanceState) |
|
||||||
|
|
||||||
|
|
||||||
setContent { |
|
||||||
Body { |
|
||||||
MainBoxFrame(background = { /*TODO*/ }, contentAlignment = Alignment.Center) { |
|
||||||
val initViewModel: InitViewModel = viewModel() |
|
||||||
//检查网络 |
|
||||||
val isNetWorkWorking: Boolean? by initViewModel.isNetWorkWorking.observeAsState( |
|
||||||
null |
|
||||||
) |
|
||||||
Logger.i("初始化") |
|
||||||
when (isNetWorkWorking) { |
|
||||||
null -> AnimationText(text = "测试服务端运行状态中。。。") |
|
||||||
true -> init(initViewModel = initViewModel) |
|
||||||
false -> AnimationText(text = "无法连接到服务端,请检查服务端地址${BuildConfig.SERVER_ADDRESS}是否配置正确") |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
private fun init(initViewModel: InitViewModel) { |
|
||||||
//后台检查token |
|
||||||
initViewModel.hasOnlyUserToken(onSuccess = { |
|
||||||
startActivity(Intent(this, MainActivity::class.java)) |
|
||||||
}, onFail = { |
|
||||||
startActivity(Intent(this, AccountActivity::class.java)) |
|
||||||
}) |
|
||||||
GlobalScope.launch { |
|
||||||
delay(1000) |
|
||||||
finish() |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
|
override val api: AccountApi = AccountApi.ForegroundToken |
||||||
|
} |
@ -1,34 +0,0 @@ |
|||||||
package com.gyf.lib |
|
||||||
|
|
||||||
import android.app.Application |
|
||||||
import androidx.lifecycle.AndroidViewModel |
|
||||||
import androidx.lifecycle.LiveData |
|
||||||
import androidx.lifecycle.MutableLiveData |
|
||||||
import androidx.lifecycle.ViewModel |
|
||||||
|
|
||||||
@Deprecated(message = "", replaceWith = ReplaceWith("")) |
|
||||||
abstract class ScrollList<T> : ViewModel() { |
|
||||||
protected val _data = MutableLiveData<MutableList<T>>(mutableListOf()) |
|
||||||
val data: LiveData<MutableList<T>> = _data |
|
||||||
|
|
||||||
abstract val initSize: Int |
|
||||||
|
|
||||||
//加载列表 |
|
||||||
abstract fun load() |
|
||||||
|
|
||||||
//加载更多数据 |
|
||||||
abstract fun loadMore(callback: (message: String) -> Unit) |
|
||||||
} |
|
||||||
|
|
||||||
abstract class ScrollListW<T>(application: Application) : AndroidViewModel(application) { |
|
||||||
protected val _data = MutableLiveData<MutableList<T>>(mutableListOf()) |
|
||||||
val data: LiveData<MutableList<T>> = _data |
|
||||||
|
|
||||||
abstract val initSize: Int |
|
||||||
|
|
||||||
//加载列表 |
|
||||||
abstract fun load() |
|
||||||
|
|
||||||
//加载更多数据 |
|
||||||
abstract fun loadMore(callback: (message: String) -> Unit) |
|
||||||
} |
|
@ -0,0 +1,80 @@ |
|||||||
|
package com.gyf.lib.model |
||||||
|
|
||||||
|
import android.app.Application |
||||||
|
import androidx.lifecycle.AndroidViewModel |
||||||
|
import androidx.lifecycle.LiveData |
||||||
|
import androidx.lifecycle.MutableLiveData |
||||||
|
import androidx.lifecycle.viewModelScope |
||||||
|
import com.google.gson.reflect.TypeToken |
||||||
|
import com.gyf.lib.R |
||||||
|
import com.gyf.lib.uikit.ValidStringForm |
||||||
|
import com.gyf.lib.util.* |
||||||
|
import com.orhanobut.logger.Logger |
||||||
|
import kotlinx.coroutines.launch |
||||||
|
|
||||||
|
abstract class AbstractLoginViewModel(application: Application) : AndroidViewModel(application) { |
||||||
|
|
||||||
|
protected val loginDesc = application.getString(R.string.login_btn) |
||||||
|
|
||||||
|
abstract val id: ValidStringForm |
||||||
|
abstract val password: ValidStringForm |
||||||
|
|
||||||
|
|
||||||
|
//注册按钮 |
||||||
|
protected val _isValidForm = MutableLiveData<Boolean>() |
||||||
|
|
||||||
|
val isValidForm: LiveData<Boolean> = _isValidForm |
||||||
|
|
||||||
|
/** |
||||||
|
* 完成登录状态 |
||||||
|
*/ |
||||||
|
private val _finishLogin = MutableLiveData<Boolean>() |
||||||
|
val finishLogin: LiveData<Boolean> = _finishLogin |
||||||
|
|
||||||
|
abstract fun checkForm(): Boolean |
||||||
|
|
||||||
|
abstract fun loginParam(): Any |
||||||
|
|
||||||
|
abstract val api: AccountApi |
||||||
|
|
||||||
|
/** |
||||||
|
* 登录 |
||||||
|
* |
||||||
|
*/ |
||||||
|
fun login(callback: (message: String) -> Unit) { |
||||||
|
viewModelScope.launch { |
||||||
|
if (checkForm()) { |
||||||
|
val url = Api.buildUrl(api) |
||||||
|
Logger.i("开始$loginDesc,请求接口:$url") |
||||||
|
val id = "${id.formValue.value}" |
||||||
|
val password = "${password.formValue.value}" |
||||||
|
Logger.i("使用账号:$id,密码:$password 进行登录") |
||||||
|
HttpClient.post( |
||||||
|
url, |
||||||
|
HttpCallback<Token>( |
||||||
|
action = loginDesc, |
||||||
|
onSuccess = { |
||||||
|
Logger.i(it.message) |
||||||
|
callback(it.message) |
||||||
|
val context = getApplication<Application>().applicationContext |
||||||
|
it.body?.let { |
||||||
|
val db = AppDatabase.getInstance(context) |
||||||
|
viewModelScope.launch { |
||||||
|
TokenManager.token = it |
||||||
|
db?.tokenDao()?.save(token = it) |
||||||
|
}.invokeOnCompletion { |
||||||
|
_finishLogin.postValue(true) |
||||||
|
} |
||||||
|
} |
||||||
|
}, |
||||||
|
onFail = { callback(it) }, |
||||||
|
type = object : TypeToken<ApiResponse<Token>>() {}.type |
||||||
|
), |
||||||
|
jsonParam = loginParam() |
||||||
|
) |
||||||
|
} else { |
||||||
|
Logger.wtf("表单校验失败,无法$loginDesc!!!") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,11 +1,10 @@ |
|||||||
package com.gyf |
package com.gyf.lib.model |
||||||
|
|
||||||
import android.app.Application |
import android.app.Application |
||||||
import com.gyf.lib.R |
import com.gyf.lib.R |
||||||
import com.gyf.lib.ScrollListW |
|
||||||
import com.gyf.lib.uikit.StringForm |
import com.gyf.lib.uikit.StringForm |
||||||
|
|
||||||
abstract class ApplyViewModel<T>(application: Application) : ScrollListW<T>(application) { |
abstract class ApplyViewModel<T>(application: Application) : ScrollViewModel<T>(application) { |
||||||
val approverOrigin = |
val approverOrigin = |
||||||
StringForm(formDesc = application.getString(R.string.approver_origin), textLength = 30) |
StringForm(formDesc = application.getString(R.string.approver_origin), textLength = 30) |
||||||
} |
} |
@ -0,0 +1,19 @@ |
|||||||
|
package com.gyf.lib.model |
||||||
|
|
||||||
|
import android.app.Application |
||||||
|
import androidx.lifecycle.AndroidViewModel |
||||||
|
import androidx.lifecycle.LiveData |
||||||
|
import androidx.lifecycle.MutableLiveData |
||||||
|
|
||||||
|
abstract class ScrollViewModel<T>(application: Application) : AndroidViewModel(application) { |
||||||
|
protected val _data = MutableLiveData<MutableList<T>>(mutableListOf()) |
||||||
|
val data: LiveData<MutableList<T>> = _data |
||||||
|
|
||||||
|
abstract val initSize: Int |
||||||
|
|
||||||
|
//加载列表 |
||||||
|
abstract fun load() |
||||||
|
|
||||||
|
//加载更多数据 |
||||||
|
abstract fun loadMore(callback: (message: String) -> Unit) |
||||||
|
} |
@ -0,0 +1,66 @@ |
|||||||
|
package com.gyf.lib.uikit |
||||||
|
|
||||||
|
import android.app.Activity |
||||||
|
import android.content.Intent |
||||||
|
import android.os.Bundle |
||||||
|
import androidx.activity.ComponentActivity |
||||||
|
import androidx.activity.compose.setContent |
||||||
|
import androidx.compose.runtime.getValue |
||||||
|
import androidx.compose.runtime.livedata.observeAsState |
||||||
|
import androidx.compose.ui.Alignment |
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel |
||||||
|
import com.gyf.lib.BuildConfig |
||||||
|
import com.gyf.lib.model.InitViewModel |
||||||
|
import com.gyf.lib.util.AccountApi |
||||||
|
import com.orhanobut.logger.Logger |
||||||
|
import kotlinx.coroutines.GlobalScope |
||||||
|
import kotlinx.coroutines.delay |
||||||
|
import kotlinx.coroutines.launch |
||||||
|
|
||||||
|
abstract class AbstractInitActivity : ComponentActivity() { |
||||||
|
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) { |
||||||
|
super.onCreate(savedInstanceState) |
||||||
|
|
||||||
|
|
||||||
|
setContent { |
||||||
|
Body { |
||||||
|
MainBoxFrame(background = { /*TODO*/ }, contentAlignment = Alignment.Center) { |
||||||
|
val initViewModel: InitViewModel = viewModel() |
||||||
|
//检查网络 |
||||||
|
val isNetWorkWorking: Boolean? by initViewModel.isNetWorkWorking.observeAsState( |
||||||
|
null |
||||||
|
) |
||||||
|
Logger.i("初始化") |
||||||
|
when (isNetWorkWorking) { |
||||||
|
null -> AnimationText(text = "测试服务端运行状态中。。。") |
||||||
|
true -> init(initViewModel = initViewModel) |
||||||
|
false -> AnimationText(text = "无法连接到服务端,请检查服务端地址${BuildConfig.SERVER_ADDRESS}是否配置正确") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
protected abstract val main: Class<out Activity> |
||||||
|
|
||||||
|
protected abstract val login: Class<out Activity> |
||||||
|
|
||||||
|
abstract val api: AccountApi |
||||||
|
|
||||||
|
private fun init(initViewModel: InitViewModel) { |
||||||
|
//后台检查token |
||||||
|
initViewModel.hasOnlyUserToken(onSuccess = { |
||||||
|
startActivity(Intent(this, main)) |
||||||
|
}, onFail = { |
||||||
|
startActivity(Intent(this, login)) |
||||||
|
}, api = api) |
||||||
|
GlobalScope.launch { |
||||||
|
delay(1000) |
||||||
|
finish() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
@ -0,0 +1,33 @@ |
|||||||
|
package com.gyf.lib.uikit |
||||||
|
|
||||||
|
import androidx.compose.animation.animateColor |
||||||
|
import androidx.compose.animation.core.* |
||||||
|
import androidx.compose.material.MaterialTheme |
||||||
|
import androidx.compose.material.Text |
||||||
|
import androidx.compose.runtime.Composable |
||||||
|
import androidx.compose.runtime.getValue |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 淡入淡出并且颜色变化文本 |
||||||
|
* |
||||||
|
* @param text |
||||||
|
*/ |
||||||
|
@Composable |
||||||
|
fun AnimationText(text: String) { |
||||||
|
val infiniteTransition = rememberInfiniteTransition() |
||||||
|
val color by infiniteTransition.animateColor( |
||||||
|
initialValue = MaterialTheme.colors.primary, |
||||||
|
targetValue = MaterialTheme.colors.onPrimary, |
||||||
|
animationSpec = infiniteRepeatable( |
||||||
|
animation = tween(1000, easing = LinearEasing), |
||||||
|
repeatMode = RepeatMode.Reverse |
||||||
|
) |
||||||
|
) |
||||||
|
|
||||||
|
Text( |
||||||
|
text = text, |
||||||
|
color = color, |
||||||
|
style = MaterialTheme.typography.body1 |
||||||
|
) |
||||||
|
} |
Loading…
Reference in new issue