Skip to content

Commit

Permalink
Merge pull request EventFahrplan#654 from EventFahrplan/apprepository…
Browse files Browse the repository at this point in the history
…-sessions

Clean up separation between app/database/network layers.
  • Loading branch information
johnjohndoe authored Jul 1, 2024
2 parents a1bbad1 + 96214ab commit 53885ac
Show file tree
Hide file tree
Showing 45 changed files with 696 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ data class ChangeStatistic private constructor(
* [changed][Session.isChanged] or [new][Session.changedIsNew].
*/
fun getChangedFavoritesCount() = sessions
.count { it.highlight && (it.changedIsCanceled || it.isChanged || it.changedIsNew) }
.count { it.isHighlight && (it.changedIsCanceled || it.isChanged || it.changedIsNew) }
.also { log("$it changed favorites") }

private fun log(message: String) = logging.d(LOG_TAG, message)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@ import info.metadude.android.eventfahrplan.commons.temporal.Moment
import info.metadude.android.eventfahrplan.network.serialization.FahrplanParser
import nerd.tuxmobil.fahrplan.congress.models.DateInfo
import nerd.tuxmobil.fahrplan.congress.models.Room
import nerd.tuxmobil.fahrplan.congress.models.Session
import nerd.tuxmobil.fahrplan.congress.schedule.TrackBackgrounds
import org.threeten.bp.ZoneOffset
import info.metadude.android.eventfahrplan.database.models.Highlight as HighlightDatabaseModel
import info.metadude.android.eventfahrplan.database.models.Session as SessionDatabaseModel
import info.metadude.android.eventfahrplan.network.models.Session as SessionNetworkModel
import nerd.tuxmobil.fahrplan.congress.models.Session as SessionAppModel

fun Session.shiftRoomIndexOnDays(dayIndices: Set<Int>) =
fun SessionNetworkModel.shiftRoomIndexOnDays(dayIndices: Set<Int>) =
if (dayIndex in dayIndices) {
copy(roomIndex = roomIndex + 1)
} else {
this
}

fun Session.toRoom() = Room(identifier = roomIdentifier, name = roomName)
fun SessionAppModel.toRoom() = Room(identifier = roomIdentifier, name = roomName)

fun Session.toDateInfo(): DateInfo = DateInfo(dayIndex, Moment.parseDate(dateText))
fun SessionDatabaseModel.toDateInfo(): DateInfo = DateInfo(dayIndex, Moment.parseDate(dateText))

fun Session.toHighlightDatabaseModel() = HighlightDatabaseModel(
fun SessionAppModel.toHighlightDatabaseModel() = HighlightDatabaseModel(
sessionId = Integer.parseInt(sessionId),
isHighlight = highlight
isHighlight = isHighlight
)

fun Session.toSessionDatabaseModel() = SessionDatabaseModel(
fun SessionDatabaseModel.toSessionAppModel(): SessionAppModel {
return SessionAppModel(
sessionId = sessionId,
abstractt = abstractt,
date = dateText,
dateText = dateText,
dateUTC = dateUTC,
dayIndex = dayIndex,
description = description,
Expand All @@ -41,42 +42,43 @@ fun Session.toSessionDatabaseModel() = SessionDatabaseModel(
hasAlarm = hasAlarm,
language = language,
links = links,
isHighlight = highlight,
isHighlight = isHighlight,
recordingLicense = recordingLicense,
recordingOptOut = recordingOptOut,
relativeStartTime = relStartTime,
relativeStartTime = relativeStartTime,
roomName = roomName,
roomIdentifier = roomIdentifier,
roomIndex = roomIndex,
slug = slug,
speakers = createSpeakersString(speakers),
speakers = createSpeakersList(speakers),
startTime = startTime, // minutes since day start
subtitle = subtitle,
timeZoneOffset = timeZoneOffset?.totalSeconds, // seconds
timeZoneOffset = timeZoneOffset?.let { ZoneOffset.ofTotalSeconds(it) },
title = title,
track = track,
type = type,
url = url,

changedDay = changedDayIndex,
changedDayIndex = changedDayIndex,
changedDuration = changedDuration,
changedIsCanceled = changedIsCanceled,
changedIsNew = changedIsNew,
changedLanguage = changedLanguage,
changedRecordingOptOut = changedRecordingOptOut,
changedRoomName = changedRoomName,
changedSpeakers = changedSpeakers,
changedStartTime = changedStartTime,
changedSubtitle = changedSubtitle,
changedTime = changedStartTime,
changedTitle = changedTitle,
changedTrack = changedTrack
)
changedTrack = changedTrack,
)
}

fun SessionDatabaseModel.toSessionAppModel(): Session {
return Session(
fun SessionDatabaseModel.toSessionNetworkModel(): SessionNetworkModel {
return SessionNetworkModel(
sessionId = sessionId,
abstractt = abstractt,
dateText = date,
dateText = dateText,
dateUTC = dateUTC,
dayIndex = dayIndex,
description = description,
Expand All @@ -85,43 +87,43 @@ fun SessionDatabaseModel.toSessionAppModel(): Session {
hasAlarm = hasAlarm,
language = language,
links = links,
highlight = isHighlight,
isHighlight = isHighlight,
recordingLicense = recordingLicense,
recordingOptOut = recordingOptOut,
relStartTime = relativeStartTime,
relativeStartTime = relativeStartTime,
roomName = roomName,
roomIdentifier = roomIdentifier,
roomGuid = roomIdentifier,
roomIndex = roomIndex,
slug = slug,
speakers = createSpeakersList(speakers),
speakers = speakers,
startTime = startTime, // minutes since day start
subtitle = subtitle,
timeZoneOffset = timeZoneOffset?.let { ZoneOffset.ofTotalSeconds(it) }, // seconds
timeZoneOffset = timeZoneOffset,
title = title,
track = track,
type = type,
url = url,

changedDayIndex = changedDay,
changedDayIndex = changedDayIndex,
changedDuration = changedDuration,
changedIsCanceled = changedIsCanceled,
changedIsNew = changedIsNew,
changedLanguage = changedLanguage,
changedRecordingOptOut = changedRecordingOptOut,
changedRoomName = changedRoomName,
changedSpeakers = changedSpeakers,
changedStartTime = changedStartTime,
changedSubtitle = changedSubtitle,
changedStartTime = changedTime,
changedTitle = changedTitle,
changedTrack = changedTrack,
)
}

fun SessionNetworkModel.toSessionAppModel(): Session {
return Session(
fun SessionNetworkModel.toSessionDatabaseModel(): SessionDatabaseModel {
return SessionDatabaseModel(
sessionId = sessionId,
abstractt = abstractt,
dateText = date,
dateText = dateText,
dateUTC = dateUTC,
dayIndex = dayIndex,
description = description,
Expand All @@ -130,18 +132,18 @@ fun SessionNetworkModel.toSessionAppModel(): Session {
hasAlarm = hasAlarm,
language = language,
links = links,
highlight = isHighlight,
isHighlight = isHighlight,
recordingLicense = recordingLicense,
recordingOptOut = recordingOptOut,
relStartTime = relativeStartTime,
relativeStartTime = relativeStartTime,
roomName = roomName,
roomIdentifier = roomGuid,
roomIndex = roomIndex,
slug = slug,
speakers = createSpeakersList(speakers),
speakers = speakers,
startTime = startTime, // minutes since day start
subtitle = subtitle,
timeZoneOffset = timeZoneOffset?.let { ZoneOffset.ofTotalSeconds(it) }, // seconds
timeZoneOffset = timeZoneOffset,
title = title,
track = track,
type = type,
Expand All @@ -155,8 +157,8 @@ fun SessionNetworkModel.toSessionAppModel(): Session {
changedRecordingOptOut = changedRecordingOptOut,
changedRoomName = changedRoomName,
changedSpeakers = changedSpeakers,
changedSubtitle = changedSubtitle,
changedStartTime = changedStartTime,
changedSubtitle = changedSubtitle,
changedTitle = changedTitle,
changedTrack = changedTrack,
)
Expand All @@ -169,7 +171,7 @@ fun SessionNetworkModel.toSessionAppModel(): Session {
* scheme is used for similar sessions. This is achieved by customizing related track names. Colors
* are derived from track names, see [TrackBackgrounds].
*/
fun Session.sanitize(): Session {
fun SessionNetworkModel.sanitize(): SessionNetworkModel {
var tempTitle = title
var tempSubtitle = subtitle
var tempAbstract = abstractt
Expand All @@ -186,7 +188,7 @@ fun Session.sanitize(): Session {
if (tempAbstract == tempDescription) {
tempAbstract = ""
}
if (createSpeakersString(speakers) == tempSubtitle) {
if (speakers == tempSubtitle) {
tempSubtitle = ""
}
if (tempDescription.isEmpty()) {
Expand Down Expand Up @@ -221,12 +223,8 @@ fun Session.sanitize(): Session {
/**
* Delimiter which is used in [FahrplanParser] to construct the speakers string.
*/
private const val SPEAKERS_DELIMITER = ";"
private const val SPEAKERS_DELIMITER_FOR_SPLITTING = ";"

private fun createSpeakersList(speakers: String): List<String> {
return if (speakers.isEmpty()) emptyList() else speakers.split(SPEAKERS_DELIMITER)
}

private fun createSpeakersString(speakers: List<String>): String {
return speakers.joinToString(SPEAKERS_DELIMITER)
return if (speakers.isEmpty()) emptyList() else speakers.split(SPEAKERS_DELIMITER_FOR_SPLITTING).map { it.trim() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package nerd.tuxmobil.fahrplan.congress.dataconverters

import info.metadude.android.eventfahrplan.commons.temporal.DayRange
import info.metadude.android.eventfahrplan.commons.temporal.Moment
import nerd.tuxmobil.fahrplan.congress.models.Session
import nerd.tuxmobil.fahrplan.congress.models.VirtualDay
import info.metadude.android.eventfahrplan.database.models.Session as SessionDatabaseModel
import info.metadude.android.eventfahrplan.network.models.Session as SessionNetworkModel
import nerd.tuxmobil.fahrplan.congress.models.Session as SessionAppModel

fun List<Session>.shiftRoomIndicesOfMainSchedule(dayIndices: Set<Int>) = map {
fun List<SessionNetworkModel>.shiftRoomIndicesOfMainSchedule(dayIndices: Set<Int>) = map {
it.shiftRoomIndexOnDays(dayIndices)
}

fun List<Session>.toDayIndices(): Set<Int> {
fun List<SessionNetworkModel>.toDayIndices(): Set<Int> {
val dayIndices = HashSet<Int>()
forEach {
dayIndices.add(it.dayIndex)
Expand All @@ -20,10 +20,10 @@ fun List<Session>.toDayIndices(): Set<Int> {
}

/**
* Splits the given sessions into [VirtualDay]s. The [Session.dateText] field is used to separate them.
* Splits the given sessions into [VirtualDay]s. The [SessionAppModel.dateText] field is used to separate them.
* This field is unique for a virtual day, even for sessions after midnight.
*/
fun List<Session>.toVirtualDays(): List<VirtualDay> {
fun List<SessionAppModel>.toVirtualDays(): List<VirtualDay> {
var index = 0
return groupBy { it.dateText }
.map { (_, sessions) ->
Expand All @@ -32,11 +32,9 @@ fun List<Session>.toVirtualDays(): List<VirtualDay> {
}
}

fun List<Session>.toDateInfos() = map(Session::toDateInfo)
fun List<SessionDatabaseModel>.toDateInfos() = map(SessionDatabaseModel::toDateInfo)

fun List<Session>.toSessionsDatabaseModel() = map(Session::toSessionDatabaseModel)

fun List<Session>.toDayRanges(): List<DayRange> {
fun List<SessionDatabaseModel>.toDayRanges(): List<DayRange> {
val ranges = mutableSetOf<DayRange>()
forEach {
val day = Moment.parseDate(it.dateText)
Expand All @@ -46,8 +44,10 @@ fun List<Session>.toDayRanges(): List<DayRange> {
return ranges.sortedBy { it.startsAt }.toList()
}

fun List<SessionNetworkModel>.toSessionsAppModel2(): List<Session> = map(SessionNetworkModel::toSessionAppModel)
fun List<SessionDatabaseModel>.toSessionsNetworkModel() = map(SessionDatabaseModel::toSessionNetworkModel)

fun List<SessionNetworkModel>.toSessionsDatabaseModel() = map(SessionNetworkModel::toSessionDatabaseModel)

fun List<SessionDatabaseModel>.toSessionsAppModel() = map(SessionDatabaseModel::toSessionAppModel)

fun List<Session>.sanitize(): List<Session> = map(Session::sanitize)
fun List<SessionNetworkModel>.sanitize(): List<SessionNetworkModel> = map(SessionNetworkModel::sanitize)
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import info.metadude.android.eventfahrplan.commons.temporal.DayRange
import info.metadude.android.eventfahrplan.commons.temporal.Moment.Companion.MILLISECONDS_OF_ONE_SECOND
import info.metadude.android.eventfahrplan.commons.temporal.Moment.Companion.toMoment
import info.metadude.kotlin.library.engelsystem.models.Shift
import nerd.tuxmobil.fahrplan.congress.models.Session
import org.threeten.bp.Duration
import info.metadude.android.eventfahrplan.network.models.Session as SessionNetworkModel

private const val LOG_TAG = "ShiftsExtensions"

// Avoid conflicts with the IDs of the main schedule.
private const val SHIFT_ID_OFFSET = 300000

fun Shift.toSessionAppModel(
fun Shift.toSessionNetworkModel(

logging: Logging,
virtualRoomName: String,
dayRanges: List<DayRange>

) = Session(
) = SessionNetworkModel(
sessionId = "${SHIFT_ID_OFFSET + sID}",
abstractt = "",
dateText = startsAtLocalDateString,
dateUTC = dateUtcMs,
dayIndex = oneBasedDayIndex(logging, dayRanges),
description = descriptionText,
duration = shiftDuration, // minutes
relStartTime = minuteOfDay,
relativeStartTime = minuteOfDay,
roomName = virtualRoomName,
speakers = emptyList(),
speakers = "",
startTime = minuteOfDay, // minutes since day start
title = talkTitle,
subtitle = locationName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import info.metadude.android.eventfahrplan.commons.logging.Logging
import info.metadude.android.eventfahrplan.commons.temporal.DayRange
import info.metadude.kotlin.library.engelsystem.models.Shift

fun List<Shift>.toSessionAppModels(
fun List<Shift>.toSessionsNetworkModel(

logging: Logging,
virtualRoomName: String,
dayRanges: List<DayRange>

) = map { it.toSessionAppModel(logging, virtualRoomName, dayRanges) }
) = map { it.toSessionNetworkModel(logging, virtualRoomName, dayRanges) }

/**
* Returns a list of shifts which only contains shifts which are within the given day ranges extent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ internal class SessionDetailsViewModel(
hasWikiLinks = links.containsWikiLink(),
sessionLink = sessionLink,
// Options menu
isFlaggedAsFavorite = highlight,
isFlaggedAsFavorite = isHighlight,
hasAlarm = hasAlarm,
supportsFeedback = supportsFeedback,
supportsIndoorNavigation = supportsIndoorNavigation,
Expand Down Expand Up @@ -206,7 +206,7 @@ internal class SessionDetailsViewModel(
fun favorSession() {
loadSelectedSession { session ->
val favoredSession = session.copy(
highlight = true // Required: Update property because updateHighlight refers to its value!
isHighlight = true // Required: Update property because updateHighlight refers to its value!
)
repository.updateHighlight(favoredSession)
}
Expand All @@ -215,7 +215,7 @@ internal class SessionDetailsViewModel(
fun unfavorSession() {
loadSelectedSession { session ->
val unfavoredSession = session.copy (
highlight = false // Required: Update property because updateHighlight refers to its value!
isHighlight = false // Required: Update property because updateHighlight refers to its value!
)
repository.updateHighlight(unfavoredSession)
}
Expand Down

This file was deleted.

Loading

0 comments on commit 53885ac

Please sign in to comment.