Skip to content

Commit 4724a0e

Browse files
committed
Add new features/refactor
- Refactor Bottom app bar (less code) - Topics Screen - Data layer for topics screen
1 parent 5583007 commit 4724a0e

26 files changed

+417
-161
lines changed

app/src/main/java/st/slex/csplashscreen/data/core/Mappers.kt

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import st.slex.csplashscreen.data.model.remote.collection.RemoteLinksCollectionM
55
import st.slex.csplashscreen.data.model.remote.download.RemoteDownloadModel
66
import st.slex.csplashscreen.data.model.remote.image.*
77
import st.slex.csplashscreen.data.model.remote.statistic.*
8+
import st.slex.csplashscreen.data.model.remote.topics.RemotePreviewPhotosModel
9+
import st.slex.csplashscreen.data.model.remote.topics.RemoteTopicsModel
810
import st.slex.csplashscreen.data.model.remote.user.RemoteBadgeModel
911
import st.slex.csplashscreen.data.model.remote.user.RemoteProfileImageModel
1012
import st.slex.csplashscreen.data.model.remote.user.RemoteUserLinksModel
@@ -13,6 +15,8 @@ import st.slex.csplashscreen.data.model.ui.*
1315
import st.slex.csplashscreen.data.model.ui.collection.CollectionModel
1416
import st.slex.csplashscreen.data.model.ui.collection.LinksCollectionModel
1517
import st.slex.csplashscreen.data.model.ui.image.*
18+
import st.slex.csplashscreen.data.model.ui.topics.PreviewPhotosModel
19+
import st.slex.csplashscreen.data.model.ui.topics.TopicsModel
1620
import st.slex.csplashscreen.data.model.ui.user.BadgeModel
1721
import st.slex.csplashscreen.data.model.ui.user.ProfileImageModel
1822
import st.slex.csplashscreen.data.model.ui.user.UserLinksModel
@@ -179,3 +183,31 @@ internal fun RemoteHistorical.toHistorical(): Historical =
179183
internal fun RemoteValue.toValue(): Value = Value(date = date, value = value)
180184

181185
internal fun RemoteDownloadModel.toDownloadModel(): DownloadModel = DownloadModel(url = url)
186+
187+
internal fun RemoteTopicsModel.toTopicsModel(): TopicsModel =
188+
TopicsModel(
189+
id = id.toString(),
190+
slug = slug.toString(),
191+
title = title.toString(),
192+
description = description.toString(),
193+
published_at = published_at.toString(),
194+
updated_at = updated_at.toString(),
195+
starts_at = starts_at.toString(),
196+
ends_at = ends_at.toString(),
197+
only_submissions_after = only_submissions_after.toString(),
198+
featured = featured.toString(),
199+
total_photos = total_photos.toString(),
200+
links = links.toLinksCollectionModel(),
201+
status = status.toString(),
202+
owners = owners?.map { it.toUserModel() },
203+
cover_photo = cover_photo?.toImageModel(),
204+
preview_photos = preview_photos?.map { it.toPreviewPhotosModel() }
205+
)
206+
207+
internal fun RemotePreviewPhotosModel.toPreviewPhotosModel() =
208+
PreviewPhotosModel(
209+
id = id.toString(),
210+
created_at = created_at.toString(),
211+
updated_at = updated_at.toString(),
212+
urls = urls?.toUrlsModel()
213+
)

app/src/main/java/st/slex/csplashscreen/data/model/remote/collection/RemoteLinksCollectionModel.kt

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package st.slex.csplashscreen.data.model.remote.collection
33
import com.google.gson.annotations.SerializedName
44

55
data class RemoteLinksCollectionModel(
6-
@SerializedName("self")
7-
val self: String,
8-
@SerializedName("html")
9-
val html: String,
10-
@SerializedName("photos")
11-
val photos: String
6+
@SerializedName("self") val self: String,
7+
@SerializedName("html") val html: String,
8+
@SerializedName("photos") val photos: String
129
)

app/src/main/java/st/slex/csplashscreen/data/model/remote/image/RemoteExifModel.kt

+6-12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@ package st.slex.csplashscreen.data.model.remote.image
33
import com.google.gson.annotations.SerializedName
44

55
data class RemoteExifModel(
6-
@SerializedName("make")
7-
val make: String?,
8-
@SerializedName("model")
9-
val model: String?,
10-
@SerializedName("exposure_time")
11-
val exposure_time: String?,
12-
@SerializedName("aperture")
13-
val aperture: String?,
14-
@SerializedName("focal_length")
15-
val focal_length: String?,
16-
@SerializedName("iso")
17-
val iso: Int?
6+
@SerializedName("make") val make: String?,
7+
@SerializedName("model") val model: String?,
8+
@SerializedName("exposure_time") val exposure_time: String?,
9+
@SerializedName("aperture") val aperture: String?,
10+
@SerializedName("focal_length") val focal_length: String?,
11+
@SerializedName("iso") val iso: Int?
1812
)

app/src/main/java/st/slex/csplashscreen/data/model/remote/image/RemoteImageModel.kt

+22-44
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,26 @@ import st.slex.csplashscreen.data.model.remote.statistic.RemotePhotoStatistics
66
import st.slex.csplashscreen.data.model.remote.user.RemoteUserModel
77

88
class RemoteImageModel(
9-
@SerializedName("id")
10-
val id: String,
11-
@SerializedName("created_at")
12-
val created_at: String?,
13-
@SerializedName("updated_at")
14-
val updated_at: String?,
15-
@SerializedName("width")
16-
val width: Int?,
17-
@SerializedName("height")
18-
val height: Int?,
19-
@SerializedName("color")
20-
val color: String? = "#E0E0E0",
21-
@SerializedName("blur_hash")
22-
val blur_hash: String?,
23-
@SerializedName("views")
24-
val views: Int?,
25-
@SerializedName("downloads")
26-
val downloads: Int?,
27-
@SerializedName("likes")
28-
val likes: Int?,
29-
@SerializedName("liked_by_user")
30-
var liked_by_user: Boolean?,
31-
@SerializedName("description")
32-
val description: String?,
33-
@SerializedName("alt_description")
34-
val alt_description: String?,
35-
@SerializedName("exif")
36-
val exif: RemoteExifModel?,
37-
@SerializedName("location")
38-
val location: RemoteLocationModel?,
39-
@SerializedName("tags")
40-
val tags: List<RemoteTagModel>?,
41-
@SerializedName("current_user_collections")
42-
val current_user_collections: List<RemoteCollectionModel>?,
43-
@SerializedName("sponsorship")
44-
val sponsorship: RemoteSponsorship?,
45-
@SerializedName("urls")
46-
val urls: RemoteUrlsModel,
47-
@SerializedName("links")
48-
val links: RemoteLinksImageModel?,
49-
@SerializedName("user")
50-
val user: RemoteUserModel?,
51-
@SerializedName("statistics")
52-
val statistics: RemotePhotoStatistics?
9+
@SerializedName("id") val id: String,
10+
@SerializedName("created_at") val created_at: String?,
11+
@SerializedName("updated_at") val updated_at: String?,
12+
@SerializedName("width") val width: Int?,
13+
@SerializedName("height") val height: Int?,
14+
@SerializedName("color") val color: String? = "#E0E0E0",
15+
@SerializedName("blur_hash") val blur_hash: String?,
16+
@SerializedName("views") val views: Int?,
17+
@SerializedName("downloads") val downloads: Int?,
18+
@SerializedName("likes") val likes: Int?,
19+
@SerializedName("liked_by_user") var liked_by_user: Boolean?,
20+
@SerializedName("description") val description: String?,
21+
@SerializedName("alt_description") val alt_description: String?,
22+
@SerializedName("exif") val exif: RemoteExifModel?,
23+
@SerializedName("location") val location: RemoteLocationModel?,
24+
@SerializedName("tags") val tags: List<RemoteTagModel>?,
25+
@SerializedName("current_user_collections") val current_user_collections: List<RemoteCollectionModel>?,
26+
@SerializedName("sponsorship") val sponsorship: RemoteSponsorship?,
27+
@SerializedName("urls") val urls: RemoteUrlsModel,
28+
@SerializedName("links") val links: RemoteLinksImageModel?,
29+
@SerializedName("user") val user: RemoteUserModel?,
30+
@SerializedName("statistics") val statistics: RemotePhotoStatistics?
5331
)

app/src/main/java/st/slex/csplashscreen/data/model/remote/image/RemoteLinksImageModel.kt

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ package st.slex.csplashscreen.data.model.remote.image
33
import com.google.gson.annotations.SerializedName
44

55
data class RemoteLinksImageModel(
6-
@SerializedName("self")
7-
val self: String,
8-
@SerializedName("html")
9-
val html: String,
10-
@SerializedName("download")
11-
val download: String,
12-
@SerializedName("download_location")
13-
val download_location: String
6+
@SerializedName("self") val self: String,
7+
@SerializedName("html") val html: String,
8+
@SerializedName("download") val download: String,
9+
@SerializedName("download_location") val download_location: String
1410
)

app/src/main/java/st/slex/csplashscreen/data/model/remote/image/RemoteUrlsModel.kt

+5-10
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@ package st.slex.csplashscreen.data.model.remote.image
33
import com.google.gson.annotations.SerializedName
44

55
data class RemoteUrlsModel(
6-
@SerializedName("raw")
7-
val raw: String,
8-
@SerializedName("full")
9-
val full: String,
10-
@SerializedName("regular")
11-
val regular: String,
12-
@SerializedName("small")
13-
val small: String,
14-
@SerializedName("thumb")
15-
val thumb: String
6+
@SerializedName("raw") val raw: String,
7+
@SerializedName("full") val full: String,
8+
@SerializedName("regular") val regular: String,
9+
@SerializedName("small") val small: String,
10+
@SerializedName("thumb") val thumb: String
1611
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package st.slex.csplashscreen.data.model.remote.topics
2+
3+
import com.google.gson.annotations.SerializedName
4+
import st.slex.csplashscreen.data.model.remote.image.RemoteUrlsModel
5+
6+
data class RemotePreviewPhotosModel(
7+
@SerializedName("id") val id: String? = "",
8+
@SerializedName("created_at") val created_at: String? = "",
9+
@SerializedName("updated_at") val updated_at: String? = "",
10+
@SerializedName("urls") val urls: RemoteUrlsModel?
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package st.slex.csplashscreen.data.model.remote.topics
2+
3+
import com.google.gson.annotations.SerializedName
4+
import st.slex.csplashscreen.data.model.remote.collection.RemoteLinksCollectionModel
5+
import st.slex.csplashscreen.data.model.remote.image.RemoteImageModel
6+
import st.slex.csplashscreen.data.model.remote.user.RemoteUserModel
7+
8+
data class RemoteTopicsModel(
9+
@SerializedName("id") val id: String? = "",
10+
@SerializedName("slug") val slug: String? = "",
11+
@SerializedName("title") val title: String? = "",
12+
@SerializedName("description") val description: String? = "",
13+
@SerializedName("published_at") val published_at: String? = "",
14+
@SerializedName("updated_at") val updated_at: String? = "",
15+
@SerializedName("starts_at") val starts_at: String? = "",
16+
@SerializedName("ends_at") val ends_at: String? = "",
17+
@SerializedName("only_submissions_after") val only_submissions_after: String? = "",
18+
@SerializedName("featured") val featured: String? = "",
19+
@SerializedName("total_photos") val total_photos: String? = "",
20+
@SerializedName("links") val links: RemoteLinksCollectionModel,
21+
@SerializedName("status") val status: String? = "",
22+
@SerializedName("owners") val owners: List<RemoteUserModel>?,
23+
@SerializedName("cover_photo") val cover_photo: RemoteImageModel?,
24+
@SerializedName("preview_photos") val preview_photos: List<RemotePreviewPhotosModel>?
25+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package st.slex.csplashscreen.data.model.ui.topics
2+
3+
import st.slex.csplashscreen.data.model.ui.image.UrlsModel
4+
5+
data class PreviewPhotosModel(
6+
val id: String,
7+
val created_at: String,
8+
val updated_at: String,
9+
val urls: UrlsModel?
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package st.slex.csplashscreen.data.model.ui.topics
2+
3+
import st.slex.csplashscreen.data.model.ui.collection.LinksCollectionModel
4+
import st.slex.csplashscreen.data.model.ui.image.ImageModel
5+
import st.slex.csplashscreen.data.model.ui.user.UserModel
6+
7+
data class TopicsModel(
8+
val id: String,
9+
val slug: String,
10+
val title: String,
11+
val description: String,
12+
val published_at: String,
13+
val updated_at: String,
14+
val starts_at: String,
15+
val ends_at: String,
16+
val only_submissions_after: String,
17+
val featured: String,
18+
val total_photos: String,
19+
val links: LinksCollectionModel,
20+
val status: String,
21+
val owners: List<UserModel>?,
22+
val cover_photo: ImageModel?,
23+
val preview_photos: List<PreviewPhotosModel>?
24+
)

app/src/main/java/st/slex/csplashscreen/data/titles/TitlesRepository.kt

-3
This file was deleted.

app/src/main/java/st/slex/csplashscreen/data/titles/TitlesService.kt

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package st.slex.csplashscreen.data.titles
2+
3+
import androidx.paging.PagingSource
4+
import androidx.paging.PagingState
5+
import retrofit2.HttpException
6+
import st.slex.csplashscreen.data.core.toTopicsModel
7+
import st.slex.csplashscreen.data.model.ui.topics.TopicsModel
8+
import st.slex.csplashscreen.utiles.API_KEY
9+
import javax.inject.Inject
10+
11+
class TopicsPagingSource @Inject constructor(
12+
private val service: TopicsService
13+
) : PagingSource<Int, TopicsModel>() {
14+
15+
override fun getRefreshKey(state: PagingState<Int, TopicsModel>): Int? {
16+
val anchorPosition = state.anchorPosition ?: return null
17+
val anchorPage = state.closestPageToPosition(anchorPosition) ?: return null
18+
return anchorPage.prevKey?.plus(1) ?: anchorPage.nextKey?.minus(1)
19+
}
20+
21+
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, TopicsModel> {
22+
try {
23+
val pageNumber = params.key ?: INITIAL_PAGE_NUMBER
24+
val pageSize = params.loadSize
25+
26+
val response = service.getTopics(pageNumber, pageSize, API_KEY)
27+
28+
return if (response.isSuccessful) {
29+
val topics = response.body()!!.map { it.toTopicsModel() }
30+
val nextPageNumber = if (topics.isEmpty()) null else pageNumber + 1
31+
val prevPageNumber = if (pageNumber > 1) pageNumber - 1 else null
32+
LoadResult.Page(topics, prevPageNumber, nextPageNumber)
33+
} else {
34+
LoadResult.Error(HttpException(response))
35+
}
36+
} catch (e: HttpException) {
37+
return LoadResult.Error(e)
38+
} catch (e: Exception) {
39+
return LoadResult.Error(e)
40+
}
41+
}
42+
43+
companion object {
44+
const val INITIAL_PAGE_NUMBER = 1
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package st.slex.csplashscreen.data.titles
22

3-
class TitlesPagingSource
3+
interface TopicsRepository
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package st.slex.csplashscreen.data.titles
2+
3+
import retrofit2.Response
4+
import retrofit2.http.GET
5+
import retrofit2.http.Query
6+
import st.slex.csplashscreen.data.model.remote.topics.RemoteTopicsModel
7+
import st.slex.csplashscreen.utiles.GET_TOPICS
8+
import st.slex.csplashscreen.utiles.QUERY_API_KEY
9+
import st.slex.csplashscreen.utiles.QUERY_PAGE
10+
import st.slex.csplashscreen.utiles.QUERY_PAGE_SIZE
11+
12+
interface TopicsService {
13+
14+
@GET(GET_TOPICS)
15+
suspend fun getTopics(
16+
@Query(QUERY_PAGE) page: Int,
17+
@Query(QUERY_PAGE_SIZE) page_size: Int,
18+
@Query(QUERY_API_KEY) api_key: String
19+
): Response<List<RemoteTopicsModel>>
20+
}

app/src/main/java/st/slex/csplashscreen/di/module/AppModule.kt

+8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package st.slex.csplashscreen.di.module
22

33
import android.app.Application
44
import androidx.compose.material.ExperimentalMaterialApi
5+
import androidx.paging.PagingSource
56
import coil.ImageLoader
67
import coil.annotation.ExperimentalCoilApi
78
import com.google.accompanist.pager.ExperimentalPagerApi
89
import dagger.Module
910
import dagger.Provides
1011
import kotlinx.coroutines.ExperimentalCoroutinesApi
12+
import st.slex.csplashscreen.data.model.ui.topics.TopicsModel
13+
import st.slex.csplashscreen.data.titles.TopicsPagingSource
14+
import st.slex.csplashscreen.data.titles.TopicsService
1115
import javax.inject.Singleton
1216

1317
@ExperimentalCoilApi
@@ -29,4 +33,8 @@ class AppModule {
2933
@Provides
3034
fun provideImageLoader(application: Application): ImageLoader =
3135
ImageLoader.Builder(application).build()
36+
37+
@Provides
38+
fun providesTopicsPagingSource(service: TopicsService): PagingSource<Int, TopicsModel> =
39+
TopicsPagingSource(service)
3240
}

0 commit comments

Comments
 (0)