-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/main' into analog_clock
# Conflicts: # app/src/main/res/values/strings.xml
- Loading branch information
Showing
46 changed files
with
1,027 additions
and
629 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
package com.bnyro.clock | ||
|
||
import android.app.Application | ||
import com.bnyro.clock.data.database.DatabaseHolder | ||
import com.bnyro.clock.data.database.AppDatabase | ||
import com.bnyro.clock.util.NotificationHelper | ||
import com.bnyro.clock.util.Preferences | ||
|
||
class App : Application() { | ||
lateinit var container: AppContainer | ||
private val database by lazy { AppDatabase.getDatabase(this) } | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
DatabaseHolder.init(this) | ||
Preferences.init(this) | ||
NotificationHelper.createNotificationChannels(this) | ||
NotificationHelper().createNotificationChannels(this) | ||
|
||
container = AppContainer(database) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.bnyro.clock | ||
|
||
import com.bnyro.clock.data.database.AppDatabase | ||
import com.bnyro.clock.domain.repository.AlarmRepository | ||
import com.bnyro.clock.domain.repository.TimezoneRepository | ||
|
||
class AppContainer(database: AppDatabase) { | ||
val alarmRepository: AlarmRepository by lazy { | ||
AlarmRepository(database.alarmsDao()) | ||
} | ||
val timezoneRepository: TimezoneRepository by lazy { | ||
TimezoneRepository(database.timeZonesDao()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
app/src/main/java/com/bnyro/clock/data/database/DatabaseHolder.kt
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
app/src/main/java/com/bnyro/clock/domain/model/Permission.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.bnyro.clock.domain.model | ||
|
||
import android.Manifest | ||
import android.app.Activity | ||
import android.app.AlarmManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.provider.Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM | ||
import androidx.annotation.DrawableRes | ||
import androidx.annotation.StringRes | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.net.toUri | ||
import com.bnyro.clock.BuildConfig | ||
import com.bnyro.clock.R | ||
|
||
sealed class Permission( | ||
@StringRes | ||
val titleRes: Int, | ||
@StringRes | ||
val descriptionRes: Int, | ||
@DrawableRes | ||
val iconRes: Int | ||
) { | ||
abstract fun hasPermission(context: Context): Boolean | ||
abstract fun requestPermission(activity: Activity) | ||
|
||
object NotificationPermission : | ||
Permission( | ||
titleRes = R.string.notification_permission_title, | ||
descriptionRes = R.string.notification_permission_description, | ||
iconRes = R.drawable.ic_alarm | ||
) { | ||
override fun hasPermission(context: Context): Boolean { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return true | ||
return ActivityCompat.checkSelfPermission( | ||
context, | ||
Manifest.permission.POST_NOTIFICATIONS | ||
) == PackageManager.PERMISSION_GRANTED | ||
} | ||
|
||
override fun requestPermission(activity: Activity) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return | ||
ActivityCompat.requestPermissions( | ||
activity, | ||
arrayOf(Manifest.permission.POST_NOTIFICATIONS), | ||
1 | ||
) | ||
} | ||
} | ||
|
||
object AlarmPermission : Permission( | ||
titleRes = R.string.alarm_permission_title, | ||
descriptionRes = R.string.alarm_permission_description, | ||
iconRes = R.drawable.ic_alarm | ||
) { | ||
|
||
override fun hasPermission(context: Context): Boolean { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return true | ||
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager | ||
return alarmManager.canScheduleExactAlarms() | ||
} | ||
|
||
override fun requestPermission(activity: Activity) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return | ||
val intent = Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM).apply { | ||
data = "package:${BuildConfig.APPLICATION_ID}".toUri() | ||
} | ||
activity.startActivity(intent) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/bnyro/clock/domain/repository/AlarmRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.bnyro.clock.domain.repository | ||
|
||
import com.bnyro.clock.data.database.dao.AlarmsDao | ||
import com.bnyro.clock.domain.model.Alarm | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.withContext | ||
|
||
class AlarmRepository(private val alarmsDao: AlarmsDao) { | ||
suspend fun addAlarm(alarm: Alarm) = withContext(Dispatchers.IO) { alarmsDao.insert(alarm) } | ||
|
||
suspend fun getAlarms(): List<Alarm> = withContext(Dispatchers.IO) { alarmsDao.getAll() } | ||
fun getAlarmsStream(): Flow<List<Alarm>> = alarmsDao.getAllStream() | ||
|
||
suspend fun getAlarmById(id: Long): Alarm = | ||
withContext(Dispatchers.IO) { alarmsDao.findById(id) } | ||
|
||
suspend fun deleteAlarm(alarm: Alarm) = withContext(Dispatchers.IO) { alarmsDao.delete(alarm) } | ||
|
||
suspend fun updateAlarm(alarm: Alarm) = withContext(Dispatchers.IO) { alarmsDao.update(alarm) } | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/bnyro/clock/domain/repository/TimezoneRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.bnyro.clock.domain.repository | ||
|
||
import android.content.Context | ||
import com.bnyro.clock.data.database.dao.TimeZonesDao | ||
import com.bnyro.clock.domain.model.CountryTimezone | ||
import com.bnyro.clock.domain.model.TimeZone | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.json.Json | ||
import java.util.Calendar | ||
|
||
class TimezoneRepository(private val timeZonesDao: TimeZonesDao) { | ||
suspend fun getTimezones(): List<TimeZone> = | ||
withContext(Dispatchers.IO) { timeZonesDao.getAll() } | ||
|
||
fun getTimezonesStream(): Flow<List<TimeZone>> = timeZonesDao.getAllStream() | ||
suspend fun replaceAll(vararg timeZone: TimeZone) = withContext(Dispatchers.IO) { | ||
timeZonesDao.clear() | ||
timeZonesDao.insertAll(*timeZone) | ||
} | ||
|
||
suspend fun delete(timeZone: TimeZone) = | ||
withContext(Dispatchers.IO) { timeZonesDao.delete(timeZone) } | ||
|
||
fun getTimezonesForCountries(context: Context): List<TimeZone> { | ||
val countryTimezones = getCountryTimezones(context) | ||
return getTimezonesForCountries(countryTimezones) | ||
} | ||
|
||
private fun getTimezonesForCountries(zoneIds: List<CountryTimezone>): List<TimeZone> { | ||
return zoneIds.map { | ||
val zone = java.util.TimeZone.getTimeZone(it.zoneId) | ||
val zoneKey = arrayOf(it.zoneId, it.zoneName, it.countryName).joinToString(",") | ||
val offset = zone.getOffset(Calendar.getInstance().timeInMillis) | ||
TimeZone(zoneKey, it.zoneId, offset, it.zoneName, it.countryName) | ||
}.sortedBy { it.zoneName } | ||
} | ||
|
||
private fun getCountryTimezones(context: Context): List<CountryTimezone> { | ||
val tzData = | ||
context.resources.assets.open("tz_data.json").bufferedReader() | ||
.use { it.readText() } | ||
|
||
val json = Json { ignoreUnknownKeys = true } | ||
return json.decodeFromString(tzData) | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/bnyro/clock/domain/usecase/CreateUpdateDeleteAlarmUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.bnyro.clock.domain.usecase | ||
|
||
import android.content.Context | ||
import com.bnyro.clock.domain.model.Alarm | ||
import com.bnyro.clock.domain.repository.AlarmRepository | ||
import com.bnyro.clock.util.AlarmHelper | ||
|
||
class CreateUpdateDeleteAlarmUseCase( | ||
private val context: Context, | ||
private val alarmRepository: AlarmRepository | ||
) { | ||
suspend fun createAlarm(alarm: Alarm) { | ||
alarm.enabled = true | ||
AlarmHelper.enqueue(context, alarm) | ||
alarmRepository.addAlarm(alarm) | ||
} | ||
|
||
suspend fun updateAlarm(alarm: Alarm) { | ||
AlarmHelper.enqueue(context, alarm) | ||
alarmRepository.updateAlarm(alarm) | ||
} | ||
|
||
suspend fun deleteAlarm(alarm: Alarm) { | ||
AlarmHelper.cancel(context, alarm) | ||
alarmRepository.deleteAlarm(alarm) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.