diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index 6d02af7df..b34c753df 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -239,17 +239,27 @@ open class NewPostViewModel : fun user(): User? = account?.userProfile() + open fun init(accountVM: AccountViewModel) { + this.accountViewModel = accountVM + this.account = accountVM.account + this.canAddInvoice = hasLnAddress() + this.canAddZapRaiser = hasLnAddress() + + this.userSuggestions?.reset() + this.userSuggestions = UserSuggestionState(accountVM) + + this.emojiSuggestions?.reset() + this.emojiSuggestions = EmojiSuggestionState(accountVM) + } + open fun load( - accountViewModel: AccountViewModel, replyingTo: Note?, quote: Note?, fork: Note?, version: Note?, draft: Note?, ) { - this.accountViewModel = accountViewModel - this.account = accountViewModel.account - + val accountViewModel = accountViewModel ?: return val noteEvent = draft?.event val noteAuthor = draft?.author diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NewPostScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NewPostScreen.kt index bba53539d..985cf68a3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NewPostScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NewPostScreen.kt @@ -159,7 +159,7 @@ fun NewPostScreen( nav: Nav, ) { val postViewModel: NewPostViewModel = viewModel() - postViewModel.account = accountViewModel.account + postViewModel.init(accountViewModel) postViewModel.wantsToAddGeoHash = enableGeolocation val context = LocalContext.current @@ -183,7 +183,7 @@ fun NewPostScreen( LaunchedEffect(Unit) { launch(Dispatchers.IO) { - postViewModel.load(accountViewModel, baseReplyTo, quote, fork, version, draft) + postViewModel.load(baseReplyTo, quote, fork, version, draft) message?.ifBlank { null }?.let { postViewModel.updateMessage(TextFieldValue(it)) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt index 7564ea2b3..4d624a49e 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/publicChannels/send/ChannelNewMessageViewModel.kt @@ -162,6 +162,9 @@ open class ChannelNewMessageViewModel : this.userSuggestions?.reset() this.userSuggestions = UserSuggestionState(accountVM) + this.emojiSuggestions?.reset() + this.emojiSuggestions = EmojiSuggestionState(accountVM) + this.uploadState = ChatFileUploadState( account?.settings?.defaultFileServer ?: DEFAULT_MEDIA_SERVERS[0], diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt index 65f4018ee..76053df10 100644 --- a/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt @@ -36,6 +36,7 @@ import io.mockk.mockkObject import io.mockk.unmockkAll import io.mockk.verify import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.test.runTest import org.junit.After import org.junit.Before @@ -75,9 +76,12 @@ class NewPostViewModelTest { every { replyingTo.event } returns textNoteEvent every { accountViewModel.userProfile() } returns mockk(relaxed = true) + every { accountViewModel.account.userProfile() } returns mockk(relaxed = true) + every { accountViewModel.account.myEmojis } returns mockk>>(relaxed = true) // Act: Call load with mentions - newPostViewModelUnderTest.load(accountViewModel, replyingTo, quote = null, fork = null, version = null, draft = null) + newPostViewModelUnderTest.init(accountViewModel) + newPostViewModelUnderTest.load(replyingTo, quote = null, fork = null, version = null, draft = null) // Assert // Two mentions should call LocalCache.getOrCreateUser twice @@ -87,9 +91,9 @@ class NewPostViewModelTest { @Test fun `test load with zero mentions`() = runTest { - // Arrange: Setup Note with zero mentions every { accountViewModel.account } returns mockk() + // Arrange: Setup Note with zero mentions val textNoteEvent = mockk(relaxed = true) every { textNoteEvent.mentions() } returns emptyList() every { replyingTo.event } returns textNoteEvent @@ -97,7 +101,7 @@ class NewPostViewModelTest { every { accountViewModel.userProfile() } returns mockk(relaxed = true) // Act: Call load with empty mentions - newPostViewModelUnderTest.load(accountViewModel, replyingTo, quote = null, fork = null, version = null, draft = null) + newPostViewModelUnderTest.load(replyingTo, quote = null, fork = null, version = null, draft = null) // Assert // With no mentions LocalCache.getOrCreateUser should not be called @@ -107,9 +111,9 @@ class NewPostViewModelTest { @Test fun `test load with empty mentions`() = runTest { - // Arrange: Setup empty mentions every { accountViewModel.account } returns mockk() + // Arrange: Setup empty mentions val textNoteEvent = mockk(relaxed = true) every { textNoteEvent.mentions() } returns emptyList() every { replyingTo.event } returns textNoteEvent @@ -117,7 +121,7 @@ class NewPostViewModelTest { every { accountViewModel.userProfile() } returns mockk(relaxed = true) // Act: Call load with empty mentions - newPostViewModelUnderTest.load(accountViewModel, replyingTo, quote = null, fork = null, version = null, draft = null) + newPostViewModelUnderTest.load(replyingTo, quote = null, fork = null, version = null, draft = null) // Assert // Verify LocalCache.getOrCreateUser(it) is not called with empty hex, it will crash the app