implement reply draft

This commit is contained in:
greenart7c3 2024-01-24 09:55:31 -03:00
parent 2c086f76e2
commit cdd620987b
4 changed files with 27 additions and 4 deletions

View File

@ -112,6 +112,7 @@ private object PrefKeys {
const val AUTOMATICALLY_SHOW_PROFILE_PICTURE = "automatically_show_profile_picture" const val AUTOMATICALLY_SHOW_PROFILE_PICTURE = "automatically_show_profile_picture"
const val SIGNER_PACKAGE_NAME = "signer_package_name" const val SIGNER_PACKAGE_NAME = "signer_package_name"
const val NEW_POST_DRAFT = "draft_new_post" const val NEW_POST_DRAFT = "draft_new_post"
const val DRAFT_REPLY_POST = "draft_reply_post"
const val ALL_ACCOUNT_INFO = "all_saved_accounts_info" const val ALL_ACCOUNT_INFO = "all_saved_accounts_info"
const val SHARED_SETTINGS = "shared_settings" const val SHARED_SETTINGS = "shared_settings"
@ -461,15 +462,22 @@ object LocalPreferences {
fun saveDraft( fun saveDraft(
message: String, message: String,
replyPost: String?,
account: Account, account: Account,
) { ) {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub()) val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
with(prefs.edit()) { with(prefs.edit()) {
putString(PrefKeys.NEW_POST_DRAFT, message) putString(PrefKeys.NEW_POST_DRAFT, message)
putString(PrefKeys.DRAFT_REPLY_POST, replyPost)
apply() apply()
} }
} }
fun loadReplyDraft(account: Account): String? {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
return prefs.getString(PrefKeys.DRAFT_REPLY_POST, null)
}
fun loadDraft(account: Account): String? { fun loadDraft(account: Account): String? {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub()) val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
return prefs.getString(PrefKeys.NEW_POST_DRAFT, null) return prefs.getString(PrefKeys.NEW_POST_DRAFT, null)
@ -478,6 +486,7 @@ object LocalPreferences {
fun clearDraft(account: Account) { fun clearDraft(account: Account) {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub()) val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
with(prefs.edit()) { with(prefs.edit()) {
remove(PrefKeys.DRAFT_REPLY_POST)
remove(PrefKeys.NEW_POST_DRAFT) remove(PrefKeys.NEW_POST_DRAFT)
apply() apply()
} }

View File

@ -130,7 +130,9 @@ import com.fonfon.kgeohash.toGeoHash
import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState import com.google.accompanist.permissions.rememberPermissionState
import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.Nip96MediaServers import com.vitorpamplona.amethyst.service.Nip96MediaServers
@ -207,9 +209,19 @@ fun NewPostView(
var relayList = remember { accountViewModel.account.activeWriteRelays().toImmutableList() } var relayList = remember { accountViewModel.account.activeWriteRelays().toImmutableList() }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
postViewModel.load(accountViewModel, baseReplyTo, quote)
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
val replyDraft = LocalPreferences.loadReplyDraft(accountViewModel.account)
if (replyDraft.isNullOrBlank()) {
postViewModel.load(accountViewModel, baseReplyTo, quote)
} else {
val note = LocalCache.checkGetOrCreateNote(replyDraft)
if (note == null) {
postViewModel.load(accountViewModel, baseReplyTo, quote)
} else {
postViewModel.load(accountViewModel, note, quote)
}
}
postViewModel.imageUploadingError.collect { error -> postViewModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) { Toast.makeText(context, error, Toast.LENGTH_SHORT).show() } withContext(Dispatchers.Main) { Toast.makeText(context, error, Toast.LENGTH_SHORT).show() }
} }

View File

@ -515,6 +515,7 @@ open class NewPostViewModel() : ViewModel() {
urlPreview = null urlPreview = null
isUploadingImage = false isUploadingImage = false
pTags = null pTags = null
eTags = null
wantsDirectMessage = false wantsDirectMessage = false
@ -543,6 +544,7 @@ open class NewPostViewModel() : ViewModel() {
userSuggestions = emptyList() userSuggestions = emptyList()
userSuggestionAnchor = null userSuggestionAnchor = null
userSuggestionsMainMessage = null userSuggestionsMainMessage = null
originalNote = null
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
clearDraft() clearDraft()
@ -564,7 +566,7 @@ open class NewPostViewModel() : ViewModel() {
} }
open fun saveDraft(message: String) { open fun saveDraft(message: String) {
account?.let { LocalPreferences.saveDraft(message, it) } account?.let { LocalPreferences.saveDraft(message, originalNote?.idHex, it) }
} }
open fun loadDraft(): String? { open fun loadDraft(): String? {

View File

@ -638,7 +638,7 @@ fun ListContent(
}, },
{ {
coroutineScope.launch(Dispatchers.IO) { coroutineScope.launch(Dispatchers.IO) {
LocalPreferences.saveDraft(it, accountViewModel.account) LocalPreferences.saveDraft(it, null, accountViewModel.account)
draftText = null draftText = null
showDraft = false showDraft = false
wantsToPost = true wantsToPost = true