Skip to content

Commit f57cebb

Browse files
committed
refactor: Bump version code and name
Signed-off-by: Pranav Purwar <purwarpranav80@gmail.com>
1 parent 4e71ead commit f57cebb

8 files changed

Lines changed: 129 additions & 461 deletions

File tree

Reef/build.gradle.kts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
applicationId = "dev.pranav.reef"
1414
minSdk = 26
1515
targetSdk = 36
16-
versionCode = 202
17-
versionName = "2.0.2"
16+
versionCode = 203
17+
versionName = "2.0.3"
1818
}
1919

2020
viewBinding.enable = true
@@ -26,8 +26,9 @@ android {
2626

2727
buildTypes {
2828
release {
29-
isMinifyEnabled = true
30-
isShrinkResources = true
29+
// Causing issues smh
30+
isMinifyEnabled = false
31+
isShrinkResources = false
3132
proguardFiles(
3233
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
3334
)

Reef/src/main/java/dev/pranav/reef/CreateRoutineActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ fun CreateRoutineScreen(
414414
if (result) onSaveComplete()
415415
},
416416
modifier = Modifier.fillMaxWidth(),
417-
shape = RoundedCornerShape(16.dp),
417+
shapes = ButtonDefaults.shapes(),
418418
contentPadding = PaddingValues(vertical = 16.dp)
419419
) {
420420
Text(
@@ -427,7 +427,7 @@ fun CreateRoutineScreen(
427427
OutlinedButton(
428428
onClick = { showDeleteDialog = true },
429429
modifier = Modifier.fillMaxWidth(),
430-
shape = RoundedCornerShape(16.dp),
430+
shapes = ButtonDefaults.shapes(),
431431
contentPadding = PaddingValues(vertical = 16.dp),
432432
colors = ButtonDefaults.outlinedButtonColors(
433433
contentColor = MaterialTheme.colorScheme.error

Reef/src/main/java/dev/pranav/reef/RoutinesActivity.kt

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,6 @@ class RoutinesActivity : ComponentActivity() {
9898
}
9999
}
100100
}
101-
102-
override fun onResume() {
103-
super.onResume()
104-
setContent {
105-
ReefTheme {
106-
RoutinesScreen(
107-
onBackPressed = { onBackPressedDispatcher.onBackPressed() },
108-
onCreateRoutine = {
109-
startActivity(Intent(this, CreateRoutineActivity::class.java))
110-
},
111-
onEditRoutine = { routine ->
112-
val intent = Intent(this, CreateRoutineActivity::class.java).apply {
113-
putExtra("routine_id", routine.id)
114-
}
115-
startActivity(intent)
116-
}
117-
)
118-
}
119-
}
120-
}
121101
}
122102

123103
@OptIn(ExperimentalMaterial3Api::class)
@@ -198,7 +178,7 @@ fun RoutinesScreen(
198178
onEditRoutine(routine)
199179
}
200180
},
201-
onToggle = { enabled ->
181+
onToggle = { _ ->
202182
RoutineManager.toggleRoutine(routine.id, context)
203183
routines = RoutineManager.getRoutines()
204184
}
@@ -219,6 +199,7 @@ fun RoutinesScreen(
219199
activateRoutineNow(routine, context)
220200
showActivateDialog = null
221201

202+
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
222203
val limitsText = when (routine.limits.size) {
223204
0 -> "No app limits were applied"
224205
1 -> "1 app limit has been applied"
@@ -408,7 +389,9 @@ fun RoutineItem(
408389
}
409390
}
410391

411-
private fun formatSchedule(schedule: RoutineSchedule): String {
392+
private fun formatSchedule(schedule: RoutineSchedule?): String {
393+
if (schedule == null) return "Unknown schedule"
394+
412395
return when (schedule.type) {
413396
RoutineSchedule.ScheduleType.DAILY -> {
414397
val timeRange = if (schedule.time != null && schedule.endTime != null) {

Reef/src/main/java/dev/pranav/reef/util/RoutineManager.kt

Lines changed: 104 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,130 @@ package dev.pranav.reef.util
22

33
import android.content.Context
44
import androidx.core.content.edit
5-
import com.google.gson.GsonBuilder
6-
import com.google.gson.JsonDeserializationContext
7-
import com.google.gson.JsonDeserializer
8-
import com.google.gson.JsonElement
9-
import com.google.gson.JsonPrimitive
10-
import com.google.gson.JsonSerializationContext
11-
import com.google.gson.JsonSerializer
12-
import com.google.gson.reflect.TypeToken
135
import dev.pranav.reef.data.Routine
146
import dev.pranav.reef.data.RoutineSchedule
15-
import java.lang.reflect.Type
7+
import org.json.JSONArray
8+
import org.json.JSONObject
169
import java.time.DayOfWeek
1710
import java.util.UUID
1811

1912
object RoutineManager {
2013
private const val ROUTINES_KEY = "routines"
2114

22-
private val gson = GsonBuilder()
23-
.registerTypeAdapter(DayOfWeek::class.java, object : JsonSerializer<DayOfWeek> {
24-
override fun serialize(
25-
src: DayOfWeek?,
26-
typeOfSrc: Type?,
27-
context: JsonSerializationContext?
28-
): JsonElement {
29-
return JsonPrimitive(src?.name)
15+
fun getRoutines(): List<Routine> {
16+
val json = prefs.getString(ROUTINES_KEY, "[]") ?: "[]"
17+
return try {
18+
val jsonArray = JSONArray(json)
19+
val routines = mutableListOf<Routine>()
20+
21+
for (i in 0 until jsonArray.length()) {
22+
val routineJson = jsonArray.getJSONObject(i)
23+
val routine = parseRoutine(routineJson)
24+
if (routine != null) {
25+
routines.add(routine)
26+
}
3027
}
31-
})
32-
.registerTypeAdapter(DayOfWeek::class.java, object : JsonDeserializer<DayOfWeek> {
33-
override fun deserialize(
34-
json: JsonElement?,
35-
typeOfT: Type?,
36-
context: JsonDeserializationContext?
37-
): DayOfWeek? {
38-
return json?.asString?.let { DayOfWeek.valueOf(it) }
28+
routines
29+
} catch (_: Exception) {
30+
emptyList()
31+
}
32+
}
33+
34+
private fun parseRoutine(json: JSONObject): Routine? {
35+
return try {
36+
val scheduleJson = json.getJSONObject("schedule")
37+
val schedule = RoutineSchedule(
38+
type = RoutineSchedule.ScheduleType.valueOf(scheduleJson.getString("type")),
39+
timeHour = if (scheduleJson.has("timeHour")) scheduleJson.getInt("timeHour") else null,
40+
timeMinute = if (scheduleJson.has("timeMinute")) scheduleJson.getInt("timeMinute") else null,
41+
endTimeHour = if (scheduleJson.has("endTimeHour")) scheduleJson.getInt("endTimeHour") else null,
42+
endTimeMinute = if (scheduleJson.has("endTimeMinute")) scheduleJson.getInt("endTimeMinute") else null,
43+
daysOfWeek = parseDaysOfWeek(scheduleJson.optJSONArray("daysOfWeek")),
44+
isRecurring = scheduleJson.optBoolean("isRecurring", true)
45+
)
46+
47+
val limitsArray = json.getJSONArray("limits")
48+
val limits = mutableListOf<Routine.AppLimit>()
49+
for (i in 0 until limitsArray.length()) {
50+
val limitJson = limitsArray.getJSONObject(i)
51+
limits.add(
52+
Routine.AppLimit(
53+
packageName = limitJson.getString("packageName"),
54+
limitMinutes = limitJson.getInt("limitMinutes")
55+
)
56+
)
3957
}
40-
})
41-
.create()
4258

43-
fun getRoutines(): List<Routine> {
44-
val json = prefs.getString(ROUTINES_KEY, "[]") ?: "[]"
45-
val type = object : TypeToken<List<Routine>>() {}.type
46-
return gson.fromJson(json, type)
59+
Routine(
60+
id = json.getString("id"),
61+
name = json.getString("name"),
62+
isEnabled = json.getBoolean("isEnabled"),
63+
schedule = schedule,
64+
limits = limits
65+
)
66+
} catch (_: Exception) {
67+
null
68+
}
69+
}
70+
71+
private fun parseDaysOfWeek(jsonArray: JSONArray?): Set<DayOfWeek> {
72+
if (jsonArray == null) return emptySet()
73+
val days = mutableSetOf<DayOfWeek>()
74+
for (i in 0 until jsonArray.length()) {
75+
try {
76+
days.add(DayOfWeek.valueOf(jsonArray.getString(i)))
77+
} catch (_: Exception) {
78+
// Skip invalid days
79+
}
80+
}
81+
return days
4782
}
4883

4984
fun saveRoutines(routines: List<Routine>, context: Context? = null) {
50-
val json = gson.toJson(routines)
51-
prefs.edit { putString(ROUTINES_KEY, json) }
85+
val jsonArray = JSONArray()
86+
routines.forEach { routine ->
87+
jsonArray.put(routineToJson(routine))
88+
}
89+
prefs.edit { putString(ROUTINES_KEY, jsonArray.toString()) }
5290

53-
// Reschedule all routines when saved
5491
context?.let { ctx ->
5592
RoutineScheduler.scheduleAllRoutines(ctx)
5693
}
5794
}
5895

96+
private fun routineToJson(routine: Routine): JSONObject {
97+
return JSONObject().apply {
98+
put("id", routine.id)
99+
put("name", routine.name)
100+
put("isEnabled", routine.isEnabled)
101+
102+
val schedule = routine.schedule
103+
val scheduleJson = JSONObject().apply {
104+
put("type", schedule?.type?.name ?: "MANUAL")
105+
schedule?.timeHour?.let { put("timeHour", it) }
106+
schedule?.timeMinute?.let { put("timeMinute", it) }
107+
schedule?.endTimeHour?.let { put("endTimeHour", it) }
108+
schedule?.endTimeMinute?.let { put("endTimeMinute", it) }
109+
110+
val daysArray = JSONArray()
111+
schedule?.daysOfWeek?.forEach { daysArray.put(it.name) }
112+
put("daysOfWeek", daysArray)
113+
114+
put("isRecurring", schedule?.isRecurring ?: true)
115+
}
116+
put("schedule", scheduleJson)
117+
118+
val limitsArray = JSONArray()
119+
routine.limits.forEach { limit ->
120+
limitsArray.put(JSONObject().apply {
121+
put("packageName", limit.packageName)
122+
put("limitMinutes", limit.limitMinutes)
123+
})
124+
}
125+
put("limits", limitsArray)
126+
}
127+
}
128+
59129
fun addRoutine(routine: Routine, context: Context? = null) {
60130
val routines = getRoutines().toMutableList()
61131
routines.add(routine)
@@ -89,16 +159,13 @@ object RoutineManager {
89159

90160
context?.let { ctx ->
91161
if (oldRoutine.isEnabled) {
92-
// Was enabled, now disabled - cancel scheduling and clear limits if active
93162
RoutineScheduler.cancelRoutine(ctx, routineId)
94163

95-
// Check if this routine is currently active and clear its limits
96164
val activeRoutineId = RoutineLimits.getActiveRoutineId()
97165
if (activeRoutineId == routineId) {
98166
RoutineLimits.clearRoutineLimits()
99167
}
100168
} else {
101-
// Was disabled, now enabled - schedule it
102169
RoutineScheduler.scheduleRoutine(ctx, routines[index])
103170
}
104171
}

Reef/src/main/java/dev/pranav/reef/util/RoutineScheduler.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ object RoutineScheduler {
2323
}
2424

2525
fun scheduleRoutine(context: Context, routine: Routine) {
26-
if (!routine.isEnabled || routine.schedule.type == RoutineSchedule.ScheduleType.MANUAL) {
26+
val schedule = routine.schedule
27+
if (!routine.isEnabled || schedule.type == RoutineSchedule.ScheduleType.MANUAL) {
2728
return
2829
}
2930

@@ -33,15 +34,15 @@ object RoutineScheduler {
3334
activateRoutineNow(context, routine)
3435

3536
// Only schedule deactivation
36-
if (routine.schedule.endTime != null) {
37+
if (schedule.endTime != null) {
3738
scheduleRoutineAction(context, routine, isActivation = false)
3839
}
3940
} else {
4041
// Schedule activation
4142
scheduleRoutineAction(context, routine, isActivation = true)
4243

4344
// Schedule deactivation if end time exists
44-
if (routine.schedule.endTime != null) {
45+
if (schedule.endTime != null) {
4546
scheduleRoutineAction(context, routine, isActivation = false)
4647
}
4748
}
@@ -82,6 +83,8 @@ object RoutineScheduler {
8283
}
8384

8485
fun scheduleRoutineAction(context: Context, routine: Routine, isActivation: Boolean) {
86+
val schedule = routine.schedule
87+
8588
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
8689
val intent = Intent(context, RoutineActivationReceiver::class.java).apply {
8790
putExtra("routine_id", routine.id)
@@ -96,9 +99,9 @@ object RoutineScheduler {
9699
)
97100

98101
val triggerTime = if (isActivation) {
99-
calculateNextTriggerTime(routine.schedule, useStartTime = true)
102+
calculateNextTriggerTime(schedule, useStartTime = true)
100103
} else {
101-
calculateNextTriggerTime(routine.schedule, useStartTime = false)
104+
calculateNextTriggerTime(schedule, useStartTime = false)
102105
}
103106

104107
if (triggerTime != null) {
@@ -125,7 +128,7 @@ object RoutineScheduler {
125128
"Scheduled routine ${routine.name} $action (inexact) for ${Date(triggerTime)}"
126129
)
127130
}
128-
} catch (e: SecurityException) {
131+
} catch (_: SecurityException) {
129132
alarmManager.set(
130133
AlarmManager.RTC_WAKEUP,
131134
triggerTime,
@@ -233,15 +236,13 @@ class RoutineActivationReceiver : BroadcastReceiver() {
233236
private fun deactivateRoutine(context: Context, routine: Routine) {
234237
Log.d("RoutineActivationReceiver", "Deactivating routine: ${routine.name}")
235238

236-
// Clear routine limits
237239
RoutineLimits.clearRoutineLimits()
238240

239241
// Schedule next occurrence if recurring
240242
if (routine.schedule.isRecurring) {
241243
RoutineScheduler.scheduleRoutine(context, routine)
242244
}
243245

244-
// Show notification about routine deactivation
245246
NotificationHelper.showRoutineDeactivatedNotification(context, routine)
246247
}
247248
}

0 commit comments

Comments
 (0)