-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Fetch settings and divide events view model #2104
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no guarantee that settings have been fetched till the next statement is executed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then onErrorResumeNext will execute and settings will fetch from api.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still there is no guarantee that settings have been fetched till the next statement is executed, as getSettings is asynchronous
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand which next statement. During fetching/getting settings progress dialog is displaying and if settings fetch successfully or there is an error(=15), value for mutableOrderExpiryTime is set and only then countdown timer will start.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The statement which is immediately proceeding getSettings