diff --git a/app/src/main/java/com/gyf/csams/ui/MainActivity.kt b/app/src/main/java/com/gyf/csams/ui/MainActivity.kt index 0ddaaaa..6a18c38 100644 --- a/app/src/main/java/com/gyf/csams/ui/MainActivity.kt +++ b/app/src/main/java/com/gyf/csams/ui/MainActivity.kt @@ -15,6 +15,7 @@ import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier 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.MarqueeViewModel 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) { 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) { Box(modifier = Modifier.fillMaxSize()) { @@ -187,6 +207,8 @@ fun CommunityListBody(model: ListViewModel = viewModel()) { val communityList: MutableList? by model.communityDto.observeAsState() val listState = rememberLazyListState() +// Text(text ="communityList.size=${communityList?.size}") + LazyColumn(state = listState) { communityList?.chunked(2)?.forEach { item{ @@ -212,9 +234,15 @@ fun CommunityListBody(model: ListViewModel = viewModel()) { .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() } } diff --git a/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt b/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt index 6fe125b..846ed67 100644 --- a/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt +++ b/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt @@ -71,17 +71,19 @@ class CarouselViewModel:ViewModel(){ data class CommunityDto(val name:String) class ListViewModel:ViewModel(){ - private val _name=MutableLiveData() + private val _name=MutableLiveData("") val name:LiveData = _name val nameDesc="社团名称" val namePlaceholder="请输入$nameDesc" - private val _desc=MutableLiveData() + private val _desc=MutableLiveData("") val desc:LiveData = _desc val descDesc="社团简介" val descPlaceholder="请输入$descDesc" - + //注册请求响应信息 + private val _snackBarMsg=MutableLiveData() + val snackBarMsg:LiveData = _snackBarMsg //社团列表 private val _communityList=MutableLiveData>(mutableListOf()) @@ -101,6 +103,7 @@ class ListViewModel:ViewModel(){ fun search(){ Logger.i("使用社团名称:${_name.value}和社团简介${_desc.value}搜索社团") + _snackBarMsg.value="搜索失败,请联系管理员" } /** @@ -111,36 +114,48 @@ class ListViewModel:ViewModel(){ viewModelScope.launch { _communityList.value?.apply { - repeat(2 + repeat(10 ) { - add(CommunityDto(name = "羽毛球社")) - add(CommunityDto(name = "篮球社")) - add(CommunityDto(name = "足球社")) - add(CommunityDto(name = "桌游社")) - add(CommunityDto(name = "乒乓球社")) + add(CommunityDto(name = "社团${_communityList.value?.size}")) } } - - Logger.i("初始化社团size=${_communityList.value?.size}") + Logger.i("初始化社团列表size=${_communityList.value?.size}") } } + + /** * 加载更多社团列表 * */ fun addMore(){ viewModelScope.launch { - Logger.i("加载更多") - _communityList.value?.apply { - add(CommunityDto(name = "羽毛球社")) - add(CommunityDto(name = "篮球社")) - add(CommunityDto(name = "足球社")) - add(CommunityDto(name = "桌游社")) - add(CommunityDto(name = "乒乓球社")) + val c = _communityList.value + + if(c!=null){ + val t=mutableListOf() + t.addAll(c) + t.apply { + 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="" } } \ No newline at end of file