Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr 210 #211

Closed
wants to merge 15 commits into from
13 changes: 9 additions & 4 deletions app/src/main/java/com/mensinator/app/CalendarScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ fun CalendarScreen() {
var previousFirstPeriodDate by remember { mutableStateOf<LocalDate?>(null) }
val colorMap = ColorSource.getColorMap(isDarkMode())

val messageText = dbHelper.getSettingByKey("period_notification_message").toString()

val circleSize = 30.dp

// Colors from app_settings in the database
Expand Down Expand Up @@ -507,7 +509,8 @@ fun CalendarScreen() {
context,
notificationScheduler,
reminderDays,
nextPeriodDate
nextPeriodDate,
messageText
)
}
Toast.makeText(context, successSaved, Toast.LENGTH_SHORT).show()
Expand Down Expand Up @@ -581,7 +584,8 @@ fun CalendarScreen() {
context,
notificationScheduler,
reminderDays,
nextPeriodDate
nextPeriodDate,
messageText
)
}
},
Expand Down Expand Up @@ -648,7 +652,7 @@ fun containsOvulationDate(selectedDates: Set<LocalDate>, ovulationDates: Set<Loc
}
}

fun newSendNotification(context: Context, scheduler: INotificationScheduler, daysForReminding: Int, periodDate: LocalDate) {
fun newSendNotification(context: Context, scheduler: INotificationScheduler, daysForReminding: Int, periodDate: LocalDate, messageText: String) {
val notificationDate = periodDate.minusDays(daysForReminding.toLong())
if (notificationDate.isBefore(LocalDate.now())) {
Log.d(
Expand All @@ -662,7 +666,8 @@ fun newSendNotification(context: Context, scheduler: INotificationScheduler, day
).show()
} else {
//Schedule notification
scheduler.scheduleNotification(notificationDate)

scheduler.scheduleNotification(notificationDate, messageText)
Log.d("CalendarScreen", "Notification scheduled for $notificationDate")
}
}
13 changes: 11 additions & 2 deletions app/src/main/java/com/mensinator/app/DatabaseUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ object DatabaseUtils {
// LI == List
// NO == Number
// SW == Switch
// TX == Text
db.execSQL("""
ALTER TABLE app_settings ADD COLUMN setting_type TEXT
""")
Expand Down Expand Up @@ -180,7 +181,15 @@ object DatabaseUtils {
VALUES
('screen_protection', 'Protect screen', '1', '3', 'SW')
""")


}
fun databaseVersion9(db: SQLiteDatabase) {
//Insert new row for custom period notification message
db.execSQL(
"""
INSERT INTO app_settings(setting_key, setting_label, setting_value, group_label_id, setting_type)
VALUES
('period_notification_message', 'Period Notification Message', 'Your period is about to start', '2', 'TX')
"""
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.mensinator.app
import java.time.LocalDate

interface INotificationScheduler {
fun scheduleNotification(notificationDate: LocalDate)
fun scheduleNotification(notificationDate: LocalDate, messageText: String)
fun cancelNotification(notificationId: Int)
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/mensinator/app/NotificationReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import androidx.core.app.NotificationManagerCompat

class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val messageText = intent.getStringExtra("messageText")
// Create and show the notification
val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.baseline_bloodtype_24)
.setContentTitle("Mensinator")
.setContentText("Your period is about to start")
.setContentText(messageText)
.setPriority(NotificationCompat.PRIORITY_HIGH)

val notificationManager = NotificationManagerCompat.from(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class NotificationScheduler(
private const val ACTION_NOTIFICATION = "com.mensinator.app.SEND_NOTIFICATION"
}

override fun scheduleNotification(notificationDate: LocalDate) {
override fun scheduleNotification(notificationDate: LocalDate, messageText: String) {
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.cancelAll()
Log.d("NotificationScheduler", "Notification canceled")
val notificationTimeInMillis = notificationDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()

val intent = Intent(context, NotificationReceiver::class.java).apply {
action = ACTION_NOTIFICATION
putExtra("messageText", messageText)
}
val pendingIntent = PendingIntent.getBroadcast(
context,
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/mensinator/app/PeriodDatabaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PeriodDatabaseHelper(context: Context) :

companion object {
private const val DATABASE_NAME = "periods.db"
private const val DATABASE_VERSION = 8
private const val DATABASE_VERSION = 9
private const val TABLE_PERIODS = "periods"
private const val COLUMN_ID = "id"
private const val COLUMN_DATE = "date"
Expand Down Expand Up @@ -82,6 +82,9 @@ class PeriodDatabaseHelper(context: Context) :
if (oldVersion < 8) {
DatabaseUtils.databaseVersion8(db)
}
if (oldVersion < 9) {
DatabaseUtils.databaseVersion9(db)
}
}

override fun addDateToPeriod(date: LocalDate, periodId: Int) {
Expand Down Expand Up @@ -819,4 +822,4 @@ class PeriodDatabaseHelper(context: Context) :
db.close()
return latestPeriodStart
}
}
}
63 changes: 63 additions & 0 deletions app/src/main/java/com/mensinator/app/settings/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -30,12 +31,14 @@ import androidx.core.app.NotificationManagerCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.mensinator.app.ExportDialog
import com.mensinator.app.FaqDialog
import com.mensinator.app.IPeriodDatabaseHelper
import com.mensinator.app.ImportDialog
import com.mensinator.app.R
import com.mensinator.app.data.ColorSource
import com.mensinator.app.ui.theme.MensinatorTheme
import com.mensinator.app.ui.theme.isDarkMode
import org.koin.androidx.compose.koinViewModel
import org.koin.compose.koinInject

private val colorCircleSize = 24.dp

Expand All @@ -51,6 +54,7 @@ object ResourceMapper {
"expected_period_color" to R.string.expected_period_color,
"ovulation_color" to R.string.ovulation_color,
"expected_ovulation_color" to R.string.expected_ovulation_color,
"period_notification_message" to R.string.period_notification_message,
"reminders" to R.string.reminders,
"reminder_days" to R.string.days_before_reminder,
"other_settings" to R.string.other_settings,
Expand Down Expand Up @@ -126,6 +130,10 @@ fun SettingsScreen(
},
onOpenIntPicker = { viewModel.showIntPicker(it) }
)
SettingText(
text = stringResource(R.string.period_notification_message),
dbKey = "period_notification_message"
)

Spacer(Modifier.height(16.dp))

Expand Down Expand Up @@ -443,6 +451,61 @@ private fun ColorPickerPreview() {
}
}

@Composable
private fun SettingText(
text: String,
modifier: Modifier = Modifier,
dbKey: String,
) {
val dbHelper: IPeriodDatabaseHelper = koinInject()
val message = dbHelper.getSettingByKey(dbKey)?.value.toString()
var showDialog by remember { mutableStateOf(false) }
var newMessage by remember { mutableStateOf(message) }

Row(
modifier = modifier
.fillMaxWidth()
.padding(vertical = 8.dp)
.clickable{ showDialog = true },
verticalAlignment = Alignment.CenterVertically
) {
Text(text = text, modifier = Modifier.weight(1f))
}

if (showDialog) {
AlertDialog(
onDismissRequest = {
newMessage = message
showDialog = false
},
title = { Text(text = text) },
text = {
TextField(
value = newMessage,
onValueChange = { newMessage = it },
singleLine = false
)
},
confirmButton = {
Button(onClick = {
dbHelper.updateSetting(key = dbKey, value = newMessage)
showDialog = false
}) {
Text(text = stringResource(id = R.string.save_button))
}
},
dismissButton = {
Button(onClick = {
newMessage = message
showDialog = false
}) {
Text(text = stringResource(id = R.string.cancel_button))
}
}
)
}
}

@Composable
private fun SettingNumberSelection(
intSetting: IntSetting,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-sv/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<string name="expected_ovulation_color">Förväntad ägglossning</string>
<string name="reminders">Påminnelser</string>
<string name="days_before_reminder">Dagar före påminnelse</string>
<string name="period_notification_message">Notifieringstext för mens</string>
<string name="other_settings">Andra inställningar</string>
<string name="luteal_phase_calculation">Lutealberäkning</string>
<string name="period_history">Menshistorik</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<string name="expected_ovulation_color">Expected Ovulation</string>
<string name="reminders">Reminders</string>
<string name="days_before_reminder">Days Before Reminder</string>
<string name="period_notification_message">Period Notification Message</string>

Check warning

Code scanning / Android Lint

Incomplete translation Warning

"period_notification_message" is not translated in "de" (German), "hi" (Hindi), "sl" (Slovenian), "it" (Italian), "bn" (Bangla), "fr" (French), "pl" (Polish), "ro" (Romanian), "ta" (Tamil), "es" (Spanish)
<string name="other_settings">Other Settings</string>
<string name="luteal_phase_calculation">Luteal Phase Calculation</string>
<string name="period_history">Period History</string>
Expand Down
Loading