Skip to content

Commit

Permalink
Merge pull request boostcampwm-2022#163 from eoeo0326/feat/search_com…
Browse files Browse the repository at this point in the history
…pose_paging

검색 Compose Paging 적용
  • Loading branch information
junhyeongleeee authored Jul 24, 2023
2 parents 63ebe2c + 984f062 commit 447c9ea
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 702 deletions.
4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
</activity>
<activity
android:name=".presentation.searchfilm.SearchFilmComposeActivity"
android:exported="false"
android:theme="@style/Theme.DailyFilm" />
<activity
android:name=".presentation.searchfilm.SearchFilmActivity"
android:exported="false" />
<activity
android:name=".presentation.settings.SettingsActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ import kotlinx.coroutines.flow.Flow
interface CalendarDao {
@Query(
"SELECT * FROM film_entity " +
"WHERE updateDate BETWEEN :startAt AND :endAt "
"WHERE updateDate BETWEEN :startAt AND :endAt ",
)
fun loadFilmFlow(startAt: Int, endAt: Int): Flow<List<FilmEntity?>>

@Query(
"SELECT * FROM film_entity " +
"WHERE updateDate BETWEEN :startAt AND :endAt "
"WHERE updateDate BETWEEN :startAt AND :endAt ",
)
suspend fun loadFilm(startAt: Int, endAt: Int): List<FilmEntity?>

@Query(
"SELECT * FROM film_entity " +
"WHERE updateDate BETWEEN :startAt AND :endAt " +
"ORDER BY updateDate ASC LIMIT :count OFFSET :page",
)
suspend fun loadPagedFilm(startAt: Int, endAt: Int, page: Int, count: Int = 10): List<FilmEntity?>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(filmEntityList: List<FilmEntity?>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface CalendarDataSource {

suspend fun loadFilm(startAt: Int, endAt: Int): List<FilmEntity?>

suspend fun loadPagedFilm(startAt: Int, endAt: Int, page: Int): List<FilmEntity?>

suspend fun insertFilm(film: FilmEntity)

suspend fun insertAllFilm(filmList: List<FilmEntity>)
Expand All @@ -22,7 +24,7 @@ interface CalendarDataSource {
}

class CalendarLocalDataSource(
private val calendarDao: CalendarDao
private val calendarDao: CalendarDao,
) : CalendarDataSource {

override fun loadFilmFlow(startAt: Int, endAt: Int): Flow<List<FilmEntity?>> =
Expand All @@ -31,6 +33,9 @@ class CalendarLocalDataSource(
override suspend fun loadFilm(startAt: Int, endAt: Int): List<FilmEntity?> =
calendarDao.loadFilm(startAt, endAt)

override suspend fun loadPagedFilm(startAt: Int, endAt: Int, page: Int): List<FilmEntity?> =
calendarDao.loadPagedFilm(startAt, endAt, page)

override suspend fun insertFilm(film: FilmEntity) {
calendarDao.insert(film)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.boostcamp.dailyfilm.data.calendar

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.boostcamp.dailyfilm.data.model.DailyFilmItem

class CalendarPagingSource(
private val startAt: Int,
private val endAt: Int,
private val calendarDataSource: CalendarDataSource,
) : PagingSource<Int, DailyFilmItem>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, DailyFilmItem> {
runCatching {
val page = params.key ?: 0
val data = calendarDataSource.loadPagedFilm(startAt, endAt, page).filterNotNull().map { it.mapToDailyFilmItem() }
return LoadResult.Page(
data = data,
prevKey = if (page == 0) null else shouldPositive(page - PAGING_SIZE),
nextKey = if (data.isEmpty()) null else page + data.size,
)
}.onFailure {
return LoadResult.Error(it)
}
return LoadResult.Error(Exception("Unknown Error in CalendarPagingSource"))
}

override fun getRefreshKey(state: PagingState<Int, DailyFilmItem>): Int? {
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(PAGING_SIZE)
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(PAGING_SIZE)
}
}

private fun shouldPositive(num: Int) = if (num < 0) 0 else num

companion object {
const val PAGING_SIZE = 10
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.boostcamp.dailyfilm.data.calendar

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.boostcamp.dailyfilm.data.calendar.CalendarPagingSource.Companion.PAGING_SIZE
import com.boostcamp.dailyfilm.data.model.DailyFilmItem
import com.boostcamp.dailyfilm.data.model.Result
import kotlinx.coroutines.flow.Flow
Expand All @@ -10,11 +14,13 @@ interface CalendarRepository {

suspend fun loadFilm(startAt: String, endAt: String): List<DailyFilmItem?>

suspend fun loadPagedFilm(startAt: String, endAt: String): Flow<PagingData<DailyFilmItem>>

suspend fun deleteAllData(): Result<Unit>
}

class CalendarRepositoryImpl(
private val calendarLocalDataSource: CalendarDataSource
private val calendarLocalDataSource: CalendarDataSource,
) : CalendarRepository {

override fun loadFilmInfo(startAt: String, endAt: String): Flow<List<DailyFilmItem?>> =
Expand All @@ -29,6 +35,11 @@ class CalendarRepositoryImpl(
filmEntity?.mapToDailyFilmItem()
}

override suspend fun loadPagedFilm(startAt: String, endAt: String): Flow<PagingData<DailyFilmItem>> =
Pager(config = PagingConfig(pageSize = PAGING_SIZE)) {
CalendarPagingSource(startAt.toInt(), endAt.toInt(), calendarLocalDataSource)
}.flow

override suspend fun deleteAllData(): Result<Unit> =
calendarLocalDataSource.deleteAllData()
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 447c9ea

Please sign in to comment.