Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Draft in Activity onStop instead of Fragment one #1839

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/src/main/java/com/infomaniak/mail/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.infomaniak.mail.ui

import android.os.Bundle
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import com.infomaniak.lib.applock.LockActivity
import com.infomaniak.lib.applock.Utils.isKeyguardSecure
Expand Down Expand Up @@ -67,4 +68,9 @@ open class BaseActivity : AppCompatActivity() {
resetLastAppClosing()
}
}

fun getCurrentFragment(@IdRes fragmentContainerViewId: Int) = supportFragmentManager
.findFragmentById(fragmentContainerViewId)
?.childFragmentManager
?.primaryNavigationFragment
}
6 changes: 1 addition & 5 deletions app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ class MainActivity : BaseActivity() {
}
}

private val currentFragment
get() = supportFragmentManager
.findFragmentById(R.id.mainHostFragment)
?.childFragmentManager
?.primaryNavigationFragment
private val currentFragment get() = getCurrentFragment(R.id.mainHostFragment)

@Inject
lateinit var draftsActionsWorkerScheduler: DraftsActionsWorker.Scheduler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import com.infomaniak.mail.BuildConfig
import com.infomaniak.mail.MatomoMail.trackDestination
import com.infomaniak.mail.R
import com.infomaniak.mail.data.models.AppSettings
import com.infomaniak.mail.data.models.draft.Draft
import com.infomaniak.mail.databinding.ActivityNewMessageBinding
import com.infomaniak.mail.ui.BaseActivity
import com.infomaniak.mail.ui.LaunchActivity
import com.infomaniak.mail.ui.main.SnackbarManager
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.SentryDebug
import com.infomaniak.mail.workers.DraftsActionsWorker
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

Expand All @@ -52,6 +54,9 @@ class NewMessageActivity : BaseActivity() {
@Inject
lateinit var snackbarManager: SnackbarManager

@Inject
lateinit var draftsActionsWorkerScheduler: DraftsActionsWorker.Scheduler

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG)
Expand Down Expand Up @@ -109,4 +114,34 @@ class NewMessageActivity : BaseActivity() {
SentryDebug.addNavigationBreadcrumb(destination.displayName, arguments)
trackDestination(destination)
}


override fun onStop() {
saveDraft()
super.onStop()
}

fun saveDraft() {

val fragment = getCurrentFragment(R.id.newMessageHostFragment)

val (subjectValue, uiBodyValue) = if (fragment is NewMessageFragment) {
fragment.getSubjectAndBodyValues()
} else with(newMessageViewModel) {
lastOnStopSubjectValue to lastOnStopBodyValue
}

newMessageViewModel.executeDraftActionWhenStopping(
action = if (newMessageViewModel.shouldSendInsteadOfSave) Draft.DraftAction.SEND else Draft.DraftAction.SAVE,
isFinishing = isFinishing,
isTaskRoot = isTaskRoot,
subjectValue = subjectValue,
uiBodyValue = uiBodyValue,
startWorkerCallback = ::startWorker,
)
}

private fun startWorker() {
draftsActionsWorkerScheduler.scheduleWork(newMessageViewModel.draftLocalUuid())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import com.infomaniak.mail.utils.WebViewUtils
import com.infomaniak.mail.utils.WebViewUtils.Companion.destroyAndClearHistory
import com.infomaniak.mail.utils.WebViewUtils.Companion.setupNewMessageWebViewSettings
import com.infomaniak.mail.utils.extensions.*
import com.infomaniak.mail.workers.DraftsActionsWorker
import dagger.hilt.android.AndroidEntryPoint
import io.sentry.Sentry
import io.sentry.SentryLevel
Expand Down Expand Up @@ -124,9 +123,6 @@ class NewMessageFragment : Fragment() {
@Inject
lateinit var localSettings: LocalSettings

@Inject
lateinit var draftsActionsWorkerScheduler: DraftsActionsWorker.Scheduler

@Inject
lateinit var informationDialog: InformationAlertDialog

Expand Down Expand Up @@ -478,7 +474,8 @@ class NewMessageFragment : Fragment() {
} else if (attachments.count() > attachmentAdapter.itemCount) {
// If we are adding Attachments, directly save the Draft, so the Attachments' upload starts now.
// TODO: Only save Attachments, and not the whole Draft.
saveDraft()
// TODO: When not using `saveDraft()` anymore, make it private again.
newMessageActivity.saveDraft()
}

// When removing an Attachment, both counts will be the same, because the Adapter is already notified.
Expand Down Expand Up @@ -527,24 +524,19 @@ class NewMessageFragment : Fragment() {
}
}

override fun onStop() {
saveDraft()
super.onStop()
}
override fun onStop() = with(newMessageViewModel) {

private fun saveDraft() = with(newMessageViewModel) {
executeDraftActionWhenStopping(
action = if (shouldSendInsteadOfSave) DraftAction.SEND else DraftAction.SAVE,
isFinishing = requireActivity().isFinishing,
isTaskRoot = requireActivity().isTaskRoot,
subjectValue = binding.subjectTextField.text.toString(),
uiBodyValue = binding.bodyTextField.text.toString(),
startWorkerCallback = ::startWorker,
)
}
/**
* When the Activity is being stopped, we save the Draft.
* We then need the up-to-date subject & body values.
* If we are in the NewMessageFragment when stopping, it's easy, we just get them from the binding.
* If we are not (ex: AI fragments), we get them from the ViewModel.
* Hence, here, we save them to the ViewModel when stopping the NewMessageFragment.
*/
lastOnStopSubjectValue = binding.subjectTextField.text.toString()
lastOnStopBodyValue = binding.bodyTextField.text.toString()

private fun startWorker() {
draftsActionsWorkerScheduler.scheduleWork(newMessageViewModel.draftLocalUuid())
super.onStop()
}

private fun onDeleteAttachment(position: Int) {
Expand Down Expand Up @@ -600,4 +592,8 @@ class NewMessageFragment : Fragment() {
fun closeAiPrompt() = aiManager.closeAiPrompt()

fun isSubjectBlank() = binding.subjectTextField.text?.isBlank() == true

fun getSubjectAndBodyValues(): Pair<String, String> = with(binding) {
return subjectTextField.text.toString() to bodyTextField.text.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class NewMessageViewModel @Inject constructor(
val uiQuoteLiveData = MutableLiveData<String?>()
//endregion

var lastOnStopSubjectValue = ""
var lastOnStopBodyValue = ""

var isAutoCompletionOpened = false
var isEditorExpanded = false
var isExternalBannerManuallyClosed = false
Expand Down
Loading