Skip to content

Commit

Permalink
added wasmjs
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomanaia committed Jul 2, 2024
1 parent e3c7743 commit 33c87e7
Show file tree
Hide file tree
Showing 29 changed files with 538 additions and 87 deletions.
13 changes: 13 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlugin
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension

plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
Expand All @@ -8,3 +13,11 @@ plugins {
alias(libs.plugins.ksp) apply false
alias(libs.plugins.hilt) apply false
}

// Use the pre-installed yarn and nodejs
rootProject.plugins.withType<YarnPlugin> {
rootProject.the<YarnRootExtension>().download = false
}
rootProject.plugins.withType<NodeJsRootPlugin> {
rootProject.the<NodeJsRootExtension>().download = false
}
62 changes: 45 additions & 17 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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

plugins {
alias(libs.plugins.kotlin.multiplatform)
Expand All @@ -22,11 +23,22 @@ kotlin {

jvm("desktop")

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = "composeApp"
browser {
commonWebpackConfig {
outputFileName = "composeApp.js"
}
}
binaries.executable()
}

applyDefaultHierarchyTemplate()

jvmToolchain(17)

sourceSets {
val desktopMain by getting

commonMain {
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")

Expand All @@ -45,15 +57,13 @@ kotlin {
implementation(libs.kotlinx.serialization.json)

implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.datastore.preferences)

// lifecycle runtime compose causing problems
// implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.viewmodel.compose)

implementation(project.dependencies.platform(libs.koin.bom))
implementation(libs.koin.core)
implementation(libs.koin.logger.slf4j)
api(project.dependencies.platform(libs.koin.bom))
api(libs.koin.core)
implementation(libs.koin.compose)
implementation(libs.koin.compose.viewmodel)

Expand All @@ -70,29 +80,47 @@ kotlin {

commonTest.dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit5"))
// implementation(kotlin("test-junit5"))
implementation(libs.junit.jupiter.params)
implementation(libs.assertk)

implementation(libs.kotlinx.coroutines.test)
}

androidMain.dependencies {
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.core.splashscreen)
val androidJvmMain by creating {
dependsOn(commonMain.get())

dependencies {
implementation(libs.androidx.datastore.preferences)
}
}

androidMain {
dependsOn(androidJvmMain)

implementation(libs.kotlinx.coroutines.android)
dependencies {
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.core.splashscreen)

implementation(libs.kotlinx.coroutines.android)

implementation(libs.google.material)
implementation(libs.google.material)

implementation(libs.koin.android)
implementation(libs.koin.android)
}
}

desktopMain.dependencies {
implementation(compose.desktop.currentOs)
val desktopMain by getting {
dependsOn(androidJvmMain)

implementation(libs.kotlinx.coroutines.swing)
dependencies {
implementation(compose.desktop.currentOs)

implementation(libs.koin.logger.slf4j)

implementation(libs.kotlinx.coroutines.swing)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.joaomanaia.game2048.core.datastore

import androidx.datastore.preferences.core.Preferences as AndroidxPreferences
import androidx.datastore.preferences.core.MutablePreferences as AndroidxMutablePreferences

actual typealias Preferences = AndroidxPreferences
actual typealias MutablePreferences = AndroidxMutablePreferences

actual typealias PreferencesKey <T> = AndroidxPreferences.Key<T>
actual typealias PreferencesPair <T> = AndroidxPreferences.Pair<T>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.joaomanaia.game2048.core.datastore.manager

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map

actual class DataStoreManagerImpl(
private val dataStore: DataStore<Preferences>
) : DataStoreManager {
override val preferenceFlow = dataStore.data

override suspend fun <T> getPreference(
preferenceEntry: PreferenceRequest<T>
) = preferenceFlow
.firstOrNull()
?.get(preferenceEntry.key) ?: preferenceEntry.defaultValue

override fun <T> getPreferenceFlow(request: PreferenceRequest<T>) = preferenceFlow.map {
it[request.key] ?: request.defaultValue
}.distinctUntilChanged()

override suspend fun <T> editPreference(key: Preferences.Key<T>, newValue: T) {
dataStore.edit { preferences -> preferences[key] = newValue }
}

override suspend fun editPreferences(vararg prefs: Preferences.Pair<*>) {
dataStore.edit { preferences ->
prefs.forEach {
preferences.plusAssign(it)
}
}
}

override suspend fun clearPreferences() {
dataStore.edit { preferences -> preferences.clear() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MainActivity : ComponentActivity() {
}

setContent {
val windowSizeClass = calculateWindowSizeClass(this)
val windowSizeClass = calculateWindowSizeClass()

CompositionLocalProvider(
LocalKeyEventHandlers provides keyEventHandlers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.joaomanaia.game2048.core.common.preferences

import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.floatPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import com.joaomanaia.game2048.core.datastore.booleanPreferencesKey
import com.joaomanaia.game2048.core.datastore.floatPreferencesKey
import com.joaomanaia.game2048.core.datastore.intPreferencesKey
import com.joaomanaia.game2048.core.datastore.manager.PreferenceRequest
import com.joaomanaia.game2048.core.datastore.stringPreferencesKey
import com.joaomanaia.game2048.core.presentation.theme.TileColorsGenerator
import com.joaomanaia.game2048.core.presentation.theme.DarkThemeConfig as EnumDarkThemeConfig

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.joaomanaia.game2048.core.datastore

expect abstract class Preferences internal constructor() {
abstract fun asMap(): Map<PreferencesKey<*>, Any>

abstract operator fun <T> contains(key: PreferencesKey<T>): Boolean

abstract operator fun <T> get(key: PreferencesKey<T>): T?

fun toMutablePreferences(): MutablePreferences

fun toPreferences(): Preferences
}

expect class PreferencesKey<T> internal constructor(name: String) {
val name: String

override operator fun equals(other: Any?): Boolean

override fun hashCode(): Int

infix fun to(value: T): PreferencesPair<T>

override fun toString(): String
}

expect class PreferencesPair<T> internal constructor(key: PreferencesKey<T>, value: T) {
internal val key: PreferencesKey<T>

internal val value: T
}


expect class MutablePreferences : Preferences

fun booleanPreferencesKey(name: String): PreferencesKey<Boolean> = PreferencesKey(name)

fun intPreferencesKey(name: String): PreferencesKey<Int> = PreferencesKey(name)

fun floatPreferencesKey(name: String): PreferencesKey<Float> = PreferencesKey(name)

fun stringPreferencesKey(name: String): PreferencesKey<String> = PreferencesKey(name)

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.joaomanaia.game2048.core.datastore.manager

import androidx.datastore.preferences.core.Preferences
import com.joaomanaia.game2048.core.datastore.Preferences
import com.joaomanaia.game2048.core.datastore.PreferencesKey
import com.joaomanaia.game2048.core.datastore.PreferencesPair
import kotlinx.coroutines.flow.Flow

interface DataStoreManager {
Expand All @@ -10,9 +12,9 @@ interface DataStoreManager {

fun <T> getPreferenceFlow(request: PreferenceRequest<T>): Flow<T>

suspend fun <T> editPreference(key: Preferences.Key<T>, newValue: T)
suspend fun <T> editPreference(key: PreferencesKey<T>, newValue: T)

suspend fun editPreferences(vararg prefs: Preferences.Pair<*>)
suspend fun editPreferences(vararg prefs: PreferencesPair<*>)

suspend fun clearPreferences()
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@
package com.joaomanaia.game2048.core.datastore.manager

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map

class DataStoreManagerImpl(
private val dataStore: DataStore<Preferences>
) : DataStoreManager {
override val preferenceFlow = dataStore.data

override suspend fun <T> getPreference(
preferenceEntry: PreferenceRequest<T>
) = preferenceFlow
.firstOrNull()
?.get(preferenceEntry.key) ?: preferenceEntry.defaultValue

override fun <T> getPreferenceFlow(request: PreferenceRequest<T>) = preferenceFlow.map {
it[request.key] ?: request.defaultValue
}.distinctUntilChanged()

override suspend fun <T> editPreference(key: Preferences.Key<T>, newValue: T) {
dataStore.edit { preferences -> preferences[key] = newValue }
}

override suspend fun editPreferences(vararg prefs: Preferences.Pair<*>) {
dataStore.edit { preferences ->
prefs.forEach {
preferences.plusAssign(it)
}
}
}

override suspend fun clearPreferences() {
dataStore.edit { preferences -> preferences.clear() }
}
}
expect class DataStoreManagerImpl : DataStoreManager
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.joaomanaia.game2048.core.datastore.manager

import androidx.datastore.preferences.core.Preferences
import com.joaomanaia.game2048.core.datastore.PreferencesKey

open class PreferenceRequest<T>(
val key: Preferences.Key<T>,
val key: PreferencesKey<T>,
val defaultValue: T
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.joaomanaia.game2048.core.presentation.theme

import androidx.annotation.Keep
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
Expand All @@ -9,7 +8,6 @@ import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Keep
@Immutable
data class Spacing(
/** 8.dp **/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.joaomanaia.game2048.di

import io.github.oshai.kotlinlogging.KotlinLogging
import org.koin.core.context.startKoin
import org.koin.dsl.KoinAppDeclaration

object KoinStarter {
private val logger = KotlinLogging.logger("KoinStarter")

fun init(config: KoinAppDeclaration? = null) = startKoin {
logger.trace { "Starting Koin" }
config?.invoke(this)
modules(appModule, dataStoreModule, repositoryModule, wallpaperColorsModule)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.joaomanaia.game2048.model

import androidx.annotation.Keep

/**
* Container class that describes a location in a 2D grid.
*/
@Keep
data class Cell(
val row: Int,
val col: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.joaomanaia.game2048.model

import androidx.annotation.Keep

/**
* Container class describing a tile at a certain location in the grid.
*/
@Keep
data class GridTile(
val cell: Cell,
val tile: Tile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.joaomanaia.game2048.model

import androidx.annotation.Keep

/**
* Container class describing how a tile has moved within the grid.
*/
@Keep
data class GridTileMovement(
val fromGridTile: GridTile?,
val toGridTile: GridTile
Expand Down
Loading

0 comments on commit 33c87e7

Please sign in to comment.