Extract AttachmentsViewState to a separated file
MAILAND-1555
This commit is contained in:
parent
60c021ca32
commit
85f30b2be3
|
@ -63,6 +63,7 @@ import ch.protonmail.android.adapters.AttachmentListAdapter;
|
|||
import ch.protonmail.android.api.models.room.messages.Attachment;
|
||||
import ch.protonmail.android.api.models.room.messages.LocalAttachment;
|
||||
import ch.protonmail.android.attachments.AttachmentsViewModel;
|
||||
import ch.protonmail.android.attachments.AttachmentsViewState;
|
||||
import ch.protonmail.android.attachments.ImportAttachmentsWorker;
|
||||
import ch.protonmail.android.core.Constants;
|
||||
import ch.protonmail.android.core.ProtonMailApplication;
|
||||
|
@ -412,15 +413,15 @@ public class AddAttachmentsActivity extends BaseStoragePermissionActivity implem
|
|||
}
|
||||
}
|
||||
|
||||
private void viewStateChanged(AttachmentsViewModel.ViewState viewState) {
|
||||
if (viewState instanceof AttachmentsViewModel.ViewState.MissingConnectivity) {
|
||||
private void viewStateChanged(AttachmentsViewState viewState) {
|
||||
if (viewState instanceof AttachmentsViewState.MissingConnectivity) {
|
||||
onMessageReady();
|
||||
}
|
||||
|
||||
if (viewState instanceof AttachmentsViewModel.ViewState.UpdateAttachments) {
|
||||
if (viewState instanceof AttachmentsViewState.UpdateAttachments) {
|
||||
onMessageReady();
|
||||
updateDisplayedAttachments(
|
||||
((AttachmentsViewModel.ViewState.UpdateAttachments) viewState).getAttachments()
|
||||
((AttachmentsViewState.UpdateAttachments) viewState).getAttachments()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@ import androidx.lifecycle.ViewModel
|
|||
import androidx.lifecycle.viewModelScope
|
||||
import ch.protonmail.android.activities.AddAttachmentsActivity.EXTRA_DRAFT_ID
|
||||
import ch.protonmail.android.activities.messageDetails.repository.MessageDetailsRepository
|
||||
import ch.protonmail.android.api.models.room.messages.Attachment
|
||||
import ch.protonmail.android.api.models.room.messages.Message
|
||||
import ch.protonmail.android.core.QueueNetworkUtil
|
||||
import ch.protonmail.android.utils.MessageUtils
|
||||
|
@ -46,7 +45,7 @@ class AttachmentsViewModel @ViewModelInject constructor(
|
|||
private val networkUtil: QueueNetworkUtil
|
||||
) : ViewModel() {
|
||||
|
||||
val viewState: MutableLiveData<ViewState> = MutableLiveData()
|
||||
val viewState: MutableLiveData<AttachmentsViewState> = MutableLiveData()
|
||||
|
||||
fun init() {
|
||||
viewModelScope.launch(dispatchers.Io) {
|
||||
|
@ -58,12 +57,12 @@ class AttachmentsViewModel @ViewModelInject constructor(
|
|||
val messageFlow = messageDetailsRepository.findMessageByDbId(messageDbId)
|
||||
|
||||
if (!networkUtil.isConnected()) {
|
||||
postViewState(ViewState.MissingConnectivity)
|
||||
postViewState(AttachmentsViewState.MissingConnectivity)
|
||||
}
|
||||
|
||||
messageFlow.onEach { updatedMessage ->
|
||||
if (isDraftCreationEvent(existingMessage, updatedMessage)) {
|
||||
postViewState(ViewState.UpdateAttachments(updatedMessage.Attachments))
|
||||
postViewState(AttachmentsViewState.UpdateAttachments(updatedMessage.Attachments))
|
||||
this.cancel()
|
||||
}
|
||||
}.collect()
|
||||
|
@ -74,15 +73,10 @@ class AttachmentsViewModel @ViewModelInject constructor(
|
|||
private fun isDraftCreationEvent(existingMessage: Message, updatedMessage: Message) =
|
||||
!isRemoteMessage(existingMessage) && isRemoteMessage(updatedMessage)
|
||||
|
||||
private suspend fun postViewState(state: ViewState) = withContext(dispatchers.Main) {
|
||||
private suspend fun postViewState(state: AttachmentsViewState) = withContext(dispatchers.Main) {
|
||||
viewState.value = state
|
||||
}
|
||||
|
||||
private fun isRemoteMessage(message: Message) = !MessageUtils.isLocalMessageId(message.messageId)
|
||||
|
||||
sealed class ViewState {
|
||||
object MissingConnectivity : ViewState()
|
||||
data class UpdateAttachments(val attachments: List<Attachment>) : ViewState()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2020 Proton Technologies AG
|
||||
*
|
||||
* This file is part of ProtonMail.
|
||||
*
|
||||
* ProtonMail is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* ProtonMail is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
package ch.protonmail.android.attachments
|
||||
|
||||
import ch.protonmail.android.api.models.room.messages.Attachment
|
||||
|
||||
sealed class AttachmentsViewState {
|
||||
object MissingConnectivity : AttachmentsViewState()
|
||||
data class UpdateAttachments(val attachments: List<Attachment>) : AttachmentsViewState()
|
||||
}
|
|
@ -26,9 +26,8 @@ import ch.protonmail.android.activities.AddAttachmentsActivity
|
|||
import ch.protonmail.android.activities.messageDetails.repository.MessageDetailsRepository
|
||||
import ch.protonmail.android.api.models.room.messages.Attachment
|
||||
import ch.protonmail.android.api.models.room.messages.Message
|
||||
import ch.protonmail.android.attachments.AttachmentsViewModel.ViewState
|
||||
import ch.protonmail.android.attachments.AttachmentsViewModel.ViewState.MissingConnectivity
|
||||
import ch.protonmail.android.attachments.AttachmentsViewModel.ViewState.UpdateAttachments
|
||||
import ch.protonmail.android.attachments.AttachmentsViewState.MissingConnectivity
|
||||
import ch.protonmail.android.attachments.AttachmentsViewState.UpdateAttachments
|
||||
import ch.protonmail.android.core.QueueNetworkUtil
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
|
@ -57,7 +56,7 @@ class AttachmentsViewModelTest : CoroutinesTest {
|
|||
lateinit var networkUtils: QueueNetworkUtil
|
||||
|
||||
@RelaxedMockK
|
||||
private lateinit var mockObserver: Observer<ViewState>
|
||||
private lateinit var mockObserver: Observer<AttachmentsViewState>
|
||||
|
||||
@RelaxedMockK
|
||||
private lateinit var savedState: SavedStateHandle
|
||||
|
|
Loading…
Reference in New Issue