temporary solution to notification when app is in background
This commit is contained in:
		
							parent
							
								
									ea4e7a4790
								
							
						
					
					
						commit
						addfb7e126
					
				
					 2 changed files with 22 additions and 13 deletions
				
			
		|  | @ -1,5 +1,8 @@ | |||
| package be.ugent.sel.studeez.activities | ||||
| 
 | ||||
| import android.media.MediaPlayer | ||||
| import android.media.RingtoneManager | ||||
| import android.net.Uri | ||||
| import android.os.Bundle | ||||
| import androidx.activity.ComponentActivity | ||||
| import androidx.activity.compose.setContent | ||||
|  | @ -22,7 +25,13 @@ var onTimerInvisible: Job? = null | |||
| 
 | ||||
| @AndroidEntryPoint | ||||
| class MainActivity : ComponentActivity() { | ||||
| 
 | ||||
|     private var mediaPlayer: MediaPlayer? = null | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
|         val uri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | ||||
|         mediaPlayer = MediaPlayer.create(this, uri) | ||||
|         mediaPlayer?.isLooping = false | ||||
| 
 | ||||
|         super.onCreate(savedInstanceState) | ||||
|         setContent { | ||||
|             StudeezTheme { | ||||
|  | @ -37,19 +46,24 @@ class MainActivity : ComponentActivity() { | |||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     override fun onStop() { | ||||
|         onTimerInvisible = lifecycleScope.launch { | ||||
|             InvisibleSessionManager.updateTimer() | ||||
|             InvisibleSessionManager.updateTimer(mediaPlayer) | ||||
|         } | ||||
|         super.onStop() | ||||
|     } | ||||
| 
 | ||||
|     override fun onStart() { | ||||
|         mediaPlayer?.stop() | ||||
|         onTimerInvisible?.cancel() | ||||
|         super.onStart() | ||||
|     } | ||||
| 
 | ||||
|     override fun onDestroy() { | ||||
|         mediaPlayer?.stop() | ||||
|         mediaPlayer?.release() | ||||
|         super.onDestroy() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
|  |  | |||
|  | @ -31,12 +31,8 @@ import androidx.hilt.navigation.compose.hiltViewModel | |||
| import be.ugent.sel.studeez.R | ||||
| import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalPomodoroTimer | ||||
| import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer.StudyState | ||||
| import be.ugent.sel.studeez.data.local.models.timer_functional.Time | ||||
| import be.ugent.sel.studeez.resources | ||||
| import kotlinx.coroutines.delay | ||||
| import java.time.LocalDateTime | ||||
| import java.time.ZoneId | ||||
| import java.util.* | ||||
| import kotlin.time.Duration.Companion.seconds | ||||
| 
 | ||||
| @Composable | ||||
|  | @ -50,6 +46,7 @@ fun SessionScreen( | |||
|     val mediaplayer = MediaPlayer.create(context, uri) | ||||
|     mediaplayer.isLooping = false | ||||
| 
 | ||||
|     // evt mediaplayer meegeven vanaf hier als reserve oplossing | ||||
|     InvisibleSessionManager.setNewViewModel(viewModel = viewModel) | ||||
| 
 | ||||
|     Column( | ||||
|  | @ -88,11 +85,6 @@ fun SessionScreen( | |||
|     } | ||||
| } | ||||
| 
 | ||||
| private operator fun Time.minus(time: Time): Time { | ||||
|     return Time(this.time - time.time) | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| @Composable | ||||
| private fun Timer(viewModel: SessionViewModel = hiltViewModel(), mediaplayer: MediaPlayer) { | ||||
|     var tikker by remember { mutableStateOf(false) } | ||||
|  | @ -169,11 +161,14 @@ object InvisibleSessionManager { | |||
|         this.viewModel = viewModel | ||||
|     } | ||||
| 
 | ||||
|     suspend fun updateTimer() { | ||||
|     suspend fun updateTimer(mediaPlayer: MediaPlayer?) { | ||||
|         if (isSession) { | ||||
|             while (true) { | ||||
|                 delay(1.seconds) | ||||
|                 viewModel.getTimer().tick() | ||||
|                 if (viewModel.getTimer().hasCurrentCountdownEnded() && !viewModel.getTimer().hasEnded()) { | ||||
|                     mediaPlayer?.start() | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  |  | |||
		Reference in a new issue
	
	 Rune Dyselinck
						Rune Dyselinck