Skip to content

Commit 87f563d

Browse files
committed
ios working with cats & dogs
1 parent 416fef9 commit 87f563d

File tree

9 files changed

+112
-76
lines changed

9 files changed

+112
-76
lines changed

common/src/commonMain/kotlin/com/willowtreeapps/common/repo/KtorCatsRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ open class KtorCatsRepository(private val networkContext: CoroutineContext) : Co
5151
}
5252
GatewayResponse.createSuccess(listOfBreeds, 200, "Success")
5353
} catch (e: Exception) {
54+
com.willowtreeapps.common.Logger.d("FAILURE FETCHING CAT IMAGES: ${e.message}")
5455
GatewayResponse.createError(GenericError(e.message
5556
?: "Failure"), 500, e.message ?: "failure")
5657
}

common/src/commonMain/kotlin/com/willowtreeapps/common/repo/KtorDogsRepository.kt

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.willowtreeapps.common.repo
22

3+
import com.willowtreeapps.common.util.TimeUtil
34
import com.willowtreeapps.common.util.profile
45
import io.ktor.client.HttpClient
56
import io.ktor.client.features.json.JsonFeature
@@ -41,56 +42,66 @@ open class KtorDogsRepository(private val networkContext: CoroutineContext) : Co
4142
if (breed.value.isNotEmpty()) {
4243
breed.value.map { subBreed ->
4344
async {
44-
val dogImageResponse = retrySuccessOrThrow(
45-
numRetries = 3,
46-
retryWaitInMs = 2000,
47-
f = suspend { randomSubBreedImage(breed.key, subBreed) },
48-
ex = Exception("Unable to fetch image for $subBreed"))
49-
Dog(breed = breed.key, subBreed = subBreed, imageUrl = dogImageResponse.response?.message!!)
45+
val dogImageResponse =
46+
try {
47+
randomSubBreedImage(breed.key, subBreed)
48+
} catch (e: Exception) {
49+
GatewayResponse.createSuccess<DogImageResponse, GenericError>(DogImageResponse("200", ""), 200, "")
50+
}
51+
Dog(breed = breed.key.capitalize(), subBreed = subBreed.capitalize(), imageUrl = dogImageResponse.response?.message!!)
5052
}
5153
}
5254
} else {
53-
listOf(async {
54-
val dogImageResponse = retrySuccessOrThrow(
55-
numRetries = 3,
56-
retryWaitInMs = 2000,
57-
f = suspend { randomBreedImage(breed.key) },
58-
ex = Exception("Unable to fetch image for $breed"))
59-
Dog(breed = breed.key, imageUrl = dogImageResponse.response?.message!!)
60-
})
55+
56+
listOf(
57+
async {
58+
val dogImageResponse =
59+
try {
60+
randomBreedImage(breed.key)
61+
} catch (e: Exception) {
62+
com.willowtreeapps.common.Logger.d("Failure fetching image: ${e.message}")
63+
GatewayResponse.createSuccess<DogImageResponse, GenericError>(DogImageResponse("200", ""), 200, "")
64+
}
65+
Dog(breed = breed.key, imageUrl = dogImageResponse.response?.message!!)
66+
}
67+
)
6168
}
62-
}.flatten().awaitAll()
69+
}.flatten()
70+
.awaitAll()
6371
}
64-
6572
GatewayResponse.createSuccess(listOfBreeds, 200, "Success")
6673
} catch (e: Exception) {
74+
com.willowtreeapps.common.Logger.d("Failure fetching dogs: $e")
75+
com.willowtreeapps.common.Logger.d("Failure fetching dogs: ${e::class.qualifiedName}")
6776
GatewayResponse.createError(GenericError(e.message
6877
?: "Failure"), 500, e.message ?: "failure")
6978
}
7079
}
7180

7281
suspend fun randomBreedImage(breed: String): GatewayResponse<DogImageResponse, GenericError> {
73-
return try {
74-
val response: DogImageResponse = client.get {
75-
apiUrl(randomBreedImagePath(breed))
76-
}
77-
GatewayResponse.createSuccess(response, 200, "Success")
78-
} catch (e: Exception) {
79-
GatewayResponse.createError(GenericError(e.message
80-
?: "Failure"), 500, e.message ?: "failure")
82+
// return try {
83+
val response: DogImageResponse = client.get {
84+
apiUrl(randomBreedImagePath(breed))
8185
}
86+
return GatewayResponse.createSuccess(response, 200, "Success")
87+
// } catch (e: Exception) {
88+
// com.willowtreeapps.common.Logger.d("Failure fetching breed image: ${e.message}")
89+
// GatewayResponse.createError(GenericError(e.message
90+
// ?: "Failure"), 500, e.message ?: "failure")
91+
// }
8292
}
8393

8494
suspend fun randomSubBreedImage(breed: String, subBreed: String): GatewayResponse<DogImageResponse, GenericError> {
85-
return try {
86-
val response: DogImageResponse = client.get {
87-
apiUrl(randomSubBreedImagePath(breed, subBreed))
88-
}
89-
GatewayResponse.createSuccess(response, 200, "Success")
90-
} catch (e: Exception) {
91-
GatewayResponse.createError(GenericError(e.message
92-
?: "Failure"), 500, e.message ?: "failure")
95+
// return try {
96+
val response: DogImageResponse = client.get {
97+
apiUrl(randomSubBreedImagePath(breed, subBreed))
9398
}
99+
return GatewayResponse.createSuccess(response, 200, "Success")
100+
// } catch (e: Exception) {
101+
// com.willowtreeapps.common.Logger.d("Failure fetching subbreed image: ${e.message}")
102+
// GatewayResponse.createError(GenericError(e.message
103+
// ?: "Failure"), 500, e.message ?: "failure")
104+
// }
94105
}
95106

96107
private val client by lazy {
@@ -100,6 +111,7 @@ open class KtorDogsRepository(private val networkContext: CoroutineContext) : Co
100111
install(JsonFeature) {
101112
serializer = KotlinxSerializer(Json.nonstrict).apply {
102113
setMapper(DogResponse::class, DogResponse.serializer())
114+
setMapper(DogImageResponse::class, DogImageResponse.serializer())
103115
}
104116
}
105117
install(Logging) {

common/src/commonMain/kotlin/com/willowtreeapps/common/repo/KtorProfilesRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open class KtorProfilesRepository : ProfilesRepository {
2323
return try {
2424
val response: ProfileListHolder = client.get {
2525

26-
apiUrl("api/v1.0/cats")
26+
apiUrl("api/v1.0/profiles")
2727
}
2828
GatewayResponse.createSuccess(response.profiles, 200, "Success")
2929
} catch (e: Exception) {

common/src/iosMain/kotlin/com/willowtreeapps/common/util/TimeUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import platform.Foundation.NSDate
44
import platform.Foundation.timeIntervalSince1970
55

66
actual object TimeUtil {
7-
actual fun systemTimeMs(): Long = NSDate().timeIntervalSince1970.toLong()
7+
actual fun systemTimeMs(): Long = NSDate().timeIntervalSince1970.toLong() * 1000
88
}

iOS/NameGame/NameGame.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@
583583
"$(SRCROOT)",
584584
);
585585
"FRAMEWORK_SEARCH_PATHS[arch=*]" = (
586-
"$(SRCROOT)/../../common/build/bin/ios/mainDebugFramework",
586+
"$(SRCROOT)/../../common/build/xcode-frameworks",
587587
"$(PROJECT_DIR)",
588588
"$(SRCROOT)",
589589
);

0 commit comments

Comments
 (0)