Skip to content

Commit

Permalink
Merge pull request #12 from kriticalflare/master
Browse files Browse the repository at this point in the history
Add a datastore backed implementation for light & dark mode switch. Fixes #11
  • Loading branch information
Spikeysanju authored Oct 2, 2020
2 parents 395a914 + 602f259 commit 033b083
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 44 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ plugins {
}

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30

defaultConfig {
applicationId "www.spikeysanju.jetquotes"
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -89,4 +88,7 @@ dependencies {

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

// Preferences DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha01"
}
13 changes: 0 additions & 13 deletions app/src/main/java/www/spikeysanju/jetquotes/app/JetQuotes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,9 @@
package www.spikeysanju.jetquotes.app

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import www.spikeysanju.jetquotes.utils.isNight

class JetQuotes : Application() {
override fun onCreate() {
super.onCreate()

// check for ui mode & set accordingly
val mode = if (isNight()) {
AppCompatDelegate.MODE_NIGHT_YES
} else {
AppCompatDelegate.MODE_NIGHT_NO
}

AppCompatDelegate.setDefaultNightMode(mode)
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun CTAButtons(quote: String, author: String) {
val context = ContextAmbient.current
Stack(modifier = Modifier.fillMaxSize()) {
Row(modifier = Modifier.background(MaterialTheme.colors.primaryVariant)
.gravity(Alignment.BottomEnd)
.align(Alignment.BottomEnd)
.padding(30.dp, 30.dp, 0.dp, 30.dp)) {

Button(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ fun DetailCard(quote: String, author: String) {
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).gravity(Alignment.Center).padding(12.dp),
}).background(MaterialTheme.colors.primaryVariant).padding(40.dp).align(Alignment.Center).padding(12.dp),

) {

Text(
modifier = Modifier.gravity(Alignment.CenterHorizontally),
modifier = Modifier.align(Alignment.CenterHorizontally),
text = """ " """,
style = typography.h4,
color = MaterialTheme.colors.onBackground,
Expand All @@ -68,7 +68,7 @@ fun DetailCard(quote: String, author: String) {
Spacer(Modifier.preferredHeight(16.dp))

Text(
modifier = Modifier.gravity(Alignment.CenterHorizontally),
modifier = Modifier.align(Alignment.CenterHorizontally),
text = quote.ifBlank { " No Quotes found" },
style = typography.h5,
color = MaterialTheme.colors.onBackground,
Expand All @@ -78,7 +78,7 @@ fun DetailCard(quote: String, author: String) {
Spacer(Modifier.preferredHeight(16.dp))

Text(
modifier = Modifier.gravity(Alignment.CenterHorizontally),
modifier = Modifier.align(Alignment.CenterHorizontally),
text = author.ifBlank { " - Unknown" },
style = typography.body1,
color = MaterialTheme.colors.onBackground,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fun QuotesCard(quote: Quote) {

Stack(modifier = Modifier.fillMaxSize()) {
Text(
modifier = Modifier.gravity(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 @@ -26,12 +26,19 @@
*
*/

package www.spikeysanju.jetquotes.utils
package www.spikeysanju.jetquotes.components

import java.util.*
import androidx.compose.foundation.Icon
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R

fun isNight(): Boolean {
val currentHour = Calendar.getInstance()
.get(Calendar.HOUR_OF_DAY)
return (currentHour <= 7 || currentHour >= 18)
}
@Composable
fun QuotesThemeSwitch(onToggle: () -> Unit) {
val icon = vectorResource(R.drawable.ic_share)
Icon(icon, Modifier.padding(end = 8.dp).clickable(onClick = onToggle))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* *
* * * MIT License
* * *
* * * Copyright (c) 2020 Sanju S
* * *
* * * Permission is hereby granted, free of charge, to any person obtaining a copy
* * * of this software and associated documentation files (the "Software"), to deal
* * * in the Software without restriction, including without limitation the rights
* * * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* * * copies of the Software, and to permit persons to whom the Software is
* * * furnished to do so, subject to the following conditions:
* * *
* * * The above copyright notice and this permission notice shall be included in all
* * * copies or substantial portions of the Software.
* * *
* * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* * * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* * * SOFTWARE.
* *
*
*/

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 kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import java.io.IOException

enum class UiMode {
LIGHT, DARK
}

class PrefsManager(context: Context) {

private val dataStore = context.createDataStore(name = "jet_quotes_prefs")

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
}
}

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

companion object {
val IS_DARK_MODE = preferencesKey<Boolean>("dark_mode")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package www.spikeysanju.jetquotes.view
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.RowScope.gravity
import androidx.compose.foundation.layout.RowScope.align
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
Expand Down Expand Up @@ -76,7 +76,7 @@ fun DetailQuoteApp(quote: String, author: String) {
title = { Text(text = "JetQuotes", textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth()) },
backgroundColor = MaterialTheme.colors.primary,
contentColor = MaterialTheme.colors.onPrimary,
modifier = Modifier.gravity(Alignment.CenterVertically),
modifier = Modifier.align(Alignment.CenterVertically),
elevation = 0.dp
)
}, bodyContent = {
Expand Down
76 changes: 65 additions & 11 deletions app/src/main/java/www/spikeysanju/jetquotes/view/QuotesActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,85 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.RowScope.gravity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.RowScope.align
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
import androidx.lifecycle.lifecycleScope
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import www.spikeysanju.jetquotes.components.QuotesList
import www.spikeysanju.jetquotes.components.QuotesThemeSwitch
import www.spikeysanju.jetquotes.data.preference.PrefsManager
import www.spikeysanju.jetquotes.data.preference.UiMode
import www.spikeysanju.jetquotes.model.Quote
import www.spikeysanju.jetquotes.ui.JetQuotesTheme


class QuotesActivity : AppCompatActivity() {


@InternalCoroutinesApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val prefsManager = PrefsManager(context = this)
lifecycleScope.launch {
prefsManager.uiModeFlow.collect {
when (it) {
UiMode.DARK -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
UiMode.LIGHT -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
}
}

setContent {
JetQuotesTheme {
val currentTheme = isSystemInDarkTheme()
val darkMode by prefsManager.uiModeFlow.map { uiMode ->
when (uiMode) {
UiMode.DARK -> {
true
}
UiMode.LIGHT -> {
false
}
}
}.collectAsState(initial = currentTheme)

val toggleTheme: () -> Unit = {
lifecycleScope.launch {
prefsManager.setUiMode(if (darkMode) UiMode.LIGHT else UiMode.DARK)
}
}

JetQuotesTheme(darkMode) {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
App()
App(toggleTheme)
}
}
}
Expand Down Expand Up @@ -102,16 +148,24 @@ fun getQuotes(): List<Quote>? {

}

@Preview(showBackground = true)
@Composable
fun App() {
fun App(toggleTheme: () -> Unit) {
Scaffold(topBar = {
TopAppBar(
title = { Text(text = "JetQuotes", textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth()) },
backgroundColor = MaterialTheme.colors.primary,
contentColor = MaterialTheme.colors.onPrimary,
modifier = Modifier.gravity(Alignment.CenterVertically),
elevation = 0.dp
title = {
Text(
text = "JetQuotes",
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
},
backgroundColor = MaterialTheme.colors.primary,
contentColor = MaterialTheme.colors.onPrimary,
modifier = Modifier.align(Alignment.CenterVertically),
elevation = 0.dp,
actions = {
QuotesThemeSwitch(toggleTheme)
}
)
}, bodyContent = {
// pass list of quotes
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
compose_version = '1.0.0-alpha02'
compose_version = '1.0.0-alpha03'
}
ext.kotlin_version = "1.4.0"
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
Expand Down

0 comments on commit 033b083

Please sign in to comment.