mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-21 21:29:00 +00:00
improve RYA handling
This commit is contained in:
parent
f35768b096
commit
0e30b03b0b
3 changed files with 29 additions and 11 deletions
|
|
@ -48,7 +48,7 @@ interface UserRepository : BaseRepository {
|
|||
fun unlockPath(user: User, customization: Customization): Observable<UnlockResponse>
|
||||
fun unlockPath(user: User, set: CustomizationSet): Observable<UnlockResponse>
|
||||
|
||||
fun runCron(tasks: List<Task>)
|
||||
fun runCron(tasks: MutableList<Task>)
|
||||
fun runCron()
|
||||
|
||||
fun readNotification(id: String): Observable<List<*>>
|
||||
|
|
|
|||
|
|
@ -182,8 +182,7 @@ class UserRepositoryImpl(localRepository: UserLocalRepository, apiClient: ApiCli
|
|||
}
|
||||
|
||||
override fun resetAccount(): Observable<User> {
|
||||
return apiClient.resetAccount()
|
||||
.flatMap { retrieveUser(true, true) }
|
||||
return apiClient.resetAccount().flatMap { retrieveUser(true, true) }
|
||||
}
|
||||
|
||||
override fun deleteAccount(password: String): Observable<Void> =
|
||||
|
|
@ -243,15 +242,21 @@ class UserRepositoryImpl(localRepository: UserLocalRepository, apiClient: ApiCli
|
|||
}
|
||||
}
|
||||
|
||||
override fun runCron(tasks: List<Task>) {
|
||||
val observable: Observable<List<TaskScoringResult?>> = if (tasks.isNotEmpty()) {
|
||||
Observable.from(tasks)
|
||||
.flatMap { task -> taskRepository.taskChecked(null, task, true, true) }
|
||||
.toList()
|
||||
} else {
|
||||
Observable.just(null)
|
||||
override fun runCron(tasks: MutableList<Task>) {
|
||||
var observable: Observable<Any> = localRepository.getUser(userId).first()
|
||||
.filter { it.needsCron }
|
||||
.map { user -> localRepository.executeTransaction {
|
||||
user.needsCron = false
|
||||
user.lastCron = Date()
|
||||
}
|
||||
user
|
||||
}
|
||||
if (tasks.isNotEmpty()) {
|
||||
for (task in tasks) {
|
||||
observable = observable.flatMap { taskRepository.taskChecked(null, task, true, true) }
|
||||
}
|
||||
observable.toList()
|
||||
}
|
||||
localRepository.getUser(userId).first().subscribe(Action1 { user -> localRepository.executeTransaction { user.needsCron = false } }, RxErrorHandler.handleEmptyError())
|
||||
observable.flatMap { apiClient.runCron() }
|
||||
.flatMap { this.retrieveUser(true, true) }
|
||||
.subscribe(Action1 { }, RxErrorHandler.handleEmptyError())
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.habitrpg.android.habitica.data.local.implementation
|
||||
|
||||
import com.habitrpg.android.habitica.data.local.UserLocalRepository
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.models.Skill
|
||||
import com.habitrpg.android.habitica.models.Tag
|
||||
import com.habitrpg.android.habitica.models.TutorialStep
|
||||
|
|
@ -10,6 +11,8 @@ import io.realm.Realm
|
|||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
import rx.Observable
|
||||
import rx.functions.Action0
|
||||
import rx.functions.Action1
|
||||
|
||||
class RealmUserLocalRepository(realm: Realm) : RealmBaseLocalRepository(realm), UserLocalRepository {
|
||||
|
||||
|
|
@ -27,6 +30,16 @@ class RealmUserLocalRepository(realm: Realm) : RealmBaseLocalRepository(realm),
|
|||
}
|
||||
|
||||
override fun saveUser(user: User) {
|
||||
val oldUser = realm.where(User::class.java)
|
||||
.equalTo("id", user.id)
|
||||
.findFirst()
|
||||
if (oldUser != null && oldUser.isValid) {
|
||||
if (user.needsCron && !oldUser.needsCron) {
|
||||
if (user.lastCron.before(oldUser.lastCron)) {
|
||||
user.needsCron = false
|
||||
}
|
||||
}
|
||||
}
|
||||
realm.executeTransaction { realm1 -> realm1.insertOrUpdate(user) }
|
||||
if (user.tags != null) {
|
||||
removeOldTags(user.id, user.tags)
|
||||
|
|
|
|||
Loading…
Reference in a new issue