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

55 lines
2.0 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-30 20:40:27 +01:00
package com.keylesspalace.tusky.components.scheduled
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
2019-12-30 20:48:01 +01:00
import com.keylesspalace.tusky.appstore.EventHub
2019-12-30 20:40:27 +01:00
import com.keylesspalace.tusky.entity.ScheduledStatus
import com.keylesspalace.tusky.network.MastodonApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx3.await
2019-12-30 20:40:27 +01:00
import javax.inject.Inject
class ScheduledStatusViewModel @Inject constructor(
val mastodonApi: MastodonApi,
val eventHub: EventHub
) : ViewModel() {
2019-12-30 20:40:27 +01:00
private val pagingSourceFactory = ScheduledStatusPagingSourceFactory(mastodonApi)
2019-12-30 20:40:27 +01:00
val data = Pager(
config = PagingConfig(pageSize = 20, initialLoadSize = 20),
pagingSourceFactory = pagingSourceFactory
).flow
.cachedIn(viewModelScope)
2019-12-30 20:40:27 +01:00
fun deleteScheduledStatus(status: ScheduledStatus) {
viewModelScope.launch {
try {
mastodonApi.deleteScheduledStatus(status.id).await()
pagingSourceFactory.remove(status)
} catch (throwable: Throwable) {
Log.w("ScheduledTootViewModel", "Error deleting scheduled status", throwable)
}
}
2019-12-30 20:40:27 +01:00
}
}