-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from KevinSchildhorn/JsonPlaylist
Json playlist
- Loading branch information
Showing
26 changed files
with
553 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...nMain/kotlin/com/kevinschildhorn/fotopresenter/data/datasources/PlaylistFileDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.kevinschildhorn.fotopresenter.data.datasources | ||
|
||
import co.touchlab.kermit.Logger | ||
import com.kevinschildhorn.fotopresenter.data.PlaylistDetails | ||
import com.kevinschildhorn.fotopresenter.data.network.NetworkHandler | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.koin.core.component.KoinComponent | ||
|
||
class PlaylistFileDataSource( | ||
private val logger: Logger?, | ||
private val networkHandler: NetworkHandler, | ||
) : KoinComponent { | ||
|
||
suspend fun importPlaylists(): List<PlaylistDetails> = | ||
networkHandler.getPlaylists().mapNotNull { | ||
try { | ||
Json.decodeFromString<PlaylistDetails>(it) | ||
} catch (e: Exception) { | ||
logger?.e(e) { "Error importing Playlist" } | ||
null | ||
} | ||
} | ||
|
||
suspend fun deletePlaylist(playlist: PlaylistDetails): Boolean = | ||
try { | ||
networkHandler.deletePlaylist(playlist.name) | ||
true | ||
} catch (e: Exception) { | ||
logger?.e(e) { "Error Deleting Playlist" } | ||
false | ||
} | ||
|
||
suspend fun exportPlaylist(playlist: PlaylistDetails): Boolean { | ||
try { | ||
val jsonString = Json.encodeToString(playlist) | ||
networkHandler.savePlaylist(playlist.name, jsonString) | ||
} catch (e: Exception) { | ||
logger?.e(e) { "Error Exporting Playlists" } | ||
return false | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 32 additions & 19 deletions
51
...mmonMain/kotlin/com/kevinschildhorn/fotopresenter/data/repositories/PlaylistRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,44 @@ | ||
package com.kevinschildhorn.fotopresenter.data.repositories | ||
|
||
import com.kevinschildhorn.fotopresenter.Playlist | ||
import com.kevinschildhorn.fotopresenter.PlaylistItems | ||
import com.kevinschildhorn.fotopresenter.data.Directory | ||
import com.kevinschildhorn.fotopresenter.data.ImageDirectory | ||
import com.kevinschildhorn.fotopresenter.data.PlaylistDetails | ||
import com.kevinschildhorn.fotopresenter.data.datasources.PlaylistDataSource | ||
import com.kevinschildhorn.fotopresenter.data.PlaylistItem | ||
import com.kevinschildhorn.fotopresenter.data.datasources.PlaylistFileDataSource | ||
import com.kevinschildhorn.fotopresenter.data.datasources.PlaylistSQLDataSource | ||
|
||
class PlaylistRepository( | ||
private val playlistDataSource: PlaylistDataSource, | ||
private val playlistSQLDataSource: PlaylistSQLDataSource, | ||
private val playlistFileDataSource: PlaylistFileDataSource, | ||
) { | ||
|
||
fun createPlaylist(name: String, directories: List<ImageDirectory> = emptyList()): Playlist? = | ||
playlistDataSource.createPlaylist(name, directories) | ||
suspend fun createPlaylist(name: String, directories: List<ImageDirectory> = emptyList()): Playlist? { | ||
val playlist = playlistSQLDataSource.createPlaylist(name, directories) | ||
playlistSQLDataSource.getPlaylistByName(name)?.let { | ||
playlistFileDataSource.exportPlaylist(it) | ||
} | ||
return playlist | ||
} | ||
|
||
suspend fun getAllPlaylists(): List<PlaylistDetails> { | ||
playlistFileDataSource.importPlaylists() | ||
return playlistSQLDataSource.getAllPlaylists() | ||
} | ||
|
||
suspend fun insertPlaylistImage(playlistId: Long, directory: Directory): PlaylistItem? { | ||
val item = playlistSQLDataSource.insertPlaylistImage(playlistId, directory) | ||
playlistSQLDataSource.getPlaylistById(playlistId)?.let { | ||
playlistFileDataSource.exportPlaylist(it) | ||
} | ||
return item | ||
} | ||
|
||
suspend fun deletePlaylist(id: Long): Boolean { | ||
playlistSQLDataSource.getPlaylistById(id)?.let { | ||
playlistFileDataSource.deletePlaylist(it) | ||
} | ||
return playlistSQLDataSource.deletePlaylist(id) | ||
} | ||
|
||
fun getAllPlaylists(): List<PlaylistDetails> = | ||
playlistDataSource.getAllPlaylists() | ||
|
||
fun getPlaylistByName(name: String): PlaylistDetails? = | ||
playlistDataSource.getPlaylistByName(name) | ||
|
||
fun insertPlaylistImage(playlistId: Long, directory: Directory): PlaylistItems? = | ||
playlistDataSource.insertPlaylistImage(playlistId, directory) | ||
|
||
fun getPlaylistImage(playlistId: Long, directoryPath: String): PlaylistItems? = | ||
playlistDataSource.getPlaylistImage(playlistId, directoryPath) | ||
|
||
fun deletePlaylist(id: Long): Boolean = | ||
playlistDataSource.deletePlaylist(id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.