Skip to content

Commit

Permalink
Merge pull request #26 from fethij/feature_download_images
Browse files Browse the repository at this point in the history
Feature download images
  • Loading branch information
fethij authored Sep 28, 2024
2 parents e8d42cb + f828da5 commit 80d2bd4
Show file tree
Hide file tree
Showing 93 changed files with 2,364 additions and 219 deletions.
6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

@OptIn(org.jetbrains.kotlin.gradle.ExperimentalWasmDsl::class)
internal fun Project.configureKotlinMultiplatform(
extension: KotlinMultiplatformExtension
) = extension.apply {
Expand All @@ -20,7 +20,6 @@ internal fun Project.configureKotlinMultiplatform(

jvm("desktop")

@OptIn(ExperimentalWasmDsl::class)
wasmJs { browser() }

listOf(iosArm64(), iosSimulatorArm64())
Expand All @@ -33,6 +32,7 @@ internal fun Project.configureKotlinMultiplatform(
implementation(libs.findLibrary("kotlinx.coroutines.core").get())
api(libs.findLibrary("koin.core").get())
implementation(libs.findLibrary("kermit").get())
implementation(libs.findLibrary("arrow.core").get())
}

androidMain {
Expand Down
2 changes: 2 additions & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ kotlin {
implementation(libs.ktor.client.core)
implementation(libs.coil.compose)
implementation(libs.coil.network.ktor)

implementation(libs.filekit.core)
}

desktopMain.dependencies {
Expand Down
6 changes: 5 additions & 1 deletion composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"
tools:ignore="ScopedStorage" />

<application
android:name=".RijksmuseumApplication"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tewelde.rijksmuseum

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import androidx.activity.ComponentActivity
Expand All @@ -9,15 +10,33 @@ import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.WindowCompat
import com.tewelde.rijksmuseum.di.appModule
import io.github.vinceglb.filekit.core.FileKit
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.dsl.module

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startKoin {
androidLogger()
androidContext(applicationContext)
modules(
appModule,
module {
single<Activity> { this@MainActivity }
}
)
}

enableEdgeToEdge(
statusBarStyle = SystemBarStyle.light(Color.TRANSPARENT, Color.TRANSPARENT),
navigationBarStyle = SystemBarStyle.light(Color.TRANSPARENT, Color.TRANSPARENT)
)
WindowCompat.setDecorFitsSystemWindows(window, false)
FileKit.init(this)
setContent {
App()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
package com.tewelde.rijksmuseum

import android.app.Application
import com.tewelde.rijksmuseum.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

class RijksmuseumApplication: Application() {

override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@RijksmuseumApplication)
modules(appModule)
}
}
}
class RijksmuseumApplication: Application()
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- TODO-->
<color name="rijksmuseum_launcher_background">#FFFFFF</color>
</resources>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.tewelde.rijksmuseum

import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import coil3.ImageLoader
import coil3.PlatformContext
import coil3.annotation.ExperimentalCoilApi
Expand All @@ -12,7 +14,7 @@ import coil3.request.CachePolicy
import coil3.request.crossfade
import coil3.util.DebugLogger
import com.tewelde.rijksmuseum.navigation.RijksmuseumNavGraph
import com.tewelde.rijksmuseum.theme.RijksmuseumTheme
import com.tewelde.rijksmuseum.core.designsystem.theme.RijksmuseumTheme
import okio.FileSystem
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.koin.compose.KoinContext
Expand All @@ -27,8 +29,7 @@ fun App(disableDiskCache: Boolean = false) {
if (disableDiskCache) context.asyncImageLoader() else
context.asyncImageLoader().enableDiskCache()
}

RijksmuseumNavGraph()
RijksmuseumNavGraph(snackbarHostState = remember { SnackbarHostState() })
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import coil3.network.NetworkFetcher
import coil3.network.ktor.asNetworkClient
import com.tewelde.rijksmuseum.feature.arts.di.artsModule
import io.ktor.client.HttpClient
import org.koin.dsl.module

@OptIn(ExperimentalCoilApi::class)
val appModule = org.koin.dsl.module {
val appModule = module {
includes(artsModule)
single {
NetworkFetcher.Factory(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tewelde.rijksmuseum.navigation

import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.ViewModel
Expand All @@ -23,11 +25,12 @@ import org.koin.compose.currentKoinScope
@Composable
fun RijksmuseumNavGraph(
modifier: Modifier = Modifier,
snackbarHostState: SnackbarHostState,
startDestination: RijksmuseumDestination = RijksmuseumDestination.Gallery,
navController: NavHostController = rememberNavController(),
) {
NavHost(
modifier = Modifier.then(modifier),
modifier = modifier,
startDestination = startDestination.route,
navController = navController,
) {
Expand All @@ -42,14 +45,22 @@ fun RijksmuseumNavGraph(
DetailScreenRoute(
objectId = id,
viewModel = viewModel,
onBackClick = navController::popBackStack
onBackClick = navController::navigateUp,
snackbarHostState = snackbarHostState,
onShowSnackbar = { message, action, duration ->
snackbarHostState.showSnackbar(
message = message,
actionLabel = action,
duration = duration,
) == SnackbarResult.ActionPerformed
}
)
}
}
}

@Composable
inline fun <reified T: ViewModel> koinViewModel(): T {
inline fun <reified T : ViewModel> koinViewModel(): T {
val scope = currentKoinScope()
return viewModel {
scope.get<T>()
Expand Down
16 changes: 16 additions & 0 deletions core/common/src/commonMain/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,20 @@
<string name="no_arts">No Arts for now!</string>
<string name="art_item">ArtItem</string>
<string name="collection">Collection</string>

<string name="unknown">Unknown</string>
<string name="save_to_photos">Save</string>
<string name="details">Details</string>
<string name="description">Description</string>
<string name="size">Size</string>
<string name="title">Title</string>
<string name="color">Color</string>
<string name="show_more">Show More</string>
<string name="show_less">Show Less</string>
<string name="production_places">Production Places</string>

<string name="permission_denied">Please grant storage permission.</string>
<string name="settings">Settings</string>
<string name="saving_failed">Image not saved, try again later.</string>
<string name="saving_success">Image is saved.</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ sealed interface Result<out T> {
data class Error(val exception: Throwable? = null) : Result<Nothing>
}

/**
* Sealed class to represent the different types of API responses.
* [ApiResponse.IOException] Represents an IO exception.
* [ApiResponse.HttpError] Represents an HTTP error.
*/
sealed interface ApiResponse {
data object HttpError : ApiResponse
data object IOException : ApiResponse
}

/**
* Extension function to convert a Flow<T> to a Flow<Result<T>>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import org.koin.core.module.dsl.withOptions
import org.koin.core.qualifier.named
import org.koin.dsl.module

val commonModule = module {
single { provideIoDispatcher() } withOptions {
named(RijksmuseumDispatchers.IO)
single(named(RijksmuseumDispatchers.IO)) { provideIoDispatcher() }
single(named(RijksmuseumDispatchers.Default)) { Dispatchers.Default }
single<CoroutineScope> {
provideApplicationScope(get(named(RijksmuseumDispatchers.Default)))
}

single { provideDefaultDispatcher() } withOptions {
named(RijksmuseumDispatchers.Default)
}

single {
provideApplicationScope(
get(named(RijksmuseumDispatchers.Default))
)
}

}

expect fun provideIoDispatcher(): CoroutineDispatcher
fun provideDefaultDispatcher() = Dispatchers.Default
fun provideApplicationScope(dispatcher: CoroutineDispatcher): CoroutineScope =
CoroutineScope(SupervisorJob() + dispatcher)

Expand Down
2 changes: 1 addition & 1 deletion core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ kotlin {
commonMain.dependencies {
api(projects.core.common)
implementation(projects.core.model)
implementation(projects.core.network)
api(projects.core.network)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.tewelde.rijksmuseum.core.data

import com.tewelde.rijksmuseum.core.common.Result
import arrow.core.Either
import com.tewelde.rijksmuseum.core.common.ApiResponse
import com.tewelde.rijksmuseum.core.model.Art
import com.tewelde.rijksmuseum.core.model.ArtObject
import io.ktor.utils.io.ByteReadChannel

/**
* Data layer interface for the arts
Expand All @@ -11,10 +13,12 @@ interface ArtRepository {
/**
* Returns a list of [Art]s.
*/
suspend fun getCollection(page: Int): Result<List<Art>>
suspend fun getCollection(page: Int): Either<ApiResponse, List<Art>>

/**
* Returns an [ArtObject] with the given [objectId].
*/
suspend fun getArt(objectId: String): Result<ArtObject>
suspend fun getArt(objectId: String): Either<ApiResponse, ArtObject>

suspend fun downloadImage(url: String, onDownload: (Long, Long?) -> Unit): ByteReadChannel
}
Loading

0 comments on commit 80d2bd4

Please sign in to comment.