save a draft while you are typing the post

pull/749/head
greenart7c3 2024-01-22 09:02:44 -03:00
rodzic f1388b46d5
commit 26a1624399
2 zmienionych plików z 55 dodań i 0 usunięć

Wyświetl plik

@ -111,6 +111,7 @@ private object PrefKeys {
const val LOGIN_WITH_EXTERNAL_SIGNER = "login_with_external_signer"
const val AUTOMATICALLY_SHOW_PROFILE_PICTURE = "automatically_show_profile_picture"
const val SIGNER_PACKAGE_NAME = "signer_package_name"
const val NEW_POST_DRAFT = "draft_new_post"
const val ALL_ACCOUNT_INFO = "all_saved_accounts_info"
const val SHARED_SETTINGS = "shared_settings"
@ -458,6 +459,30 @@ object LocalPreferences {
}
}
fun saveDraft(
message: String,
account: Account,
) {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
with(prefs.edit()) {
putString(PrefKeys.NEW_POST_DRAFT, message)
apply()
}
}
fun loadDraft(account: Account): String? {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
return prefs.getString(PrefKeys.NEW_POST_DRAFT, "")
}
fun clearDraft(account: Account) {
val prefs = encryptedPreferences(account.keyPair.pubKey.toNpub())
with(prefs.edit()) {
remove(PrefKeys.NEW_POST_DRAFT)
apply()
}
}
suspend fun innerLoadCurrentAccountFromEncryptedStorage(npub: String?): Account? =
withContext(Dispatchers.IO) {
checkNotInMainThread()

Wyświetl plik

@ -35,6 +35,7 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fonfon.kgeohash.toGeoHash
import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
@ -213,6 +214,14 @@ open class NewPostViewModel() : ViewModel() {
zapRaiserAmount = null
forwardZapTo = Split()
forwardZapToEditting = TextFieldValue("")
viewModelScope.launch(Dispatchers.IO) {
val draft = loadDraft()
if (draft != null) {
message = TextFieldValue(draft)
updateMessage(message)
}
}
}
fun sendPost(relayList: List<Relay>? = null) {
@ -535,6 +544,10 @@ open class NewPostViewModel() : ViewModel() {
userSuggestionAnchor = null
userSuggestionsMainMessage = null
viewModelScope.launch(Dispatchers.IO) {
clearDraft()
}
NostrSearchEventOrUserDataSource.clear()
}
@ -550,7 +563,24 @@ open class NewPostViewModel() : ViewModel() {
pTags = pTags?.filter { it != userToRemove }
}
open fun saveDraft(message: String) {
account?.let { LocalPreferences.saveDraft(message, it) }
}
open fun loadDraft(): String? {
account?.let { return LocalPreferences.loadDraft(it) }
return null
}
open fun clearDraft() {
account?.let { LocalPreferences.clearDraft(it) }
}
open fun updateMessage(it: TextFieldValue) {
viewModelScope.launch(Dispatchers.IO) {
saveDraft(it.text)
}
message = it
urlPreview = findUrlInMessage()