-
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.
- Loading branch information
1 parent
88c2e77
commit 529a3d3
Showing
19 changed files
with
475 additions
and
2 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
12 changes: 12 additions & 0 deletions
12
shared/src/androidMain/kotlin/com/kevinschildhorn/fotopresenter/ui/shared/DriverFactory.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,12 @@ | ||
package com.kevinschildhorn.fotopresenter.ui.shared | ||
|
||
import android.content.Context | ||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.android.AndroidSqliteDriver | ||
import com.kevinschildhorn.fotopresenter.PlaylistDatabase | ||
|
||
actual class DriverFactory(private val context: Context) { | ||
actual fun createDriver(): SqlDriver { | ||
return AndroidSqliteDriver(PlaylistDatabase.Schema, context, "playlist.db") | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
...ommonMain/kotlin/com/kevinschildhorn/fotopresenter/data/datasources/PlaylistDataSource.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,87 @@ | ||
package com.kevinschildhorn.fotopresenter.data.datasources | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import com.kevinschildhorn.fotopresenter.Playlist | ||
import com.kevinschildhorn.fotopresenter.PlaylistDatabase | ||
import com.kevinschildhorn.fotopresenter.PlaylistImage | ||
import com.kevinschildhorn.fotopresenter.data.ImageDirectory | ||
import org.koin.core.component.KoinComponent | ||
|
||
class PlaylistDataSource( | ||
driver: SqlDriver, | ||
) : KoinComponent { | ||
|
||
private val database = PlaylistDatabase(driver) | ||
|
||
fun createPlaylist(name: String, directories: List<ImageDirectory> = emptyList()): Playlist? { | ||
return try { | ||
//logger?.i { "Creating Playlist $name with images: ${directories.count()}" } | ||
database.playlistQueries.insertPlaylist(name) | ||
val playlist = database.playlistQueries.selectPlaylistByName(name).executeAsOne() | ||
//logger?.i { "Playlist Created, now adding images" } | ||
|
||
directories.forEach { | ||
insertPlaylistImage(playlistId = playlist.id, directory = it) | ||
} | ||
playlist | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
|
||
fun getAllPlaylists(): List<Playlist> { | ||
return try { | ||
database.playlistQueries.selectAllPlaylists().executeAsList() | ||
} catch (e: Exception) { | ||
emptyList() | ||
} | ||
|
||
} | ||
|
||
fun getPlaylistByName(name: String): Playlist? { | ||
return try { | ||
//logger?.i { "Selecting playlist by name $name" } | ||
val playList: Playlist = | ||
database.playlistQueries.selectPlaylistByName(name).executeAsOne() | ||
//logger?.i { "Retrieved Playlist!" } | ||
playList | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
|
||
fun insertPlaylistImage(playlistId: Long, directory: ImageDirectory): PlaylistImage? { | ||
//logger?.i { "Inserting Playlist Image ${directory.name}" } | ||
|
||
database.imageDirectoryQueries.insertPlaylistImage( | ||
playlist_id = playlistId, | ||
directory_path = directory.details.fullPath, | ||
directory_id = directory.id.toLong(), | ||
) | ||
return getPlaylistImage(playlistId, directory.details.fullPath) | ||
} | ||
|
||
fun getPlaylistImage(playlistId: Long, directoryPath: String): PlaylistImage? { | ||
return try { | ||
//logger?.i { "Selecting Playlist Image $playlistId" } | ||
val image: PlaylistImage = | ||
database.imageDirectoryQueries.selectPlaylistImage(playlistId, directoryPath) | ||
.executeAsOne() | ||
//logger?.i { "Selecting Playlist Image" } | ||
image | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
|
||
fun deletePlaylistByName(name: String): Boolean { | ||
return try { | ||
val playlist = database.playlistQueries.selectPlaylistByName(name).executeAsOne() | ||
database.playlistQueries.deletePlaylist(playlist.name) | ||
database.imageDirectoryQueries.deletePlaylist(playlist.id) | ||
true | ||
} catch (e: Exception) { | ||
false | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.kevinschildhorn.fotopresenter.data.repositories | ||
|
||
import com.kevinschildhorn.fotopresenter.Playlist | ||
import com.kevinschildhorn.fotopresenter.PlaylistImage | ||
import com.kevinschildhorn.fotopresenter.data.ImageDirectory | ||
import com.kevinschildhorn.fotopresenter.data.datasources.PlaylistDataSource | ||
|
||
class PlaylistRepository( | ||
private val playlistDataSource: PlaylistDataSource, | ||
) { | ||
|
||
fun createPlaylist(name: String, directories: List<ImageDirectory> = emptyList()): Playlist? = | ||
playlistDataSource.createPlaylist(name, directories) | ||
|
||
fun getAllPlaylists(): List<Playlist> = | ||
playlistDataSource.getAllPlaylists() | ||
|
||
fun getPlaylistByName(name: String): Playlist? = | ||
playlistDataSource.getPlaylistByName(name) | ||
|
||
fun insertPlaylistImage(playlistId: Long, directory: ImageDirectory): PlaylistImage? = | ||
playlistDataSource.insertPlaylistImage(playlistId, directory) | ||
|
||
fun getPlaylistImage(playlistId: Long, directoryPath: String): PlaylistImage? = | ||
playlistDataSource.getPlaylistImage(playlistId, directoryPath) | ||
|
||
fun deletePlaylistByName(name: String): Boolean = | ||
playlistDataSource.deletePlaylistByName(name) | ||
} |
22 changes: 22 additions & 0 deletions
22
...commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/playlist/PlaylistScreen.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,22 @@ | ||
package com.kevinschildhorn.fotopresenter.ui.screens.playlist | ||
|
||
|
||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
|
||
@Composable | ||
fun PlaylistScreen( | ||
viewModel: PlaylistViewModel, | ||
onLoginSuccess: () -> Unit, | ||
) { | ||
val uiState by viewModel.uiState.collectAsState() | ||
LazyColumn { | ||
items(uiState.playlists) { | ||
Text(it.name) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...nMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/playlist/PlaylistScreenState.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,11 @@ | ||
package com.kevinschildhorn.fotopresenter.ui.screens.playlist | ||
|
||
import com.kevinschildhorn.fotopresenter.Playlist | ||
import com.kevinschildhorn.fotopresenter.ui.UiState | ||
import com.kevinschildhorn.fotopresenter.ui.screens.common.ScreenState | ||
|
||
data class PlaylistScreenState( | ||
val playlists: List<Playlist> = emptyList(), | ||
val selectedId: Long = 0L, | ||
override val state: UiState = UiState.IDLE, | ||
) : ScreenState |
28 changes: 28 additions & 0 deletions
28
...monMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/playlist/PlaylistViewModel.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,28 @@ | ||
package com.kevinschildhorn.fotopresenter.ui.screens.playlist | ||
|
||
import co.touchlab.kermit.Logger | ||
import com.kevinschildhorn.fotopresenter.data.repositories.PlaylistRepository | ||
import com.kevinschildhorn.fotopresenter.ui.shared.ViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import org.koin.core.component.KoinComponent | ||
|
||
class PlaylistViewModel( | ||
private val playlistRepository: PlaylistRepository, | ||
private val logger: Logger, | ||
) : ViewModel(), KoinComponent { | ||
|
||
private val _uiState = MutableStateFlow(PlaylistScreenState()) | ||
val uiState: StateFlow<PlaylistScreenState> = _uiState.asStateFlow() | ||
|
||
init { | ||
refreshPlaylists() | ||
} | ||
|
||
fun refreshPlaylists(){ | ||
val allPlaylists = playlistRepository.getAllPlaylists() | ||
_uiState.update { it.copy(playlists = allPlaylists) } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/shared/DriverFactory.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,7 @@ | ||
package com.kevinschildhorn.fotopresenter.ui.shared | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
|
||
expect class DriverFactory { | ||
fun createDriver(): SqlDriver | ||
} |
25 changes: 25 additions & 0 deletions
25
shared/src/commonMain/sqldelight/com/kevinschildhorn/fotopresenter/ImageDirectory.sq
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,25 @@ | ||
CREATE TABLE PlaylistImage ( | ||
id INTEGER PRIMARY KEY NOT NULL, | ||
playlist_id INTEGER NOT NULL, | ||
directory_path TEXT NOT NULL, | ||
directory_id INTEGER NOT NULL | ||
); | ||
|
||
insertPlaylistImage: | ||
INSERT INTO PlaylistImage(playlist_id, directory_path, directory_id) | ||
VALUES (?, ?, ?); | ||
|
||
selectPlaylistImage: | ||
SELECT * | ||
FROM PlaylistImage | ||
WHERE playlist_id = ? AND directory_path = ?; | ||
|
||
deletePlaylistImage: | ||
DELETE | ||
FROM PlaylistImage | ||
WHERE directory_path = ?; | ||
|
||
deletePlaylist: | ||
DELETE | ||
FROM PlaylistImage | ||
WHERE playlist_id = ?; |
Oops, something went wrong.