Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

선호학과 정보 저장 #366

Merged
merged 14 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/java/com/wafflestudio/snutt2/data/SNUTTStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ class SNUTTStorage @Inject constructor(
),
)

val recentSearchedDepartments = PrefValue<List<TagDto>>(
prefContext,
PrefListValueMetaData(
domain = DOMAIN_SCOPE_LOGIN,
key = "recent_searched_departments",
type = TagDto::class.java,
defaultValue = listOf(),
),
)

fun clearLoginScope() {
prefContext.clear(DOMAIN_SCOPE_LOGIN)
prefContext.clear(DOMAIN_SCOPE_CURRENT_VERSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import com.wafflestudio.snutt2.lib.network.dto.core.LectureBuildingDto
import com.wafflestudio.snutt2.lib.network.dto.core.LectureDto
import com.wafflestudio.snutt2.model.SearchTimeDto
import com.wafflestudio.snutt2.model.TagDto
import com.wafflestudio.snutt2.model.TagType
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

interface LectureSearchRepository {

val recentSearchedDepartments: StateFlow<List<TagDto>>

fun getLectureSearchResultStream(
year: Long,
semester: Long,
Expand All @@ -23,4 +27,6 @@ interface LectureSearchRepository {
suspend fun getBuildings(
places: String,
): List<LectureBuildingDto>

fun storeRecentSearchedDepartment(tag: TagDto)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.wafflestudio.snutt2.data.lecture_search

import android.util.Log
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.wafflestudio.snutt2.data.SNUTTStorage
import com.wafflestudio.snutt2.lib.SnuttUrls
import com.wafflestudio.snutt2.lib.network.SNUTTRestApi
import com.wafflestudio.snutt2.lib.network.dto.core.LectureBuildingDto
Expand All @@ -18,8 +20,11 @@ import javax.inject.Singleton
class LectureSearchRepositoryImpl @Inject constructor(
private val api: SNUTTRestApi,
private val snuttUrls: SnuttUrls,
private val stoarge: SNUTTStorage
) : LectureSearchRepository {

override val recentSearchedDepartments = stoarge.recentSearchedDepartments.asStateFlow()

override fun getLectureSearchResultStream(
year: Long,
semester: Long,
Expand Down Expand Up @@ -66,6 +71,16 @@ class LectureSearchRepositoryImpl @Inject constructor(
return response.content
}

override fun storeRecentSearchedDepartment(tag: TagDto) {
stoarge.recentSearchedDepartments.update(
stoarge.recentSearchedDepartments.get().toMutableList().apply {
if (contains(tag)) remove(tag)
add(tag)
while (count() > 5) removeAt(0)
}
)
}

companion object {
const val LECTURES_LOAD_PAGE_SIZE = 30
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wafflestudio.snutt2.views.logged_in.home.search

import android.util.Log
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.SizeTransform
Expand All @@ -20,6 +21,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -84,6 +86,7 @@ fun SearchPage(
val pageMode by searchViewModel.pageMode.collectAsState()
val firstBookmarkAlert by userViewModel.firstBookmarkAlert.collectAsState()
val draggedTimeBlock = searchViewModel.draggedTimeBlock.collectAsState()
val recentSearchedDepartments by searchViewModel.recentSearchedDepartments.collectAsState()

var searchEditTextFocused by remember { mutableStateOf(false) }
val isDarkMode = isDarkMode()
Expand All @@ -96,6 +99,10 @@ fun SearchPage(
}
}

LaunchedEffect(recentSearchedDepartments) {
Log.d("plgafhdtest",recentSearchedDepartments.toString())
}

BackHandler(pageMode == SearchPageMode.Bookmark) {
searchViewModel.togglePageMode()
}
Expand Down Expand Up @@ -169,6 +176,7 @@ fun SearchPage(
launchSuspendApi(apiOnProgress, apiOnError) {
searchViewModel.query()
}
searchViewModel.storeRecentSearchedDepartments()
}
scope.launch { bottomSheet.hide() }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class SearchViewModel @Inject constructor(
// 검색 쿼리에 들어가는 시간대 검색 시간대 목록
private val _searchTimeList = MutableStateFlow<List<SearchTimeDto>?>(null)

val recentSearchedDepartments: StateFlow<List<TagDto>> = lectureSearchRepository.recentSearchedDepartments

init {
viewModelScope.launch {
semesterChange.distinctUntilChanged().collectLatest {
Expand Down Expand Up @@ -254,6 +256,12 @@ class SearchViewModel @Inject constructor(
}
}

fun storeRecentSearchedDepartments() {
_selectedTags.value.filter { it.type == TagType.DEPARTMENT }.forEach { tag ->
lectureSearchRepository.storeRecentSearchedDepartment(tag)
}
}

private suspend fun clear() {
_searchTitle.emit("")
_selectedLecture.emit(null)
Expand Down