From 589f19b69d865d3b609cbfb0ebbfed1c243881c1 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Mon, 17 May 2021 16:31:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=B8=AA=E4=BA=BA=E4=B8=AD?= =?UTF-8?q?=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gyf/csams/main/model/MainViewModel.kt | 34 +++ .../com/gyf/csams/main/ui/MainActivity.kt | 226 +++++++++++++----- 2 files changed, 203 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/com/gyf/csams/main/model/MainViewModel.kt b/app/src/main/java/com/gyf/csams/main/model/MainViewModel.kt index 70098b0..4f4fb7c 100644 --- a/app/src/main/java/com/gyf/csams/main/model/MainViewModel.kt +++ b/app/src/main/java/com/gyf/csams/main/model/MainViewModel.kt @@ -9,6 +9,8 @@ import com.gyf.csams.R import com.gyf.csams.uikit.ScrollList import com.gyf.csams.uikit.SendInterface import com.gyf.csams.uikit.StringForm +import com.gyf.csams.util.randomChinese +import com.gyf.csams.util.randomNum import com.orhanobut.logger.Logger import kotlinx.coroutines.Job import kotlinx.coroutines.delay @@ -192,10 +194,42 @@ class ListViewModel: ScrollList() { } } +/** + * 我的信息 + * + * @property studentId 学号 + * @property name 姓名 + * @property duty 职务 + * @property headImg 头像 + * @property desc 个人简介 + */ +data class InfoVo(val studentId:String,val name:String,val duty:String,val headImg:String,val desc:String) + /** * 个人中心 * */ class CenterViewModel:ViewModel(){ val myAssociationDesc="我的社团" + val myJoinActivity="我参加的社团活动" + val myLikeActivity="我点赞的社团活动" + val myCollectActivity="我收藏的社团活动" + + private val _info=MutableLiveData() + val info:LiveData = _info + + init { + load() + } + + private fun load(){ + viewModelScope.launch { + _info.value= InfoVo(studentId = randomNum(), + name = randomChinese(3), + duty = randomChinese(3), + headImg = "", + desc = randomChinese(10)) + } + + } } diff --git a/app/src/main/java/com/gyf/csams/main/ui/MainActivity.kt b/app/src/main/java/com/gyf/csams/main/ui/MainActivity.kt index 054af15..06df68a 100644 --- a/app/src/main/java/com/gyf/csams/main/ui/MainActivity.kt +++ b/app/src/main/java/com/gyf/csams/main/ui/MainActivity.kt @@ -74,36 +74,103 @@ class MainActivity : ComponentActivity() { * */ @Composable - private fun Center(model:CenterViewModel= viewModel(), scaffoldModel: ScaffoldModel= viewModel(), navController: NavHostController){ - MainFrame(background = { Background(image = BackgroundImage.Center,alpha = 0.5F) }, mainMenu = MainMenu.Center, nav = navController) { - Column(modifier = Modifier - .weight(0.33F) - .fillMaxWidth(),verticalArrangement = Arrangement.Bottom) { - Card(backgroundColor = MaterialTheme.colors.background) { - val context= LocalContext.current + private fun Center( + model: CenterViewModel = viewModel(), + scaffoldModel: ScaffoldModel = viewModel(), + navController: NavHostController + ) { + MainFrame( + background = { Background(image = BackgroundImage.Center, alpha = 0.5F) }, + mainMenu = MainMenu.Center, + nav = navController + ) { + val context = LocalContext.current + + val info by model.info.observeAsState() + + + Column( + modifier = Modifier.weight(0.3F).padding(10.dp), + ) { + info?.let { Row(modifier = Modifier .fillMaxWidth() - .clickable(onClick = { - context.startActivity(Intent(context, AssociationActivity::class.java)) - }),verticalAlignment = Alignment.CenterVertically) { - Spacer(modifier = Modifier.weight(0.33F)) - Row(modifier = Modifier.weight(0.33F),horizontalArrangement = Arrangement.Center) { - Text(text = model.myAssociationDesc) + .weight(0.7F) + .border(width = 1.dp, color = MaterialTheme.colors.background), + horizontalArrangement = Arrangement.SpaceBetween) { + Image( + painter = painterResource( + id = R.drawable.ic_launcher_foreground + ), + contentDescription = null, + modifier = Modifier + .weight(0.4F) + .fillMaxHeight() + ) + Column( + modifier = Modifier + .weight(0.4F) + .fillMaxHeight() + .border(width = 1.dp, color = MaterialTheme.colors.background), + verticalArrangement = Arrangement.SpaceEvenly, + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text(text = it.name) + Text(text = it.duty) } - Row(modifier = Modifier.weight(0.33F),horizontalArrangement = Arrangement.End) { - Icon( - painter = painterResource(id = R.drawable.ic_arrow_right), - contentDescription = null, - modifier = Modifier.size(50.dp) - ) + } + + Spacer(modifier = Modifier.weight(0.05F)) + + Card(backgroundColor = MaterialTheme.colors.background,modifier = Modifier.weight(0.15F)) { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.Center + ) { + Text(text = it.desc) } } + } + } + + Column( + modifier = Modifier.weight(0.7F), + verticalArrangement = Arrangement.SpaceEvenly + ) { + CenterMenuItem(text = model.myAssociationDesc) { + context.startActivity(Intent(context, AssociationActivity::class.java)) + } + CenterMenuItem(text = model.myJoinActivity) + CenterMenuItem(text = model.myLikeActivity) + CenterMenuItem(text = model.myCollectActivity) + } + + } + } + + @Composable + private fun CenterMenuItem(text: String, onClick: () -> Unit = {}) { + Card(backgroundColor = MaterialTheme.colors.background) { + Row( + modifier = Modifier + .fillMaxWidth() + .clickable(onClick = onClick), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically + ) { + Spacer(modifier = Modifier) + Row { + Text(text = text) + } + Row { + Icon( + painter = painterResource(id = R.drawable.ic_arrow_right), + contentDescription = null, + modifier = Modifier.size(50.dp) + ) } } - Spacer(modifier = Modifier - .weight(0.66F) - .fillMaxWidth()) } } @@ -112,7 +179,11 @@ class MainActivity : ComponentActivity() { */ @Composable private fun Main(navController: NavHostController) { - MainFrame(background = { Background(image = BackgroundImage.Main) }, mainMenu = MainMenu.Main, nav = navController) { + MainFrame( + background = { Background(image = BackgroundImage.Main) }, + mainMenu = MainMenu.Main, + nav = navController + ) { Column(modifier = Modifier.weight(0.33F)) { Notification() MessageBoard() @@ -151,7 +222,7 @@ class MainActivity : ComponentActivity() { */ @Composable private fun RegisterAssociation() { - val context= LocalContext.current + val context = LocalContext.current Row( horizontalArrangement = Arrangement.End, modifier = Modifier @@ -175,52 +246,68 @@ class MainActivity : ComponentActivity() { * */ @Composable - private fun AssociationListBody(model: ListViewModel = viewModel(),scaffoldModel: ScaffoldModel= viewModel()) { + private fun AssociationListBody( + model: ListViewModel = viewModel(), + scaffoldModel: ScaffoldModel = viewModel() + ) { val associationList: MutableList? by model.associationDto.observeAsState() val listState = rememberLazyListState() - LazyColumn(state = listState) { + LazyColumn(state = listState) { associationList?.chunked(2)?.forEach { - item{ - Row { + item { + Row { Spacer(modifier = Modifier.weight(0.1F)) Box(modifier = Modifier.weight(0.35F)) { Association(associationDto = it[0]) } Spacer(modifier = Modifier.weight(0.05F)) - if(it.size==2) { + if (it.size == 2) { Box(modifier = Modifier.weight(0.35F)) { Association(associationDto = it[1]) } - }else{ - Box(modifier = Modifier - .weight(0.35F) - .border(width = 1.dp, color = MaterialTheme.colors.onBackground)) + } else { + Box( + modifier = Modifier + .weight(0.35F) + .border(width = 1.dp, color = MaterialTheme.colors.onBackground) + ) } Spacer(modifier = Modifier.weight(0.1F)) } - Spacer(modifier = Modifier - .fillMaxWidth() - .height(10.dp)) + Spacer( + modifier = Modifier + .fillMaxWidth() + .height(10.dp) + ) } } } - if(listState.layoutInfo.totalItemsCount-listState.firstVisibleItemIndex==model.associationListSize/2-1){ - model.loadMore { scaffoldModel.update(message=it) } + if (listState.layoutInfo.totalItemsCount - listState.firstVisibleItemIndex == model.associationListSize / 2 - 1) { + model.loadMore { scaffoldModel.update(message = it) } } } @Composable private fun Association(associationDto: AssociationDto) { - val context= LocalContext.current - Card(modifier = Modifier.clickable(onClick = { context.startActivity(Intent(context,AssociationActivity::class.java)) })) { + val context = LocalContext.current + Card(modifier = Modifier.clickable(onClick = { + context.startActivity( + Intent( + context, + AssociationActivity::class.java + ) + ) + })) { Image( painter = painterResource(id = R.drawable.association_list_border), contentDescription = null ) - Row(modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.Center) { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.Center + ) { Text(text = associationDto.name) } @@ -233,18 +320,35 @@ class MainActivity : ComponentActivity() { * */ @Composable - private fun AssociationSearch(model: ListViewModel = viewModel(),scaffoldModel: ScaffoldModel= viewModel()) { + private fun AssociationSearch( + model: ListViewModel = viewModel(), + scaffoldModel: ScaffoldModel = viewModel() + ) { Card(modifier = Modifier.padding(horizontal = 50.dp, vertical = 10.dp)) { Column { - BaseTextField(form = model.name,singeLine = true,modifier = Modifier.padding(horizontal = 10.dp)) - BaseTextField(form = model.desc,singeLine = true,modifier = Modifier.padding(horizontal = 10.dp)) + BaseTextField( + form = model.name, + singeLine = true, + modifier = Modifier.padding(horizontal = 10.dp) + ) + BaseTextField( + form = model.desc, + singeLine = true, + modifier = Modifier.padding(horizontal = 10.dp) + ) Spacer( modifier = Modifier.height(10.dp) ) - Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth()) { - OutlinedButton(onClick = { model.search { scaffoldModel.update(message=it) } }, modifier = Modifier.width(100.dp)) { + Row( + horizontalArrangement = Arrangement.Center, + modifier = Modifier.fillMaxWidth() + ) { + OutlinedButton( + onClick = { model.search { scaffoldModel.update(message = it) } }, + modifier = Modifier.width(100.dp) + ) { Text(text = model.searchDesc) } } @@ -262,7 +366,10 @@ class MainActivity : ComponentActivity() { * */ @Composable - private fun Notification(mainViewModel: MainViewModel= viewModel(),scaffoldModel: ScaffoldModel= viewModel()) { + private fun Notification( + mainViewModel: MainViewModel = viewModel(), + scaffoldModel: ScaffoldModel = viewModel() + ) { Row( horizontalArrangement = Arrangement.End, modifier = Modifier @@ -270,7 +377,7 @@ class MainActivity : ComponentActivity() { .padding(10.dp) ) { IconButton(onClick = { - mainViewModel.openNotification { scaffoldModel.update(message=it) } + mainViewModel.openNotification { scaffoldModel.update(message = it) } }) { Icon( painter = painterResource(id = R.drawable.ic_notification), @@ -306,13 +413,17 @@ class MainActivity : ComponentActivity() { * */ @Composable - private fun MessageBoard(model: MarqueeViewModel = viewModel(),mainViewModel:MainViewModel= viewModel(),scaffoldModel: ScaffoldModel= viewModel()) { + private fun MessageBoard( + model: MarqueeViewModel = viewModel(), + mainViewModel: MainViewModel = viewModel(), + scaffoldModel: ScaffoldModel = viewModel() + ) { MyBorder { Row( verticalAlignment = Alignment.CenterVertically ) { - IconButton(onClick = { mainViewModel.openDialog() }) { + IconButton(onClick = { mainViewModel.openDialog() }) { Icon( painter = painterResource(id = R.drawable.ic_comments), contentDescription = null, @@ -370,14 +481,15 @@ class MainActivity : ComponentActivity() { @Composable private fun PosterWithDesc(model: CarouselViewModel = viewModel()) { Carousel(model = model) { - val context= LocalContext.current + val context = LocalContext.current Column(modifier = Modifier.clickable(onClick = { - context.startActivity(Intent(context,ActivityDetailActivity::class.java)) + context.startActivity(Intent(context, ActivityDetailActivity::class.java)) })) { - Poster(modifier = Modifier - .weight(0.6F) - .fillMaxWidth() - ,id = it) + Poster( + modifier = Modifier + .weight(0.6F) + .fillMaxWidth(), id = it + ) DescCard( modifier = Modifier