From 2d8df82230fda2a2b7d930fe38032e39121039bd Mon Sep 17 00:00:00 2001 From: TaejungKim Date: Fri, 13 Dec 2024 17:20:41 +0900 Subject: [PATCH] =?UTF-8?q?feat/#30:=20UnivListViewModel=20=ED=95=9C?= =?UTF-8?q?=EA=B8=80=20=EC=A0=95=EB=A0=AC=20=ED=95=A8=EC=88=98=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/calendar/UnivListScreen.kt | 74 +++++++++++++------ .../calendar/UnivListViewModel.kt | 60 +++++++++++++++ 2 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListViewModel.kt diff --git a/app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListScreen.kt b/app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListScreen.kt index 76448bb..303e299 100644 --- a/app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListScreen.kt +++ b/app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListScreen.kt @@ -2,30 +2,38 @@ package com.example.togedy_android.presentation.calendar import androidx.compose.foundation.background import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.* +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.pager.HorizontalPager import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp +import androidx.hilt.navigation.compose.hiltViewModel +import com.example.togedy_android.R import com.example.togedy_android.core.design_system.component.TopBarBasic +import com.example.togedy_android.core.design_system.component.WritingContentTextField import com.example.togedy_android.core.design_system.theme.TogedyTheme import kotlinx.coroutines.launch -import com.example.togedy_android.R -import com.example.togedy_android.core.design_system.component.WritingContentTextField @Composable -fun UnivListScreen() { +fun UnivListScreen( + viewModel: UnivListViewModel = hiltViewModel() +) { val pagerState = rememberPagerState { 2 } val tabTitles = listOf("전체", "저장한 대학") val coroutineScope = rememberCoroutineScope() + val allUnivGroups = viewModel.univGroups.collectAsState().value + val savedUnivGroups = viewModel.savedUnivGroups.collectAsState().value Column( modifier = Modifier @@ -65,25 +73,49 @@ fun UnivListScreen() { ) HorizontalPager(state = pagerState) { page -> - Box( - modifier = Modifier - .fillMaxSize() - .background( - when (page) { - 0 -> Color.Red - 1 -> Color.Green - else -> Color.Gray - } - ), - contentAlignment = Alignment.Center - ) { - Text(text = "Page: $page", color = Color.White, fontSize = 24.sp) + when (page) { + 0 -> UnivListPager(univGroups = allUnivGroups) + 1 -> UnivListPager(univGroups = savedUnivGroups) + } + } + } +} + +@Composable +fun UnivListPager(univGroups: Map>) { + LazyColumn( + modifier = Modifier + .fillMaxSize() + .padding(vertical = 8.dp) + ) { + univGroups.forEach { (initial, universities) -> + item { + Text( + text = initial.toString(), + style = TogedyTheme.typography.headline3B, + color = TogedyTheme.colors.black, + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 4.dp) + ) + } + + itemsIndexed(universities) { _, university -> + Text( + text = university, + style = TogedyTheme.typography.body2R, + color = TogedyTheme.colors.gray500, + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 2.dp) + ) } } } } + @Preview @Composable fun UnivListScreenPreview() { diff --git a/app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListViewModel.kt b/app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListViewModel.kt new file mode 100644 index 0000000..70da289 --- /dev/null +++ b/app/src/main/java/com/example/togedy_android/presentation/calendar/UnivListViewModel.kt @@ -0,0 +1,60 @@ +package com.example.togedy_android.presentation.calendar + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch +import javax.inject.Inject + +@HiltViewModel +class UnivListViewModel @Inject constructor( + +): ViewModel() { + private val allUniversities = listOf( + "경희대학교", "고려대학교", "가톨릭대학교", + "나사렛대학교", "남서울대학교", + "단국대학교", "덕성여자대학교", + "서울대학교", "성균관대학교", "숙명여자대학교", + "연세대학교", "이화여자대학교", + "중앙대학교", "전남대학교", + "충북대학교", "청주대학교", + "한국외국어대학교", "한양대학교", + "홍익대학교" + ) + private val savedUniversities = listOf("고려대학교", "연세대학교", "한양대학교") + + private val _univGroups = MutableStateFlow>>(emptyMap()) + val univGroups: StateFlow>> = _univGroups + + private val _savedUnivGroups = MutableStateFlow>>(emptyMap()) + val savedUnivGroups: StateFlow>> = _savedUnivGroups + + init { + viewModelScope.launch { + _univGroups.value = groupUniversitiesByInitial(allUniversities) + _savedUnivGroups.value = groupUniversitiesByInitial(savedUniversities) + } + } + + private fun groupUniversitiesByInitial(universities: List): Map> { + val initials = listOf( + 'ㄱ', 'ㄴ', 'ㄷ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅅ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' + ) + + return universities + .groupBy { university -> + val char = university.first() + if (char in '가'..'힣') { + val unicode = char.code - 0xAC00 + val initialIndex = unicode / (21 * 28) + initials.getOrNull(initialIndex) ?: '?' + } else { + '?' + } + } + .filterKeys { it in initials } // 초성 범위 외 값 필터링 + .toSortedMap() + } +} \ No newline at end of file