Skip to content

Commit

Permalink
Code quality improvements & optimizations (#97)
Browse files Browse the repository at this point in the history
While on that, also update compose BOM
---------
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Apr 8, 2024
1 parent 56508b0 commit cefb798
Show file tree
Hide file tree
Showing 31 changed files with 1,054 additions and 855 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ aboutLibraries {

dependencies {

def composeBom = platform('androidx.compose:compose-bom:2024.02.02')
def composeBom = platform('androidx.compose:compose-bom:2024.03.00')
implementation composeBom
androidTestImplementation composeBom

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/starry/greenstash/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import androidx.lifecycle.ViewModelProvider
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.starry.greenstash.ui.navigation.NavGraph
import com.starry.greenstash.ui.screens.settings.viewmodels.SettingsViewModel
import com.starry.greenstash.ui.screens.settings.viewmodels.ThemeMode
import com.starry.greenstash.ui.screens.settings.SettingsViewModel
import com.starry.greenstash.ui.screens.settings.ThemeMode
import com.starry.greenstash.ui.theme.GreenStashTheme
import com.starry.greenstash.utils.Utils
import dagger.hilt.android.AndroidEntryPoint
Expand Down
13 changes: 1 addition & 12 deletions app/src/main/java/com/starry/greenstash/di/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
package com.starry.greenstash.di

import android.content.Context
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.ui.ExperimentalComposeUiApi
import com.starry.greenstash.backup.BackupManager
import com.starry.greenstash.database.core.AppDatabase
import com.starry.greenstash.database.goal.GoalDao
Expand All @@ -43,15 +38,9 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.ExperimentalCoroutinesApi
import javax.inject.Singleton

@ExperimentalCoroutinesApi
@ExperimentalMaterialApi
@ExperimentalFoundationApi
@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@ExperimentalMaterial3Api

@InstallIn(SingletonComponent::class)
@Module
class MainModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@ class ReminderNotificationSender(

val remainingAmount = (goal.targetAmount - goalItem.getCurrentlySavedAmount())
val defCurrency = preferenceUtil.getString(PreferenceUtil.DEFAULT_CURRENCY_STR, "")!!
val datePattern = preferenceUtil.getString(PreferenceUtil.DATE_FORMAT_STR, "")!!

if (goal.deadline.isNotEmpty() && goal.deadline.isNotBlank()) {
val calculatedDays = GoalTextUtils(preferenceUtil).calcRemainingDays(goal)
val calculatedDays = GoalTextUtils.calcRemainingDays(goal, datePattern)
when (goal.priority) {
GoalPriority.High -> {
val amountDay = remainingAmount / calculatedDays.remainingDays
notification.addAction(
R.drawable.ic_notification_deposit,
"${context.getString(R.string.deposit_button)} ${
Utils.formatCurrency(Utils.roundDecimal(amountDay), defCurrency)
Utils.formatCurrency(
amount = Utils.roundDecimal(amountDay),
currencyCode = defCurrency
)
}",
createDepositIntent(goal.goalId, amountDay)
)
Expand All @@ -92,7 +96,10 @@ class ReminderNotificationSender(
notification.addAction(
R.drawable.ic_notification_deposit,
"${context.getString(R.string.deposit_button)} ${
Utils.formatCurrency(Utils.roundDecimal(amountSemiWeek), defCurrency)
Utils.formatCurrency(
amount = Utils.roundDecimal(amountSemiWeek),
currencyCode = defCurrency
)
}",
createDepositIntent(goal.goalId, amountSemiWeek)
)
Expand All @@ -103,7 +110,10 @@ class ReminderNotificationSender(
notification.addAction(
R.drawable.ic_notification_deposit,
"${context.getString(R.string.deposit_button)} ${
Utils.formatCurrency(Utils.roundDecimal(amountWeek), defCurrency)
Utils.formatCurrency(
amount = Utils.roundDecimal(amountWeek),
currencyCode = defCurrency
)
}",
createDepositIntent(goal.goalId, amountWeek)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -66,15 +67,46 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


/**
* Data class to hold the currency names and values for the currency picker.
* @param currencyNames Array of currency names.
* @param currencyValues Array of currency values.
*/
@Immutable
data class CurrencyPickerData(
val currencyNames: Array<String>,
val currencyValues: Array<String>
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as CurrencyPickerData

if (!currencyNames.contentEquals(other.currencyNames)) return false
if (!currencyValues.contentEquals(other.currencyValues)) return false

return true
}

override fun hashCode(): Int {
var result = currencyNames.contentHashCode()
result = 31 * result + currencyValues.contentHashCode()
return result
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CurrencyPicker(
defaultCurrencyValue: String,
currencyNames: Array<String>,
currencyValues: Array<String>,
currencyPickerData: CurrencyPickerData,
showBottomSheet: MutableState<Boolean>,
onCurrencySelected: (String) -> Unit
) {
val currencyNames = currencyPickerData.currencyNames
val currencyValues = currencyPickerData.currencyValues

val defaultCurrencyEntry = currencyNames[currencyValues.indexOf(defaultCurrencyValue)]
val (selectedCurrencyOption, onCurrencyOptionSelected) = remember {
mutableStateOf(defaultCurrencyEntry)
Expand Down
113 changes: 113 additions & 0 deletions app/src/main/java/com/starry/greenstash/ui/common/DateTimeCard.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* MIT License
*
* Copyright (c) [2022 - Present] Stɑrry Shivɑm
*
* 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 com.starry.greenstash.ui.common

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.starry.greenstash.R
import com.starry.greenstash.ui.screens.settings.DateStyle
import com.starry.greenstash.ui.theme.greenstashFont
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@Composable
fun DateTimeCard(
selectedDateTime: LocalDateTime,
dateTimeStyle: () -> DateStyle,
onClick: () -> Unit
) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 18.dp, vertical = 8.dp)
.clickable { onClick() }
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.Center
) {
Row {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_dw_date),
contentDescription = null,
modifier = Modifier.size(24.dp)
)
Text(
text = selectedDateTime.format(
DateTimeFormatter.ofPattern(dateTimeStyle().pattern)
),
fontFamily = greenstashFont,
fontWeight = FontWeight.Medium,
modifier = Modifier.padding(start = 8.dp, top = 2.dp)
)
}

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

Row {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_dw_time),
contentDescription = null,
modifier = Modifier.size(24.dp)
)
Text(
text = selectedDateTime.format(DateTimeFormatter.ofPattern("h:mm a")),
fontFamily = greenstashFont,
fontWeight = FontWeight.Medium,
modifier = Modifier.padding(start = 8.dp, top = 2.dp)
)
}
}
}
}

@Preview
@Composable
private fun DateTimeCardPreview() {
DateTimeCard(
selectedDateTime = LocalDateTime.now(),
dateTimeStyle = { DateStyle.DateMonthYear },
onClick = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.starry.greenstash.ui.screens.backups.BackupScreen
import com.starry.greenstash.ui.screens.backups.composables.BackupScreen
import com.starry.greenstash.ui.screens.dwscreen.composables.DWScreen
import com.starry.greenstash.ui.screens.home.composables.HomeScreen
import com.starry.greenstash.ui.screens.info.composables.GoalInfoScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/


package com.starry.greenstash.ui.screens.backups
package com.starry.greenstash.ui.screens.backups.composables

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -71,6 +71,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.starry.greenstash.R
import com.starry.greenstash.ui.screens.backups.BackupViewModel
import com.starry.greenstash.ui.theme.greenstashFont
import kotlinx.coroutines.launch
import java.io.InputStreamReader
Expand Down Expand Up @@ -154,7 +155,7 @@ fun BackupScreen(navController: NavController) {
}

@Composable
fun BackupScreenContent(
private fun BackupScreenContent(
paddingValues: PaddingValues, onBackupClicked: () -> Unit, onRestoreClicked: () -> Unit
) {
Column(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.starry.greenstash.ui.screens.input.viewmodels
package com.starry.greenstash.ui.screens.dwscreen

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -9,7 +9,7 @@ import com.starry.greenstash.database.goal.GoalDao
import com.starry.greenstash.database.transaction.Transaction
import com.starry.greenstash.database.transaction.TransactionDao
import com.starry.greenstash.database.transaction.TransactionType
import com.starry.greenstash.ui.screens.settings.viewmodels.DateStyle
import com.starry.greenstash.ui.screens.settings.DateStyle
import com.starry.greenstash.utils.PreferenceUtil
import com.starry.greenstash.utils.Utils
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -33,9 +33,17 @@ class DWViewModel @Inject constructor(

var state by mutableStateOf(DWScreenState())

fun getDateStyleValue() = preferenceUtil.getString(
PreferenceUtil.DATE_FORMAT_STR, DateStyle.DateMonthYear.pattern
)
fun getDateStyle(): DateStyle {
val dateStyleValue = preferenceUtil.getString(
PreferenceUtil.DATE_FORMAT_STR,
DateStyle.DateMonthYear.pattern
)
return if (dateStyleValue == DateStyle.DateMonthYear.pattern) {
DateStyle.DateMonthYear
} else {
DateStyle.YearMonthDate
}
}

fun convertTransactionType(type: String): TransactionType {
return when (type) {
Expand Down
Loading

0 comments on commit cefb798

Please sign in to comment.