-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
238 additions
and
124 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/easyhz/picly/data/data_source/album/AlbumPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.easyhz.picly.data.data_source.album | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.easyhz.picly.data.entity.album.Album | ||
import com.easyhz.picly.data.mapper.toAlbumItem | ||
import com.easyhz.picly.domain.model.album.AlbumItem | ||
import com.easyhz.picly.domain.repository.album.AlbumRepository | ||
import com.google.firebase.firestore.QuerySnapshot | ||
import kotlinx.coroutines.tasks.await | ||
|
||
class AlbumPagingSource( | ||
private val albumRepository: AlbumRepository | ||
): PagingSource<QuerySnapshot, AlbumItem>() { | ||
override fun getRefreshKey(state: PagingState<QuerySnapshot, AlbumItem>): QuerySnapshot? = null | ||
|
||
override suspend fun load(params: LoadParams<QuerySnapshot>): LoadResult<QuerySnapshot, AlbumItem> = try { | ||
val current = params.key ?: albumRepository.fetchAlbums().get().await() | ||
val data = current.toObjects(Album::class.java).toAlbumItem() | ||
if (data.isEmpty()) { | ||
LoadResult.Page(data = emptyList(), prevKey = null, nextKey = null) | ||
} else { | ||
val lastVisibleProduct = current.documents.last() | ||
val nextPage = albumRepository.fetchAlbums().startAfter(lastVisibleProduct).get().await() | ||
|
||
LoadResult.Page(data = data, prevKey = null, nextKey = nextPage) | ||
} | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/easyhz/picly/data/data_source/album/SearchAlbumPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.easyhz.picly.data.data_source.album | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.easyhz.picly.data.entity.album.Album | ||
import com.easyhz.picly.data.mapper.toAlbumItem | ||
import com.easyhz.picly.domain.model.album.AlbumItem | ||
import com.easyhz.picly.domain.repository.album.AlbumRepository | ||
import com.google.firebase.firestore.QuerySnapshot | ||
import kotlinx.coroutines.tasks.await | ||
|
||
class SearchAlbumPagingSource( | ||
private val albumRepository: AlbumRepository, | ||
private val searchText: String, | ||
): PagingSource<QuerySnapshot, AlbumItem>() { | ||
override fun getRefreshKey(state: PagingState<QuerySnapshot, AlbumItem>): QuerySnapshot? = null | ||
|
||
override suspend fun load(params: LoadParams<QuerySnapshot>): LoadResult<QuerySnapshot, AlbumItem> = try { | ||
val current = params.key ?: albumRepository.searchAlbums(searchText).get().await() | ||
val data = current.toObjects(Album::class.java).toAlbumItem() | ||
if (data.isEmpty()) { | ||
LoadResult.Page(data = emptyList(), prevKey = null, nextKey = null) | ||
} else { | ||
val lastVisibleProduct = current.documents.last() | ||
val nextPage = albumRepository.searchAlbums(searchText).startAfter(lastVisibleProduct).get().await() | ||
|
||
LoadResult.Page(data = data, prevKey = null, nextKey = nextPage) | ||
} | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
app/src/main/java/com/easyhz/picly/domain/usecase/album/AlbumUseCase.kt
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/easyhz/picly/domain/usecase/album/FetchAlbumUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.easyhz.picly.domain.usecase.album | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import com.easyhz.picly.data.data_source.album.AlbumPagingSource | ||
import com.easyhz.picly.data.repository.album.AlbumRepositoryImpl.Companion.PAGE_SIZE | ||
import com.easyhz.picly.domain.repository.album.AlbumRepository | ||
import javax.inject.Inject | ||
|
||
class FetchAlbumUseCase | ||
@Inject constructor( | ||
private val repository: AlbumRepository | ||
) { | ||
private var pagingSource: AlbumPagingSource? = null | ||
get() { | ||
if (field == null || field?.invalid == true) { | ||
field = AlbumPagingSource(repository) | ||
} | ||
return field | ||
} | ||
operator fun invoke() = Pager( | ||
config = PagingConfig( | ||
pageSize = PAGE_SIZE.toInt() | ||
), | ||
pagingSourceFactory = { | ||
pagingSource!! | ||
} | ||
) | ||
|
||
fun setPagingSource() { | ||
pagingSource?.invalidate() | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/easyhz/picly/domain/usecase/album/SearchAlbumUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.easyhz.picly.domain.usecase.album | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import com.easyhz.picly.data.data_source.album.SearchAlbumPagingSource | ||
import com.easyhz.picly.data.repository.album.AlbumRepositoryImpl.Companion.PAGE_SIZE | ||
import com.easyhz.picly.domain.repository.album.AlbumRepository | ||
import javax.inject.Inject | ||
|
||
class SearchAlbumUseCase | ||
@Inject constructor( | ||
private val repository: AlbumRepository | ||
) { | ||
var searchText: String = "" | ||
private var pagingSource: SearchAlbumPagingSource? = null | ||
get() { | ||
if (field == null || field?.invalid == true) { | ||
field = SearchAlbumPagingSource(repository, searchText) | ||
} | ||
return field | ||
} | ||
operator fun invoke() = Pager( | ||
config = PagingConfig( | ||
pageSize = PAGE_SIZE.toInt() | ||
), | ||
pagingSourceFactory = { | ||
pagingSource!! | ||
} | ||
) | ||
|
||
fun setPagingSource() { | ||
pagingSource?.invalidate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.