Skip to content

Commit

Permalink
Merge pull request #28 from NielsLee/dev
Browse files Browse the repository at this point in the history
Publish v1.8
  • Loading branch information
NielsLee authored Aug 11, 2024
2 parents a6aaab9 + 83beba5 commit 9e5cce1
Show file tree
Hide file tree
Showing 28 changed files with 269 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "lying.fengfeng.foodrecords"
minSdk 26
targetSdk 34
versionCode 8
versionName "1.7"
versionCode 9
versionName "1.8"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 7,
"versionName": "1.6",
"versionCode": 9,
"versionName": "1.8",
"outputFile": "app-release.apk"
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"formatVersion": 1,
"database": {
"version": 4,
"identityHash": "0d6f2862611e707ae0a56726870b569f",
"entities": [
{
"tableName": "FoodInfo",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`foodName` TEXT NOT NULL, `productionDate` TEXT NOT NULL, `foodType` TEXT NOT NULL, `shelfLife` TEXT NOT NULL, `expirationDate` TEXT NOT NULL, `uuid` TEXT NOT NULL, `amount` INTEGER NOT NULL, `tips` TEXT NOT NULL, PRIMARY KEY(`uuid`))",
"fields": [
{
"fieldPath": "foodName",
"columnName": "foodName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "productionDate",
"columnName": "productionDate",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "foodType",
"columnName": "foodType",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "shelfLife",
"columnName": "shelfLife",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "expirationDate",
"columnName": "expirationDate",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "uuid",
"columnName": "uuid",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "amount",
"columnName": "amount",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "tips",
"columnName": "tips",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"uuid"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '0d6f2862611e707ae0a56726870b569f')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,20 @@ class MigrationTest {

db = helper.runMigrationsAndValidate("FoodInfoDatabase", 3, true, MIGRATION_2_3)
}

@Test
@Throws(IOException::class)
fun migrate3To4() {
var db = helper.createDatabase("FoodInfoDatabase", 3).apply {
close()
}

val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE FoodInfo ADD COLUMN amount INTEGER NOT NULL DEFAULT 1")
}
}

db = helper.runMigrationsAndValidate("FoodInfoDatabase", 3, true, MIGRATION_3_4)
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/lying/fengfeng/foodrecords/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Constants {
const val DB_NAME_FOOD_INFO = "FoodInfoDatabase"
const val DB_NAME_FOOD_TYPE_INFO = "FoodTypeInfoDatabase"
const val DB_NAME_SHELF_LIFE_INFO = "ShelfLifeInfoDatabase"
const val FOOD_INFO_DB_VERSION = 3
const val FOOD_INFO_DB_VERSION = 4
const val FOOD_TYPE_DB_VERSION = 1
const val SHELF_LIFE_DB_VERSION = 1
}
6 changes: 4 additions & 2 deletions app/src/main/java/lying/fengfeng/foodrecords/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class MainActivity : ComponentActivity() {
foodInfo.shelfLife,
foodInfo.expirationDate,
foodInfo.tips,
foodInfo.amount,
Base64Util.fileToBase64(AppRepo.getPicturePath(foodInfo.uuid))
)
}
Expand Down Expand Up @@ -101,12 +102,13 @@ class MainActivity : ComponentActivity() {
productionDate = line[3],
shelfLife = line[4],
expirationDate = line[5],
tips = line[6]
tips = line[6],
amount = line[7].toInt()
).also {
foodInfoList.add(it)
appViewModel.addFoodInfo(it)
}
Base64Util.base64ToFile(line[7], AppRepo.getPicturePath(line[0]))
Base64Util.base64ToFile(line[8], AppRepo.getPicturePath(line[0]))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import lying.fengfeng.foodrecords.utils.DateUtil
import kotlin.math.absoluteValue

@Entity
data class FoodInfo(
Expand All @@ -13,5 +14,12 @@ data class FoodInfo(
@ColumnInfo val shelfLife: String,
@ColumnInfo val expirationDate: String,
@PrimaryKey val uuid: String,
@ColumnInfo var amount: Int,
@ColumnInfo val tips: String = ""
)
) {
fun getSortIndex(): Int {
val remainingDate = DateUtil.getRemainingDays(productionDate, shelfLife, expirationDate)
val nameHashNum = foodName.hashCode().absoluteValue % 10
return 10 * remainingDate + nameHashNum
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ object AppRepo {
cursor.close()
}
}
private val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE FoodInfo ADD COLUMN amount INTEGER NOT NULL DEFAULT 1")
}
}

fun init(application: Application) {

Expand All @@ -75,6 +80,7 @@ object AppRepo {
foodInfoDB = Room.databaseBuilder(app, FoodInfoDatabase::class.java, DB_NAME_FOOD_INFO)
.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_2_3)
.addMigrations(MIGRATION_3_4)
.build()
foodInfoDao = foodInfoDB.foodInfoDao()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ class FoodRecordsAppViewModel: ViewModel() {
AppRepo.removeShelfLifeInfo(shelfLifeInfo)
}

fun updateFoodInfo(foodInfo: FoodInfo) {
foodInfoList.also {
if (it.contains(foodInfo)) {
it.remove(foodInfo)
it.add(foodInfo)
AppRepo.addFoodInfo(foodInfo)
}
}
}

fun updateDaysBeforeNotification(days: Int) {
daysBeforeNotification.value = days
AppRepo.setDaysBeforeNotification(days)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun FoodRecordsNavHost(

val activityContext = LocalActivityContext.current
val appViewModel: FoodRecordsAppViewModel = viewModel(viewModelStoreOwner = (activityContext as MainActivity))
val foodInfoList = remember { appViewModel.foodInfoList }.sortedBy { DateUtil.getRemainingDays(it.productionDate, it.shelfLife, it.expirationDate) }
val foodInfoList = remember { appViewModel.foodInfoList }.sortedBy { it.getSortIndex() }

NavHost(
navController = navController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddShoppingCart
import androidx.compose.material.icons.filled.DeleteForever
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.ShoppingCartCheckout
import androidx.compose.material.icons.filled.TypeSpecimen
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.MoreHoriz
Expand All @@ -42,6 +44,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -50,6 +53,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
Expand All @@ -68,6 +72,7 @@ import lying.fengfeng.foodrecords.ui.FoodRecordsAppViewModel
import lying.fengfeng.foodrecords.ui.theme.ExpiredGreen
import lying.fengfeng.foodrecords.ui.theme.ExpiredRed
import lying.fengfeng.foodrecords.utils.DateUtil
import lying.fengfeng.foodrecords.utils.EffectUtil
import lying.fengfeng.foodrecords.utils.ImageUtil
import java.io.File
import kotlin.math.absoluteValue
Expand All @@ -86,6 +91,7 @@ fun FoodInfoCard(

val tipsButtonShown by remember { mutableStateOf(foodInfo.tips.isNotEmpty()) }
var tipsShown by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()


Card(
Expand Down Expand Up @@ -178,10 +184,23 @@ fun FoodInfoCard(
) {

Column(
Modifier.padding(horizontal = 8.dp).fillMaxWidth(0.7f)
Modifier
.padding(horizontal = 8.dp)
.fillMaxWidth(0.7f)
) {
val scrollState = rememberScrollState()

Row(
verticalAlignment = Alignment.CenterVertically
) {
Icon(imageVector = Icons.Filled.ShoppingCartCheckout, contentDescription = null)
Text(
text = foodInfo.amount.toString(),
fontStyle = FontStyle.Italic,
modifier = Modifier.horizontalScroll(scrollState)
)
}

Row(
verticalAlignment = Alignment.CenterVertically
) {
Expand Down Expand Up @@ -212,6 +231,7 @@ fun FoodInfoCard(
IconButton(
onClick = {
dropDownMenuExpanded = true
EffectUtil.playVibrationEffect(context)
},
modifier = Modifier
) {
Expand All @@ -223,23 +243,63 @@ fun FoodInfoCard(
onDismissRequest = { dropDownMenuExpanded = false }
) {
DropdownMenuItem(
leadingIcon = {
Icon(
painter = painterResource(id = R.drawable.eat_svg),
null,
)
},
text = {
Row(
Modifier.fillMaxSize()
) {
Icon(imageVector = Icons.Outlined.Delete, null)
Text(text = context.getString(R.string.delete_record))
}
Text(text = context.getString(R.string.eat))
},
onClick = {
EffectUtil.playVibrationEffect(context)
dropDownMenuExpanded = false
CoroutineScope(Dispatchers.IO).launch {
File(AppRepo.getPicturePath(foodInfo.uuid)).also {
if (it.exists()) {
it.delete()
}
coroutineScope.launch(Dispatchers.IO) {
if (foodInfo.amount > 1) {
foodInfo.amount -= 1
appViewModel.updateFoodInfo(foodInfo)
} else {
deleteFood(appViewModel, foodInfo)
}
appViewModel.removeFoodInfo(foodInfo)
}
})

DropdownMenuItem(
leadingIcon = {
Icon(
imageVector = Icons.Filled.AddShoppingCart,
null,
)
},
text = {
Text(text = context.getString(R.string.supplement))
},
onClick = {
EffectUtil.playVibrationEffect(context)
dropDownMenuExpanded = false
coroutineScope.launch(Dispatchers.IO) {
foodInfo.amount += 1
appViewModel.updateFoodInfo(foodInfo)
}
})

DropdownMenuItem(
leadingIcon = {
Icon(
imageVector = Icons.Outlined.Delete,
null,
modifier = Modifier.padding(1.dp)
)
},
text = {
Text(text = context.getString(R.string.delete_record),)
},
onClick = {
EffectUtil.playVibrationEffect(context)
dropDownMenuExpanded = false
coroutineScope.launch(Dispatchers.IO) {
deleteFood(appViewModel, foodInfo)
}
})
}
Expand Down Expand Up @@ -285,6 +345,15 @@ fun FoodInfoCard(
}
}

fun deleteFood(appViewModel: FoodRecordsAppViewModel, foodInfo: FoodInfo) {
File(AppRepo.getPicturePath(foodInfo.uuid)).also {
if (it.exists()) {
it.delete()
}
}
appViewModel.removeFoodInfo(foodInfo)
}

@Composable
fun RemainingDaysWindow(
productionDate: String,
Expand Down
Loading

0 comments on commit 9e5cce1

Please sign in to comment.