Skip to content

Commit

Permalink
Added an ability to undo archiving action.| #504
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jan 27, 2020
1 parent 90a15cc commit 85356ad
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class MessagesViewModel(application: Application) : BaseAndroidViewModel(applica
}
}

fun changeMsgsState(ids: Collection<Long>, localFolder: LocalFolder, newMsgState: MessageState) {
fun changeMsgsState(ids: Collection<Long>, localFolder: LocalFolder, newMsgState: MessageState,
notifyMsgStatesListener: Boolean = true) {
viewModelScope.launch {
val entities = roomDatabase.msgDao().getMsgsByIDSuspend(localFolder.account,
localFolder.fullName, ids.map { it })
Expand All @@ -147,7 +148,9 @@ class MessagesViewModel(application: Application) : BaseAndroidViewModel(applica
} else {
val candidates = prepareCandidates(entities, newMsgState)
roomDatabase.msgDao().updateSuspend(candidates)
msgStatesLiveData.postValue(newMsgState)
if (notifyMsgStatesListener) {
msgStatesLiveData.postValue(newMsgState)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,25 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen
if (position != RecyclerView.NO_POSITION) {
val item = adapter.getItemId(position)
listener?.currentFolder?.let {
messagesViewModel.changeMsgsState(listOf(item), it, MessageState.PENDING_ARCHIVING)
messagesViewModel.changeMsgsState(listOf(item), it, MessageState
.PENDING_ARCHIVING, false)
}

val snackBar = showSnackbar(view, getString(R.string.marked_for_archiving),
getString(R.string.undo), Snackbar.LENGTH_LONG, View.OnClickListener {
listener?.currentFolder?.let {
messagesViewModel.changeMsgsState(listOf(item), it, MessageState.NONE, false)
}
})

snackBar?.addCallback(object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
if (event != DISMISS_EVENT_ACTION) {
messagesViewModel.msgStatesLiveData.postValue(MessageState.PENDING_ARCHIVING)
}
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,16 @@ abstract class BaseFragment : Fragment(), LoaderManager.LoaderCallbacks<LoaderRe
* @param duration How long to display the message.
*/
@JvmOverloads
fun showInfoSnackbar(view: View?, msgText: String, duration: Int = Snackbar.LENGTH_INDEFINITE) {
view?.let { Snackbar.make(it, msgText, duration).setAction(android.R.string.ok) {}.show() }
fun showInfoSnackbar(view: View?, msgText: String, duration: Int = Snackbar.LENGTH_INDEFINITE)
: Snackbar? {
view?.let {
snackBar = Snackbar.make(it, msgText, duration).setAction(android.R.string.ok) {}.apply {
show()
}
return snackBar
}

return snackBar
}

/**
Expand All @@ -123,8 +131,9 @@ abstract class BaseFragment : Fragment(), LoaderManager.LoaderCallbacks<LoaderRe
* @param btnName The text of the Snackbar button
* @param onClickListener The Snackbar button click listener.
*/
fun showSnackbar(view: View, msgText: String, btnName: String, onClickListener: View.OnClickListener) {
showSnackbar(view, msgText, btnName, Snackbar.LENGTH_INDEFINITE, onClickListener)
fun showSnackbar(view: View, msgText: String, btnName: String,
onClickListener: View.OnClickListener): Snackbar? {
return showSnackbar(view, msgText, btnName, Snackbar.LENGTH_INDEFINITE, onClickListener)
}

/**
Expand All @@ -136,9 +145,18 @@ abstract class BaseFragment : Fragment(), LoaderManager.LoaderCallbacks<LoaderRe
* @param duration How long to display the message.
* @param onClickListener The Snackbar button click listener.
*/
fun showSnackbar(view: View?, msgText: String, btnName: String, duration: Int, onClickListener:
View.OnClickListener) {
view?.let { Snackbar.make(view, msgText, duration).setAction(btnName, onClickListener).show() }
fun showSnackbar(view: View?, msgText: String, btnName: String, duration: Int,
onClickListener: View.OnClickListener): Snackbar? {
view?.let {
snackBar = Snackbar.make(it, msgText, duration).setAction(btnName, onClickListener).apply {
show()
}

return snackBar
}

return snackBar

}

fun dismissCurrentSnackBar() {
Expand Down
2 changes: 2 additions & 0 deletions FlowCrypt/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,6 @@
<string name="can_not_send_due_to_auth_failure">Can\'t send message: auth failure</string>
<string name="mark_read">Mark read</string>
<string name="selection_text">Selected: %1$d</string>
<string name="marked_for_archiving">Marked for archiving</string>
<string name="undo">Undo</string>
</resources>

0 comments on commit 85356ad

Please sign in to comment.