Skip to content

Commit

Permalink
feat/#30: UnivListViewModel 한글 정렬 함수 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaejugkim committed Dec 13, 2024
1 parent 155b295 commit 2d8df82
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Char, List<String>>) {
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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Map<Char, List<String>>>(emptyMap())
val univGroups: StateFlow<Map<Char, List<String>>> = _univGroups

private val _savedUnivGroups = MutableStateFlow<Map<Char, List<String>>>(emptyMap())
val savedUnivGroups: StateFlow<Map<Char, List<String>>> = _savedUnivGroups

init {
viewModelScope.launch {
_univGroups.value = groupUniversitiesByInitial(allUniversities)
_savedUnivGroups.value = groupUniversitiesByInitial(savedUniversities)
}
}

private fun groupUniversitiesByInitial(universities: List<String>): Map<Char, List<String>> {
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()
}
}

0 comments on commit 2d8df82

Please sign in to comment.