Skip to content

Commit

Permalink
Immediately put the logged in user into cache
Browse files Browse the repository at this point in the history
The user resource is returned in the successful login reply from the
server. We now immediately put it into cache s.t. it is always around
and does not need any further network accesses to fetch it.

This fixes an issue where on a fresh install the map was not centered
on the logged in users' home address in case a current location is not
known from any location provider. This was due to the user resource of
the logged in account not being around when the map was first centered.
  • Loading branch information
saemy committed Aug 28, 2018
1 parent 5e6bd1e commit f55ec95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
20 changes: 18 additions & 2 deletions app/src/main/java/fi/bitrite/android/ws/auth/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import fi.bitrite.android.ws.api.WarmshowersWebservice;
import fi.bitrite.android.ws.api.response.LoginResponse;
import fi.bitrite.android.ws.di.AppScope;
import fi.bitrite.android.ws.repository.UserRepository;
import fi.bitrite.android.ws.ui.MainActivity;
import fi.bitrite.android.ws.util.MaybeNull;
import io.reactivex.Maybe;
Expand All @@ -44,8 +45,9 @@ public class AccountManager {

private final WarmshowersWebservice mGeneralWebservice;
private final android.accounts.AccountManager mAndroidAccountManager;
private final ReentrantReadWriteLock mLock = new ReentrantReadWriteLock();
private final UserRepository.AppUserRepository mAppUserRepository;

private final ReentrantReadWriteLock mLock = new ReentrantReadWriteLock();
private final BehaviorSubject<Account[]> mAccounts = BehaviorSubject.create();
private final BehaviorSubject<MaybeNull<Account>> mCurrentAccount = BehaviorSubject.create();
private final Observable<Integer> mCurrentUserId;
Expand All @@ -55,9 +57,11 @@ public class AccountManager {

@Inject
AccountManager(WarmshowersWebservice generalWebservice,
android.accounts.AccountManager androidAccountManager) {
android.accounts.AccountManager androidAccountManager,
UserRepository.AppUserRepository appUserRepository) {
mGeneralWebservice = generalWebservice;
mAndroidAccountManager = androidAccountManager;
mAppUserRepository = appUserRepository;

mAndroidAccountManager.addOnAccountsUpdatedListener(
accounts_unused -> handleAccountUpdate(), null, false);
Expand Down Expand Up @@ -304,6 +308,18 @@ private void dismissEventuallyCreateOrAuth() {
public Observable<LoginResult> login(String username, String password) {
return mGeneralWebservice.login(username, password)
.subscribeOn(Schedulers.io())
.flatMap(response -> {
if (response.isSuccessful()) {
// Stores the account in the repository. With this it is already available
// without the need of any further network accesses.
return mAppUserRepository.save(response.body().user.toUser())
.toSingle(() -> response)
.toObservable();

} else {
return Observable.just(response);
}
})
.map(response -> {
if (!response.isSuccessful()) {
return new LoginResult(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Observable<Resource<User>> get(int userId, Repository.ShouldSaveInDb shou
}

public Completable save(@NonNull User user) {
return getAppUserRepository().saveRx(user.id, user);
return getAppUserRepository().save(user);
}

public Observable<List<Integer>> searchByKeyword(String keyword) {
Expand Down Expand Up @@ -81,7 +81,7 @@ private AppUserRepository getAppUserRepository() {
* users.
*/
@AppScope
static class AppUserRepository extends Repository<User> {
public static class AppUserRepository extends Repository<User> {
@Inject UserDao mUserDao;
WarmshowersAccountWebservice mLastWebservice;

Expand All @@ -98,6 +98,10 @@ List<Observable<Resource<User>>> get(@NonNull Collection<Integer> userIds,
return users;
}

public Completable save(@NonNull User user) {
return saveRx(user.id, user);
}

@Override
void saveInDb(int id, @NonNull User user) {
mUserDao.save(user);
Expand Down

0 comments on commit f55ec95

Please sign in to comment.