2020-08-31 23:03:58 +02:00
/ * *
* ProjectLaogai
*
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
package org.mosad.seil0.projectlaogai.worker
2020-09-10 18:26:42 +02:00
import android.app.PendingIntent
2020-08-31 23:03:58 +02:00
import android.content.Context
2020-09-10 18:26:42 +02:00
import android.content.Intent
2020-08-31 23:03:58 +02:00
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import kotlinx.coroutines.runBlocking
2020-09-10 18:26:42 +02:00
import org.mosad.seil0.projectlaogai.MainActivity
2020-08-31 23:03:58 +02:00
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.GradesController
import org.mosad.seil0.projectlaogai.controller.cache.CacheController
import org.mosad.seil0.projectlaogai.controller.preferences.EncryptedPreferences
import org.mosad.seil0.projectlaogai.util.NotificationUtils
import org.mosad.seil0.projectlaogai.util.NotificationUtils.Companion.CHANNEL_ID_GRADES
class GradesUpdateWorker ( val context : Context , params : WorkerParameters ) : Worker ( context , params ) {
override fun doWork ( ) : Result {
// check if credentials are present, if not do nothing
val credentials = EncryptedPreferences . readCredentials ( context )
if ( credentials . first . isEmpty ( ) || credentials . second . isEmpty ( ) ) {
return Result . success ( )
}
// TODO show updating notification, for debugging
val notificationIdDBG = NotificationUtils . getId ( )
val builderDBG = NotificationCompat . Builder ( context , CHANNEL _ID _GRADES )
. setSmallIcon ( R . drawable . ic _grading _black _24dp )
. setContentTitle ( context . getString ( R . string . notification _grades ) )
. setContentText ( context . getString ( R . string . notification _grades _updating _desc ) )
. setNotificationSilent ( )
NotificationManagerCompat . from ( context ) . apply {
notify ( notificationIdDBG , builderDBG . build ( ) )
}
// get old grades
val oldGrades = CacheController ( context ) . readGrades ( )
// get update from qispos
runBlocking { CacheController . updateGrades ( context ) . join ( ) }
val newGrades = CacheController ( context ) . readGrades ( )
// check for changes
val diff = GradesController ( ) . diffGrades ( oldGrades , newGrades )
// show message
2020-09-10 18:26:42 +02:00
if ( diff . isNotEmpty ( ) || diff . isEmpty ( ) ) {
2020-08-31 23:03:58 +02:00
val text = if ( diff . size < 2 ) {
context . getString ( R . string . notification _grades _single _desc , diff . first ( ) . name )
} else {
context . getString ( R . string . notification _grades _multiple _desc , diff . first ( ) . name , ( diff . size - 1 ) )
}
2020-09-10 18:26:42 +02:00
val intent = Intent ( context , MainActivity :: class . java ) . apply {
flags = Intent . FLAG _ACTIVITY _NEW _TASK or Intent . FLAG _ACTIVITY _CLEAR _TASK
action = context . getString ( R . string . intent _action _gradesFragment )
}
val pendingIntent : PendingIntent = PendingIntent . getActivity ( context , 0 , intent , 0 )
2020-08-31 23:03:58 +02:00
val builder = NotificationCompat . Builder ( context , CHANNEL _ID _GRADES )
. setSmallIcon ( R . drawable . ic _grading _black _24dp )
. setContentTitle ( context . getString ( R . string . notification _grades ) )
. setContentText ( text )
2020-09-10 18:26:42 +02:00
. setContentIntent ( pendingIntent )
. setAutoCancel ( true )
2020-08-31 23:03:58 +02:00
// if there are multiple subjects, use BigText
if ( diff . size > 1 )
builder . setStyle ( NotificationCompat . BigTextStyle ( ) . bigText ( text ) )
NotificationManagerCompat . from ( context ) . apply {
notify ( NotificationUtils . getId ( ) , builder . build ( ) )
}
}
// TODO remove debug notification
NotificationManagerCompat . from ( context ) . apply {
cancel ( notificationIdDBG )
}
return Result . success ( )
}
}