Skip to content

Commit e637626

Browse files
Merge pull request #15 from hyunjung-choi/feature/display-image-title
Feature/display image title
2 parents baf115a + eeaabbe commit e637626

File tree

16 files changed

+142
-60
lines changed

16 files changed

+142
-60
lines changed

app/src/main/java/com/prography/android/test/hyunjung/data/local/PhotoDatabase.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package com.prography.android.test.hyunjung.data.local
22

33
import androidx.room.Database
44
import androidx.room.RoomDatabase
5+
import androidx.room.TypeConverters
56

6-
@Database(entities = [PhotoEntity::class], version = 1)
7+
@Database(entities = [PhotoEntity::class], version = 2, exportSchema = false)
8+
@TypeConverters(PhotoTypeConverters::class)
79
abstract class PhotoDatabase : RoomDatabase() {
810
abstract fun photoDao(): PhotoDao
911
}

app/src/main/java/com/prography/android/test/hyunjung/data/local/PhotoEntity.kt

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,98 @@ package com.prography.android.test.hyunjung.data.local
22

33
import androidx.room.Entity
44
import androidx.room.PrimaryKey
5+
import androidx.room.TypeConverter
6+
import androidx.room.TypeConverters
57
import com.prography.android.test.hyunjung.data.model.Photo
8+
import com.prography.android.test.hyunjung.data.model.Tag
69
import com.prography.android.test.hyunjung.data.model.Urls
10+
import com.prography.android.test.hyunjung.data.model.User
11+
import kotlinx.serialization.SerialName
12+
import kotlinx.serialization.json.Json
713

814
@Entity(tableName = "bookmarks")
15+
@TypeConverters(PhotoTypeConverters::class)
916
data class PhotoEntity(
1017
@PrimaryKey val id: String,
11-
val altDescription: String?,
12-
val imageUrl: String
18+
val title: String = "",
19+
val description: String? = null,
20+
@SerialName("alt_description")
21+
val altDescription: String? = null,
22+
val tags: List<Tag> = emptyList(),
23+
val urls: Urls? = null,
24+
val user: User? = null
1325
)
1426

1527
fun PhotoEntity.toDomain(): Photo {
1628
return Photo(
1729
id = this.id,
30+
title = this.title,
31+
description = this.description,
1832
altDescription = this.altDescription,
19-
urls = Urls(
20-
raw = this.imageUrl,
21-
full = this.imageUrl,
22-
regular = this.imageUrl,
23-
small = this.imageUrl,
24-
thumb = this.imageUrl
25-
)
33+
tags = this.tags,
34+
urls = Urls(this.urls?.regular ?: ""),
35+
user = this.user
2636
)
2737
}
2838

39+
@TypeConverters
2940
fun Photo.toEntity(): PhotoEntity {
3041
return PhotoEntity(
3142
id = this.id,
43+
title = this.title,
44+
description = this.description,
3245
altDescription = this.altDescription,
33-
imageUrl = this.urls.regular
46+
tags = this.tags,
47+
urls = this.urls,
48+
user = this.user
3449
)
50+
}
51+
52+
class PhotoTypeConverters {
53+
private val json = Json { ignoreUnknownKeys = true }
54+
55+
@TypeConverter
56+
fun fromTagsList(tags: List<Tag>): String {
57+
return json.encodeToString(tags)
58+
}
59+
60+
@TypeConverter
61+
fun toTagsList(tagsString: String): List<Tag> {
62+
if (tagsString.isBlank()) return emptyList()
63+
return try {
64+
json.decodeFromString(tagsString)
65+
} catch (e: Exception) {
66+
emptyList()
67+
}
68+
}
69+
70+
@TypeConverter
71+
fun fromUser(user: User?): String {
72+
return if (user == null) "" else json.encodeToString(user)
73+
}
74+
75+
@TypeConverter
76+
fun toUser(userString: String): User? {
77+
if (userString.isBlank()) return null
78+
return try {
79+
json.decodeFromString(userString)
80+
} catch (e: Exception) {
81+
null
82+
}
83+
}
84+
85+
@TypeConverter
86+
fun fromUrls(urls: Urls?): String {
87+
return if (urls == null) "" else json.encodeToString(urls)
88+
}
89+
90+
@TypeConverter
91+
fun toUrls(urlsString: String): Urls? {
92+
if (urlsString.isBlank()) return null
93+
return try {
94+
json.decodeFromString(urlsString)
95+
} catch (e: Exception) {
96+
null
97+
}
98+
}
3599
}

app/src/main/java/com/prography/android/test/hyunjung/data/model/Photo.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import kotlinx.serialization.Serializable
66
@Serializable
77
data class Photo(
88
val id: String,
9+
val title: String = "",
10+
val description: String? = null,
911
@SerialName("alt_description")
10-
val altDescription: String?,
11-
val urls: Urls
12+
val altDescription: String? = null,
13+
val tags: List<Tag> = emptyList(),
14+
val urls: Urls,
15+
val user: User? = null
1216
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.prography.android.test.hyunjung.data.model
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class ProfileImage(
7+
val small: String? = null,
8+
val medium: String? = null,
9+
val large: String? = null
10+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.prography.android.test.hyunjung.data.model
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Tag(
7+
val title: String
8+
)
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package com.prography.android.test.hyunjung.data.model
22

3-
43
import kotlinx.serialization.Serializable
54

65
@Serializable
76
data class Urls(
8-
val raw: String,
9-
val full: String,
107
val regular: String,
11-
val small: String,
12-
val thumb: String
138
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.prography.android.test.hyunjung.data.model
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
data class User(
8+
val id: String,
9+
val username: String,
10+
val name: String? = null,
11+
@SerialName("profile_image")
12+
val profileImage: ProfileImage? = null
13+
)

app/src/main/java/com/prography/android/test/hyunjung/data/network/KtorClient.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

app/src/main/java/com/prography/android/test/hyunjung/data/network/PhotoApiService.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.prography.android.test.hyunjung.data.network
22

3+
import android.util.Log
34
import com.prography.android.test.hyunjung.BuildConfig
45
import com.prography.android.test.hyunjung.data.model.Photo
56
import com.prography.android.test.hyunjung.utils.Constants
67
import io.ktor.client.HttpClient
78
import io.ktor.client.call.body
89
import io.ktor.client.request.get
910
import io.ktor.client.request.headers
11+
import io.ktor.client.request.parameter
1012
import io.ktor.client.statement.HttpResponse
1113
import javax.inject.Inject
1214
import javax.inject.Singleton
@@ -20,11 +22,14 @@ class PhotoApiService @Inject constructor(
2022

2123
suspend fun getLatestPhotos(page: Int): List<Photo> {
2224
return try {
23-
val response: HttpResponse = client.get("$baseUrl/photos?page=$page") {
25+
val response: HttpResponse = client.get("$baseUrl/photos") {
26+
parameter("page", page)
2427
headers {
2528
append("Authorization", "Client-ID $accessKey")
2629
}
2730
}
31+
val responseBody = response.body<String>()
32+
Log.d("API_RESPONSE", "Response Body: $responseBody")
2833
response.body()
2934
} catch (e: Exception) {
3035
emptyList()

app/src/main/java/com/prography/android/test/hyunjung/data/repository/PhotoRepository.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.prography.android.test.hyunjung.data.repository
22

3-
import android.util.Log
4-
import com.prography.android.test.hyunjung.BuildConfig
53
import com.prography.android.test.hyunjung.data.local.PhotoDao
64
import com.prography.android.test.hyunjung.data.local.toDomain
75
import com.prography.android.test.hyunjung.data.local.toEntity
@@ -15,13 +13,12 @@ class PhotoRepository(
1513
private val apiService: PhotoApiService,
1614
private val photoDao: PhotoDao
1715
) {
18-
private val loadedPhotoIds = mutableSetOf<String>()
1916

2017
suspend fun fetchLatestPhotos(page: Int): List<Photo> {
21-
Log.d("HJ:::API_KEY", BuildConfig.UNSPLASH_ACCESS_KEY)
2218
return try {
2319
apiService.getLatestPhotos(page)
2420
} catch (e: Exception) {
21+
e.printStackTrace()
2522
emptyList()
2623
}
2724
}

0 commit comments

Comments
 (0)