Skip to content

Commit dfc60c5

Browse files
committed
Reduce the complexity of the writeEvent function
1 parent e707b02 commit dfc60c5

1 file changed

Lines changed: 44 additions & 46 deletions

File tree

app/src/main/kotlin/org/fossify/calendar/helpers/IcsExporter.kt

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -98,58 +98,56 @@ class IcsExporter(private val context: Context) {
9898
}
9999
}
100100

101-
private fun writeEvent(outputStream: OutputStream, event: Event) {
101+
private fun writeEvent(out: OutputStream, event: Event) {
102102
val calendarColors = context.eventsHelper.getCalendarColors()
103-
with(outputStream) {
104-
writeContentLine(BEGIN_EVENT)
105-
event.title.let { if (it.isNotEmpty()) writeTextProperty(SUMMARY, it) }
106-
event.importId.let { if (it.isNotEmpty()) writeContentLine("$UID$it") }
107-
writeContentLine("$CATEGORY_COLOR${context.calendarsDB.getCalendarWithId(event.calendarId)?.color}")
108-
if (event.color != 0 && event.color != calendarColors[event.calendarId]) {
109-
val color = CssColors.findClosestCssColor(event.color)
110-
if (color != null) {
111-
writeContentLine("$COLOR${color}")
112-
}
113-
writeContentLine("$FOSSIFY_COLOR${event.color}")
103+
out.writeContentLine(BEGIN_EVENT)
104+
if (event.title.isNotEmpty()) out.writeTextProperty(SUMMARY, event.title)
105+
if (event.importId.isNotEmpty()) out.writeContentLine("$UID${event.importId}")
106+
out.writeContentLine("$CATEGORY_COLOR${context.calendarsDB.getCalendarWithId(event.calendarId)?.color}")
107+
if (event.color != 0 && event.color != calendarColors[event.calendarId]) {
108+
val color = CssColors.findClosestCssColor(event.color)
109+
if (color != null) {
110+
out.writeContentLine("$COLOR${color}")
114111
}
115-
writeTextProperty("CATEGORIES", context.calendarsDB.getCalendarWithId(event.calendarId)?.title ?: "")
116-
writeContentLine("$LAST_MODIFIED:${Formatter.getExportedTime(event.lastUpdated)}")
117-
writeContentLine("$TRANSP${if (event.availability == Events.AVAILABILITY_FREE) TRANSPARENT else OPAQUE}")
118-
event.location.let { if (it.isNotEmpty()) writeTextProperty(LOCATION, it) }
119-
120-
if (event.getIsAllDay()) {
121-
val tz = try {
122-
DateTimeZone.forID(event.timeZone)
123-
} catch (ignored: IllegalArgumentException) {
124-
DateTimeZone.getDefault()
125-
}
126-
writeContentLine("$DTSTART;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.startTS, tz)}")
127-
writeContentLine(
128-
"$DTEND;$VALUE=$DATE:${
129-
Formatter.getDayCodeFromTS(
130-
event.endTS + TWELVE_HOURS,
131-
tz
132-
)
133-
}"
134-
)
135-
} else {
136-
writeContentLine("$DTSTART:${Formatter.getExportedTime(event.startTS * 1000L)}")
137-
writeContentLine("$DTEND:${Formatter.getExportedTime(event.endTS * 1000L)}")
112+
out.writeContentLine("$FOSSIFY_COLOR${event.color}")
113+
}
114+
out.writeTextProperty("CATEGORIES", context.calendarsDB.getCalendarWithId(event.calendarId)?.title ?: "")
115+
out.writeContentLine("$LAST_MODIFIED:${Formatter.getExportedTime(event.lastUpdated)}")
116+
out.writeContentLine("$TRANSP${if (event.availability == Events.AVAILABILITY_FREE) TRANSPARENT else OPAQUE}")
117+
if (event.location.isNotEmpty()) out.writeTextProperty(LOCATION, event.location)
118+
119+
if (event.getIsAllDay()) {
120+
val tz = try {
121+
DateTimeZone.forID(event.timeZone)
122+
} catch (ignored: IllegalArgumentException) {
123+
DateTimeZone.getDefault()
138124
}
139-
writeContentLine("$MISSING_YEAR${if (event.hasMissingYear()) 1 else 0}")
125+
out.writeContentLine("$DTSTART;$VALUE=$DATE:${Formatter.getDayCodeFromTS(event.startTS, tz)}")
126+
out.writeContentLine(
127+
"$DTEND;$VALUE=$DATE:${
128+
Formatter.getDayCodeFromTS(
129+
event.endTS + TWELVE_HOURS,
130+
tz
131+
)
132+
}"
133+
)
134+
} else {
135+
out.writeContentLine("$DTSTART:${Formatter.getExportedTime(event.startTS * 1000L)}")
136+
out.writeContentLine("$DTEND:${Formatter.getExportedTime(event.endTS * 1000L)}")
137+
}
138+
out.writeContentLine("$MISSING_YEAR${if (event.hasMissingYear()) 1 else 0}")
140139

141-
writeContentLine("$DTSTAMP$exportTime")
142-
writeContentLine("$CLASS:${getAccessLevelStringFromEventAccessLevel(event.accessLevel)}")
143-
writeContentLine("$STATUS${getStatusStringFromEventStatus(event.status)}")
144-
Parser().getRepeatCode(event).let { if (it.isNotEmpty()) writeContentLine("$RRULE$it") }
140+
out.writeContentLine("$DTSTAMP$exportTime")
141+
out.writeContentLine("$CLASS:${getAccessLevelStringFromEventAccessLevel(event.accessLevel)}")
142+
out.writeContentLine("$STATUS${getStatusStringFromEventStatus(event.status)}")
143+
Parser().getRepeatCode(event).let { if (it.isNotEmpty()) out.writeContentLine("$RRULE$it") }
145144

146-
writeTextProperty(DESCRIPTION, event.description)
147-
fillReminders(event, outputStream, reminderLabel)
148-
fillIgnoredOccurrences(event, outputStream)
145+
out.writeTextProperty(DESCRIPTION, event.description)
146+
fillReminders(event, out, reminderLabel)
147+
fillIgnoredOccurrences(event, out)
149148

150-
eventsExported++
151-
writeContentLine(END_EVENT)
152-
}
149+
eventsExported++
150+
out.writeContentLine(END_EVENT)
153151
}
154152

155153
private fun writeTask(outputStream: OutputStream, task: Event) {

0 commit comments

Comments
 (0)