parent
a38dcf6587
commit
4145e3fbef
@ -0,0 +1,57 @@ |
||||
package com.gyf.lib.model |
||||
|
||||
import android.app.Application |
||||
import android.net.Uri |
||||
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.csams.module.ApiResponse |
||||
import com.gyf.lib.util.* |
||||
import kotlinx.coroutines.launch |
||||
import java.io.File |
||||
import java.io.InputStream |
||||
|
||||
open class ChoosePhotoViewModel(application: Application) : AndroidViewModel(application) { |
||||
protected val _picture = MutableLiveData<Uri?>() |
||||
val picture: LiveData<Uri?> = _picture |
||||
|
||||
fun setPicture(uri: Uri) { |
||||
_picture.value = uri |
||||
} |
||||
|
||||
|
||||
protected fun getInputSteam(): InputStream? { |
||||
_picture.value?.let { |
||||
val resolver = getApplication<Application>().contentResolver |
||||
return resolver.openInputStream(it) |
||||
} |
||||
throw IllegalArgumentException("图片uri为空") |
||||
} |
||||
|
||||
/** |
||||
* 上传头像 |
||||
* |
||||
* @param callback |
||||
*/ |
||||
fun upload(callback: (flag: Boolean) -> Unit) { |
||||
viewModelScope.launch { |
||||
getInputSteam()?.readBytes()?.apply { |
||||
val context = getApplication<Application>() |
||||
runCatching { |
||||
val cacheFile = File(context.cacheDir, "${System.currentTimeMillis()}") |
||||
cacheFile.writeBytes(this@apply) |
||||
HttpClient.uploadFile( |
||||
url = Api.buildUrl(AccountApi.UploadHeadimg), |
||||
HttpCallback<Boolean>(action = "上传头像", onSuccess = { |
||||
it.body?.let(callback) |
||||
_picture.postValue(null) |
||||
}, typeToken = object : TypeToken<ApiResponse<Boolean>>() {}.type), |
||||
fileList = arrayOf(cacheFile) |
||||
) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,61 @@ |
||||
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.csams.module.* |
||||
import com.gyf.lib.uikit.AsyncStringForm |
||||
import com.gyf.lib.util.* |
||||
import kotlinx.coroutines.launch |
||||
|
||||
class RefreshViewModel(application: Application) : AndroidViewModel(application) { |
||||
|
||||
private val _refresh = MutableLiveData<Boolean>() |
||||
val refresh: LiveData<Boolean> = _refresh |
||||
|
||||
val name = AsyncStringForm(formDesc = "姓名", textLength = 4) |
||||
val desc = AsyncStringForm(formDesc = "简介", textLength = 20) |
||||
|
||||
fun <T : OwnInfoVo> onSuccess(res: ApiResponse<T>) { |
||||
res.body.let { |
||||
if (it != null) { |
||||
TokenManager.update(it) |
||||
} |
||||
_refresh.postValue(it != null) |
||||
} |
||||
} |
||||
|
||||
fun refresh() { |
||||
viewModelScope.launch { |
||||
val jsonParam = InfoUpdateVo( |
||||
name = name.formValue.value, desc = desc.formValue.value, |
||||
token = TokenManager.getToken(), |
||||
clientType = TokenManager.getClientType() |
||||
) |
||||
when (jsonParam.clientType) { |
||||
ClientType.Foreground -> HttpClient.post( |
||||
url = Api.buildUrl(AccountApi.Refresh), |
||||
callback = HttpCallback<UserVo>( |
||||
action = "刷新个人信息", |
||||
onSuccess = { onSuccess(it) }, |
||||
typeToken = object : TypeToken<ApiResponse<UserVo>>() {}.type |
||||
), |
||||
jsonParam = jsonParam |
||||
) |
||||
ClientType.Background -> HttpClient.post( |
||||
url = Api.buildUrl(AccountApi.Refresh), |
||||
callback = HttpCallback<ManagerVo>( |
||||
action = "刷新个人信息", |
||||
onSuccess = { onSuccess(it) }, |
||||
typeToken = object : TypeToken<ApiResponse<ManagerVo>>() {}.type |
||||
), |
||||
jsonParam = jsonParam |
||||
) |
||||
} |
||||
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,83 @@ |
||||
package com.gyf.lib.uikit |
||||
|
||||
import android.Manifest |
||||
import android.content.Intent |
||||
import android.content.pm.PackageManager |
||||
import android.provider.MediaStore |
||||
import androidx.activity.ComponentActivity |
||||
import androidx.activity.compose.ManagedActivityResultLauncher |
||||
import androidx.activity.compose.rememberLauncherForActivityResult |
||||
import androidx.activity.result.ActivityResult |
||||
import androidx.activity.result.contract.ActivityResultContracts |
||||
import androidx.compose.runtime.Composable |
||||
import androidx.core.content.ContextCompat |
||||
import androidx.lifecycle.viewmodel.compose.viewModel |
||||
import com.gyf.lib.R |
||||
import com.gyf.lib.model.ChoosePhotoViewModel |
||||
import com.orhanobut.logger.Logger |
||||
|
||||
class ChoosePhoto(val activity: ComponentActivity) { |
||||
|
||||
fun loadPicture(photoLauncher: ManagedActivityResultLauncher<Intent, *>) { |
||||
photoLauncher.launch( |
||||
Intent( |
||||
Intent.ACTION_PICK, |
||||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI |
||||
) |
||||
) |
||||
} |
||||
|
||||
@Composable |
||||
fun photoLauncher(model: ChoosePhotoViewModel = viewModel()): ManagedActivityResultLauncher<Intent, ActivityResult> { |
||||
return rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { |
||||
when (it.resultCode) { |
||||
ComponentActivity.RESULT_OK -> { |
||||
|
||||
it.data?.data?.apply { |
||||
Logger.i("uri=$this") |
||||
model.setPicture(this) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Composable |
||||
fun permissionLauncher( |
||||
photoLauncher: ManagedActivityResultLauncher<Intent, *>, |
||||
scaffoldModel: ScaffoldModel = viewModel() |
||||
): ManagedActivityResultLauncher<String, Boolean> { |
||||
|
||||
return rememberLauncherForActivityResult( |
||||
ActivityResultContracts.RequestPermission() |
||||
) { isGranted: Boolean -> |
||||
if (isGranted) { |
||||
// Permission Accepted: Do something |
||||
loadPicture(photoLauncher = photoLauncher) |
||||
} else { |
||||
// Permission Denied: Do something |
||||
scaffoldModel.update(message = activity.getString(R.string.denined_photo_permission)) |
||||
} |
||||
} |
||||
} |
||||
|
||||
fun requestPhoto( |
||||
photoLauncher: ManagedActivityResultLauncher<Intent, *>, |
||||
permissionLauncher: ManagedActivityResultLauncher<String, Boolean> |
||||
) { |
||||
when (PackageManager.PERMISSION_GRANTED) { |
||||
ContextCompat.checkSelfPermission( |
||||
activity.applicationContext, |
||||
Manifest.permission.READ_EXTERNAL_STORAGE |
||||
) -> { |
||||
// Some works that require permission |
||||
loadPicture(photoLauncher = photoLauncher) |
||||
} |
||||
else -> { |
||||
// Asking for permission |
||||
permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE) |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue