-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
1a3ecec
commit 67cb46f
Showing
14 changed files
with
2,240 additions
and
116 deletions.
There are no files selected for viewing
1,973 changes: 1,973 additions & 0 deletions
1,973
app/schemas/org.fossasia.openevent.general.OpenEventDatabase/8.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
app/src/main/java/org/fossasia/openevent/general/StartupViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.fossasia.openevent.general | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import io.reactivex.disposables.CompositeDisposable | ||
import io.reactivex.rxkotlin.plusAssign | ||
import org.fossasia.openevent.general.auth.AuthHolder | ||
import org.fossasia.openevent.general.auth.AuthService | ||
import org.fossasia.openevent.general.auth.RequestPasswordReset | ||
import org.fossasia.openevent.general.auth.forgot.PasswordReset | ||
import org.fossasia.openevent.general.common.SingleLiveEvent | ||
import org.fossasia.openevent.general.data.Preference | ||
import org.fossasia.openevent.general.data.Resource | ||
import org.fossasia.openevent.general.event.NEW_NOTIFICATIONS | ||
import org.fossasia.openevent.general.notification.NotificationService | ||
import org.fossasia.openevent.general.settings.SettingsService | ||
import org.fossasia.openevent.general.utils.HttpErrors | ||
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers | ||
import retrofit2.HttpException | ||
import timber.log.Timber | ||
|
||
class StartupViewModel( | ||
private val preference: Preference, | ||
private val resource: Resource, | ||
private val authHolder: AuthHolder, | ||
private val authService: AuthService, | ||
private val notificationService: NotificationService, | ||
private val settingsService: SettingsService | ||
) : ViewModel() { | ||
private val compositeDisposable = CompositeDisposable() | ||
val mutableNewNotifications = MutableLiveData<Boolean>() | ||
val newNotifications: LiveData<Boolean> = mutableNewNotifications | ||
private val mutableDialogProgress = MutableLiveData<Boolean>() | ||
val dialogProgress: LiveData<Boolean> = mutableDialogProgress | ||
private val mutableIsRefresh = MutableLiveData<Boolean>() | ||
val isRefresh: LiveData<Boolean> = mutableIsRefresh | ||
private val mutableResetPasswordEmail = MutableLiveData<String>() | ||
val resetPasswordEmail: LiveData<String> = mutableResetPasswordEmail | ||
private val mutableMessage = SingleLiveEvent<String>() | ||
val message: LiveData<String> = mutableMessage | ||
|
||
fun isLoggedIn() = authHolder.isLoggedIn() | ||
|
||
fun getId() = authHolder.getId() | ||
|
||
fun syncNotifications() { | ||
if (!isLoggedIn()) | ||
return | ||
compositeDisposable += notificationService.syncNotifications(getId()) | ||
.withDefaultSchedulers() | ||
.subscribe({ list -> | ||
list?.forEach { | ||
if (!it.isRead) { | ||
preference.putBoolean(NEW_NOTIFICATIONS, true) | ||
mutableNewNotifications.value = true | ||
} | ||
} | ||
}, { | ||
if (it is HttpException) { | ||
if (authHolder.isLoggedIn() && it.code() == HttpErrors.UNAUTHORIZED) { | ||
logoutAndRefresh() | ||
} | ||
} | ||
Timber.e(it, "Error fetching notifications") | ||
}) | ||
} | ||
|
||
private fun logoutAndRefresh() { | ||
compositeDisposable += authService.logout() | ||
.withDefaultSchedulers() | ||
.subscribe({ | ||
mutableIsRefresh.value = true | ||
}, { | ||
Timber.e(it, "Error while logout") | ||
mutableMessage.value = resource.getString(R.string.error) | ||
}) | ||
} | ||
|
||
fun checkAndReset(token: String, newPassword: String) { | ||
val resetRequest = RequestPasswordReset(PasswordReset(token, newPassword)) | ||
if (authHolder.isLoggedIn()) { | ||
compositeDisposable += authService.logout() | ||
.withDefaultSchedulers() | ||
.doOnSubscribe { | ||
mutableDialogProgress.value = true | ||
}.subscribe { | ||
resetPassword(resetRequest) | ||
} | ||
} else | ||
resetPassword(resetRequest) | ||
} | ||
|
||
private fun resetPassword(resetRequest: RequestPasswordReset) { | ||
compositeDisposable += authService.resetPassword(resetRequest) | ||
.withDefaultSchedulers() | ||
.doOnSubscribe { | ||
mutableDialogProgress.value = true | ||
}.doFinally { | ||
mutableDialogProgress.value = false | ||
}.subscribe({ | ||
Timber.e(it.toString()) | ||
mutableMessage.value = resource.getString(R.string.reset_password_message) | ||
mutableResetPasswordEmail.value = it.email | ||
}, { | ||
Timber.e(it, "Failed to reset password") | ||
}) | ||
} | ||
|
||
fun fetchSettings() { | ||
compositeDisposable += settingsService.fetchSettings() | ||
.withDefaultSchedulers() | ||
.subscribe({ | ||
Timber.d("Settings fetched successfully") | ||
}, { | ||
Timber.e(it, "Error in fetching settings form API") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.