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 package dev.octoshrimpy.quik.repository
import dev.octoshrimpy.quik.model.ScheduledMessage 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.Realm
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmResults import io.realm.RealmResults
@@ -26,6 +12,8 @@ import javax.inject.Inject
class ScheduledMessageRepositoryImpl @Inject constructor() : ScheduledMessageRepository { class ScheduledMessageRepositoryImpl @Inject constructor() : ScheduledMessageRepository {
private val disposables = CompositeDisposable()
override fun saveScheduledMessage( override fun saveScheduledMessage(
date: Long, date: Long,
subId: Int, subId: Int,
@@ -62,15 +50,27 @@ class ScheduledMessageRepositoryImpl @Inject constructor() : ScheduledMessageRep
} }
override fun deleteScheduledMessage(id: Long) { override fun deleteScheduledMessage(id: Long) {
Realm.getDefaultInstance() val subscription = Completable.fromAction {
.apply { refresh() } Realm.getDefaultInstance().use { realm ->
.use { realm ->
val message = realm.where(ScheduledMessage::class.java) val message = realm.where(ScheduledMessage::class.java)
.equalTo("id", id) .equalTo("id", id)
.findFirst() .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 package dev.octoshrimpy.quik.feature.scheduled
import android.content.Context import android.content.Context
@@ -29,8 +11,11 @@ import dev.octoshrimpy.quik.manager.BillingManager
import dev.octoshrimpy.quik.repository.ScheduledMessageRepository import dev.octoshrimpy.quik.repository.ScheduledMessageRepository
import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable import com.uber.autodispose.autoDisposable
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom import io.reactivex.rxkotlin.withLatestFrom
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject import javax.inject.Inject
class ScheduledViewModel @Inject constructor( class ScheduledViewModel @Inject constructor(
@@ -63,7 +48,20 @@ class ScheduledViewModel @Inject constructor(
ClipboardUtils.copy(context, message.body) ClipboardUtils.copy(context, message.body)
context.makeToast(R.string.toast_copied) context.makeToast(R.string.toast_copied)
} }
2 -> scheduledMessageRepo.deleteScheduledMessage(messageId) 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 Unit
} }
@@ -78,5 +76,4 @@ class ScheduledViewModel @Inject constructor(
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { navigator.showQksmsPlusActivity("schedule_fab") } .subscribe { navigator.showQksmsPlusActivity("schedule_fab") }
} }
} }