Skip to content

Commit

Permalink
Simplify selection of reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
izivkov committed Nov 19, 2023
1 parent a9cb309 commit e8c4ef0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
9 changes: 2 additions & 7 deletions api/src/main/java/org/avmedia/gshockapi/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ class Event(
private var daysOfWeek: ArrayList<DayOfWeek>?,
var enabled: Boolean,
var incompatible: Boolean,
var selected: Boolean
) {
init {
if (endDate == null) {
endDate = startDate
}
}

@RequiresApi(Build.VERSION_CODES.O)
constructor(json: JSONObject) : this(
"", null, null, RepeatPeriod.NEVER, null, false, false, false
"", null, null, RepeatPeriod.NEVER, null, false, false
) {
val event = createEvent(json)

Expand All @@ -42,7 +40,6 @@ class Event(
this.repeatPeriod = event.repeatPeriod
this.daysOfWeek = event.daysOfWeek
this.enabled = event.enabled
this.selected = event.selected
}

@RequiresApi(Build.VERSION_CODES.O)
Expand Down Expand Up @@ -111,7 +108,6 @@ class Event(
val weekDays = timeObj.getJSONArray("daysOfWeek")
val enabled = timeObj.getBooleanSafe("enabled") ?: false
val incompatible = timeObj.getBooleanSafe("incompatible") ?: false
val selected = timeObj.getBooleanSafe("selected") ?: false
val repeatPeriod = stringToRepeatPeriod(timeObj.getStringSafe("repeatPeriod") as String)

return Event(
Expand All @@ -130,7 +126,6 @@ class Event(
getArrayListFromJSONArray(weekDays),
enabled,
incompatible,
selected
)
}

Expand Down Expand Up @@ -229,6 +224,6 @@ class Event(
}

override fun toString(): String {
return "Event(title='$title', startDate=$startDate, endDate=$endDate, repeatPeriod=$repeatPeriod, daysOfWeek=$daysOfWeek, enabled=$enabled, incompatible=$incompatible, selected=$selected)"
return "Event(title='$title', startDate=$startDate, endDate=$endDate, repeatPeriod=$repeatPeriod, daysOfWeek=$daysOfWeek, enabled=$enabled, incompatible=$incompatible)"
}
}
34 changes: 30 additions & 4 deletions api/src/main/java/org/avmedia/gshockapi/io/EventsIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ object EventsIO {

fun set(events: ArrayList<Event>) {

val MAX_REMINDERS = 5

if (events.isEmpty()) {
Timber.d("Events model not initialised! Cannot set reminders")
return
Expand All @@ -55,12 +57,36 @@ object EventsIO {
return gson.toJson(events)
}

fun getSelectedEvents(events: ArrayList<Event>): String {
val selectedEvents = events.filter { it.selected } as ArrayList<Event>
return toJson(selectedEvents)
fun <T> appendAndTruncate(
arr1: ArrayList<T>,
arr2: ArrayList<T>,
maxSize: Int
): ArrayList<T> {
val result = ArrayList<T>()

// Append elements from arr1 and arr2
result.addAll(arr1)
result.addAll(arr2)

// Truncate the result to the specific size
while (result.size > maxSize) {
result.removeLast()
}

return result
}

fun getEnabledEvents(events: ArrayList<Event>): ArrayList<Event> {
return events.filter { it.enabled } as ArrayList<Event>
}

// send all enabled events and not-enabled if enabled less then MAX_REMINDERS
fun getEventsToSend() : ArrayList<Event> {
return appendAndTruncate(getEnabledEvents(events), events.filter { !it.enabled } as ArrayList<Event>, MAX_REMINDERS)
}

Connection.sendMessage("{action: \"SET_REMINDERS\", value: ${getSelectedEvents(events)} }")
val eventsToSend = toJson(getEventsToSend())
Connection.sendMessage("{action: \"SET_REMINDERS\", value: $eventsToSend }")
}

fun onReceived(data: String) {
Expand Down

0 comments on commit e8c4ef0

Please sign in to comment.