@@ -31,21 +31,21 @@ class GroupMakeRoomViewModel @Inject constructor(
3131
3232 private val _uiState = MutableStateFlow (GroupMakeRoomUiState ())
3333 val uiState: StateFlow <GroupMakeRoomUiState > = _uiState .asStateFlow()
34-
34+
3535 private var searchJob: Job ? = null
36-
36+
3737 companion object {
3838 private val DATE_FORMATTER = DateTimeFormatter .ofPattern(" yyyy.MM.dd" )
3939 }
40-
40+
4141 private fun updateState (update : (GroupMakeRoomUiState ) -> GroupMakeRoomUiState ) {
4242 _uiState .value = update(_uiState .value)
4343 }
44-
44+
4545 init {
4646 loadGenres()
4747 }
48-
48+
4949 private fun loadGenres () {
5050 viewModelScope.launch {
5151 roomsRepository.getGenres()
@@ -58,19 +58,19 @@ class GroupMakeRoomViewModel @Inject constructor(
5858 fun selectBook (book : BookData ) {
5959 updateState { it.copy(selectedBook = book) }
6060 }
61-
61+
6262 fun setPreselectedBook (isbn : String , title : String , imageUrl : String , author : String ) {
6363 val preselectedBook = BookData (
6464 title = title,
6565 imageUrl = imageUrl,
6666 author = author,
6767 isbn = isbn
6868 )
69- updateState {
69+ updateState {
7070 it.copy(
7171 selectedBook = preselectedBook,
7272 isBookPreselected = true
73- )
73+ )
7474 }
7575 }
7676
@@ -80,21 +80,27 @@ class GroupMakeRoomViewModel @Inject constructor(
8080 loadBooks()
8181 }
8282 }
83-
83+
8484 private fun loadBooks () {
8585 viewModelScope.launch {
8686 updateState { it.copy(isLoadingBooks = true ) }
8787 try {
8888 val savedBooksResult = bookRepository.getBooks(" SAVED" )
8989 savedBooksResult.onSuccess { response ->
90- updateState { it.copy(savedBooks = response?.bookList?.map { dto -> dto.toBookData() } ? : emptyList()) }
90+ updateState {
91+ it.copy(savedBooks = response?.bookList?.map { dto -> dto.toBookData() }
92+ ? : emptyList())
93+ }
9194 }.onFailure {
9295 updateState { it.copy(savedBooks = emptyList()) }
9396 }
9497
9598 val groupBooksResult = bookRepository.getBooks(" JOINING" )
9699 groupBooksResult.onSuccess { response ->
97- updateState { it.copy(groupBooks = response?.bookList?.map { dto -> dto.toBookData() } ? : emptyList()) }
100+ updateState {
101+ it.copy(groupBooks = response?.bookList?.map { dto -> dto.toBookData() }
102+ ? : emptyList())
103+ }
98104 }.onFailure {
99105 updateState { it.copy(groupBooks = emptyList()) }
100106 }
@@ -105,7 +111,7 @@ class GroupMakeRoomViewModel @Inject constructor(
105111 }
106112 }
107113 }
108-
114+
109115 private fun BookSavedResponse.toBookData (): BookData {
110116 return BookData (
111117 title = this .bookTitle,
@@ -114,7 +120,7 @@ class GroupMakeRoomViewModel @Inject constructor(
114120 isbn = this .isbn
115121 )
116122 }
117-
123+
118124 private fun BookSearchItem.toBookData (): BookData {
119125 return BookData (
120126 title = this .title,
@@ -123,29 +129,47 @@ class GroupMakeRoomViewModel @Inject constructor(
123129 isbn = this .isbn
124130 )
125131 }
126-
132+
127133 fun searchBooks (query : String ) {
128134 searchJob?.cancel()
129-
135+
130136 if (query.isBlank()) {
131137 updateState { it.copy(searchResults = emptyList(), isSearching = false ) }
132138 return
133139 }
134-
140+
135141 searchJob = viewModelScope.launch {
136142 delay(300 ) // 디바운싱
137143 updateState { it.copy(isSearching = true ) }
138-
144+
139145 try {
140146 val result = bookRepository.searchBooks(query, page = 1 , isFinalized = false )
141147 result.onSuccess { response ->
142- val searchResults = response?.searchResult?.map { it.toBookData() } ? : emptyList()
143- updateState { it.copy(searchResults = searchResults, isSearching = false ) }
148+ val searchResults =
149+ response?.searchResult?.map {
150+ it.toBookData()
151+ } ? : emptyList()
152+ updateState {
153+ it.copy(
154+ searchResults = searchResults,
155+ isSearching = false
156+ )
157+ }
144158 }.onFailure {
145- updateState { it.copy(searchResults = emptyList(), isSearching = false ) }
159+ updateState {
160+ it.copy(
161+ searchResults = emptyList(),
162+ isSearching = false
163+ )
164+ }
146165 }
147166 } catch (e: Exception ) {
148- updateState { it.copy(searchResults = emptyList(), isSearching = false ) }
167+ updateState {
168+ it.copy(
169+ searchResults = emptyList(),
170+ isSearching = false
171+ )
172+ }
149173 }
150174 }
151175 }
@@ -163,7 +187,7 @@ class GroupMakeRoomViewModel @Inject constructor(
163187 }
164188
165189 fun setDateRange (startDate : LocalDate , endDate : LocalDate ) {
166- updateState {
190+ updateState {
167191 it.copy(
168192 meetingStartDate = startDate,
169193 meetingEndDate = endDate
@@ -176,7 +200,7 @@ class GroupMakeRoomViewModel @Inject constructor(
176200 }
177201
178202 fun togglePrivate (isPrivate : Boolean ) {
179- updateState {
203+ updateState {
180204 it.copy(
181205 isPrivate = isPrivate,
182206 password = if (! isPrivate) " " else it.password
@@ -222,16 +246,26 @@ class GroupMakeRoomViewModel @Inject constructor(
222246 result.onSuccess { roomId ->
223247 onSuccess(roomId)
224248 }.onFailure { exception ->
225- onError(stringResourceProvider.getString(R .string.error_room_creation_failed, exception.message ? : " " ))
249+ onError(
250+ stringResourceProvider.getString(
251+ R .string.error_room_creation_failed,
252+ exception.message ? : " "
253+ )
254+ )
226255 }
227256 } catch (e: Exception ) {
228- onError(stringResourceProvider.getString(R .string.error_network_error, e.message ? : " " ))
257+ onError(
258+ stringResourceProvider.getString(
259+ R .string.error_network_error,
260+ e.message ? : " "
261+ )
262+ )
229263 } finally {
230264 updateState { it.copy(isLoading = false ) }
231265 }
232266 }
233267 }
234-
268+
235269 private fun getApiCategoryName (genreIndex : Int ): String {
236270 val currentGenres = uiState.value.genres
237271 if (genreIndex >= 0 && genreIndex < currentGenres.size) {
0 commit comments