Yuito-app-android/app/src/main/java/com/keylesspalace/tusky/components/scheduled/ScheduledStatusActivity.kt

144 lines
5.3 KiB
Kotlin
Raw Normal View History

2019-12-30 21:09:10 +01:00
/* Copyright 2019 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>. */
2019-12-22 11:47:34 +01:00
package com.keylesspalace.tusky.components.scheduled
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.androidx.lifecycle.autoDispose
2019-12-22 11:47:34 +01:00
import com.keylesspalace.tusky.BaseActivity
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.StatusScheduledEvent
ComposeActivity refactor (#1541) * Convert ComposeActivity to Kotlin * More ComposeActivity cleanups * Move ComposeActivity to it's own package * Remove ComposeActivity.IntentBuilder * Re-do part of the media downsizing/uploading * Add sending of status to ViewModel, draft media descriptions * Allow uploading video, update description after uploading * Enable camera, enable upload cancelling * Cleanup of ComposeActivity * Extract CaptionDialog, extract ComposeActivity methods * Fix handling of redrafted media * Add initial state and media uploading out of Activity * Change ComposeOptions.mentionedUsernames to be Set rather than List We probably don't want repeated usernames when we are writing a post and Set provides such guarantee for free plus it tells it to the callers. The only disadvantage is lack of order but it shouldn't be a problem. * Add combineOptionalLiveData. Add docs. It it useful for nullable LiveData's. I think we cannot differentiate between value not being set and value being null so I just added the variant without null check. * Add poll support to Compose. * cleanup code * move more classes into compose package * cleanup code * fix button behavior * add error handling for media upload * add caching for instance data again * merge develop * fix scheduled toots * delete unused string * cleanup ComposeActivity * fix restoring media from drafts * make media upload code a little bit clearer * cleanup autocomplete search code * avoid duplicate object creation in SavedTootActivity * perf: avoid unnecessary work when initializing ComposeActivity * add license header to new files * use small toot button on bigger displays * fix ComposeActivityTest * fix bad merge * use Singles.zip instead of Single.zip
2019-12-19 19:09:40 +01:00
import com.keylesspalace.tusky.components.compose.ComposeActivity
import com.keylesspalace.tusky.databinding.ActivityScheduledStatusBinding
import com.keylesspalace.tusky.di.Injectable
2019-12-30 20:40:27 +01:00
import com.keylesspalace.tusky.di.ViewModelFactory
import com.keylesspalace.tusky.entity.ScheduledStatus
import com.keylesspalace.tusky.util.hide
import com.keylesspalace.tusky.util.show
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.inject.Inject
class ScheduledStatusActivity : BaseActivity(), ScheduledStatusActionListener, Injectable {
2019-12-30 20:40:27 +01:00
@Inject
lateinit var viewModelFactory: ViewModelFactory
@Inject
lateinit var eventHub: EventHub
private val viewModel: ScheduledStatusViewModel by viewModels { viewModelFactory }
private val adapter = ScheduledStatusAdapter(this)
2019-12-30 20:48:01 +01:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityScheduledStatusBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.includedToolbar.toolbar)
2019-12-22 11:55:26 +01:00
supportActionBar?.run {
title = getString(R.string.title_scheduled_posts)
2019-12-22 11:55:26 +01:00
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}
binding.swipeRefreshLayout.setOnRefreshListener(this::refreshStatuses)
binding.swipeRefreshLayout.setColorSchemeResources(R.color.tusky_blue)
binding.scheduledTootList.setHasFixedSize(true)
binding.scheduledTootList.layoutManager = LinearLayoutManager(this)
2019-12-30 20:48:01 +01:00
val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
binding.scheduledTootList.addItemDecoration(divider)
binding.scheduledTootList.adapter = adapter
2019-12-30 20:40:27 +01:00
lifecycleScope.launch {
viewModel.data.collectLatest { pagingData ->
adapter.submitData(pagingData)
}
}
2019-12-30 20:40:27 +01:00
adapter.addLoadStateListener { loadState ->
if (loadState.refresh is LoadState.Error) {
binding.progressBar.hide()
binding.errorMessageView.setup(R.drawable.elephant_error, R.string.error_generic) {
refreshStatuses()
2019-12-30 20:40:27 +01:00
}
binding.errorMessageView.show()
}
if (loadState.refresh != LoadState.Loading) {
binding.swipeRefreshLayout.isRefreshing = false
}
if (loadState.refresh is LoadState.NotLoading) {
binding.progressBar.hide()
2021-06-28 22:04:34 +02:00
if (adapter.itemCount == 0) {
binding.errorMessageView.setup(R.drawable.elephant_friend_empty, R.string.no_scheduled_posts)
binding.errorMessageView.show()
} else {
binding.errorMessageView.hide()
2019-12-30 20:40:27 +01:00
}
}
}
eventHub.events
.observeOn(AndroidSchedulers.mainThread())
.autoDispose(this)
.subscribe { event ->
if (event is StatusScheduledEvent) {
adapter.refresh()
}
}
2019-12-30 20:48:01 +01:00
}
2019-12-30 20:40:27 +01:00
private fun refreshStatuses() {
adapter.refresh()
}
2019-12-30 20:48:01 +01:00
override fun edit(item: ScheduledStatus) {
2021-06-28 22:04:34 +02:00
val intent = ComposeActivity.startIntent(
this,
ComposeActivity.ComposeOptions(
scheduledTootId = item.id,
content = item.params.text,
2021-06-28 22:04:34 +02:00
contentWarning = item.params.spoilerText,
mediaAttachments = item.mediaAttachments,
inReplyToId = item.params.inReplyToId,
visibility = item.params.visibility,
scheduledAt = item.scheduledAt,
sensitive = item.params.sensitive
)
)
startActivity(intent)
}
2019-12-30 20:48:01 +01:00
override fun delete(item: ScheduledStatus) {
2019-12-30 20:40:27 +01:00
viewModel.deleteScheduledStatus(item)
}
2019-12-30 20:48:01 +01:00
companion object {
fun newIntent(context: Context) = Intent(context, ScheduledStatusActivity::class.java)
2019-12-30 20:48:01 +01:00
}
}