package com.example.data.prefs import android.content.Context import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.floatPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore import com.example.data.model.MediaTypeFilter import com.example.data.model.RepeatMode import com.example.data.model.SortOption import com.example.data.model.SortOrder import com.example.data.model.VideoScaleMode import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import androidx.datastore.preferences.core.longPreferencesKey data class LastPlayedInfo( val mediaId: String, val mediaUri: String, val positionMs: Long, val isVideo: Boolean, val title: String = "" ) private val Context.dataStore: DataStore by preferencesDataStore(name = "swipeplay_settings") class UserPreferencesRepository(private val context: Context) { private object Keys { val FEED_FILTER = stringPreferencesKey("feed_filter") val VIDEO_SCALE_MODE = stringPreferencesKey("video_scale_mode") val RESUME_PLAYBACK = booleanPreferencesKey("resume_playback") val BACKGROUND_AUDIO = booleanPreferencesKey("background_audio") val PLAYBACK_SPEED = floatPreferencesKey("playback_speed") val REPEAT_MODE = stringPreferencesKey("repeat_mode") val SHUFFLE = booleanPreferencesKey("shuffle") val REDUCED_MOTION = booleanPreferencesKey("reduced_motion") val MUSIC_SORT_OPTION = stringPreferencesKey("music_sort_option") val MUSIC_SORT_ORDER = stringPreferencesKey("music_sort_order") val VIDEO_SORT_OPTION = stringPreferencesKey("video_sort_option") val VIDEO_SORT_ORDER = stringPreferencesKey("video_sort_order") val IS_GRID_LAYOUT = booleanPreferencesKey("is_grid_layout") val LAST_PLAYED_ID = stringPreferencesKey("last_played_id") val LAST_PLAYED_URI = stringPreferencesKey("last_played_uri") val LAST_PLAYED_POSITION_MS = longPreferencesKey("last_played_position_ms") val LAST_PLAYED_IS_VIDEO = booleanPreferencesKey("last_played_is_video") val LAST_PLAYED_TITLE = stringPreferencesKey("last_played_title") } val lastPlayedFlow: Flow = context.dataStore.data.map { prefs -> val id = prefs[Keys.LAST_PLAYED_ID] val uri = prefs[Keys.LAST_PLAYED_URI] if (!id.isNullOrBlank() && !uri.isNullOrBlank()) { LastPlayedInfo( mediaId = id, mediaUri = uri, positionMs = prefs[Keys.LAST_PLAYED_POSITION_MS] ?: 0L, isVideo = prefs[Keys.LAST_PLAYED_IS_VIDEO] ?: false, title = prefs[Keys.LAST_PLAYED_TITLE] ?: "" ) } else { null } } val feedFilterFlow: Flow = context.dataStore.data.map { prefs -> try { MediaTypeFilter.valueOf(prefs[Keys.FEED_FILTER] ?: MediaTypeFilter.ALL.name) } catch (_: Exception) { MediaTypeFilter.ALL } } val videoScaleModeFlow: Flow = context.dataStore.data.map { prefs -> try { VideoScaleMode.valueOf(prefs[Keys.VIDEO_SCALE_MODE] ?: VideoScaleMode.FIT.name) } catch (_: Exception) { VideoScaleMode.FIT } } val resumePlaybackFlow: Flow = context.dataStore.data.map { prefs -> prefs[Keys.RESUME_PLAYBACK] ?: true } val backgroundAudioFlow: Flow = context.dataStore.data.map { prefs -> prefs[Keys.BACKGROUND_AUDIO] ?: true } val playbackSpeedFlow: Flow = context.dataStore.data.map { prefs -> prefs[Keys.PLAYBACK_SPEED] ?: 1.0f } val repeatModeFlow: Flow = context.dataStore.data.map { prefs -> try { RepeatMode.valueOf(prefs[Keys.REPEAT_MODE] ?: RepeatMode.OFF.name) } catch (_: Exception) { RepeatMode.OFF } } val shuffleFlow: Flow = context.dataStore.data.map { prefs -> prefs[Keys.SHUFFLE] ?: false } val reducedMotionFlow: Flow = context.dataStore.data.map { prefs -> prefs[Keys.REDUCED_MOTION] ?: false } val isGridLayoutFlow: Flow = context.dataStore.data.map { prefs -> prefs[Keys.IS_GRID_LAYOUT] ?: false } val musicSortOptionFlow: Flow = context.dataStore.data.map { prefs -> try { SortOption.valueOf(prefs[Keys.MUSIC_SORT_OPTION] ?: SortOption.NAME.name) } catch (_: Exception) { SortOption.NAME } } val musicSortOrderFlow: Flow = context.dataStore.data.map { prefs -> try { SortOrder.valueOf(prefs[Keys.MUSIC_SORT_ORDER] ?: SortOrder.ASCENDING.name) } catch (_: Exception) { SortOrder.ASCENDING } } suspend fun setFeedFilter(filter: MediaTypeFilter) { context.dataStore.edit { it[Keys.FEED_FILTER] = filter.name } } suspend fun setVideoScaleMode(mode: VideoScaleMode) { context.dataStore.edit { it[Keys.VIDEO_SCALE_MODE] = mode.name } } suspend fun setResumePlayback(enabled: Boolean) { context.dataStore.edit { it[Keys.RESUME_PLAYBACK] = enabled } } suspend fun setBackgroundAudio(enabled: Boolean) { context.dataStore.edit { it[Keys.BACKGROUND_AUDIO] = enabled } } suspend fun setPlaybackSpeed(speed: Float) { context.dataStore.edit { it[Keys.PLAYBACK_SPEED] = speed } } suspend fun setRepeatMode(mode: RepeatMode) { context.dataStore.edit { it[Keys.REPEAT_MODE] = mode.name } } suspend fun setShuffle(enabled: Boolean) { context.dataStore.edit { it[Keys.SHUFFLE] = enabled } } suspend fun setReducedMotion(enabled: Boolean) { context.dataStore.edit { it[Keys.REDUCED_MOTION] = enabled } } suspend fun setIsGridLayout(isGrid: Boolean) { context.dataStore.edit { it[Keys.IS_GRID_LAYOUT] = isGrid } } suspend fun setMusicSort(option: SortOption, order: SortOrder) { context.dataStore.edit { it[Keys.MUSIC_SORT_OPTION] = option.name it[Keys.MUSIC_SORT_ORDER] = order.name } } suspend fun saveLastPlayed(id: String, uri: String, positionMs: Long, isVideo: Boolean, title: String = "") { context.dataStore.edit { it[Keys.LAST_PLAYED_ID] = id it[Keys.LAST_PLAYED_URI] = uri it[Keys.LAST_PLAYED_POSITION_MS] = positionMs it[Keys.LAST_PLAYED_IS_VIDEO] = isVideo it[Keys.LAST_PLAYED_TITLE] = title } } }