forked from Ashinch/ReadYou
-
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.
feat(reading): support for specifying the composition of shared conte…
…nt (Ashinch#660)
- Loading branch information
Showing
11 changed files
with
153 additions
and
36 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
61 changes: 61 additions & 0 deletions
61
app/src/main/java/me/ash/reader/infrastructure/preference/SharedContentPreference.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,61 @@ | ||
package me.ash.reader.infrastructure.preference | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.compose.runtime.Stable | ||
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.orNotEmpty | ||
import me.ash.reader.ui.ext.put | ||
|
||
sealed class SharedContentPreference(val value: Int) : Preference() { | ||
object OnlyLink : SharedContentPreference(0) | ||
object TitleAndLink : SharedContentPreference(1) | ||
|
||
override fun put(context: Context, scope: CoroutineScope) { | ||
scope.launch { | ||
context.dataStore.put( | ||
DataStoreKeys.SharedContent, | ||
value | ||
) | ||
} | ||
} | ||
|
||
@Stable | ||
fun toDesc(context: Context): String = | ||
when (this) { | ||
OnlyLink -> context.getString(R.string.only_link) | ||
TitleAndLink -> context.getString(R.string.title_and_link) | ||
} | ||
|
||
fun share(context: Context, title: String?, link: String?) { | ||
when (this) { | ||
OnlyLink -> share(context, link.orEmpty()) | ||
TitleAndLink -> share(context, title.orNotEmpty { it + "\n" } + link.orEmpty()) | ||
} | ||
} | ||
|
||
private fun share(context: Context, content: String) { | ||
context.startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND).apply { | ||
putExtra(Intent.EXTRA_TEXT, content) | ||
type = "text/plain" | ||
}, context.getString(R.string.share))) | ||
} | ||
|
||
companion object { | ||
|
||
val default = OnlyLink | ||
val values = listOf(OnlyLink, TitleAndLink) | ||
|
||
fun fromPreferences(preferences: Preferences): SharedContentPreference = | ||
when (preferences[DataStoreKeys.SharedContent.key]) { | ||
0 -> OnlyLink | ||
1 -> TitleAndLink | ||
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
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
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