Skip to content

Commit

Permalink
- UI tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: samoylenkodmitry <samoylenkodmitry@gmail.com>
  • Loading branch information
samoylenkodmitry committed May 29, 2024
1 parent 7f54dbc commit bb1019b
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 161 deletions.
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ kotlin {
implementation(compose.components.resources)
implementation(compose.components.uiToolingPreview)
implementation(projects.shared)
implementation(libs.composeIcons.feather)
implementation(libs.lifecycle.viewmodel.compose)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.json)
Expand Down
24 changes: 14 additions & 10 deletions composeApp/src/commonMain/kotlin/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ object Api {
refreshToken = refreshToken,
)
} else {
requestTokens()
requestAndSaveNewTokens()
}
}
refreshTokens {
oldTokens?.run {
BearerTokens(refreshSessionToken(refreshToken).sessionToken, refreshToken)
} ?: requestTokens()
} ?: requestAndSaveNewTokens()
}
}
}
}

private suspend fun requestTokens(): BearerTokens =
private suspend fun requestAndSaveNewTokens(): BearerTokens =
with(authenticate()) {
CoroutineScope(Dispatchers.Default).launch {
Storage.saveTokensToStorage(sessionToken, refreshToken)
Expand Down Expand Up @@ -101,19 +101,23 @@ object Api {
*/
suspend fun shorten(s: String): UrlInfo = requestsClient doPost Endpoints.shorten(ShortenRequest(s))

suspend fun getUrls(
page: Int,
pageSize: Int,
): UrlsResponse = requestsClient doPost Endpoints.getUrls(GetUrlsRequest(page, pageSize))

suspend fun getUrls(page: Int, pageSize: Int): UrlsResponse =
requestsClient doPost Endpoints.getUrls(GetUrlsRequest(page, pageSize))
suspend fun removeUrl(urlId: Long): Boolean = requestsClient doPost Endpoints.removeUrl(RemoveUrlRequest(urlId))

suspend fun removeUrl(urlId: Long): Boolean =
requestsClient doPost Endpoints.removeUrl(RemoveUrlRequest(urlId))

private suspend inline infix fun <reified T : Any> HttpClient.doGet(endpoint: Endpoint<T>): T =
get(endpoint.path).body()
private suspend inline infix fun <reified T : Any> HttpClient.doGet(endpoint: Endpoint<T>): T = get(endpoint.path).body()

private suspend inline infix fun <reified A : Any, reified T : Any> HttpClient.doPost(endpoint: EndpointWithArg<A, T>): T =
post(endpoint.path) {
contentType(ContentType.Application.Json)
setBody(endpoint.arg)
}.body()

suspend fun logout() {
Storage.clearData()
requestAndSaveNewTokens()
}
}
Loading

0 comments on commit bb1019b

Please sign in to comment.