社团列表

master
pan 4 years ago
parent 4fae920eb1
commit edf9920f3e
  1. 32
      app/src/main/java/com/gyf/csams/ui/MainActivity.kt
  2. 51
      app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt

@ -15,6 +15,7 @@ import androidx.compose.material.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate import androidx.compose.ui.draw.rotate
@ -37,6 +38,8 @@ import com.gyf.csams.ui.model.CommunityDto
import com.gyf.csams.ui.model.ListViewModel import com.gyf.csams.ui.model.ListViewModel
import com.gyf.csams.ui.model.MarqueeViewModel import com.gyf.csams.ui.model.MarqueeViewModel
import com.gyf.csams.ui.theme.CSAMSTheme import com.gyf.csams.ui.theme.CSAMSTheme
import com.orhanobut.logger.Logger
import kotlinx.coroutines.launch
/** /**
@ -68,6 +71,23 @@ fun Body() {
} }
composable(MainMenu.List.name) { composable(MainMenu.List.name) {
CommunityList(navController = navController) CommunityList(navController = navController)
val model:ListViewModel= viewModel()
val snackBarMsg:String by model.snackBarMsg.observeAsState("")
if(snackBarMsg!=""){
val scope= rememberCoroutineScope()
scope.launch {
val result=scaffoldState.snackbarHostState.showSnackbar(
message = snackBarMsg
)
model.reset()
}
// scope.launch {
// delay(1000)
// scaffoldState.snackbarHostState.currentSnackbarData?.dismiss()
// }
}
} }
composable(MainMenu.Center.name) { composable(MainMenu.Center.name) {
Box(modifier = Modifier.fillMaxSize()) { Box(modifier = Modifier.fillMaxSize()) {
@ -187,6 +207,8 @@ fun CommunityListBody(model: ListViewModel = viewModel()) {
val communityList: MutableList<CommunityDto>? by model.communityDto.observeAsState() val communityList: MutableList<CommunityDto>? by model.communityDto.observeAsState()
val listState = rememberLazyListState() val listState = rememberLazyListState()
// Text(text ="communityList.size=${communityList?.size}")
LazyColumn(state = listState) { LazyColumn(state = listState) {
communityList?.chunked(2)?.forEach { communityList?.chunked(2)?.forEach {
item{ item{
@ -212,9 +234,15 @@ fun CommunityListBody(model: ListViewModel = viewModel()) {
.height(10.dp)) .height(10.dp))
} }
} }
} }
if(listState.firstVisibleItemIndex>0){ Logger.i("totalItemsCount=${listState.layoutInfo.totalItemsCount}" +
",firstVisibleItemIndex=${listState.firstVisibleItemIndex}," +
"firstVisibleItemScrollOffset=${listState.firstVisibleItemScrollOffset}," +
"viewportStartOffset=${listState.layoutInfo.viewportStartOffset}" +
"viewportEndOffset=${listState.layoutInfo.viewportEndOffset}")
if(listState.layoutInfo.totalItemsCount-listState.firstVisibleItemIndex==4){
model.addMore() model.addMore()
} }
} }

@ -71,17 +71,19 @@ class CarouselViewModel:ViewModel(){
data class CommunityDto(val name:String) data class CommunityDto(val name:String)
class ListViewModel:ViewModel(){ class ListViewModel:ViewModel(){
private val _name=MutableLiveData<String>() private val _name=MutableLiveData("")
val name:LiveData<String> = _name val name:LiveData<String> = _name
val nameDesc="社团名称" val nameDesc="社团名称"
val namePlaceholder="请输入$nameDesc" val namePlaceholder="请输入$nameDesc"
private val _desc=MutableLiveData<String>() private val _desc=MutableLiveData("")
val desc:LiveData<String> = _desc val desc:LiveData<String> = _desc
val descDesc="社团简介" val descDesc="社团简介"
val descPlaceholder="请输入$descDesc" val descPlaceholder="请输入$descDesc"
//注册请求响应信息
private val _snackBarMsg=MutableLiveData<String>()
val snackBarMsg:LiveData<String> = _snackBarMsg
//社团列表 //社团列表
private val _communityList=MutableLiveData<MutableList<CommunityDto>>(mutableListOf()) private val _communityList=MutableLiveData<MutableList<CommunityDto>>(mutableListOf())
@ -101,6 +103,7 @@ class ListViewModel:ViewModel(){
fun search(){ fun search(){
Logger.i("使用社团名称:${_name.value}和社团简介${_desc.value}搜索社团") Logger.i("使用社团名称:${_name.value}和社团简介${_desc.value}搜索社团")
_snackBarMsg.value="搜索失败,请联系管理员"
} }
/** /**
@ -111,36 +114,48 @@ class ListViewModel:ViewModel(){
viewModelScope.launch { viewModelScope.launch {
_communityList.value?.apply { _communityList.value?.apply {
repeat(2 repeat(10
) { ) {
add(CommunityDto(name = "羽毛球社")) add(CommunityDto(name = "社团${_communityList.value?.size}"))
add(CommunityDto(name = "篮球社"))
add(CommunityDto(name = "足球社"))
add(CommunityDto(name = "桌游社"))
add(CommunityDto(name = "乒乓球社"))
} }
} }
Logger.i("初始化社团列表size=${_communityList.value?.size}")
Logger.i("初始化社团size=${_communityList.value?.size}")
} }
} }
/** /**
* 加载更多社团列表 * 加载更多社团列表
* *
*/ */
fun addMore(){ fun addMore(){
viewModelScope.launch { viewModelScope.launch {
Logger.i("加载更多") val c = _communityList.value
_communityList.value?.apply {
add(CommunityDto(name = "羽毛球社")) if(c!=null){
add(CommunityDto(name = "篮球社")) val t=mutableListOf<CommunityDto>()
add(CommunityDto(name = "足球社")) t.addAll(c)
add(CommunityDto(name = "桌游社")) t.apply {
add(CommunityDto(name = "乒乓球社")) repeat(10
) {
add(CommunityDto(name = "社团${t.size}"))
} }
} }
Logger.i("t.size=${t.size}")
_communityList.postValue(t)
Logger.i("加载更多社团size=${_communityList.value?.size}")
_snackBarMsg.value="成功加载更多社团"
}
}
}
fun reset(){
_snackBarMsg.value=""
} }
} }
Loading…
Cancel
Save