parent
671b682711
commit
24b12f9778
@ -0,0 +1,123 @@ |
||||
package com.gyf.csams.main.model |
||||
|
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.MutableLiveData |
||||
import androidx.lifecycle.ViewModel |
||||
import androidx.lifecycle.viewModelScope |
||||
import com.gyf.lib.util.randomChinese |
||||
import com.gyf.lib.util.randomNum |
||||
import com.orhanobut.logger.Logger |
||||
import kotlinx.coroutines.launch |
||||
|
||||
enum class ColumnType { |
||||
Text, |
||||
DropMenu |
||||
} |
||||
|
||||
/** |
||||
* 职务 |
||||
* |
||||
* @property desc |
||||
*/ |
||||
enum class Duty(val desc: String) { |
||||
Minister("部长"), |
||||
Manager("干事") |
||||
} |
||||
|
||||
/** |
||||
* 人员信息 |
||||
* |
||||
* @property name 名字 |
||||
* @property studentId 学号 |
||||
* @property mobile 手机号 |
||||
* @property duty 职务 |
||||
* @property counselor 导员 |
||||
*/ |
||||
data class OfficerVo( |
||||
val name: String, |
||||
val studentId: String, |
||||
val mobile: String, |
||||
val duty: Duty, |
||||
val counselor: String |
||||
) |
||||
|
||||
data class AllOfficerVo( |
||||
val secretariat: MutableList<OfficerVo>, |
||||
val propaganda: MutableList<OfficerVo>, |
||||
val publicRelationsDepartment: MutableList<OfficerVo> |
||||
) |
||||
|
||||
/** |
||||
* 部门干事数据状态管理 |
||||
* |
||||
*/ |
||||
class ManagementOfficerModel : ViewModel() { |
||||
private val _data = MutableLiveData<AllOfficerVo>() |
||||
val data: LiveData<AllOfficerVo> = _data |
||||
|
||||
init { |
||||
load() |
||||
} |
||||
|
||||
private fun replace( |
||||
list: MutableList<OfficerVo>, |
||||
index: Int, |
||||
callback: (s: MutableList<OfficerVo>) -> Unit |
||||
) { |
||||
val s = mutableListOf<OfficerVo>() |
||||
list[index] = list[index].copy(duty = Duty.Minister) |
||||
s.add(list[index]) |
||||
s.addAll(list.filter { officerVo -> officerVo != list[index] } |
||||
.map { officerVo -> officerVo.copy(duty = Duty.Manager) }) |
||||
callback(s) |
||||
} |
||||
|
||||
fun updateDuty(list: MutableList<OfficerVo>, index: Int) { |
||||
_data.value?.apply { |
||||
Logger.i("$secretariat") |
||||
when (list) { |
||||
secretariat -> replace(list = list, index = index) { |
||||
_data.postValue(copy(secretariat = it)) |
||||
} |
||||
propaganda -> replace(list = list, index = index) { |
||||
_data.postValue(copy(propaganda = it)) |
||||
} |
||||
publicRelationsDepartment -> replace(list = list, index = index) { |
||||
_data.postValue(copy(publicRelationsDepartment = it)) |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* TODO 加载部门成员 |
||||
* |
||||
*/ |
||||
private fun load() { |
||||
viewModelScope.launch { |
||||
val officerVoList = mutableListOf<OfficerVo>() |
||||
val baseSize = 3 |
||||
val peopleSize = 5 |
||||
repeat(peopleSize * baseSize) { |
||||
officerVoList.add( |
||||
OfficerVo( |
||||
name = randomChinese(3), studentId = randomNum(8), mobile = randomNum(11), |
||||
if (it % peopleSize == 0) Duty.Minister else Duty.Manager, counselor = "" |
||||
) |
||||
) |
||||
} |
||||
val all = officerVoList.chunked(peopleSize) |
||||
_data.postValue( |
||||
AllOfficerVo( |
||||
secretariat = all[0].toMutableList(), |
||||
propaganda = all[1].toMutableList(), |
||||
publicRelationsDepartment = all[2].toMutableList() |
||||
) |
||||
) |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,172 @@ |
||||
package com.gyf.csams.main.ui |
||||
|
||||
import android.os.Bundle |
||||
import androidx.activity.ComponentActivity |
||||
import androidx.activity.compose.setContent |
||||
import androidx.annotation.StringRes |
||||
import androidx.compose.foundation.ExperimentalFoundationApi |
||||
import androidx.compose.foundation.border |
||||
import androidx.compose.foundation.clickable |
||||
import androidx.compose.foundation.layout.* |
||||
import androidx.compose.foundation.lazy.LazyRow |
||||
import androidx.compose.material.DropdownMenu |
||||
import androidx.compose.material.DropdownMenuItem |
||||
import androidx.compose.material.MaterialTheme |
||||
import androidx.compose.material.Text |
||||
import androidx.compose.runtime.* |
||||
import androidx.compose.runtime.livedata.observeAsState |
||||
import androidx.compose.ui.Alignment |
||||
import androidx.compose.ui.Modifier |
||||
import androidx.compose.ui.platform.LocalContext |
||||
import androidx.compose.ui.unit.dp |
||||
import androidx.lifecycle.viewmodel.compose.viewModel |
||||
import com.gyf.csams.R |
||||
import com.gyf.csams.main.model.Duty |
||||
import com.gyf.csams.main.model.ManagementOfficerModel |
||||
import com.gyf.csams.main.model.MinisterVo |
||||
import com.gyf.csams.main.model.OfficerVo |
||||
import com.gyf.lib.uikit.* |
||||
import com.orhanobut.logger.Logger |
||||
|
||||
/** |
||||
* 部门干事管理 |
||||
* |
||||
*/ |
||||
class ManagementOfficerActivity : ComponentActivity() { |
||||
@ExperimentalFoundationApi |
||||
override fun onCreate(savedInstanceState: Bundle?) { |
||||
super.onCreate(savedInstanceState) |
||||
setContent { |
||||
Body { scaffoldState -> |
||||
MainColumnFrame(background = { /*TODO*/ }) { |
||||
val weight = 1 / 3F |
||||
val model: ManagementOfficerModel = viewModel() |
||||
val data by model.data.observeAsState() |
||||
Logger.i("$data") |
||||
data?.apply { |
||||
Table( |
||||
modifier = Modifier |
||||
.weight(weight = weight), |
||||
id = R.string.secretariat, |
||||
officerVoList = secretariat |
||||
) |
||||
Table( |
||||
modifier = Modifier |
||||
.weight(weight = weight), |
||||
id = R.string.propaganda_department, |
||||
officerVoList = propaganda |
||||
) |
||||
Table( |
||||
modifier = Modifier |
||||
.weight(weight = weight), |
||||
id = R.string.public_relations_department, |
||||
officerVoList = publicRelationsDepartment |
||||
) |
||||
} |
||||
|
||||
ShowSnackbar(scaffoldState = scaffoldState) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Composable |
||||
private fun BoxItem(content: @Composable () -> Unit) { |
||||
Box( |
||||
Modifier |
||||
.width(150.dp) |
||||
.height(40.dp) |
||||
.border( |
||||
width = 1.dp, |
||||
color = MaterialTheme.colors.onBackground |
||||
) |
||||
.padding(vertical = 10.dp), |
||||
contentAlignment = Alignment.Center |
||||
) { |
||||
content() |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* 表格 |
||||
*/ |
||||
@ExperimentalFoundationApi |
||||
@Composable |
||||
private fun Table( |
||||
modifier: Modifier = Modifier, |
||||
model: ManagementOfficerModel = viewModel(), |
||||
scaffoldModel: ScaffoldModel = viewModel(), |
||||
@StringRes id: Int, |
||||
officerVoList: MutableList<OfficerVo>, |
||||
) { |
||||
Column( |
||||
modifier = modifier |
||||
.padding(horizontal = 10.dp) |
||||
.border(width = 1.dp, color = MaterialTheme.colors.onBackground), |
||||
verticalArrangement = Arrangement.SpaceEvenly |
||||
) { |
||||
val context = LocalContext.current |
||||
|
||||
Spacer(modifier = Modifier.weight(0.05F)) |
||||
Text(text = context.getString(id), modifier = Modifier.weight(0.1F)) |
||||
Spacer(modifier = Modifier.weight(0.05F)) |
||||
val columnSize = 4 |
||||
|
||||
LazyRow(modifier = Modifier.weight(0.6F)) { |
||||
officerVoList.withIndex().forEach { |
||||
item { |
||||
var expanded by remember { |
||||
mutableStateOf(false) |
||||
} |
||||
Profile( |
||||
modifier = Modifier |
||||
.width(200.dp) |
||||
.fillMaxHeight() |
||||
.border(width = 1.dp, color = MaterialTheme.colors.onBackground), |
||||
personInfoVo = |
||||
MinisterVo( |
||||
name = it.value.name, |
||||
duty = it.value.duty.desc, |
||||
headImg = "", |
||||
desc = it.value.mobile |
||||
) |
||||
) { |
||||
|
||||
Text( |
||||
text = it.value.duty.desc, |
||||
modifier = Modifier.clickable(onClick = { |
||||
if (it.value.duty.desc != Duty.Minister.desc) expanded = |
||||
true else scaffoldModel |
||||
.update(message = context.getString(R.string.update_duty_error)) |
||||
}) |
||||
) |
||||
// Text( |
||||
// text = it.duty.desc, |
||||
// modifier = Modifier.clickable(onClick = { |
||||
// if (duty.desc != Duty.Minister.desc) expanded = |
||||
// true else scaffoldModel |
||||
// .update(message = context.getString(R.string.update_duty_error)) |
||||
// }) |
||||
// ) |
||||
DropdownMenu( |
||||
expanded = expanded, |
||||
onDismissRequest = { /*TODO*/ }) { |
||||
DropdownMenuItem(onClick = { |
||||
model.updateDuty(list = officerVoList, index = it.index) |
||||
expanded = false |
||||
}) { |
||||
Text(text = Duty.Minister.desc) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
Spacer(modifier = Modifier.weight(0.05F)) |
||||
|
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue