2021-01-21 18:57:09 +01:00
|
|
|
/* Copyright 2020 Tusky Contributors
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.components.drafts
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.util.Log
|
|
|
|
import android.widget.LinearLayout
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.activity.viewModels
|
2021-06-24 21:23:29 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2021-01-21 18:57:09 +01:00
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2022-12-06 20:23:48 +01:00
|
|
|
import at.connyduck.calladapter.networkresult.fold
|
2021-01-21 18:57:09 +01:00
|
|
|
import com.google.android.material.bottomsheet.BottomSheetBehavior
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
import com.keylesspalace.tusky.BaseActivity
|
|
|
|
import com.keylesspalace.tusky.R
|
|
|
|
import com.keylesspalace.tusky.components.compose.ComposeActivity
|
|
|
|
import com.keylesspalace.tusky.databinding.ActivityDraftsBinding
|
|
|
|
import com.keylesspalace.tusky.db.DraftEntity
|
|
|
|
import com.keylesspalace.tusky.di.ViewModelFactory
|
2022-04-21 18:46:43 +02:00
|
|
|
import com.keylesspalace.tusky.util.parseAsMastodonHtml
|
2021-06-24 21:23:29 +02:00
|
|
|
import com.keylesspalace.tusky.util.visible
|
|
|
|
import kotlinx.coroutines.flow.collectLatest
|
|
|
|
import kotlinx.coroutines.launch
|
2021-01-21 18:57:09 +01:00
|
|
|
import retrofit2.HttpException
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
class DraftsActivity : BaseActivity(), DraftActionListener {
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
lateinit var viewModelFactory: ViewModelFactory
|
|
|
|
|
|
|
|
private val viewModel: DraftsViewModel by viewModels { viewModelFactory }
|
|
|
|
|
|
|
|
private lateinit var binding: ActivityDraftsBinding
|
|
|
|
private lateinit var bottomSheet: BottomSheetBehavior<LinearLayout>
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
binding = ActivityDraftsBinding.inflate(layoutInflater)
|
|
|
|
setContentView(binding.root)
|
|
|
|
|
|
|
|
setSupportActionBar(binding.includedToolbar.toolbar)
|
|
|
|
supportActionBar?.apply {
|
|
|
|
title = getString(R.string.title_drafts)
|
|
|
|
setDisplayHomeAsUpEnabled(true)
|
|
|
|
setDisplayShowHomeEnabled(true)
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:17:56 +02:00
|
|
|
binding.draftsErrorMessageView.setup(R.drawable.elephant_friend_empty, R.string.no_drafts)
|
2021-01-21 18:57:09 +01:00
|
|
|
|
|
|
|
val adapter = DraftsAdapter(this)
|
|
|
|
|
|
|
|
binding.draftsRecyclerView.adapter = adapter
|
|
|
|
binding.draftsRecyclerView.layoutManager = LinearLayoutManager(this)
|
|
|
|
binding.draftsRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
|
|
|
|
|
|
|
|
bottomSheet = BottomSheetBehavior.from(binding.bottomSheet.root)
|
|
|
|
|
2021-06-24 21:23:29 +02:00
|
|
|
lifecycleScope.launch {
|
|
|
|
viewModel.drafts.collectLatest { draftData ->
|
|
|
|
adapter.submitData(draftData)
|
2021-01-21 18:57:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 21:23:29 +02:00
|
|
|
|
|
|
|
adapter.addLoadStateListener {
|
|
|
|
binding.draftsErrorMessageView.visible(adapter.itemCount == 0)
|
|
|
|
}
|
2021-01-21 18:57:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOpenDraft(draft: DraftEntity) {
|
2022-12-06 20:23:48 +01:00
|
|
|
if (draft.inReplyToId == null) {
|
|
|
|
openDraftWithoutReply(draft)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val context = this as Context
|
2021-01-21 18:57:09 +01:00
|
|
|
|
2022-12-06 20:23:48 +01:00
|
|
|
lifecycleScope.launch {
|
2021-01-21 18:57:09 +01:00
|
|
|
bottomSheet.state = BottomSheetBehavior.STATE_COLLAPSED
|
2022-03-20 20:21:42 +01:00
|
|
|
viewModel.getStatus(draft.inReplyToId)
|
2022-12-06 20:23:48 +01:00
|
|
|
.fold(
|
2021-06-28 21:13:24 +02:00
|
|
|
{ status ->
|
2021-01-21 18:57:09 +01:00
|
|
|
val composeOptions = ComposeActivity.ComposeOptions(
|
2021-06-28 21:13:24 +02:00
|
|
|
draftId = draft.id,
|
2022-03-20 20:21:42 +01:00
|
|
|
content = draft.content,
|
2021-06-28 21:13:24 +02:00
|
|
|
contentWarning = draft.contentWarning,
|
|
|
|
inReplyToId = draft.inReplyToId,
|
2022-04-21 18:46:43 +02:00
|
|
|
replyingStatusContent = status.content.parseAsMastodonHtml().toString(),
|
2021-06-28 21:13:24 +02:00
|
|
|
replyingStatusAuthor = status.account.localUsername,
|
|
|
|
draftAttachments = draft.attachments,
|
|
|
|
poll = draft.poll,
|
|
|
|
sensitive = draft.sensitive,
|
2022-07-27 21:06:51 +02:00
|
|
|
visibility = draft.visibility,
|
2022-08-31 18:53:57 +02:00
|
|
|
scheduledAt = draft.scheduledAt,
|
|
|
|
language = draft.language,
|
2022-12-08 10:18:12 +01:00
|
|
|
statusId = draft.statusId,
|
Fix saving changes to statuses when editing (#3103)
* Fix saving changes to statuses when editing
With the previous code backing out of a status editing operation where changes
had been made (whether it was editing an existing status, a scheduled status,
or a draft) would prompt the user to save the changes as a new draft.
See https://github.com/tuskyapp/Tusky/issues/2704 and
https://github.com/tuskyapp/Tusky/issues/2705 for more detail.
The fix:
- Create an enum to represent the four different kinds of edits that can
happen
- Editing a new status (i.e., composing it for the first time)
- Editing a posted status
- Editing a draft
- Editing a scheduled status
- Store this in ComposeOptions, and set it appropriately everywhere
ComposeOptions is created.
- Check the edit kind when backing out of ComposeActivity, and use this to
show one of three different dialogs as appropriate so the user can:
- Save as new draft or discard changes
- Continue editing or discard changes
- Update existing draft or discard changes
Also fix ComposeViewModel.didChange(), which erroneously reported false if the
old text started with the new text (e.g., if the old text was "hello, world"
and it was edited to "hello", didChange() would not consider that to be a
change).
Fixes https://github.com/tuskyapp/Tusky/issues/2704,
https://github.com/tuskyapp/Tusky/issues/2705
* Use orEmpty extension function
2022-12-31 13:04:49 +01:00
|
|
|
kind = ComposeActivity.ComposeKind.EDIT_DRAFT
|
2021-01-21 18:57:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
|
|
|
|
|
2022-12-06 20:23:48 +01:00
|
|
|
startActivity(ComposeActivity.startIntent(context, composeOptions))
|
2021-06-28 21:13:24 +02:00
|
|
|
},
|
|
|
|
{ throwable ->
|
2021-01-21 18:57:09 +01:00
|
|
|
bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
|
|
|
|
|
|
|
|
Log.w(TAG, "failed loading reply information", throwable)
|
|
|
|
|
|
|
|
if (throwable is HttpException && throwable.code() == 404) {
|
|
|
|
// the original status to which a reply was drafted has been deleted
|
|
|
|
// let's open the ComposeActivity without reply information
|
2022-12-06 20:23:48 +01:00
|
|
|
Toast.makeText(context, getString(R.string.drafts_post_reply_removed), Toast.LENGTH_LONG).show()
|
2021-01-21 18:57:09 +01:00
|
|
|
openDraftWithoutReply(draft)
|
|
|
|
} else {
|
|
|
|
Snackbar.make(binding.root, getString(R.string.drafts_failed_loading_reply), Snackbar.LENGTH_SHORT)
|
2021-06-28 21:13:24 +02:00
|
|
|
.show()
|
2021-01-21 18:57:09 +01:00
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|
|
|
|
)
|
2021-01-21 18:57:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun openDraftWithoutReply(draft: DraftEntity) {
|
|
|
|
val composeOptions = ComposeActivity.ComposeOptions(
|
2021-06-28 21:13:24 +02:00
|
|
|
draftId = draft.id,
|
2022-03-20 20:21:42 +01:00
|
|
|
content = draft.content,
|
2021-06-28 21:13:24 +02:00
|
|
|
contentWarning = draft.contentWarning,
|
|
|
|
draftAttachments = draft.attachments,
|
|
|
|
poll = draft.poll,
|
|
|
|
sensitive = draft.sensitive,
|
2022-07-27 21:06:51 +02:00
|
|
|
visibility = draft.visibility,
|
2022-08-31 18:53:57 +02:00
|
|
|
scheduledAt = draft.scheduledAt,
|
|
|
|
language = draft.language,
|
2022-12-08 10:18:12 +01:00
|
|
|
statusId = draft.statusId,
|
Fix saving changes to statuses when editing (#3103)
* Fix saving changes to statuses when editing
With the previous code backing out of a status editing operation where changes
had been made (whether it was editing an existing status, a scheduled status,
or a draft) would prompt the user to save the changes as a new draft.
See https://github.com/tuskyapp/Tusky/issues/2704 and
https://github.com/tuskyapp/Tusky/issues/2705 for more detail.
The fix:
- Create an enum to represent the four different kinds of edits that can
happen
- Editing a new status (i.e., composing it for the first time)
- Editing a posted status
- Editing a draft
- Editing a scheduled status
- Store this in ComposeOptions, and set it appropriately everywhere
ComposeOptions is created.
- Check the edit kind when backing out of ComposeActivity, and use this to
show one of three different dialogs as appropriate so the user can:
- Save as new draft or discard changes
- Continue editing or discard changes
- Update existing draft or discard changes
Also fix ComposeViewModel.didChange(), which erroneously reported false if the
old text started with the new text (e.g., if the old text was "hello, world"
and it was edited to "hello", didChange() would not consider that to be a
change).
Fixes https://github.com/tuskyapp/Tusky/issues/2704,
https://github.com/tuskyapp/Tusky/issues/2705
* Use orEmpty extension function
2022-12-31 13:04:49 +01:00
|
|
|
kind = ComposeActivity.ComposeKind.EDIT_DRAFT
|
2021-01-21 18:57:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
startActivity(ComposeActivity.startIntent(this, composeOptions))
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDeleteDraft(draft: DraftEntity) {
|
|
|
|
viewModel.deleteDraft(draft)
|
|
|
|
Snackbar.make(binding.root, getString(R.string.draft_deleted), Snackbar.LENGTH_LONG)
|
2021-06-28 21:13:24 +02:00
|
|
|
.setAction(R.string.action_undo) {
|
|
|
|
viewModel.restoreDraft(draft)
|
|
|
|
}
|
|
|
|
.show()
|
2021-01-21 18:57:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val TAG = "DraftsActivity"
|
|
|
|
|
|
|
|
fun newIntent(context: Context) = Intent(context, DraftsActivity::class.java)
|
|
|
|
}
|
|
|
|
}
|