Skip to content

Commit

Permalink
Merge pull request #73 from Spikeysanju/Refactor-code
Browse files Browse the repository at this point in the history
Refactor code #72
  • Loading branch information
Spikeysanju authored May 2, 2021
2 parents 452bfce + 48deebd commit d4926c0
Show file tree
Hide file tree
Showing 22 changed files with 576 additions and 289 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

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

28 changes: 13 additions & 15 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'

}

android {
Expand All @@ -42,7 +39,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionName "1.2.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -54,11 +51,11 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '11'
useIR = true

}
Expand All @@ -67,7 +64,7 @@ android {
}
composeOptions {
kotlinCompilerExtensionVersion "${compose_version}"
kotlinCompilerVersion '1.4.0'
kotlinCompilerVersion '1.4.32'
}
}

Expand All @@ -76,24 +73,25 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'com.google.android.material:material:1.3.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.foundation:foundation:$compose_version"
implementation 'androidx.activity:activity-compose:1.3.0-alpha03'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-beta01'
testImplementation 'junit:junit:4.13.1'
implementation 'androidx.activity:activity-compose:1.3.0-alpha07'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha01'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

//compose navigation
implementation "androidx.navigation:navigation-compose:1.0.0-alpha09"

implementation "androidx.navigation:navigation-compose:1.0.0-alpha10"

// Moshi
implementation("com.squareup.moshi:moshi-kotlin:1.11.0")

// Preferences DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha02"
implementation "androidx.datastore:datastore-preferences:1.0.0-beta01"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha04"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
android:theme="@style/Theme.JetQuotes">

<activity
android:name=".view.QuotesActivity"
android:name=".view.MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.JetQuotes.NoActionBar">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ package www.spikeysanju.jetquotes.components
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.MaterialTheme.typography
import androidx.compose.material.Text
Expand All @@ -48,13 +53,22 @@ fun DetailCard(quote: String, author: String) {
val context = LocalContext.current

Box(modifier = Modifier.fillMaxSize()) {

Column(
modifier = Modifier.clickable(onClick = {
context.copyToClipboard(quote.plus("").plus("- $author"))
Toast.makeText(context, "Quote copied!", Toast.LENGTH_SHORT).show()
}).background(MaterialTheme.colors.primaryVariant).padding(40.dp)
.align(Alignment.Center).padding(12.dp),
modifier = Modifier
.clickable(onClick = {
context.copyToClipboard(
quote
.plus("")
.plus("- $author")
)
Toast
.makeText(context, "Quote copied!", Toast.LENGTH_SHORT)
.show()
})
.background(MaterialTheme.colors.primaryVariant)
.padding(40.dp)
.align(Alignment.Center)
.padding(12.dp),

) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ package www.spikeysanju.jetquotes.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.MaterialTheme.typography
import androidx.compose.material.Text
Expand All @@ -39,15 +45,19 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.model.Quote
import www.spikeysanju.jetquotes.utils.Actions
import www.spikeysanju.jetquotes.navigation.MainActions

@Composable
fun QuotesCard(quote: Quote, actions: Actions) {
fun QuotesCard(quote: Quote, actions: MainActions) {

Column(modifier = Modifier.wrapContentSize().padding(12.dp).clickable(onClick = {
actions.openQuoteDetails(quote.quote!!, quote.author!!)

}).background(MaterialTheme.colors.primaryVariant).padding(12.dp)) {
Column(modifier = Modifier
.wrapContentSize()
.padding(12.dp)
.clickable(onClick = {
actions.quoteDetails(quote.quote!!, quote.author!!)
})
.background(MaterialTheme.colors.primaryVariant)
.padding(12.dp)) {

Text(
text = """ " """,
Expand All @@ -66,7 +76,9 @@ fun QuotesCard(quote: Quote, actions: Actions) {

Box(modifier = Modifier.fillMaxSize()) {
Text(
modifier = Modifier.align(Alignment.CenterEnd).padding(12.dp),
modifier = Modifier
.align(Alignment.CenterEnd)
.padding(12.dp),
text = quote.author.toString().ifBlank { " - Unknown" },
style = typography.caption,
color = MaterialTheme.colors.onBackground
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.model.Quote
import www.spikeysanju.jetquotes.utils.Actions
import www.spikeysanju.jetquotes.navigation.MainActions


@Composable
fun QuotesList(quotes: List<Quote>, actions: Actions) {
fun QuotesList(quotes: List<Quote>, actions: MainActions) {
LazyColumn(modifier = Modifier.padding(36.dp, 12.dp, 0.dp, 12.dp)) {
items(items = quotes) { item ->
QuotesCard(item, actions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
package www.spikeysanju.jetquotes.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Icon
import androidx.compose.runtime.Composable
Expand All @@ -39,6 +40,16 @@ import www.spikeysanju.jetquotes.R

@Composable
fun QuotesThemeSwitch(onToggle: () -> Unit) {
val icon = painterResource(R.drawable.ic_day)
Icon(painter = icon, contentDescription = "", Modifier.padding(end = 8.dp).clickable(onClick = onToggle))

val icon = when {
isSystemInDarkTheme() -> painterResource(id = R.drawable.ic_night)
else -> painterResource(id = R.drawable.ic_day)
}

Icon(
painter = icon, contentDescription = "",
Modifier
.padding(end = 8.dp)
.clickable(onClick = onToggle)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,46 @@
package www.spikeysanju.jetquotes.data.preference

import android.content.Context
import androidx.datastore.preferences.createDataStore
import androidx.datastore.preferences.edit
import androidx.datastore.preferences.emptyPreferences
import androidx.datastore.preferences.preferencesKey
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import java.io.IOException

enum class UiMode {
LIGHT, DARK
abstract class PrefsDataStore(context: Context, fileName: String) {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(fileName)
val mDataStore: DataStore<Preferences> = context.dataStore
}

class PrefsManager(context: Context) {

private val dataStore = context.createDataStore(name = "jet_quotes_prefs")
class UIModeDataStore(context: Context) : PrefsDataStore(context = context, PREF_FILE_UI_MODE),
UIModeImpl {

val uiModeFlow: Flow<UiMode> = dataStore.data
.catch {
if (it is IOException) {
it.printStackTrace()
emit(emptyPreferences())
} else {
throw it
}
}
.map { preference ->
when (preference[IS_DARK_MODE] ?: false) {
true -> UiMode.DARK
false -> UiMode.LIGHT
}
// used to get the data from datastore
override val uiMode: Flow<Boolean>
get() = mDataStore.data.map { preferences ->
val uiMode = preferences[UI_MODE_KEY] ?: false
uiMode
}

suspend fun setUiMode(uiMode: UiMode) {
dataStore.edit { preferences ->
preferences[IS_DARK_MODE] = when (uiMode) {
UiMode.LIGHT -> false
UiMode.DARK -> true
}

// used to save the ui preference to datastore
override suspend fun saveToDataStore(isNightMode: Boolean) {
mDataStore.edit { preferences ->
preferences[UI_MODE_KEY] = isNightMode
}
}


companion object {
val IS_DARK_MODE = preferencesKey<Boolean>("dark_mode")
private const val PREF_FILE_UI_MODE = "ui_mode_preference"
private val UI_MODE_KEY = booleanPreferencesKey("ui_mode")
}
}

interface UIModeImpl {
val uiMode: Flow<Boolean>
suspend fun saveToDataStore(isNightMode: Boolean)
}
Loading

0 comments on commit d4926c0

Please sign in to comment.