-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adjust default HttpClient for MockEngine injection * Simplify Gallery state object for inclusion of a user state * Remove out variant type * Resolve test failures * Remove unused import statement
- Loading branch information
Showing
13 changed files
with
194 additions
and
207 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
40 changes: 40 additions & 0 deletions
40
gallery-app/src/androidMain/kotlin/io/ashdavies/gallery/GalleryScreen.preview.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,40 @@ | ||
package io.ashdavies.gallery | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import io.ashdavies.content.PlatformContext | ||
|
||
private val DefaultState = GalleryScreen.State( | ||
itemList = emptyList(), | ||
showCapture = false, | ||
isLoggedIn = false, | ||
eventSink = { }, | ||
) | ||
|
||
private val EmptyStorageManager = object : StorageManager { | ||
override fun create(context: PlatformContext): File = TempFile | ||
override fun delete(file: File): Boolean = false | ||
} | ||
|
||
private val TempFile = File.createTempFile( | ||
/* prefix = */ "tmp", | ||
/* suffix = */ null, | ||
) | ||
|
||
@Preview | ||
@Composable | ||
internal fun GalleryCaptureDefaultPreview() { | ||
GalleryCapture(EmptyStorageManager) { } | ||
} | ||
|
||
@Preview | ||
@Composable | ||
internal fun GalleryBottomBarDefaultPreview() { | ||
GalleryBottomBar(DefaultState, isSelecting = false) | ||
} | ||
|
||
@Preview | ||
@Composable | ||
internal fun GalleryBottomBarSelectingPreview() { | ||
GalleryBottomBar(DefaultState, isSelecting = true) | ||
} |
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
56 changes: 56 additions & 0 deletions
56
gallery-app/src/commonMain/kotlin/io/ashdavies/gallery/HttpClient.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,56 @@ | ||
package io.ashdavies.gallery | ||
|
||
import io.ktor.client.engine.HttpClientEngine | ||
import io.ktor.client.engine.mock.MockEngine | ||
import io.ktor.client.engine.mock.respond | ||
import io.ktor.client.request.HttpRequestData | ||
import io.ktor.http.Headers | ||
import io.ktor.http.HttpHeaders | ||
import io.ktor.http.HttpMethod | ||
import io.ktor.http.headersOf | ||
import io.ktor.utils.io.ByteReadChannel | ||
|
||
private val DefaultHeaders = headersOf(HttpHeaders.ContentType, "application/json") | ||
|
||
private val HttpRequestData.path: String | ||
get() = url.encodedPath.substring(1) | ||
|
||
private val Headers.contentLength: String | ||
get() = requireNotNull(get(HttpHeaders.ContentLength)) | ||
|
||
internal fun InMemoryHttpClientEngine(initialValue: List<String>): HttpClientEngine { | ||
val values = initialValue.toMutableList() | ||
|
||
return MockEngine { request -> | ||
when { | ||
request.method == HttpMethod.Get && request.path.isEmpty() -> { | ||
val text = "[${values.joinToString(transform = { "\"$it\"" })}]" | ||
|
||
respond(ByteReadChannel(text), headers = DefaultHeaders) | ||
} | ||
|
||
request.method == HttpMethod.Post && request.path.isNotEmpty() -> { | ||
require(request.body.contentLength == request.headers.contentLength.toLong()) | ||
values += request.path | ||
|
||
respond(ByteReadChannel.Empty, headers = DefaultHeaders) | ||
} | ||
|
||
request.method == HttpMethod.Put && request.path.isNotEmpty() -> { | ||
require(request.body.contentLength == 0L) | ||
values += request.path | ||
|
||
respond(ByteReadChannel.Empty, headers = DefaultHeaders) | ||
} | ||
|
||
request.method == HttpMethod.Delete && request.path.isNotEmpty() -> { | ||
require(request.body.contentLength == 0L) | ||
values -= request.path | ||
|
||
respond(ByteReadChannel.Empty, headers = DefaultHeaders) | ||
} | ||
|
||
else -> error("Unhandled request: $request") | ||
} | ||
} | ||
} |
Oops, something went wrong.