Skip to content

Commit

Permalink
save a draft while you are typing the post
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jan 22, 2024
1 parent f1388b4 commit 26a1624
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/src/main/java/com/vitorpamplona/amethyst/LocalPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -535,6 +544,10 @@ open class NewPostViewModel() : ViewModel() {
userSuggestionAnchor = null
userSuggestionsMainMessage = null

viewModelScope.launch(Dispatchers.IO) {
clearDraft()
}

NostrSearchEventOrUserDataSource.clear()
}

Expand All @@ -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()

Expand Down

0 comments on commit 26a1624

Please sign in to comment.