Skip to content

Commit

Permalink
Merge pull request #94 from Spikeysanju/Fix-Crash
Browse files Browse the repository at this point in the history
Fix crash
  • Loading branch information
Spikeysanju authored May 21, 2021
2 parents b5f1494 + 1180d69 commit 78f0d0a
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 72 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.3.0"
versionName "1.3.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -112,7 +112,7 @@ dependencies {
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltAndroidXVersion"
kapt "androidx.hilt:hilt-compiler:$hiltAndroidXVersion"
implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha01"
implementation "androidx.hilt:hilt-common:1.0.0-beta01"
implementation "androidx.hilt:hilt-common:1.0.0"
kapt "com.google.dagger:hilt-compiler:2.33-beta"

// Lottie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R

@Composable
fun Button(icon: Painter, name: String?, modifier: Modifier) {
Column(
modifier.background(MaterialTheme.colors.primaryVariant).padding(12.dp)) {
modifier
.background(MaterialTheme.colors.primaryVariant)
.padding(12.dp)) {

Icon(
painter = icon,
contentDescription = ""
contentDescription = stringResource(R.string.text_cta_icons)
)
Spacer(modifier = Modifier.height(8.dp))
Text(text = name.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R
import www.spikeysanju.jetquotes.model.Quote
Expand All @@ -64,7 +65,7 @@ fun CTAButtons(viewModel: MainViewModel, quote: String, author: String) {

Button(
icon = painterResource(id = R.drawable.ic_heart),
name = "FAVOURITE",
name = stringResource(R.string.text_favourite),
modifier = Modifier.clickable(onClick = {
val quotes = Quote(quote, author)
viewModel.insertFavourite(quotes)
Expand All @@ -76,7 +77,7 @@ fun CTAButtons(viewModel: MainViewModel, quote: String, author: String) {

Button(
icon = painterResource(id = R.drawable.ic_copy),
name = "COPY",
name = stringResource(R.string.text_copy),
modifier = Modifier.clickable(onClick = {
context.copyToClipboard(quote.plus("").plus("- $author"))
Toast.makeText(context, "Quote Copied!", Toast.LENGTH_SHORT).show()
Expand All @@ -87,7 +88,7 @@ fun CTAButtons(viewModel: MainViewModel, quote: String, author: String) {

Button(
icon = painterResource(id = R.drawable.ic_share),
name = "SHARE",
name = stringResource(R.string.text_share),
modifier = Modifier.clickable(onClick = {
context.shareToOthers(quote.plus("").plus("- $author"))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R
import www.spikeysanju.jetquotes.utils.copyToClipboard
import www.spikeysanju.jetquotes.view.viewModel.MainViewModel

Expand Down Expand Up @@ -90,7 +92,7 @@ fun DetailCard(viewModel: MainViewModel, quote: String, author: String) {
modifier = Modifier
.align(Alignment.CenterHorizontally)
.wrapContentSize(align = Alignment.Center),
text = quote.ifBlank { " No Quotes found" },
text = quote.ifBlank { stringResource(R.string.no_quotes_found) },
style = typography.h5,
color = MaterialTheme.colors.onBackground,
textAlign = TextAlign.Center
Expand All @@ -100,7 +102,7 @@ fun DetailCard(viewModel: MainViewModel, quote: String, author: String) {

Text(
modifier = Modifier.align(Alignment.CenterHorizontally),
text = author.ifBlank { " - Unknown" },
text = author.ifBlank { stringResource(R.string.text_unknow) },
style = typography.body1,
color = MaterialTheme.colors.onBackground,
textAlign = TextAlign.Center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.airbnb.lottie.compose.LottieAnimation
Expand All @@ -62,14 +63,14 @@ fun EmptyScreen(actions: MainActions) {
) {
Loader()
Text(
text = "Your wish list is empty!",
text = stringResource(R.string.text_empty_wish_list),
modifier = Modifier.fillMaxWidth(),
style = typography.h6,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "Explore more and add some quotes!",
text = stringResource(R.string.text_empty_explore),
modifier = Modifier.fillMaxWidth(),
style = typography.body2,
textAlign = TextAlign.Center,
Expand All @@ -82,7 +83,7 @@ fun EmptyScreen(actions: MainActions) {
contentColor = MaterialTheme.colors.background
)
) {
Text(text = "Back to home")
Text(text = stringResource(R.string.text_back_to_home))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R
import www.spikeysanju.jetquotes.model.Quote
import www.spikeysanju.jetquotes.navigation.MainActions

Expand All @@ -70,7 +72,7 @@ fun QuotesCard(quote: Quote, actions: MainActions) {
text = quote.quote,
style = typography.body1,
color = MaterialTheme.colors.onBackground,
modifier = Modifier.padding(12.dp, 0.dp, 0.dp, 0.dp)
modifier = Modifier.padding(start = 12.dp)
)

Spacer(Modifier.height(12.dp))
Expand All @@ -80,7 +82,7 @@ fun QuotesCard(quote: Quote, actions: MainActions) {
modifier = Modifier
.align(Alignment.CenterEnd)
.padding(12.dp),
text = quote.author.toString().ifBlank { " - Unknown" },
text = quote.author.toString().ifBlank { stringResource(R.string.text_unknow) },
style = typography.caption,
color = MaterialTheme.colors.onBackground
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.compose.material.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R

Expand All @@ -47,7 +48,7 @@ fun QuotesThemeSwitch(onToggle: () -> Unit) {
}

Icon(
painter = icon, contentDescription = "",
painter = icon, contentDescription = stringResource(id = R.string.text_day_night),
Modifier
.padding(end = 8.dp)
.clickable(onClick = onToggle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R
import www.spikeysanju.jetquotes.ui.typography
Expand Down Expand Up @@ -81,7 +82,7 @@ fun TopBar(title: String, onToggle: () -> Unit, onFavouritesClick: () -> Unit) {
val settingsIcon: Painter = painterResource(id = R.drawable.ic_heart)
Icon(
painter = settingsIcon,
contentDescription = "Favourites Icon",
contentDescription = stringResource(R.string.text_favourites_icon),
tint = MaterialTheme.colors.onPrimary
)
}
Expand All @@ -93,7 +94,7 @@ fun TopBar(title: String, onToggle: () -> Unit, onFavouritesClick: () -> Unit) {
painterResource(id = R.drawable.ic_day)
Icon(
painter = toggleIcon,
contentDescription = "Day/Night Icon",
contentDescription = stringResource(R.string.text_day_night),
tint = MaterialTheme.colors.onPrimary
)
}
Expand All @@ -115,7 +116,7 @@ fun TopBarWithBack(title: String, onBackClick: () -> Unit) {
IconButton(onClick = { onBackClick() }, modifier = Modifier.padding(start = 16.dp)) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = "Back Icon",
contentDescription = stringResource(R.string.text_back_icon),
tint = MaterialTheme.colors.onPrimary
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Singleton

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


class UIModeDataStore(context: Context) : PrefsDataStore(context = context, PREF_FILE_UI_MODE),
UIModeImpl {

Expand All @@ -68,6 +68,7 @@ class UIModeDataStore(context: Context) : PrefsDataStore(context = context, PREF
}
}

@Singleton
interface UIModeImpl {
val uiMode: Flow<Boolean>
suspend fun saveToDataStore(isNightMode: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import www.spikeysanju.jetquotes.data.preference.UIModeDataStore
import www.spikeysanju.jetquotes.data.preference.UIModeImpl
import www.spikeysanju.jetquotes.data.preference.db.FavouritesDao
import www.spikeysanju.jetquotes.data.preference.db.JetQuotesDatabase
import javax.inject.Singleton
Expand All @@ -43,6 +45,12 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
object AppComponent {

@Singleton
@Provides
fun providePreferenceManager(@ApplicationContext context: Context): UIModeImpl {
return UIModeDataStore(context)
}

@Singleton
@Provides
fun provideDao(jetQuotesDatabase: JetQuotesDatabase): FavouritesDao =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ package www.spikeysanju.jetquotes.navigation
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.HiltViewModelFactory
import androidx.hilt.navigation.compose.hiltNavGraphViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
Expand All @@ -51,13 +55,16 @@ object EndPoints {

@ExperimentalMaterialApi
@Composable
fun JetQuotesMain(viewModel: MainViewModel, toggleTheme: () -> Unit) {
fun NavGraph(toggleTheme: () -> Unit) {
val navController = rememberNavController()
val actions = remember(navController) { MainActions(navController) }

NavHost(navController, startDestination = Screen.Home.route) {
// Quotes List
composable(Screen.Home.route) {
val viewModel: MainViewModel = viewModel(
factory = HiltViewModelFactory(LocalContext.current, it)
)
QuotesListScreen(viewModel, toggleTheme, actions)
}

Expand All @@ -70,6 +77,7 @@ fun JetQuotesMain(viewModel: MainViewModel, toggleTheme: () -> Unit) {
type = NavType.StringType
})
) {
val viewModel = hiltNavGraphViewModel<MainViewModel>(backStackEntry = it)
DetailScreen(
viewModel,
actions.upPress,
Expand All @@ -80,6 +88,7 @@ fun JetQuotesMain(viewModel: MainViewModel, toggleTheme: () -> Unit) {

// Favourites
composable(Screen.Favourites.route) {
val viewModel = hiltNavGraphViewModel<MainViewModel>(backStackEntry = it)
FavouritesScreen(viewModel, actions)
}
}
Expand Down
50 changes: 25 additions & 25 deletions app/src/main/java/www/spikeysanju/jetquotes/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,57 +36,57 @@ import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.compose.viewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import www.spikeysanju.jetquotes.navigation.JetQuotesMain
import www.spikeysanju.jetquotes.data.preference.UIModeImpl
import www.spikeysanju.jetquotes.navigation.NavGraph
import www.spikeysanju.jetquotes.ui.JetQuotesTheme
import www.spikeysanju.jetquotes.view.viewModel.MainViewModel

import javax.inject.Inject

@ExperimentalMaterialApi
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MainViewModel
@Inject
lateinit var uiModeDataStore: UIModeImpl

@InternalCoroutinesApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
//init viewModel
viewModel = viewModel()
JetQuotesMain()
}
// observe theme change
observeUITheme()
}

// get all quotes
viewModel.getAllQuotes(application)
@Composable
fun JetQuotesMain() {

// observe theme change
observeUITheme()
// check UI mode
val darkMode by uiModeDataStore.uiMode.collectAsState(initial = isSystemInDarkTheme())

// check UI mode
val darkMode by viewModel.getUIMode.collectAsState(initial = isSystemInDarkTheme())
// set UI mode accordingly
val toggleTheme: () -> Unit = {
lifecycleScope.launch {
viewModel.setUIMode(!darkMode)
}
// set UI mode accordingly
val toggleTheme: () -> Unit = {
lifecycleScope.launch {
uiModeDataStore.saveToDataStore(!darkMode)
}
}

JetQuotesTheme(darkTheme = darkMode) {
Surface(color = MaterialTheme.colors.background) {
JetQuotesMain(viewModel, toggleTheme)
}
JetQuotesTheme(darkTheme = darkMode) {
Surface(color = MaterialTheme.colors.background) {
NavGraph(toggleTheme)
}
}

}

private fun observeUITheme() {
lifecycleScope.launchWhenStarted {
viewModel.getUIMode.collect {
uiModeDataStore.uiMode.collect {
val mode = when (it) {
true -> AppCompatDelegate.MODE_NIGHT_YES
false -> AppCompatDelegate.MODE_NIGHT_NO
Expand Down
Loading

0 comments on commit 78f0d0a

Please sign in to comment.