Skip to content

Commit

Permalink
Merge pull request #4 from aritra-tech/develop
Browse files Browse the repository at this point in the history
Settings Screen
  • Loading branch information
aritra-tech authored Jun 15, 2024
2 parents 67ea090 + e8a5a18 commit 335b501
Show file tree
Hide file tree
Showing 30 changed files with 768 additions and 16 deletions.
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ kotlin {
implementation(libs.koin.annotation)
implementation(libs.ktor.client.android)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.androidx.startup.runtime)

}
commonMain.dependencies {
Expand Down
13 changes: 12 additions & 1 deletion composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +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"/>

<application
android:name=".Coinify"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -20,6 +22,15 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="${applicationId}.androidx-startup"
android:name="androidx.startup.InitializationProvider"
android:exported="false"
tools:node="merge">
<meta-data
android:name="com.ContextProvider"
android:value="androidx.startup"/>
</provider>
</application>

</manifest>
19 changes: 19 additions & 0 deletions composeApp/src/androidMain/kotlin/com/ContextProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com

import android.content.Context
import androidx.startup.Initializer

internal lateinit var applicationContext: Context
private set

data object ContextProviderInitializer

class ContextProvider : Initializer<ContextProviderInitializer> {
override fun create(context: Context): ContextProviderInitializer {
applicationContext = context.applicationContext
return ContextProviderInitializer
}

override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()

}
11 changes: 11 additions & 0 deletions composeApp/src/androidMain/kotlin/com/aritra/coinify/Coinify.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.aritra.coinify

import android.app.Application
import utils.ApplicationComponent

class Coinify : Application() {
override fun onCreate() {
super.onCreate()
ApplicationComponent.init()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package data.datastore

import androidx.datastore.core.DataMigration
import androidx.datastore.core.DataStore
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.Preferences
import com.applicationContext
import kotlinx.coroutines.CoroutineScope
import java.io.File

actual fun dataStorePreferences(
corruptionHandler: ReplaceFileCorruptionHandler<Preferences>?,
coroutineScope: CoroutineScope,
migrations: List<DataMigration<Preferences>>
): DataStore<Preferences> = createDataStoreWithDefaults(
corruptionHandler = corruptionHandler,
migrations = migrations,
coroutineScope = coroutineScope,
path = {
File(applicationContext.filesDir, "datastore/$SETTINGS_PREFERENCES").path
}
)
2 changes: 2 additions & 0 deletions composeApp/src/commonMain/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
<string name="volume_24h">Volume 24h</string>
<string name="max_supply">Max Supply</string>
<string name="total_supply">Total Supply</string>
<string name="theme">Theme</string>
<string name="invite_others">Invite others</string>
</resources>
12 changes: 11 additions & 1 deletion composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import di.appModule
import navigation.Navigation
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.koin.compose.KoinApplication
import org.koin.compose.koinInject
import ui.CoinifyTheme
import utils.ThemeViewModel

@Composable
@Preview
Expand All @@ -15,7 +20,12 @@ fun App() {
KoinApplication(application = {
modules(appModule)
}) {
CoinifyTheme {
val themeViewModel: ThemeViewModel = koinInject()
val isDarkModeEnabled by themeViewModel.currentTheme.collectAsState(isSystemInDarkTheme())

CoinifyTheme(
darkTheme = isDarkModeEnabled
) {
MainContent()
}
}
Expand Down
54 changes: 54 additions & 0 deletions composeApp/src/commonMain/kotlin/component/SettingItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package component

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun SettingItem(
onClick: () -> Unit,
imageVector: ImageVector,
itemName: String,
itemColor: Color = MaterialTheme.colorScheme.onSurface
) {

Row(
modifier = Modifier
.fillMaxWidth()
.clickable { onClick() }
.padding(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp, Alignment.Start),
verticalAlignment = Alignment.CenterVertically,
) {
Image(
modifier = Modifier.size(24.dp),
imageVector = imageVector,
contentDescription = null,
colorFilter = ColorFilter.tint(color = itemColor)
)
Text(
text = itemName,
style = TextStyle(
color = itemColor,
fontSize = 16.sp,
fontWeight = FontWeight.Normal
)
)
}
}
114 changes: 114 additions & 0 deletions composeApp/src/commonMain/kotlin/component/ThemeSelectionDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package component

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import utils.onClick

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ThemeSelectionDialog(
onThemeChange: (Theme) -> Unit,
onDismissRequest: () -> Unit,
currentTheme: Theme
) {

var currentSelectedTheme by remember { mutableStateOf(currentTheme) }

BasicAlertDialog(
onDismissRequest = onDismissRequest,
content = {
Surface(
modifier = Modifier
.wrapContentWidth()
.wrapContentHeight(),
shape = MaterialTheme.shapes.large,
tonalElevation = AlertDialogDefaults.TonalElevation
) {
Column(modifier = Modifier.padding(16.dp)) {

Text(
text = "Choose a theme",
style = TextStyle(fontSize = 20.sp),
modifier = Modifier.padding(8.dp)
)

Row(
modifier = Modifier.fillMaxWidth().onClick { currentSelectedTheme = Theme.Light },
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = currentSelectedTheme == Theme.Light,
onClick = { currentSelectedTheme = Theme.Light},
colors = RadioButtonDefaults.colors(
selectedColor = MaterialTheme.colorScheme.primary
)
)
Text(text = "Light Mode")
}

Row(
modifier = Modifier.fillMaxWidth().onClick { currentSelectedTheme = Theme.Dark },
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = currentSelectedTheme == Theme.Dark,
onClick = { currentSelectedTheme = Theme.Dark },
colors = RadioButtonDefaults.colors(
selectedColor = MaterialTheme.colorScheme.primary
)
)
Text(text = "Dark Mode")
}

Spacer(modifier = Modifier.height(24.dp))

Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End
) {
TextButton(onClick = onDismissRequest) {
Text(text = "Cancel")
}
Spacer(modifier = Modifier.width(16.dp))
TextButton(onClick = { onThemeChange(currentSelectedTheme) }) {
Text(text = "Apply")
}
}
}
}
}
)
}

enum class Theme {
Light, Dark
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package data.datastore

import androidx.datastore.core.DataMigration
import androidx.datastore.core.DataStore
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.SupervisorJob
import okio.Path.Companion.toPath

expect fun dataStorePreferences(
corruptionHandler: ReplaceFileCorruptionHandler<Preferences>?,
coroutineScope: CoroutineScope,
migrations: List<DataMigration<Preferences>>,
): DataStore<Preferences>

internal const val SETTINGS_PREFERENCES = "settings_preferences.preferences_pb"

internal fun createDataStoreWithDefaults(
corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
migrations: List<DataMigration<Preferences>> = emptyList(),
path: () -> String,
) = PreferenceDataStoreFactory
.createWithPath(
corruptionHandler = corruptionHandler,
scope = coroutineScope,
migrations = migrations,
produceFile = {
path().toPath()
}
)
16 changes: 10 additions & 6 deletions composeApp/src/commonMain/kotlin/di/Koin.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package di

import domain.repository.ListingRepository
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module
import presentation.home.HomeViewModel
import presentation.HomeViewModel
import presentation.SettingsViewModel
import utils.ThemeViewModel
import utils.coreComponent
import utils.viewModelDefinition

val appModule = module {
single {
ListingRepository()
}
viewModelDefinition { HomeViewModel(get()) }

single { ListingRepository() }
single { coreComponent.appPreferences }

viewModelDefinition { HomeViewModel(get()) }
viewModelDefinition { SettingsViewModel(get()) }
viewModelDefinition { ThemeViewModel(get()) }
}
12 changes: 12 additions & 0 deletions composeApp/src/commonMain/kotlin/navigation/BottomNavScreens.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package navigation

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Newspaper
import androidx.compose.material.icons.filled.Settings
import androidx.compose.ui.graphics.vector.ImageVector

sealed class BottomNavScreens(val route: String, val icon: ImageVector, val title: String) {
object Home: BottomNavScreens(Screens.Home.route, Icons.Default.Home,"Home")
object News: BottomNavScreens(Screens.Settings.route, Icons.Default.Settings, "Settings")
}
Loading

0 comments on commit 335b501

Please sign in to comment.