This commit is contained in:
octoshrimpy
2023-11-25 14:28:36 -07:00
parent 46d3a46b69
commit 5bda0eebb1
2 changed files with 71 additions and 74 deletions
@@ -1,24 +1,10 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS 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.
*
* QKSMS 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 QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package dev.octoshrimpy.quik.repository
import dev.octoshrimpy.quik.model.ScheduledMessage
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import io.realm.Realm
import io.realm.RealmList
import io.realm.RealmResults
@@ -26,6 +12,8 @@ import javax.inject.Inject
class ScheduledMessageRepositoryImpl @Inject constructor() : ScheduledMessageRepository {
private val disposables = CompositeDisposable()
override fun saveScheduledMessage(
date: Long,
subId: Int,
@@ -40,7 +28,7 @@ class ScheduledMessageRepositoryImpl @Inject constructor() : ScheduledMessageRep
val attachmentsRealmList = RealmList(*attachments.toTypedArray())
val message = ScheduledMessage(id, date, subId, recipientsRealmList, sendAsGroup, body,
attachmentsRealmList)
attachmentsRealmList)
realm.executeTransaction { realm.insertOrUpdate(message) }
}
@@ -48,29 +36,41 @@ class ScheduledMessageRepositoryImpl @Inject constructor() : ScheduledMessageRep
override fun getScheduledMessages(): RealmResults<ScheduledMessage> {
return Realm.getDefaultInstance()
.where(ScheduledMessage::class.java)
.sort("date")
.findAll()
.where(ScheduledMessage::class.java)
.sort("date")
.findAll()
}
override fun getScheduledMessage(id: Long): ScheduledMessage? {
return Realm.getDefaultInstance()
.apply { refresh() }
.where(ScheduledMessage::class.java)
.equalTo("id", id)
.findFirst()
.apply { refresh() }
.where(ScheduledMessage::class.java)
.equalTo("id", id)
.findFirst()
}
override fun deleteScheduledMessage(id: Long) {
Realm.getDefaultInstance()
.apply { refresh() }
.use { realm ->
val message = realm.where(ScheduledMessage::class.java)
.equalTo("id", id)
.findFirst()
val subscription = Completable.fromAction {
Realm.getDefaultInstance().use { realm ->
val message = realm.where(ScheduledMessage::class.java)
.equalTo("id", id)
.findFirst()
realm.executeTransaction { message?.deleteFromRealm() }
}
realm.executeTransaction { message?.deleteFromRealm() }
}
}.subscribeOn(Schedulers.io()) // Perform the operation in a background thread
.observeOn(AndroidSchedulers.mainThread()) // Switch back to the main thread if needed
.subscribe({
// Handle completion, e.g., log success or update UI
}, {
// Handle error, e.g., log or show error message
})
disposables.add(subscription)
}
}
// Ensure to clear disposables when the repository is no longer needed
fun clearDisposables() {
disposables.clear()
}
}
@@ -1,21 +1,3 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS 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.
*
* QKSMS 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 QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package dev.octoshrimpy.quik.feature.scheduled
import android.content.Context
@@ -29,8 +11,11 @@ import dev.octoshrimpy.quik.manager.BillingManager
import dev.octoshrimpy.quik.repository.ScheduledMessageRepository
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
class ScheduledViewModel @Inject constructor(
@@ -40,43 +25,55 @@ class ScheduledViewModel @Inject constructor(
private val scheduledMessageRepo: ScheduledMessageRepository,
private val sendScheduledMessage: SendScheduledMessage
) : QkViewModel<ScheduledView, ScheduledState>(ScheduledState(
scheduledMessages = scheduledMessageRepo.getScheduledMessages()
scheduledMessages = scheduledMessageRepo.getScheduledMessages()
)) {
init {
disposables += billingManager.upgradeStatus
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
}
override fun bindView(view: ScheduledView) {
super.bindView(view)
view.messageClickIntent
.autoDisposable(view.scope())
.subscribe { view.showMessageOptions() }
.autoDisposable(view.scope())
.subscribe { view.showMessageOptions() }
view.messageMenuIntent
.withLatestFrom(view.messageClickIntent) { itemId, messageId ->
when (itemId) {
0 -> sendScheduledMessage.execute(messageId)
1 -> scheduledMessageRepo.getScheduledMessage(messageId)?.let { message ->
ClipboardUtils.copy(context, message.body)
context.makeToast(R.string.toast_copied)
}
2 -> scheduledMessageRepo.deleteScheduledMessage(messageId)
.withLatestFrom(view.messageClickIntent) { itemId, messageId ->
when (itemId) {
0 -> sendScheduledMessage.execute(messageId)
1 -> scheduledMessageRepo.getScheduledMessage(messageId)?.let { message ->
ClipboardUtils.copy(context, message.body)
context.makeToast(R.string.toast_copied)
}
2 -> {
val deleteSubscription = Observable.fromCallable {
scheduledMessageRepo.deleteScheduledMessage(messageId)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
// Handle completion, e.g., log success or update UI
}, {
// Handle error, e.g., log or show error message
})
disposables += deleteSubscription
}
Unit
}
.autoDisposable(view.scope())
.subscribe()
Unit
}
.autoDisposable(view.scope())
.subscribe()
view.composeIntent
.autoDisposable(view.scope())
.subscribe { navigator.showCompose() }
.autoDisposable(view.scope())
.subscribe { navigator.showCompose() }
view.upgradeIntent
.autoDisposable(view.scope())
.subscribe { navigator.showQksmsPlusActivity("schedule_fab") }
.autoDisposable(view.scope())
.subscribe { navigator.showQksmsPlusActivity("schedule_fab") }
}
}
}