-
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.
chore: Implement functionality of the data module
- Loading branch information
1 parent
3f01dd9
commit 50d7bd6
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
core/data/src/main/java/com/wei/picquest/core/data/model/SearchImages.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,67 @@ | ||
package com.wei.picquest.core.data.model | ||
|
||
import com.wei.picquest.core.network.model.NetworkSearchImages | ||
|
||
data class SearchImages( | ||
val total: Int, | ||
val totalHits: Int, | ||
val images: List<ImageDetail> | ||
) | ||
|
||
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 fullHDURL: String, | ||
val imageURL: 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 | ||
) | ||
|
||
fun NetworkSearchImages.asExternalModel() = SearchImages( | ||
total = this.total, | ||
totalHits = this.totalHits, | ||
images = this.hits.map { networkImageDetail -> | ||
ImageDetail( | ||
id = networkImageDetail.id, | ||
pageURL = networkImageDetail.pageURL, | ||
type = networkImageDetail.type, | ||
tags = networkImageDetail.tags, | ||
previewURL = networkImageDetail.previewURL, | ||
previewWidth = networkImageDetail.previewWidth, | ||
previewHeight = networkImageDetail.previewHeight, | ||
webformatURL = networkImageDetail.webformatURL, | ||
webformatWidth = networkImageDetail.webformatWidth, | ||
webformatHeight = networkImageDetail.webformatHeight, | ||
largeImageURL = networkImageDetail.largeImageURL, | ||
fullHDURL = networkImageDetail.fullHDURL, | ||
imageURL = networkImageDetail.imageURL, | ||
imageWidth = networkImageDetail.imageWidth, | ||
imageHeight = networkImageDetail.imageHeight, | ||
imageSize = networkImageDetail.imageSize, | ||
views = networkImageDetail.views, | ||
downloads = networkImageDetail.downloads, | ||
likes = networkImageDetail.likes, | ||
comments = networkImageDetail.comments, | ||
userId = networkImageDetail.userId, | ||
user = networkImageDetail.user, | ||
userImageURL = networkImageDetail.userImageURL | ||
) | ||
} | ||
) |
37 changes: 37 additions & 0 deletions
37
...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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.wei.picquest.core.data.repository | ||
|
||
import com.wei.picquest.core.network.Dispatcher | ||
import com.wei.picquest.core.network.PqDispatchers | ||
import com.wei.picquest.core.network.PqNetworkDataSource | ||
import com.wei.picquest.core.network.model.NetworkSearchImages | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.withContext | ||
import java.io.PrintWriter | ||
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, | ||
) : 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)) | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.wei.picquest.core.data.repository | ||
|
||
import com.wei.picquest.core.network.model.NetworkSearchImages | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
|
||
interface SearchImagesRepository { | ||
|
||
suspend fun getSearchImages(query: String): Flow<NetworkSearchImages> | ||
} |