Skip to content

Commit

Permalink
👌 IMPROVE: RepositoryImpl: use sealed class instead of method on gene…
Browse files Browse the repository at this point in the history
…ric type var

Signed-off-by: Jonas Sulzer <[email protected]>
  • Loading branch information
violoncelloCH committed May 31, 2024
1 parent 8d8c4bd commit 0b106f8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data class Association(
val description: String,
val url: String?,
val relatedTags: Set<Tag>
) {
) : DataModel() {
companion object {
val EMPTY = Association("", "", "", "", setOf())
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/github/swent/echo/data/model/DataModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.swent.echo.data.model

sealed class DataModel {
fun getId(): String =
when (this) {
is Association -> this.associationId
is Event -> this.eventId
is Tag -> this.tagId
is UserProfile -> this.userId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data class Event(
val participantCount: Int,
val maxParticipants: Int,
val imageId: Int
) {
) : DataModel() {
companion object {
val EMPTY =
Event(
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/github/swent/echo/data/model/Tag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class Tag(
val name: String,
@SerialName("parent_id")
val parentId: String? = null, // TODO: We might want to remove the default value here...
) {
) : DataModel() {
companion object {
val EMPTY = Tag(tagId = "", name = "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ data class UserProfile(
val tags: Set<Tag>,
val committeeMember: Set<AssociationHeader>,
val associationsSubscriptions: Set<AssociationHeader>,
) {
) : DataModel() {
fun toEventCreator(): EventCreator = EventCreator(userId, name)

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.swent.echo.data.repository

import com.github.swent.echo.connectivity.NetworkService
import com.github.swent.echo.data.model.Association
import com.github.swent.echo.data.model.DataModel
import com.github.swent.echo.data.model.Event
import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.UserProfile
Expand Down Expand Up @@ -40,14 +41,14 @@ class RepositoryImpl(
var tags_last_cached_subs: MutableMap<String, Long> = mutableMapOf()
}

private suspend fun <DataObject> getObject(
private suspend fun <T : DataModel> getObject(
objectId: String,
objectTTL: Long,
getLocal: suspend (String, Long) -> DataObject?,
getRemote: suspend (String) -> DataObject?,
getLocal: suspend (String, Long) -> T?,
getRemote: suspend (String) -> T?,
deleteLocal: suspend (String) -> Unit,
setLocal: suspend (DataObject) -> Unit
): DataObject? {
setLocal: suspend (T) -> Unit
): T? {
if (isOffline()) {
return getLocal(objectId, FETCH_ALL)
}
Expand All @@ -69,13 +70,13 @@ class RepositoryImpl(
return localResult
}

private suspend fun <DataObject> getObjects(
private suspend fun <T : DataModel> getObjects(
objectIds: List<String>,
objectTTL: Long,
getLocal: suspend (List<String>, Long) -> List<DataObject>,
getRemote: suspend (List<String>) -> List<DataObject>,
setLocal: suspend (List<DataObject>) -> Unit
): List<DataObject> {
getLocal: suspend (List<String>, Long) -> List<T>,
getRemote: suspend (List<String>) -> List<T>,
setLocal: suspend (List<T>) -> Unit
): List<T> {
if (isOffline()) {
return getLocal(objectIds, FETCH_ALL)
}
Expand All @@ -94,14 +95,14 @@ class RepositoryImpl(
return localResult
}

private suspend fun <DataObject> getAllObjects(
private suspend fun <T : DataModel> getAllObjects(
objectsLastCachedAll: MutableList<Long>,
objectTTL: Long,
getAllLocal: suspend (Long) -> List<DataObject>,
getAllNotInRemote: suspend (List<String>) -> List<DataObject>,
getAllLocal: suspend (Long) -> List<T>,
getAllNotInRemote: suspend (List<String>) -> List<T>,
deleteAllNotInLocal: suspend (List<String>) -> Unit,
setLocal: suspend (List<DataObject>) -> Unit
): List<DataObject> {
setLocal: suspend (List<T>) -> Unit
): List<T> {
if (isOffline() || notExpired(objectsLastCachedAll[0], objectTTL)) {
return getAllLocal(FETCH_ALL)
}
Expand All @@ -119,10 +120,10 @@ class RepositoryImpl(
return nonExpired + remoteUpdate
}

private suspend fun <DataObject> setObject(
obj: DataObject,
setLocal: suspend (DataObject) -> Unit,
setRemote: suspend (DataObject) -> Unit,
private suspend fun <T : DataModel> setObject(
obj: T,
setLocal: suspend (T) -> Unit,
setRemote: suspend (T) -> Unit,
exceptionType: String
) {
if (isOffline()) {
Expand Down Expand Up @@ -428,12 +429,3 @@ class RepositoryImpl(
fileCache.delete("$userId.jpeg")
}
}

private fun <DataObject> DataObject.getId(): String =
when (this) {
is Association -> this.associationId
is Event -> this.eventId
is Tag -> this.tagId
is UserProfile -> this.userId
else -> ""
}

0 comments on commit 0b106f8

Please sign in to comment.