clean up code

This commit is contained in:
Konrad Pozniak 2019-12-30 20:48:01 +01:00
parent 84a3280964
commit 7107018ae2
3 changed files with 44 additions and 59 deletions

View File

@ -4,15 +4,12 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.keylesspalace.tusky.BaseActivity
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.StatusScheduledEvent
import com.keylesspalace.tusky.components.compose.ComposeActivity
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.di.ViewModelFactory
@ -20,31 +17,19 @@ import com.keylesspalace.tusky.entity.ScheduledStatus
import com.keylesspalace.tusky.util.Status
import com.keylesspalace.tusky.util.hide
import com.keylesspalace.tusky.util.show
import com.uber.autodispose.AutoDispose.autoDisposable
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_scheduled_toot.*
import kotlinx.android.synthetic.main.toolbar_basic.*
import javax.inject.Inject
class ScheduledTootActivity : BaseActivity(), ScheduledTootAction, Injectable {
companion object {
@JvmStatic
fun newIntent(context: Context): Intent {
return Intent(context, ScheduledTootActivity::class.java)
}
}
lateinit var adapter: ScheduledTootAdapter
@Inject
lateinit var eventHub: EventHub
@Inject
lateinit var viewModelFactory: ViewModelFactory
lateinit var viewModel: ScheduledTootViewModel
private val adapter = ScheduledTootAdapter(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scheduled_toot)
@ -59,38 +44,13 @@ class ScheduledTootActivity : BaseActivity(), ScheduledTootAction, Injectable {
swipeRefreshLayout.setOnRefreshListener(this::refreshStatuses)
scheduledTootList.setHasFixedSize(true)
val layoutManager = LinearLayoutManager(this)
scheduledTootList.layoutManager = layoutManager
val divider = DividerItemDecoration(this, layoutManager.orientation)
scheduledTootList.layoutManager = LinearLayoutManager(this)
val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
scheduledTootList.addItemDecoration(divider)
adapter = ScheduledTootAdapter(this)
scheduledTootList.adapter = adapter
viewModel = ViewModelProvider(this, viewModelFactory)[ScheduledTootViewModel::class.java]
loadStatuses()
eventHub.events
.observeOn(AndroidSchedulers.mainThread())
.`as`(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe { event ->
if (event is StatusScheduledEvent) {
refreshStatuses()
}
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
onBackPressed()
return true
}
}
return super.onOptionsItemSelected(item)
}
fun loadStatuses() {
viewModel.data.observe(this, Observer {
adapter.submitList(it)
})
@ -123,16 +83,24 @@ class ScheduledTootActivity : BaseActivity(), ScheduledTootAction, Injectable {
}
})
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
onBackPressed()
return true
}
}
return super.onOptionsItemSelected(item)
}
private fun refreshStatuses() {
viewModel.reload()
}
override fun edit(position: Int, item: ScheduledStatus?) {
if (item == null) {
return
}
override fun edit(item: ScheduledStatus) {
val intent = ComposeActivity.startIntent(this, ComposeActivity.ComposeOptions(
tootText = item.params.text,
contentWarning = item.params.spoilerText,
@ -143,14 +111,16 @@ class ScheduledTootActivity : BaseActivity(), ScheduledTootAction, Injectable {
sensitive = item.params.sensitive
))
startActivity(intent)
delete(position, item)
}
override fun delete(position: Int, item: ScheduledStatus?) {
if (item == null) {
return
}
override fun delete(item: ScheduledStatus) {
viewModel.deleteScheduledStatus(item)
}
companion object {
@JvmStatic
fun newIntent(context: Context): Intent {
return Intent(context, ScheduledTootActivity::class.java)
}
}
}

View File

@ -27,8 +27,8 @@ import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.entity.ScheduledStatus
interface ScheduledTootAction {
fun edit(position: Int, item: ScheduledStatus?)
fun delete(position: Int, item: ScheduledStatus?)
fun edit(item: ScheduledStatus)
fun delete(item: ScheduledStatus)
}
class ScheduledTootAdapter(
@ -71,11 +71,11 @@ class ScheduledTootAdapter(
text.text = item.params.text
edit.setOnClickListener { v: View ->
v.isEnabled = false
listener.edit(adapterPosition, item)
listener.edit(item)
}
delete.setOnClickListener { v: View ->
v.isEnabled = false
listener.delete(adapterPosition, item)
listener.delete(item)
}
}

View File

@ -4,14 +4,18 @@ import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.paging.Config
import androidx.paging.toLiveData
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.StatusScheduledEvent
import com.keylesspalace.tusky.entity.ScheduledStatus
import com.keylesspalace.tusky.network.MastodonApi
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import javax.inject.Inject
class ScheduledTootViewModel @Inject constructor(
val mastodonApi: MastodonApi
val mastodonApi: MastodonApi,
val eventHub: EventHub
): ViewModel() {
private val disposables = CompositeDisposable()
@ -24,6 +28,17 @@ class ScheduledTootViewModel @Inject constructor(
val networkState = dataSourceFactory.networkState
init {
eventHub.events
.observeOn(AndroidSchedulers.mainThread())
.subscribe { event ->
if (event is StatusScheduledEvent) {
reload()
}
}
.addTo(disposables)
}
fun reload() {
dataSourceFactory.reload()
}