Skip to content

Commit

Permalink
Merge branch 'lorenzovngl:main' into search-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragkanojiya1 authored Jan 6, 2025
2 parents 0693488 + 8ec924a commit 6de88d6
Show file tree
Hide file tree
Showing 20 changed files with 583 additions and 43 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ This simple app helps you avoid forgetting to consume foods that are about to ex
<a href="https://github.com/Maha-Rajan"><img src="https://avatars.githubusercontent.com/Maha-Rajan" alt="@Maha-Rajan" height="32" width="32"></a>
<a href="https://github.com/anuragkanojiya1"><img src="https://avatars.githubusercontent.com/anuragkanojiya1" alt="@anuragkanojiya1" height="32" width="32"></a>
<a href="https://github.com/PrakashIrom"><img src="https://avatars.githubusercontent.com/PrakashIrom" alt="@PrakashIrom" height="32" width="32"></a>
<a href="https://github.com/serAKL16lysA"><img src="https://avatars.githubusercontent.com/serAKL16lysA" alt="@serAKL16lysA" height="32" width="32"></a>

## ❤️ Support

Expand Down
12 changes: 8 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ try {

android {
namespace = "com.lorenzovainigli.foodexpirationdates"
compileSdk = 34
compileSdk = 35

defaultConfig {
applicationId = "com.lorenzovainigli.foodexpirationdates"
minSdk = 24
targetSdk = 34
versionCode = 33
versionName = "2.4.2"
targetSdk = 35
versionCode = 35
versionName = "2.5.1"

base.archivesName.set("FoodExpirationDates-$versionName")

Expand Down Expand Up @@ -138,6 +138,10 @@ dependencies {
implementation(libs.androidx.navigation.compose)
testImplementation(libs.junit)
testImplementation(libs.junit.jupiter)
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.robolectric)
testImplementation(libs.mockk)
androidTestImplementation(libs.test.core.ktx)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ val contributors = listOf(
Contributor(name = "Maha Rajan", username = "Maha-Rajan"),
Contributor(name = "Anurag Kanojiya", username = "anuragkanojiya1"),
Contributor(name = "Prakash Irom", username = "PrakashIrom"),
Contributor(name = "serAKL16lysA", username = "serAKL16lysA"),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.lorenzovainigli.foodexpirationdates.model

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import com.lorenzovainigli.foodexpirationdates.view.MainActivity
import java.util.Locale
import kotlin.jvm.java

enum class Language(val code: String, val label: String) {
SYSTEM("system", "System"),
ARABIC("ar", "العربية"),
CHINESE_TRADITIONAL("zh-TW", "繁體中文"),
ENGLISH("en", "English"),
FRENCH("fr", "Français"),
GERMAN("de", "Deutsch"),
HINDI("hi", "हिन्दी"),
INDONESIAN("id", "Bahasa Indonesia"),
ITALIAN("it", "Italiano"),
JAPANESE("ja", "日本語"),
POLISH("pl", "Polski"),
RUSSIAN("ru", "Русский"),
SPANISH("es", "Español"),
TAMIL("ta", "தமிழ்"),
TURKISH("tr", "Türkçe"),
VIETNAMESE("vi", "Tiếng Việt");

companion object {
fun fromCode(code: String): Language {
return Language.entries.find { it.code == code } ?: SYSTEM
}
}
}

object LocaleHelper {

fun setLocale(context: Context, language: String): Context {
val locale = if (language == Language.SYSTEM.code) {
Locale.getDefault()
} else {
Locale(language)
}
Locale.setDefault(locale)

val config = Configuration(context.resources.configuration)
config.setLocale(locale)
config.setLayoutDirection(locale)

return context.createConfigurationContext(config)
}

fun changeLanguage(context: Context, newLanguage: String) {
setLocale(context, newLanguage)
val intent = Intent(context, MainActivity::class.java) // or current activity
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
(context as Activity).finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.view.Window
import android.view.WindowManager
import com.lorenzovainigli.foodexpirationdates.R
import com.lorenzovainigli.foodexpirationdates.model.Language
import java.lang.Exception
import java.text.DateFormat
import java.text.SimpleDateFormat
Expand All @@ -21,6 +22,8 @@ class PreferencesRepository {
const val keyThemeMode = "theme_mode"
const val keyTopBarFont = "top_bar_font"
const val keyDynamicColors = "dynamic_colors"
const val keyMonochromeIcons = "monochrome_icons"
const val keyLanguage = "language"
private val availLocaleDateFormats = arrayOf(DateFormat.SHORT, DateFormat.MEDIUM)
private val availOtherDateFormats =
arrayOf(
Expand Down Expand Up @@ -174,8 +177,8 @@ class PreferencesRepository {
sharedPrefs: String = sharedPrefsName,
): Boolean {
try {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.getBoolean(keyDynamicColors, false)
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.getBoolean(keyDynamicColors, false)
} catch (e: Exception){
e.printStackTrace()
}
Expand All @@ -186,9 +189,64 @@ class PreferencesRepository {
context: Context,
sharedPrefs: String = sharedPrefsName,
dynamicColorsEnabled: Boolean
): Boolean {
try {
context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.edit().putBoolean(keyDynamicColors, dynamicColorsEnabled).apply()
return true
} catch (_: Exception){
return false
}
}

fun getMonochromeIcons(
context: Context,
sharedPrefs: String = sharedPrefsName,
): Boolean {
try {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.getBoolean(keyMonochromeIcons, true)
} catch (e: Exception){
e.printStackTrace()
}
return true
}

fun setMonochromeIcons(
context: Context,
sharedPrefs: String = sharedPrefsName,
monochromeIconsEnabled: Boolean
): Boolean {
try {
context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.edit().putBoolean(keyMonochromeIcons, monochromeIconsEnabled).apply()
return true
} catch (_: Exception){
return false
}
}

fun getLanguage(
context: Context,
sharedPrefs: String = sharedPrefsName,
): String {
try {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.getString(keyLanguage, Language.SYSTEM.code)
?: Language.SYSTEM.code
} catch (e: Exception){
e.printStackTrace()
}
return Language.SYSTEM.code
}

fun setLanguage(
context: Context,
sharedPrefs: String = sharedPrefsName,
language: String
) {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.edit().putBoolean(keyDynamicColors, dynamicColorsEnabled).apply()
.edit().putString(keyLanguage, language).apply()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import android.content.Context
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.lorenzovainigli.foodexpirationdates.BuildConfig
import com.lorenzovainigli.foodexpirationdates.R
import com.lorenzovainigli.foodexpirationdates.model.LocaleHelper
import com.lorenzovainigli.foodexpirationdates.model.NotificationManager.Companion.CHANNEL_REMINDERS_ID
import com.lorenzovainigli.foodexpirationdates.model.entity.computeExpirationDate
import com.lorenzovainigli.foodexpirationdates.model.repository.ExpirationDateRepository
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository
import com.lorenzovainigli.foodexpirationdates.showNotification
import kotlinx.coroutines.flow.first
import java.util.Calendar
Expand Down Expand Up @@ -57,10 +60,16 @@ class CheckExpirationsWorker @Inject constructor(
var message = ""
if (sb.toString().length > 2)
message = sb.toString().substring(0, sb.toString().length - 2) + "."
val context = if (BuildConfig.DEBUG) {
LocaleHelper.setLocale(
context = applicationContext,
language = PreferencesRepository.getLanguage(applicationContext)
)
} else applicationContext
showNotification(
context = applicationContext,
context = context,
channelId = CHANNEL_REMINDERS_ID,
title = applicationContext.getString(R.string.your_food_is_expiring),
title = context.getString(R.string.your_food_is_expiring),
message = message
)
return Result.success()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lorenzovainigli.foodexpirationdates.view

import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
Expand All @@ -17,9 +18,10 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.lorenzovainigli.foodexpirationdates.BuildConfig
import com.lorenzovainigli.foodexpirationdates.model.LocaleHelper
import com.lorenzovainigli.foodexpirationdates.model.NotificationManager
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository.Companion.checkAndSetSecureFlags
Expand All @@ -37,7 +39,7 @@ class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
enableEdgeToEdge()

val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition { viewModel.isSplashScreenLoading.value }
Expand Down Expand Up @@ -105,4 +107,13 @@ class MainActivity : ComponentActivity() {
}
}

override fun attachBaseContext(newBase: Context) {
if (BuildConfig.DEBUG) {
val locale = PreferencesRepository.getLanguage(newBase)
super.attachBaseContext(LocaleHelper.setLocale(newBase, locale))
} else {
super.attachBaseContext(newBase)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fun FoodCard(
) {
val context = LocalContext.current
val dateFormat = PreferencesRepository.getUserDateFormat(context)
val monochromeIcons = PreferencesRepository.getMonochromeIcons(context)
val sdf = SimpleDateFormat(dateFormat, context.resources.configuration.locales[0])
val today = Calendar.getInstance()
val twoDaysAgo = Calendar.getInstance().apply {
Expand Down Expand Up @@ -144,10 +145,12 @@ fun FoodCard(
.size(36.dp)
.alpha(0.8f)
) {
drawImage(
image = imageBitmap,
colorFilter = ColorFilter.tint(color, BlendMode.Color)
)
if (monochromeIcons) {
drawImage(
image = imageBitmap,
colorFilter = ColorFilter.tint(color, BlendMode.Color)
)
}
drawImage(
image = imageBitmap,
blendMode = BlendMode.DstAtop
Expand Down
Loading

0 comments on commit 6de88d6

Please sign in to comment.