Skip to content

Commit

Permalink
refactor: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kirasok committed Mar 30, 2024
1 parent b7aa39a commit e4cb144
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.kirasok.alarmix.domain.repository

import io.github.kirasok.alarmix.domain.model.Alarm

// AlarmScheduler interface allows us to schedule alarms within domain layer by providing context with DI
interface AlarmScheduler {

fun schedule(alarm: Alarm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class AlarmUseCases(
val getAlarmById: GetAlarmById,
val insertAlarm: InsertAlarm,
val deleteAlarm: DeleteAlarm,
// We don't need ValidateAlarm because it's used in insertAlarm, not in presentation layer
// We don't need ValidateAlarm there because it's used in insertAlarm, not in presentation layer
)

class GetAlarms(private val repository: AlarmRepository) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun EditorScreen(navController: NavController, viewModel: EditorViewModel = hilt
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
val initialHour = 7
val initialHour = 7 // TODO: set initial hour and minute by checking viewModel.time because we could be editing alarm instead of creating new one; use time difference between .now() and time when alarm would be fired for initial hour and minute in case of editing alarm
val initialMinute = 30
val timePickerState = rememberTimePickerState(initialHour, initialMinute)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class AlarmReceiver : BroadcastReceiver() {
val serviceIntent =
Intent(context, AlarmService::class.java).putExtra(INTENT_EXTRA_ALARM_ID, id)
// We need to use foreground service because we can't start activity from broadcast receiver
// Instead, the best practice is to show user notification with foreground service
context.startForegroundService(serviceIntent)
}

companion object {
const val INTENT_EXTRA_ALARM_ID = "intent_extra_alarm_id"
const val INTENT_EXTRA_ALARM_ID = "intent_extra_alarm_id" // value for passing alarm id within intent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ import javax.inject.Inject
@AndroidEntryPoint
class AlarmService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

private val context = this
private val context = this // Used within mediaPlayer.apply{} to access context
private lateinit var vibrator: VibratorManager
private lateinit var mediaPlayer: MediaPlayer
private lateinit var alarm: Alarm

@Inject
lateinit var useCases: AlarmUseCases

// Receiver that triggers when user presses button in notification
private val alarmActionReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.getStringExtra(ALARM_ACTION_KEY)?.let { AlarmAction.valueOf(it) }) {
Expand Down Expand Up @@ -74,7 +75,7 @@ class AlarmService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
?: throw IllegalStateException("Alarm id can't be null")
// val alarm = runBlocking { useCases.getAlarmById(id) } TODO: fix getting alarm
// ?: throw InvalidAlarmException("Alarm can't be null")
this.alarm = Alarm(id, ZonedDateTime.now())
this.alarm = Alarm(id, ZonedDateTime.now()) // TODO: remove this when alarm is fixed
Log.d(null, "Got $alarm; timestamp: ${alarm.timestamp}; id: ${alarm.id}")

mediaPlayer = MediaPlayer()
Expand All @@ -94,14 +95,14 @@ class AlarmService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
private fun initMediaPlayer() = mediaPlayer.apply {
setOnPreparedListener(this@AlarmService)
setOnErrorListener(this@AlarmService)
isLooping = true
isLooping = true // repeats sound infinitely
setDataSource(
context, RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM)
context, RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM) // Only getActualDefaultRingtoneUri doesn't throw SecurityException for not having READ_PRIVILEGED_PHONE_STATE
)
setAudioAttributes(AudioAttributes.Builder().apply {
setUsage(AudioAttributes.USAGE_ALARM)
setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
}.build())
}.build()) // sets attribute that the sound is an alarm
prepare()
}

Expand All @@ -117,6 +118,8 @@ class AlarmService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr

override fun onPrepared(mp: MediaPlayer?) {
mp?.start()

// Handles vibrations
val combinedVibration = CombinedVibration.createParallel(
VibrationEffect.createWaveform(
longArrayOf(
Expand All @@ -141,6 +144,7 @@ class AlarmService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
}

private fun createNotification(): Notification {
// Starts activity on click on notification
val pendingIntent = PendingIntent.getActivity(
this, alarm.id, Intent(this, MainActivity::class.java).apply {
addFlags(
Expand Down Expand Up @@ -170,7 +174,7 @@ class AlarmService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
setOngoing(true) // User can't dismiss notification
setPriority(NotificationCompat.PRIORITY_MAX)
setCategory(NotificationCompat.CATEGORY_ALARM)
foregroundServiceBehavior = FOREGROUND_SERVICE_IMMEDIATE
foregroundServiceBehavior = FOREGROUND_SERVICE_IMMEDIATE // Shows notification in foreground
setFullScreenIntent(pendingIntent, true)
addAction(dismissAction)
addAction(snoozeAction)
Expand Down

0 comments on commit e4cb144

Please sign in to comment.