fix some minor stuff

This commit is contained in:
Mylloon 2024-01-05 10:15:15 +01:00
parent 10b207e3f5
commit 7be0eeea21
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 42 additions and 35 deletions

View file

@ -20,8 +20,8 @@
- [x] Modifier la question - [x] Modifier la question
- [x] Supprimer la question - [x] Supprimer la question
- [ ] Afficher les statistiques du jeu de question en cours - [ ] Afficher les statistiques du jeu de question en cours
- [ ] Notification - [x] Notification
- [ ] une fois par jour - [x] une fois par jour
- [x] Paramètres - [x] Paramètres
- [ ] Temps de réponse aux questions - [ ] Temps de réponse aux questions
- [ ] Thème - [ ] Thème

View file

@ -6,7 +6,6 @@ import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.util.Log
import androidx.work.PeriodicWorkRequest import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager import androidx.work.WorkManager
import fr.uparis.diamantkennel.memorisationapplication.data.QuestionsDB import fr.uparis.diamantkennel.memorisationapplication.data.QuestionsDB
@ -22,18 +21,15 @@ class MemoApplication : Application() {
super.onCreate() super.onCreate()
createChannel(this) createChannel(this)
if (this.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) if (this.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
schedule() schedule()
else }
Log.d("MemoApplication", "onCreate: no permission")
} }
private fun createChannel(c: Context) private fun createChannel(c: Context) {
{ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) val name = c.getString(R.string.notif_channel_name)
{ val descriptionText = c.getString(R.string.notif_channel_desc)
val name = "MY_CHANNEL"
val descriptionText = "notification channel for Memorisation project"
val importance = NotificationManager.IMPORTANCE_DEFAULT val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText description = descriptionText
@ -44,15 +40,13 @@ class MemoApplication : Application() {
} }
} }
private fun schedule() private fun schedule() {
{
val wm = WorkManager.getInstance(this) val wm = WorkManager.getInstance(this)
wm.cancelAllWork() wm.cancelAllWork()
wm.enqueue(request(10, 45)) wm.enqueue(request(10, 45))
} }
private fun request(h: Int, m: Int): PeriodicWorkRequest private fun request(h: Int, m: Int): PeriodicWorkRequest {
{
val now = Calendar.getInstance() val now = Calendar.getInstance()
val target = Calendar.getInstance().apply { val target = Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, h) set(Calendar.HOUR_OF_DAY, h)
@ -60,11 +54,10 @@ class MemoApplication : Application() {
} }
if (target.before(now)) if (target.before(now))
target.add(Calendar.DAY_OF_YEAR, 1) target.add(Calendar.DAY_OF_YEAR, 1)
val delta=target.timeInMillis - now.timeInMillis val delta = target.timeInMillis - now.timeInMillis
val request = PeriodicWorkRequest.Builder(RappelWorker::class.java, 1, TimeUnit.DAYS)
.setInitialDelay(delta, TimeUnit.MILLISECONDS) return PeriodicWorkRequest.Builder(RappelWorker::class.java, 1, TimeUnit.DAYS)
.build() .setInitialDelay(delta, TimeUnit.MILLISECONDS)
Log.d("Periodic", "request: $request") .build()
return request
} }
} }

View file

@ -16,8 +16,7 @@ class RappelWorker(private val context: Context, workerParams: WorkerParameters)
return Result.success() return Result.success()
} }
private fun createNotification(context: Context) private fun createNotification(context: Context) {
{
val intent = Intent(context, MainActivity::class.java) val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity( val pendingIntent = PendingIntent.getActivity(
context, 0, intent, PendingIntent.FLAG_IMMUTABLE context, 0, intent, PendingIntent.FLAG_IMMUTABLE
@ -25,8 +24,8 @@ class RappelWorker(private val context: Context, workerParams: WorkerParameters)
val builder = NotificationCompat.Builder(context, CHANNEL_ID) val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground) .setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Rappel") .setContentTitle(context.getString(R.string.notif_reminder_title))
.setContentText("Il est temps de réviser !") .setContentText(context.getString(R.string.notif_reminder_content))
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
val notification = builder.build() val notification = builder.build()

View file

@ -1,8 +1,9 @@
package fr.uparis.diamantkennel.memorisationapplication package fr.uparis.diamantkennel.memorisationapplication
import android.util.Log import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
@ -29,17 +30,22 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import fr.uparis.diamantkennel.memorisationapplication.ui.SettingsViewModel import fr.uparis.diamantkennel.memorisationapplication.ui.SettingsViewModel
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@Composable @Composable
fun SettingsScreen(padding: PaddingValues, model: SettingsViewModel = viewModel()) { fun SettingsScreen(padding: PaddingValues, model: SettingsViewModel = viewModel()) {
val context = LocalContext.current val context = LocalContext.current
var deletionDBRequest by model.deletionDB var deletionDBRequest by model.deletionDB
var cleanStatRequest by model.deletionStat var cleanStatRequest by model.deletionStat
var permissionNotif by model.gavePermissionNow
model.checkPermission(context)
val permissionLauncher = rememberLauncherForActivityResult( val permissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(), contract = ActivityResultContracts.RequestPermission(),
) { ) {
Log.d("permissions", if(it) "granted" else "denied") if (it) {
permissionNotif = true
}
} }
if (deletionDBRequest) { if (deletionDBRequest) {
@ -81,7 +87,7 @@ fun SettingsScreen(padding: PaddingValues, model: SettingsViewModel = viewModel(
} }
Button( Button(
enabled = model.isNotGranted(context), enabled = !permissionNotif,
onClick = { onClick = {
model.requestNotificationPermission(permissionLauncher) model.requestNotificationPermission(permissionLauncher)
} }

View file

@ -3,7 +3,9 @@ package fr.uparis.diamantkennel.memorisationapplication.ui
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.RequiresApi
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.core.intPreferencesKey
@ -36,6 +38,8 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
val deletionDB = mutableStateOf(false) val deletionDB = mutableStateOf(false)
val deletionStat = mutableStateOf(false) val deletionStat = mutableStateOf(false)
val gavePermissionNow = mutableStateOf(false)
fun deleteDb() { fun deleteDb() {
deletionDB.value = false deletionDB.value = false
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
@ -63,13 +67,14 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
return ((good.toFloat() / total.toFloat()) * 100).toInt() return ((good.toFloat() / total.toFloat()) * 100).toInt()
} }
fun requestNotificationPermission(launcher: ActivityResultLauncher<String>) @RequiresApi(Build.VERSION_CODES.TIRAMISU)
{ fun requestNotificationPermission(launcher: ActivityResultLauncher<String>) {
launcher.launch(android.Manifest.permission.POST_NOTIFICATIONS) launcher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
} }
fun isNotGranted(context: Context): Boolean @RequiresApi(Build.VERSION_CODES.TIRAMISU)
{ fun checkPermission(context: Context) {
return context.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED gavePermissionNow.value =
context.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
} }
} }

View file

@ -51,4 +51,8 @@
<string name="clean_stat">Réinitialiser les statistiques</string> <string name="clean_stat">Réinitialiser les statistiques</string>
<string name="clean_stat_desc">Voulez-vous réinitialiser les statistiques ?</string> <string name="clean_stat_desc">Voulez-vous réinitialiser les statistiques ?</string>
<string name="permission_button">Autoriser notification</string> <string name="permission_button">Autoriser notification</string>
<string name="notif_channel_name">Rappels</string>
<string name="notif_channel_desc">Notifications de rappel</string>
<string name="notif_reminder_title">Rappel</string>
<string name="notif_reminder_content">Il est temps de réviser !</string>
</resources> </resources>