-
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.
Merge pull request #8 from azrael8576/feat/photo-library
- Loading branch information
Showing
26 changed files
with
706 additions
and
100 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
core/data/src/main/java/com/wei/picquest/core/data/model/ImageDetail.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,53 @@ | ||
package com.wei.picquest.core.data.model | ||
|
||
import com.wei.picquest.core.network.model.NetworkImageDetail | ||
|
||
data class ImageDetail( | ||
val id: Int, | ||
val pageURL: String, | ||
val type: String, | ||
val tags: String, | ||
val previewURL: String, | ||
val previewWidth: Int, | ||
val previewHeight: Int, | ||
val webformatURL: String, | ||
val webformatWidth: Int, | ||
val webformatHeight: Int, | ||
val largeImageURL: String, | ||
val imageWidth: Int, | ||
val imageHeight: Int, | ||
val imageSize: Long, | ||
val views: Int, | ||
val downloads: Int, | ||
val likes: Int, | ||
val comments: Int, | ||
val userId: Int, | ||
val user: String, | ||
val userImageURL: String, | ||
) { | ||
val aspectRatio get() = imageWidth.toFloat() / imageHeight.toFloat() | ||
} | ||
|
||
fun NetworkImageDetail.asExternalModel() = ImageDetail( | ||
id = this.id, | ||
pageURL = this.pageURL, | ||
type = this.type, | ||
tags = this.tags, | ||
previewURL = this.previewURL, | ||
previewWidth = this.previewWidth, | ||
previewHeight = this.previewHeight, | ||
webformatURL = this.webformatURL, | ||
webformatWidth = this.webformatWidth, | ||
webformatHeight = this.webformatHeight, | ||
largeImageURL = this.largeImageURL, | ||
imageWidth = this.imageWidth, | ||
imageHeight = this.imageHeight, | ||
imageSize = this.imageSize, | ||
views = this.views, | ||
downloads = this.downloads, | ||
likes = this.likes, | ||
comments = this.comments, | ||
userId = this.userId, | ||
user = this.user, | ||
userImageURL = this.userImageURL, | ||
) |
63 changes: 0 additions & 63 deletions
63
core/data/src/main/java/com/wei/picquest/core/data/model/SearchImages.kt
This file was deleted.
Oops, something went wrong.
42 changes: 19 additions & 23 deletions
42
...data/src/main/java/com/wei/picquest/core/data/repository/DefaultSearchImagesRepository.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 |
---|---|---|
@@ -1,35 +1,31 @@ | ||
package com.wei.picquest.core.data.repository | ||
|
||
import com.wei.picquest.core.network.Dispatcher | ||
import com.wei.picquest.core.network.PqDispatchers | ||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import androidx.paging.map | ||
import com.wei.picquest.core.data.model.ImageDetail | ||
import com.wei.picquest.core.data.model.asExternalModel | ||
import com.wei.picquest.core.network.PqNetworkDataSource | ||
import com.wei.picquest.core.network.model.NetworkSearchImages | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import com.wei.picquest.core.network.pagingsource.PixabayPagingSource | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Implementation of the [SearchImagesRepository]. | ||
* @param ioDispatcher 用於執行 IO 相關操作的 CoroutineDispatcher。 | ||
* @param network 數據源的網路接口。 | ||
*/ | ||
class DefaultSearchImagesRepository @Inject constructor( | ||
@Dispatcher(PqDispatchers.IO) private val ioDispatcher: CoroutineDispatcher, | ||
private val network: PqNetworkDataSource, | ||
private val pqNetworkDataSource: PqNetworkDataSource, | ||
) : SearchImagesRepository { | ||
|
||
/** | ||
* @param query。A URL encoded search term. If omitted, all images are returned. This value may not exceed 100 characters. | ||
* Example: "yellow+flower" | ||
* @return 一個 Flow,內容為 Search Images 的數據。 | ||
*/ | ||
override suspend fun getSearchImages( | ||
query: String, | ||
): Flow<NetworkSearchImages> = withContext(ioDispatcher) { | ||
flow { | ||
emit(network.searchImages(query)) | ||
override suspend fun getSearchImages(query: String): Flow<PagingData<ImageDetail>> { | ||
return Pager( | ||
config = PagingConfig( | ||
pageSize = 20, | ||
prefetchDistance = 5, | ||
enablePlaceholders = false, | ||
), | ||
pagingSourceFactory = { PixabayPagingSource(pqNetworkDataSource, query) }, | ||
).flow.map { pagingData -> | ||
pagingData.map { it.asExternalModel() } | ||
} | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
core/data/src/main/java/com/wei/picquest/core/data/repository/SearchImagesRepository.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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package com.wei.picquest.core.data.repository | ||
|
||
import com.wei.picquest.core.network.model.NetworkSearchImages | ||
import androidx.paging.PagingData | ||
import com.wei.picquest.core.data.model.ImageDetail | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SearchImagesRepository { | ||
|
||
suspend fun getSearchImages(query: String): Flow<NetworkSearchImages> | ||
suspend fun getSearchImages(query: String): Flow<PagingData<ImageDetail>> | ||
} |
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
37 changes: 37 additions & 0 deletions
37
core/network/src/main/java/com/wei/picquest/core/network/pagingsource/PixabayPagingSource.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,37 @@ | ||
package com.wei.picquest.core.network.pagingsource | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.wei.picquest.core.network.PqNetworkDataSource | ||
import com.wei.picquest.core.network.model.NetworkImageDetail | ||
|
||
class PixabayPagingSource( | ||
private val pqNetworkDataSource: PqNetworkDataSource, | ||
private val query: String, | ||
) : PagingSource<Int, NetworkImageDetail>() { | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NetworkImageDetail> { | ||
try { | ||
val currentPage = params.key ?: 1 | ||
val response = pqNetworkDataSource.searchImages( | ||
query = query, | ||
page = currentPage, | ||
perPage = 20, | ||
) | ||
|
||
val endOfPaginationReached = response.hits.isEmpty() | ||
|
||
return LoadResult.Page( | ||
data = response.hits, | ||
prevKey = if (currentPage == 1) null else currentPage - 1, | ||
nextKey = if (endOfPaginationReached) null else currentPage + 1, | ||
) | ||
} catch (exception: Exception) { | ||
return LoadResult.Error(exception) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, NetworkImageDetail>): Int? { | ||
return state.anchorPosition | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,12 @@ | ||
plugins { | ||
alias(libs.plugins.pq.android.feature) | ||
alias(libs.plugins.pq.android.library.compose) | ||
alias(libs.plugins.pq.android.hilt) | ||
} | ||
|
||
android { | ||
namespace = "com.wei.picquest.feature.photolibrary" | ||
} | ||
|
||
dependencies { | ||
} |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
Oops, something went wrong.