-
-
Notifications
You must be signed in to change notification settings - Fork 192
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
795fa8b
commit d721f36
Showing
8 changed files
with
373 additions
and
74 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
100 changes: 100 additions & 0 deletions
100
app/src/main/java/me/ash/reader/infrastructure/preference/SwipeActionPreference.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,100 @@ | ||
package me.ash.reader.infrastructure.preference | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.datastore.preferences.core.Preferences | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import me.ash.reader.R | ||
import me.ash.reader.ui.ext.DataStoreKeys | ||
import me.ash.reader.ui.ext.dataStore | ||
import me.ash.reader.ui.ext.put | ||
|
||
data object SwipeGestureActions { | ||
const val None = 0 | ||
const val ToggleRead = 1 | ||
const val ToggleStarred = 2 | ||
} | ||
|
||
sealed class SwipeEndActionPreference(val action: Int) : Preference() { | ||
override fun put(context: Context, scope: CoroutineScope) { | ||
scope.launch { | ||
context.dataStore.put( | ||
DataStoreKeys.SwipeEndAction, action | ||
) | ||
} | ||
} | ||
|
||
data object None : SwipeEndActionPreference(SwipeGestureActions.None) | ||
data object ToggleRead : SwipeEndActionPreference(SwipeGestureActions.ToggleRead) | ||
data object ToggleStarred : | ||
SwipeEndActionPreference(SwipeGestureActions.ToggleStarred) | ||
|
||
val desc: String | ||
@Composable get() = when (this) { | ||
None -> stringResource(id = R.string.none) | ||
ToggleRead -> stringResource(id = R.string.toggle_read) | ||
ToggleStarred -> stringResource(id = R.string.toggle_starred) | ||
} | ||
|
||
companion object { | ||
val default: SwipeEndActionPreference = ToggleRead | ||
val values = listOf( | ||
None, | ||
ToggleRead, | ||
ToggleStarred | ||
) | ||
|
||
fun fromPreferences(preferences: Preferences): SwipeEndActionPreference { | ||
return when (preferences[DataStoreKeys.SwipeEndAction.key] | ||
?: SwipeGestureActions.ToggleStarred) { | ||
SwipeGestureActions.None -> None | ||
SwipeGestureActions.ToggleRead -> ToggleRead | ||
SwipeGestureActions.ToggleStarred -> ToggleStarred | ||
else -> default | ||
} | ||
} | ||
} | ||
} | ||
|
||
sealed class SwipeStartActionPreference(val action: Int) : Preference() { | ||
override fun put(context: Context, scope: CoroutineScope) { | ||
scope.launch { | ||
context.dataStore.put( | ||
DataStoreKeys.SwipeStartAction, action | ||
) | ||
} | ||
} | ||
|
||
data object None : SwipeStartActionPreference(SwipeGestureActions.None) | ||
data object ToggleRead : SwipeStartActionPreference(SwipeGestureActions.ToggleRead) | ||
data object ToggleStarred : | ||
SwipeStartActionPreference(SwipeGestureActions.ToggleStarred) | ||
|
||
val desc: String | ||
@Composable get() = when (this) { | ||
None -> stringResource(id = R.string.none) | ||
ToggleRead -> stringResource(id = R.string.toggle_read) | ||
ToggleStarred -> stringResource(id = R.string.toggle_starred) | ||
} | ||
|
||
companion object { | ||
val default: SwipeStartActionPreference = ToggleStarred | ||
val values = listOf( | ||
None, | ||
ToggleRead, | ||
ToggleStarred | ||
) | ||
|
||
fun fromPreferences(preferences: Preferences): SwipeStartActionPreference { | ||
return when (preferences[DataStoreKeys.SwipeStartAction.key] | ||
?: SwipeGestureActions.ToggleStarred) { | ||
SwipeGestureActions.None -> None | ||
SwipeGestureActions.ToggleRead -> ToggleRead | ||
SwipeGestureActions.ToggleStarred -> ToggleStarred | ||
else -> default | ||
} | ||
} | ||
} | ||
} |
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.