Skip to content

Commit

Permalink
Merge pull request #75 from Spikeysanju/Add-Favourites
Browse files Browse the repository at this point in the history
Add favourites
  • Loading branch information
Spikeysanju authored May 4, 2021
2 parents 99322f4 + e3e999e commit eb77b6c
Show file tree
Hide file tree
Showing 25 changed files with 759 additions and 106 deletions.
2 changes: 2 additions & 0 deletions .idea/misc.xml

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

22 changes: 22 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}

android {
Expand Down Expand Up @@ -94,4 +96,24 @@ dependencies {
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"

// Room
implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"

// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"


// Dagger Hilt
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
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"
kapt "com.google.dagger:hilt-compiler:2.33-beta"

}
9 changes: 4 additions & 5 deletions app/src/main/java/www/spikeysanju/jetquotes/app/JetQuotes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
package www.spikeysanju.jetquotes.app

import android.app.Application
class JetQuotes : Application() {
override fun onCreate() {
super.onCreate()
}
}
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class JetQuotes : Application()

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.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
Expand All @@ -40,19 +45,35 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.R
import www.spikeysanju.jetquotes.model.Quote
import www.spikeysanju.jetquotes.utils.copyToClipboard
import www.spikeysanju.jetquotes.utils.shareToOthers
import www.spikeysanju.jetquotes.view.viewModel.MainViewModel

@Composable
fun CTAButtons(quote: String, author: String) {
fun CTAButtons(viewModel: MainViewModel, quote: String, author: String) {
val context = LocalContext.current
Box(modifier = Modifier.fillMaxSize()) {
Row(
modifier = Modifier.background(MaterialTheme.colors.primaryVariant)
modifier = Modifier
.background(MaterialTheme.colors.primaryVariant)
.align(Alignment.BottomEnd)
.padding(30.dp, 30.dp, 0.dp, 30.dp)
) {


Button(
icon = painterResource(id = R.drawable.ic_heart),
name = "FAVOURITE",
modifier = Modifier.clickable(onClick = {
val quotes = Quote(quote, author)
viewModel.insertFavourite(quotes)
Toast.makeText(context, "Added to Favourites!", Toast.LENGTH_SHORT).show()
})
)

Spacer(modifier = Modifier.width(30.dp))

Button(
icon = painterResource(id = R.drawable.ic_copy),
name = "COPY",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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 @@ -47,9 +48,10 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import www.spikeysanju.jetquotes.utils.copyToClipboard
import www.spikeysanju.jetquotes.view.viewModel.MainViewModel

@Composable
fun DetailCard(quote: String, author: String) {
fun DetailCard(viewModel: MainViewModel, quote: String, author: String) {
val context = LocalContext.current

Box(modifier = Modifier.fillMaxSize()) {
Expand All @@ -73,7 +75,9 @@ fun DetailCard(quote: String, author: String) {
) {

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

Text(
modifier = Modifier.align(Alignment.CenterHorizontally),
modifier = Modifier
.align(Alignment.CenterHorizontally)
.wrapContentSize(align = Alignment.Center),
text = quote.ifBlank { " No Quotes found" },
style = typography.h5,
color = MaterialTheme.colors.onBackground,
Expand All @@ -100,7 +106,7 @@ fun DetailCard(quote: String, author: String) {
textAlign = TextAlign.Center
)
}
CTAButtons(quote, author)
CTAButtons(viewModel, quote, author)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ fun QuotesCard(quote: Quote, actions: MainActions) {
.wrapContentSize()
.padding(12.dp)
.clickable(onClick = {
actions.quoteDetails(quote.quote!!, quote.author!!)
actions.gotoDetails(quote.quote, quote.author!!)

})
.background(MaterialTheme.colors.primaryVariant)
.padding(12.dp)) {
Expand All @@ -66,7 +67,7 @@ fun QuotesCard(quote: Quote, actions: MainActions) {
)

Text(
text = quote.quote.toString(),
text = quote.quote,
style = typography.body1,
color = MaterialTheme.colors.onBackground,
modifier = Modifier.padding(12.dp, 0.dp, 0.dp, 0.dp)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
*
* *
* * * 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.components

import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
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.unit.dp
import www.spikeysanju.jetquotes.R
import www.spikeysanju.jetquotes.ui.typography


@Composable
fun TopBar(title: String, onToggle: () -> Unit, onFavouritesClick: () -> Unit) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(70.dp)
.background(MaterialTheme.colors.background), contentAlignment = Alignment.CenterStart
) {
Row(
modifier = Modifier.fillMaxWidth(),
Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = title,
style = typography.h5,
color = MaterialTheme.colors.onPrimary,
modifier = Modifier.padding(start = 16.dp)
)
Row(modifier = Modifier.wrapContentSize(), Arrangement.End) {

IconButton(
onClick = { onFavouritesClick() },
modifier = Modifier.padding(end = 16.dp)
) {
val settingsIcon: Painter = painterResource(id = R.drawable.ic_heart)
Icon(
painter = settingsIcon,
contentDescription = "Favourites Icon",
tint = MaterialTheme.colors.onPrimary
)
}

IconButton(onClick = { onToggle() }, modifier = Modifier.padding(end = 16.dp)) {
val toggleIcon = if (isSystemInDarkTheme())
painterResource(id = R.drawable.ic_night)
else
painterResource(id = R.drawable.ic_day)
Icon(
painter = toggleIcon,
contentDescription = "Day/Night Icon",
tint = MaterialTheme.colors.onPrimary
)
}

}

}
}
}


@Composable
fun TopBarWithBack(title: String, onBackClick: () -> Unit) {
Row(
modifier = Modifier.fillMaxWidth(),
Arrangement.Start,
verticalAlignment = Alignment.CenterVertically
) {
IconButton(onClick = { onBackClick() }, modifier = Modifier.padding(start = 16.dp)) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = "Back Icon",
tint = MaterialTheme.colors.onPrimary
)
}

Text(
text = title,
style = typography.h6,
color = MaterialTheme.colors.onPrimary,
modifier = Modifier.padding(start = 16.dp)
)

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* *
* * * 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.db

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import www.spikeysanju.jetquotes.model.Quote

@Dao
interface FavouritesDao {

@Query("SELECT * FROM favourites")
fun getAllFavourites(): Flow<List<Quote>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertFavourite(quote: Quote)

@Delete
suspend fun delete(quote: Quote)

@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateFavourite(quote: Quote)

}
Loading

0 comments on commit eb77b6c

Please sign in to comment.