Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Oct 14, 2019
1 parent 74b8f53 commit fb50539
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ class AccountsManager constructor(val context: Context) {
syncStatusObserver, ProcessLifecycleOwner.get(), false
)
services.accountManager.register(accountObserver)
if (services.accountManager.authenticatedAccount() != null) {
accountStatus = if (services.accountManager.authenticatedAccount() != null) {
if (services.accountManager.accountNeedsReauth()) {
accountStatus = AccountStatus.NEEDS_RECONNECT;
AccountStatus.NEEDS_RECONNECT

} else {
accountStatus = AccountStatus.SIGNED_IN
AccountStatus.SIGNED_IN
}

} else {
accountStatus = AccountStatus.SIGNED_OUT
AccountStatus.SIGNED_OUT
}
}

Expand Down Expand Up @@ -258,4 +258,8 @@ class AccountsManager constructor(val context: Context) {
return syncStorage.getStatus()[engine]?: false
}

fun isSignedIn(): Boolean {
return (accountStatus == AccountStatus.SIGNED_IN)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.ui.views.HoneycombButton;

import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -93,4 +94,21 @@ public static void setLeftMargin(@NonNull View view, @NonNull @Dimension float m
}
}

@BindingAdapter("lastSync")
public static void setFxALastSync(@NonNull TextView view, long lastSync) {
if (lastSync == 0) {
view.setText(view.getContext().getString(R.string.fxa_account_last_no_synced));

} else {
long timeDiff = System.currentTimeMillis() - lastSync;
if (timeDiff < 60000) {
view.setText(view.getContext().getString(R.string.fxa_account_last_synced_now));

} else {
view.setText(view.getContext().getString(R.string.fxa_account_last_synced, timeDiff / 60000));
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
public interface BookmarksCallback {
default void onClearBookmarks(@NonNull View view) {}
default void onSyncBookmarks(@NonNull View view) {}
default void onFxALogin(@NonNull View view) {}
default void onShowContextMenu(@NonNull View view, Bookmark item, boolean isLastVisibleItem) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
public interface HistoryCallback {
default void onClearHistory(@NonNull View view) {}
default void onSyncHistory(@NonNull View view) {}
default void onFxALogin(@NonNull View view) {}
default void onShowContextMenu(@NonNull View view, @NonNull VisitInfo item, boolean isLastVisibleItem) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public class BookmarksView extends FrameLayout implements BookmarksStore.Bookmar
private AccountsManager mAccountManager;
private BookmarkAdapter mBookmarkAdapter;
private boolean mIgnoreNextListener;
private boolean mIsSyncEnabled;
private boolean mIsSignedIn;
private ArrayList<BookmarksCallback> mBookmarksViewListeners;
private CustomLinearLayoutManager mLayoutManager;

Expand Down Expand Up @@ -107,13 +105,12 @@ private void initialize(Context aContext) {
mAccountManager.addAccountListener(mAccountListener);
mAccountManager.addSyncListener(mSyncListener);

mIsSyncEnabled = mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE);
mBinding.setIsSignedIn(mAccountManager.isSignedIn());
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));

updateBookmarks();
SessionStore.get().getBookmarkStore().addListener(this);

updateCurrentAccountState();

setVisibility(GONE);

setOnTouchListener((v, event) -> {
Expand Down Expand Up @@ -187,26 +184,17 @@ public void onClearBookmarks(@NonNull View view) {

@Override
public void onSyncBookmarks(@NonNull View view) {
switch(mAccountManager.getAccountStatus()) {
case NEEDS_RECONNECT:
case SIGNED_OUT:
mAccountManager.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccountManager.setLoginOrigin(AccountsManager.LoginOrigin.BOOKMARKS);
SessionStore.get().getActiveStore().loadUri(url);
}
});
break;

case SIGNED_IN:
mAccountManager.syncNowAsync(SyncReason.User.INSTANCE, false);

mBookmarksViewListeners.forEach((listener) -> listener.onSyncBookmarks(view));
break;

default:
throw new IllegalStateException("Unexpected value: " + mAccountManager.getAccountStatus());
}
mAccountManager.syncNowAsync(SyncReason.User.INSTANCE, false);
}

@Override
public void onFxALogin(@NonNull View view) {
mAccountManager.getAuthenticationUrlAsync().thenAcceptAsync((url) -> {
if (url != null) {
mAccountManager.setLoginOrigin(AccountsManager.LoginOrigin.HISTORY);
SessionStore.get().getActiveStore().loadUri(url);
}
});
}

@Override
Expand All @@ -228,60 +216,37 @@ public void removeBookmarksListener(@NonNull BookmarksCallback listener) {
private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
@Override
public void onStarted() {
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));
mBinding.executePendingBindings();
if (mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
mBinding.syncButton.setEnabled(false);
mBinding.syncButton.setHovered(false);
mSyncingAnimation.setDuration(500);
mSyncingAnimation.start();
}
}

@Override
public void onIdle() {
mIsSyncEnabled = mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE);
if (mIsSyncEnabled) {
mBinding.syncButton.setEnabled(true);
mBinding.syncButton.setHovered(false);
if (mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
mSyncingAnimation.cancel();
mBinding.setLastSync(mAccountManager.getLastSync());
}
updateUi();
}

@Override
public void onError(@Nullable Exception e) {
mBinding.setIsSyncEnabled(mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE));
mBinding.executePendingBindings();
if (mAccountManager.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE)) {
mBinding.syncButton.setEnabled(true);
mBinding.syncButton.setHovered(false);
mSyncingAnimation.cancel();
mBinding.syncDescription.setText(getContext().getString(R.string.fxa_account_last_no_synced));
}
}
};

private void updateCurrentAccountState() {
switch(mAccountManager.getAccountStatus()) {
case NEEDS_RECONNECT:
case SIGNED_OUT:
mIsSignedIn = false;
updateUi();
break;

case SIGNED_IN:
mIsSignedIn = true;
updateUi();
break;

default:
throw new IllegalStateException("Unexpected value: " + mAccountManager.getAccountStatus());
}
}

private AccountObserver mAccountListener = new AccountObserver() {

@Override
public void onAuthenticated(@NotNull OAuthAccount oAuthAccount, @NotNull AuthType authType) {
mIsSignedIn = true;
updateUi();
mBinding.setIsSignedIn(true);
}

@Override
Expand All @@ -290,51 +255,15 @@ public void onProfileUpdated(@NotNull Profile profile) {

@Override
public void onLoggedOut() {
mIsSignedIn = false;
updateBookmarks();
updateUi();
mBinding.setIsSignedIn(false);
}

@Override
public void onAuthenticationProblems() {
mIsSignedIn = false;
updateUi();
mBinding.setIsSignedIn(false);
}
};

private void updateUi() {
if (mIsSignedIn) {
mBinding.syncButton.setText(R.string.bookmarks_sync);
mBinding.syncDescription.setVisibility(VISIBLE);

if (mIsSyncEnabled) {
mBinding.syncButton.setEnabled(true);
long lastSync = mAccountManager.getLastSync();
if (lastSync == 0) {
mBinding.syncDescription.setText(getContext().getString(R.string.fxa_account_last_no_synced));

} else {
long timeDiff = System.currentTimeMillis() - lastSync;
if (timeDiff < 60000) {
mBinding.syncDescription.setText(getContext().getString(R.string.fxa_account_last_synced_now));

} else {
mBinding.syncDescription.setText(getContext().getString(R.string.fxa_account_last_synced, timeDiff / 60000));
}
}

} else {
mBinding.syncButton.setEnabled(false);
mBinding.syncDescription.setVisibility(GONE);
}

} else {
mBinding.syncButton.setEnabled(true);
mBinding.syncButton.setText(R.string.fxa_account_sing_to_sync);
mBinding.syncDescription.setVisibility(GONE);
}
}

private void updateBookmarks() {
SessionStore.get().getBookmarkStore().getTree(BookmarkRoot.Root.getId(), true).thenAcceptAsync(this::showBookmarks, new UIThreadExecutor());
}
Expand Down
Loading

0 comments on commit fb50539

Please sign in to comment.