Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds step 4 #3

Open
wants to merge 1 commit into
base: step3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 29 additions & 33 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig

plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsCompose)
alias(libs.plugins.compose.compiler)
id("app.cash.sqldelight") version "2.0.2"
}

kotlin {
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "composeApp"
browser {
commonWebpackConfig {
outputFileName = "composeApp.js"
devServer = (devServer ?: KotlinWebpackConfig.DevServer()).apply {
static = (static ?: mutableListOf()).apply {
// Serve sources to debug inside browser
add(project.projectDir.path)
}
}
android {
compilations.all {
kotlinOptions {
jvmTarget = "11"
}
}
binaries.executable()
}

androidTarget {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
jvmTarget.set(JvmTarget.JVM_11)
}
}

jvm("desktop")

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
iosTarget.binaries.framework {
).forEach {
it.binaries.framework {
baseName = "ComposeApp"
isStatic = true
if (System.getenv("XCODE_VERSION_MAJOR") == "1500") {
linkerOpts += "-ld64"
}
}
}

sourceSets {
val desktopMain by getting

androidMain.dependencies {
// sql-delight driver - Android
implementation(libs.android.driver)
implementation(compose.preview)
implementation(libs.androidx.activity.compose)
}
Expand All @@ -63,13 +45,19 @@ kotlin {
implementation(compose.ui)
implementation(compose.components.resources)
implementation(compose.components.uiToolingPreview)
implementation(projects.shared)
implementation(libs.voyager.navigator)
implementation(libs.voyager.navigator.transitions)
implementation(libs.voyager.screenmodel)
// sql-delight runtime
implementation(libs.runtime.v200)
// flows support for sql-delight
implementation(libs.coroutines.extensions)
// date-time
implementation(libs.kotlinx.datetime.v041)
}
desktopMain.dependencies {
implementation(compose.desktop.currentOs)

iosMain.dependencies {
implementation(libs.native.driver.v200)
}
}
}
Expand Down Expand Up @@ -125,3 +113,11 @@ compose.desktop {
}
}
}

sqldelight {
databases {
create("NotesDatabase") {
packageName.set("com.app.academy.notes.database")
}
}
}
4 changes: 2 additions & 2 deletions composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name="app.architect.notes.NotesAppApplication"
android:name="app.academy.NotesAppApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.Material.Light.NoActionBar">
<activity
android:name=".MainActivity"
android:name="app.academy.notes.MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|layoutDirection|locale|mcc|navigation|smallestScreenSize|touchscreen|uiMode"
android:exported="true">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package app.architect.notes
package app.academy

import android.app.Application
import app.academy.di.AppModule

class NotesAppApplication : Application() {
val appModule by lazy { AppModule(this) }

override fun onCreate() {
super.onCreate()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package app.academy.data.local

import android.content.Context
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import com.app.academy.notes.database.NotesDatabase

actual class NotesDatabaseDriverFactory(private val context: Context) {
actual fun createDriver(): SqlDriver = AndroidSqliteDriver(
schema = NotesDatabase.Schema,
context = context,
name = DatabaseDriverConstants.DATABASE_NAME
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package app.academy.di

import android.content.Context
import app.academy.data.DefaultNotesRepository
import app.academy.data.NotesRepository
import app.academy.data.local.NotesDatabaseDriverFactory
import app.academy.data.local.datasource.DefaultLocalNotesDataSource
import com.app.academy.notes.database.NotesDatabase
import kotlinx.coroutines.Dispatchers

actual class AppModule(context: Context) {

private val database by lazy {
val driver = NotesDatabaseDriverFactory(context).createDriver()
NotesDatabase(driver)
}

/**
* Provides an implementation of [NotesRepository]
*/
actual fun provideNotesRepository(): NotesRepository {
val localNotesDataSource = DefaultLocalNotesDataSource(
database = database,
ioDispatcher = Dispatchers.IO
)
return DefaultNotesRepository(localNotesDataSource = localNotesDataSource)
}


/**
* Used to provide an instance of [DispatchersProvider]
*/
actual fun provideDispatchersProvider(): DispatchersProviderList {
return DispatchersProviderList(
ioDispatcher = Dispatchers.IO,
defaultDispatcher = Dispatchers.Default,
mainDispatcher = Dispatchers.Main
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.academy.di

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

actual object DispatchersProvider {
actual val io: CoroutineDispatcher = Dispatchers.IO
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package com.app.academy.notes
package app.academy.notes

import com.app.academy.App
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import app.academy.NotesAppApplication
import app.academy.ui.App

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val application = this.application as NotesAppApplication
val appModule = application.appModule
setContent {
App()
App(appModule)
}
}
}

@Preview
@Composable
fun AppAndroidPreview() {
App()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.academy.utils

import kotlinx.coroutines.flow.StateFlow

/**
* A cross platform implementation of [StateFlow].
*/
actual class NativeStateFlow<T> actual constructor(source: StateFlow<T>) : StateFlow<T> by source
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package app.academy.utils

class AndroidPlatform : Platform {
override val name: String
get() = "Android"
}

actual fun getPlatform(): Platform {
return AndroidPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.academy.utils

/**
* A class that is used to generate a unique UUID (Universally Unique Identifier) string.
*/
actual object UUID {
actual fun randomUUIDString(): String = java.util.UUID.randomUUID().toString()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.app.academy.components
package app.academy.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -7,7 +7,7 @@ import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.items
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import app.architect.notes.model.Note
import app.academy.model.Note

@OptIn(ExperimentalFoundationApi::class)
fun LazyListScope.NoteItems(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.app.academy.components
package app.academy.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -14,7 +14,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import app.architect.notes.model.Note
import app.academy.model.Note

/**
* A composable that displays the information of a [Note], in a card. Some of the contents
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.app.academy.components
package app.academy.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.DismissDirection
Expand All @@ -9,7 +9,7 @@ import androidx.compose.material.SwipeToDismiss
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import app.architect.notes.model.Note
import app.academy.model.Note

@OptIn(ExperimentalMaterialApi::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package app.architect.notes.data
package app.academy.data

import app.architect.notes.data.local.datasource.LocalNotesDataSource
import app.architect.notes.model.Note
import app.architect.notes.model.toNote
import app.architect.notes.model.toSavedNoteEntity
import app.academy.data.local.datasource.LocalNotesDataSource
import app.academy.model.Note
import app.academy.model.toNote
import app.academy.model.toSavedNoteEntity
import app.academy.notes.database.SavedNoteEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

Expand All @@ -13,7 +14,7 @@ class DefaultNotesRepository(

override val savedNotesStream: Flow<List<Note>> =
localNotesDataSource.savedNotesStream.map { savedNoteEntities ->
savedNoteEntities.map { savedNoteEntity -> savedNoteEntity.toNote() }
savedNoteEntities.map { savedNoteEntity: SavedNoteEntity -> savedNoteEntity.toNote() }
}

override suspend fun saveNote(note: Note) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.architect.notes.data
package app.academy.data

import app.architect.notes.model.Note
import app.academy.model.Note
import kotlinx.coroutines.flow.Flow

interface NotesRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.architect.notes.data.local
package app.academy.data.local

import app.cash.sqldelight.db.SqlDriver

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package app.architect.notes.data.local.datasource
package app.academy.data.local.datasource

import app.architect.notes.data.local.datasource.LocalNotesDataSource
import app.academy.notes.database.SavedNoteEntity
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import com.example.notes.database.NotesDatabase
import com.example.notes.database.SavedNoteEntity
import com.app.academy.notes.database.NotesDatabase
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
Expand All @@ -15,9 +14,10 @@ class DefaultLocalNotesDataSource(
) : LocalNotesDataSource {
private val queries = database.notesDatabaseQueries

override val savedNotesStream: Flow<List<SavedNoteEntity>> = queries.getAllSavedNotes()
.asFlow()
.mapToList(ioDispatcher)
override val savedNotesStream: Flow<List<SavedNoteEntity>> =
queries.getAllSavedNotes()
.asFlow()
.mapToList(ioDispatcher)

override suspend fun saveNote(noteEntity: SavedNoteEntity) = withContext(ioDispatcher) {
queries.insertNote(
Expand Down
Loading