Skip to content
Snippets Groups Projects
Commit 2b00ba25 authored by Emirhan Gürkan's avatar Emirhan Gürkan
Browse files

refactor code

parent c4db3f3c
No related branches found
No related tags found
No related merge requests found
......@@ -78,19 +78,16 @@ class MainActivity : ComponentActivity() {
settingsViewModel.getAllSettings()
}
override fun onResume() {
super.onResume()
// appViewModel.runTimer()
}
override fun onPause() {
super.onPause()
// appViewModel.stopTimer()
appViewModel.stopPause()
appViewModel.stopTimer()
}
override fun onStop() {
super.onStop()
// appViewModel.stopTimer()
appViewModel.stopPause()
appViewModel.stopTimer()
}
override fun onDestroy() {
......
......@@ -2,7 +2,6 @@ package com.example.myapplication.Room
import android.content.Context
import android.media.MediaPlayer
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.myapplication.R
......@@ -10,13 +9,12 @@ import com.example.myapplication.Types.NavigationType
class AppViewModel: ViewModel() {
var isRunning = MutableLiveData(false)
var pauseText = MutableLiveData("Start")
var isInPause = MutableLiveData(false)
var selectedNavigation = MutableLiveData(NavigationType.Home)
var musicPlayer: MutableLiveData<MediaPlayer?> = MutableLiveData(null)
fun runTimer() {
isRunning.setValue(true)
pauseText.setValue("Pause")
musicPlayer.value?.apply {
start()
}
......@@ -24,12 +22,19 @@ class AppViewModel: ViewModel() {
fun stopTimer() {
isRunning.setValue(false)
pauseText.setValue("Start")
musicPlayer.value?.apply {
stop()
}
}
fun runPause() {
isInPause.setValue(true)
}
fun stopPause() {
isInPause.setValue(false)
}
fun setSelectedNavigation(navigationType: NavigationType) {
selectedNavigation.setValue(navigationType)
}
......
......@@ -2,6 +2,7 @@ package com.example.myapplication.Room
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.example.myapplication.Types.NavigationType
import java.util.Date
@Entity(tableName = "Task")
......
......@@ -34,14 +34,14 @@ fun TimerScreen(todayTasks: List<Task>, settingsTimer: SettingsTimer, notificati
val appSettings by remember(settingsTimer) { mutableStateOf(settingsTimer) }
val isRunning by ViewModelProvider.getAppViewModel().isRunning.observeAsState(initial = false)
val pauseText by ViewModelProvider.getAppViewModel().pauseText.observeAsState(initial = "Start")
val isInPause by ViewModelProvider.getAppViewModel().isInPause.observeAsState(initial = false)
var timeLeft by remember { mutableStateOf(appSettings.dauer * 60) }
var round by remember { mutableStateOf(1) }
val context = LocalContext.current
ViewModelProvider.getAppViewModel().createMediaPlayer(context, appSettings.musik)
LaunchedEffect(isRunning) {
LaunchedEffect(isRunning, isInPause) {
while (round <= appSettings.langePauseNachEinheiten) {
if (isRunning) {
while (timeLeft > 0) {
......@@ -50,23 +50,30 @@ fun TimerScreen(todayTasks: List<Task>, settingsTimer: SettingsTimer, notificati
}
appViewModel.stopTimer()
timeLeft = if(round == 4) appSettings.langePause * 60 else appSettings.kurzePause * 60
appViewModel.runPause()
}
if (!isRunning) {
if (isInPause) {
notificationCallback("Congratulation", "Nice you have ${appSettings.dauer * round} minutes worked :).")
while (timeLeft > 0) {
delay(1000)
timeLeft -= 1
}
appViewModel.runTimer()
appViewModel.stopPause()
timeLeft = appSettings.dauer * 60
round += 1
appViewModel.runTimer()
}
if (!isRunning && !isInPause) {
delay(1000)
}
}
round = 1
appViewModel.stopTimer()
appViewModel.stopPause()
timeLeft = appSettings.dauer * 60
}
if(tasks.isEmpty() && showInfoDialog) {
MyAlertDialog(text = "Please make sure you have Task for Today!") {
showInfoDialog = false
......@@ -88,7 +95,7 @@ fun TimerScreen(todayTasks: List<Task>, settingsTimer: SettingsTimer, notificati
.padding(vertical = 16.dp),
contentAlignment = Alignment.Center
) {
val text = if(!isRunning) "Pause" else "$round.Arbeitsphase"
val text = if(isInPause) "Pause" else "$round.Arbeitsphase"
TimerDisplay(text ,timeLeft = timeLeft)
}
......@@ -111,21 +118,25 @@ fun TimerScreen(todayTasks: List<Task>, settingsTimer: SettingsTimer, notificati
) {
Button(
onClick = {
if(isRunning) appViewModel.stopTimer() else appViewModel.runTimer()
// if (pauseText=="Pause") startPomodoro(context)
if(isRunning && !isInPause) appViewModel.stopTimer() else appViewModel.runTimer()
},
modifier = Modifier.weight(1f),
colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF7F56D9))
) {
Icon(imageVector = Icons.Default.PlayArrow, contentDescription = null, tint = Color.White)
Text(text = pauseText, color = Color.White, modifier = Modifier.padding(start = 8.dp))
Text(text = if(!isRunning) "Start" else "Pause", color = Color.White, modifier = Modifier.padding(start = 8.dp))
}
Spacer(modifier = Modifier.width(8.dp))
Button(
onClick = { onClickEndSession() },
onClick = {
onClickEndSession()
round = 1
appViewModel.stopTimer()
appViewModel.stopPause()
timeLeft = appSettings.dauer * 60
},
modifier = Modifier.weight(1f),
colors = ButtonDefaults.buttonColors(containerColor = Color(0xFFFF5678))
) {
......
......@@ -7,32 +7,31 @@ import androidx.work.Worker
import androidx.work.WorkerParameters
import com.example.myapplication.R
class PomodoroWorker(context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) {
override fun doWork(): Result {
// Benachrichtigung start
showNotification("Pomodoro beginnt!", "Let's GO!")
// Timer-Logik oder andere Hintergrundaufgaben
val pomodoroDuration = 25 * 60 * 1000L // 25 Minuten in Millisekunden
Thread.sleep(pomodoroDuration)
// Benachrichtigung anzeigen
showNotification("Pomodoro beendet!", "Zeit für eine Pause.")
return Result.success()
}
private fun showNotification(title: String, message: String) {
val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationBuilder = NotificationCompat.Builder(applicationContext, "POMODORO_CHANNEL")
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification)
.setPriority(NotificationCompat.PRIORITY_HIGH)
notificationManager.notify(1, notificationBuilder.build())
}
class PomodoroWorker(context: Context, workerParams: WorkerParameters) {
// override fun doWork(): Result {
//
// // Benachrichtigung start
// showNotification("Pomodoro beginnt!", "Let's GO!")
//
// // Timer-Logik oder andere Hintergrundaufgaben
// val pomodoroDuration = 25 * 60 * 1000L // 25 Minuten in Millisekunden
// Thread.sleep(pomodoroDuration)
//
// // Benachrichtigung anzeigen
// showNotification("Pomodoro beendet!", "Zeit für eine Pause.")
//
// return Result.success()
// }
//
// private fun showNotification(title: String, message: String) {
// val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// val notificationBuilder = NotificationCompat.Builder(applicationContext, "POMODORO_CHANNEL")
// .setContentTitle(title)
// .setContentText(message)
// .setSmallIcon(R.drawable.ic_notification)
// .setPriority(NotificationCompat.PRIORITY_HIGH)
//
// notificationManager.notify(1, notificationBuilder.build())
// }
}
......@@ -4,9 +4,9 @@ import android.content.Context
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
fun startPomodoro(context: Context) {
val workRequest = OneTimeWorkRequest.Builder(PomodoroWorker::class.java)
.build()
WorkManager.getInstance(context).enqueue(workRequest)
}
\ No newline at end of file
//fun startPomodoro(context: Context) {
// val workRequest = OneTimeWorkRequest.Builder(PomodoroWorker::class.java)
// .build()
//
// WorkManager.getInstance(context).enqueue(workRequest)
//}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment