-
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.
- feat: Implement search videos Pixabay API of the videolibray module
- Loading branch information
1 parent
0765fbf
commit 42f57ac
Showing
24 changed files
with
430 additions
and
4 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
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
67 changes: 67 additions & 0 deletions
67
core/data/src/main/java/com/wei/picquest/core/data/model/VideoDetail.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.NetworkVideoDetail | ||
import com.wei.picquest.core.network.model.NetworkVideoDetailSize | ||
import com.wei.picquest.core.network.model.NetworkVideoStreams | ||
|
||
data class VideoDetail( | ||
val id: Int, | ||
val pageURL: String, | ||
val type: String, | ||
val tags: String, | ||
val duration: Int, | ||
val pictureId: String, | ||
val videos: VideoStreams, | ||
val views: Int, | ||
val downloads: Int, | ||
val likes: Int, | ||
val comments: Int, | ||
val userId: Int, | ||
val user: String, | ||
val userImageURL: String, | ||
) | ||
|
||
data class VideoStreams( | ||
val large: VideoDetailSize, | ||
val medium: VideoDetailSize, | ||
val small: VideoDetailSize, | ||
val tiny: VideoDetailSize, | ||
) | ||
|
||
data class VideoDetailSize( | ||
val url: String, | ||
val width: Int, | ||
val height: Int, | ||
val size: Long, | ||
) | ||
|
||
fun NetworkVideoDetail.asExternalModel() = VideoDetail( | ||
id = this.id, | ||
pageURL = this.pageURL, | ||
type = this.type, | ||
tags = this.tags, | ||
duration = this.duration, | ||
pictureId = this.pictureId, | ||
videos = this.videos.asExternalModel(), | ||
views = this.views, | ||
downloads = this.downloads, | ||
likes = this.likes, | ||
comments = this.comments, | ||
userId = this.userId, | ||
user = this.user, | ||
userImageURL = this.userImageURL, | ||
) | ||
|
||
fun NetworkVideoStreams.asExternalModel() = VideoStreams( | ||
large = this.large.asExternalModel(), | ||
medium = this.medium.asExternalModel(), | ||
small = this.small.asExternalModel(), | ||
tiny = this.tiny.asExternalModel(), | ||
) | ||
|
||
fun NetworkVideoDetailSize.asExternalModel() = VideoDetailSize( | ||
url = this.url, | ||
width = this.width, | ||
height = this.height, | ||
size = this.size, | ||
) |
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
31 changes: 31 additions & 0 deletions
31
.../data/src/main/java/com/wei/picquest/core/data/repository/DefaultSearchVideoRepository.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.wei.picquest.core.data.repository | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import androidx.paging.map | ||
import com.wei.picquest.core.data.model.VideoDetail | ||
import com.wei.picquest.core.data.model.asExternalModel | ||
import com.wei.picquest.core.network.PqNetworkDataSource | ||
import com.wei.picquest.core.network.pagingsource.PixabayVideoPagingSource | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
class DefaultSearchVideoRepository @Inject constructor( | ||
private val pqNetworkDataSource: PqNetworkDataSource, | ||
) : SearchVideoRepository { | ||
|
||
override suspend fun getSearchVideo(query: String): Flow<PagingData<VideoDetail>> { | ||
return Pager( | ||
config = PagingConfig( | ||
pageSize = 20, | ||
prefetchDistance = 5, | ||
enablePlaceholders = false, | ||
), | ||
pagingSourceFactory = { PixabayVideoPagingSource(pqNetworkDataSource, query) }, | ||
).flow.map { pagingData -> | ||
pagingData.map { it.asExternalModel() } | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
core/data/src/main/java/com/wei/picquest/core/data/repository/SearchVideoRepository.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 androidx.paging.PagingData | ||
import com.wei.picquest.core.data.model.VideoDetail | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SearchVideoRepository { | ||
|
||
suspend fun getSearchVideo(query: String): Flow<PagingData<VideoDetail>> | ||
} |
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
3 changes: 3 additions & 0 deletions
3
core/network/src/main/java/com/wei/picquest/core/network/PqNetworkDataSource.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,11 +1,14 @@ | ||
package com.wei.picquest.core.network | ||
|
||
import com.wei.picquest.core.network.model.NetworkSearchImages | ||
import com.wei.picquest.core.network.model.NetworkSearchVideos | ||
|
||
/** | ||
* Interface representing network calls to the PicQuest backend | ||
*/ | ||
interface PqNetworkDataSource { | ||
|
||
suspend fun searchImages(query: String, page: Int, perPage: Int): NetworkSearchImages | ||
|
||
suspend fun searchVideos(query: String, page: Int, perPage: Int): NetworkSearchVideos | ||
} |
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
73 changes: 73 additions & 0 deletions
73
core/network/src/main/java/com/wei/picquest/core/network/model/NetworkSearchVideos.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,73 @@ | ||
package com.wei.picquest.core.network.model | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* Network representation of [NetworkSearchVideos] when fetched from /videos/ | ||
*/ | ||
@Serializable | ||
data class NetworkSearchVideos( | ||
@SerialName("total") | ||
val total: Int, | ||
@SerialName("totalHits") | ||
val totalHits: Int, | ||
@SerialName("hits") | ||
val hits: List<NetworkVideoDetail>, | ||
) | ||
|
||
@Serializable | ||
data class NetworkVideoDetail( | ||
@SerialName("id") | ||
val id: Int, | ||
@SerialName("pageURL") | ||
val pageURL: String, | ||
@SerialName("type") | ||
val type: String, | ||
@SerialName("tags") | ||
val tags: String, | ||
@SerialName("duration") | ||
val duration: Int, | ||
@SerialName("picture_id") | ||
val pictureId: String, | ||
@SerialName("videos") | ||
val videos: NetworkVideoStreams, | ||
@SerialName("views") | ||
val views: Int, | ||
@SerialName("downloads") | ||
val downloads: Int, | ||
@SerialName("likes") | ||
val likes: Int, | ||
@SerialName("comments") | ||
val comments: Int, | ||
@SerialName("user_id") | ||
val userId: Int, | ||
@SerialName("user") | ||
val user: String, | ||
@SerialName("userImageURL") | ||
val userImageURL: String, | ||
) | ||
|
||
@Serializable | ||
data class NetworkVideoStreams( | ||
@SerialName("large") | ||
val large: NetworkVideoDetailSize, | ||
@SerialName("medium") | ||
val medium: NetworkVideoDetailSize, | ||
@SerialName("small") | ||
val small: NetworkVideoDetailSize, | ||
@SerialName("tiny") | ||
val tiny: NetworkVideoDetailSize, | ||
) | ||
|
||
@Serializable | ||
data class NetworkVideoDetailSize( | ||
@SerialName("url") | ||
val url: String, | ||
@SerialName("width") | ||
val width: Int, | ||
@SerialName("height") | ||
val height: Int, | ||
@SerialName("size") | ||
val size: Long, | ||
) |
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
...work/src/main/java/com/wei/picquest/core/network/pagingsource/PixabayVideoPagingSource.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.NetworkVideoDetail | ||
|
||
class PixabayVideoPagingSource( | ||
private val pqNetworkDataSource: PqNetworkDataSource, | ||
private val query: String, | ||
) : PagingSource<Int, NetworkVideoDetail>() { | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NetworkVideoDetail> { | ||
try { | ||
val currentPage = params.key ?: 1 | ||
val response = pqNetworkDataSource.searchVideos( | ||
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, NetworkVideoDetail>): Int? { | ||
return state.anchorPosition | ||
} | ||
} |
Oops, something went wrong.