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] Supprimer la question
- [ ] Afficher les statistiques du jeu de question en cours
- [ ] Notification
- [ ] une fois par jour
- [x] Notification
- [x] une fois par jour
- [x] Paramètres
- [ ] Temps de réponse aux questions
- [ ] Thème

View file

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

View file

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

View file

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

View file

@ -3,7 +3,9 @@ package fr.uparis.diamantkennel.memorisationapplication.ui
import android.app.Application
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.RequiresApi
import androidx.compose.runtime.mutableStateOf
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
@ -36,6 +38,8 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
val deletionDB = mutableStateOf(false)
val deletionStat = mutableStateOf(false)
val gavePermissionNow = mutableStateOf(false)
fun deleteDb() {
deletionDB.value = false
viewModelScope.launch(Dispatchers.IO) {
@ -63,13 +67,14 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
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)
}
fun isNotGranted(context: Context): Boolean
{
return context.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun checkPermission(context: Context) {
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_desc">Voulez-vous réinitialiser les statistiques ?</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>