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

Commit

Permalink
Sync updates (#2183)
Browse files Browse the repository at this point in the history
* Update Sync UI

* Do not add accounts listeners if Account UI is disabled

* Do not debounce for explicit user sync requests

* Fit empty history items in narrow mode
  • Loading branch information
keianhzo authored and bluemarvin committed Nov 7, 2019
1 parent cb169a2 commit ebc151c
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Accounts constructor(val context: Context) {
// Enable syncing after signing in
syncStorage.setStatus(SyncEngine.Bookmarks, SettingsStore.getInstance(context).isBookmarksSyncEnabled)
syncStorage.setStatus(SyncEngine.History, SettingsStore.getInstance(context).isHistorySyncEnabled)
services.accountManager.syncNowAsync(SyncReason.EngineChange, false)
services.accountManager.syncNowAsync(SyncReason.EngineChange, true)

// Update device list
account.deviceConstellation().registerDeviceObserver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.text.style.ImageSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DimenRes;
Expand All @@ -18,6 +19,7 @@
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.ui.views.HoneycombButton;
import org.mozilla.vrbrowser.ui.views.UIButton;
import org.mozilla.vrbrowser.ui.views.settings.ButtonSetting;
import org.mozilla.vrbrowser.ui.views.settings.SwitchSetting;

import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -122,4 +124,34 @@ public static void setLayoutWidth(@NonNull UIButton button, @NonNull @Dimension
public static void setEnabled(@NonNull SwitchSetting button, boolean enabled) {
button.setEnabled(enabled);
}

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

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

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

@BindingAdapter("android:layout_height")
public static void setLayoutHeight(@NonNull ImageView view, @NonNull @Dimension float dimen) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = (int)dimen;
view.setLayoutParams(params);
}

@BindingAdapter("android:layout_width")
public static void setLayoutWidth(@NonNull ImageView view, @NonNull @Dimension float dimen) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = (int)dimen;
view.setLayoutParams(params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class BookmarksView extends FrameLayout implements BookmarksStore.Bookmar

private static final String LOGTAG = SystemUtils.createLogtag(BookmarksView.class);

private static final boolean ACCOUNTS_UI_ENABLED = false;

private BookmarksBinding mBinding;
private Accounts mAccounts;
private BookmarkAdapter mBookmarkAdapter;
Expand Down Expand Up @@ -105,8 +107,10 @@ private void initialize(Context aContext) {
mBinding.setIsLoading(true);

mAccounts = ((VRBrowserApplication)getContext().getApplicationContext()).getAccounts();
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);
if (ACCOUNTS_UI_ENABLED) {
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);
}

mBinding.setIsSignedIn(mAccounts.isSignedIn());
boolean isSyncEnabled = mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE);
Expand All @@ -116,6 +120,7 @@ private void initialize(Context aContext) {
mBinding.setIsSyncing(mAccounts.isSyncing());
}
mBinding.setIsNarrow(false);
mBinding.setIsAccountsUIEnabled(ACCOUNTS_UI_ENABLED);
mBinding.executePendingBindings();

updateBookmarks();
Expand All @@ -135,8 +140,11 @@ public void onShow() {

public void onDestroy() {
SessionStore.get().getBookmarkStore().removeListener(this);
mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);

if (ACCOUNTS_UI_ENABLED) {
mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);
}
}

private final BookmarkItemCallback mBookmarkItemCallback = new BookmarkItemCallback() {
Expand Down Expand Up @@ -249,7 +257,7 @@ public void onIdle() {
updateSyncBindings(false);

// This shouldn't be necessary but for some reason the buttons stays hovered after the sync.
// I guess Android is after enabling it it's state is restored to the latest one (hovered)
// I guess Android restoring it to the latest state (hovered) before being disabled
// Probably an Android bindings bug.
mBinding.bookmarksNarrow.syncButton.setHovered(false);
mBinding.bookmarksWide.syncButton.setHovered(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class HistoryView extends FrameLayout implements HistoryStore.HistoryList

private static final String LOGTAG = SystemUtils.createLogtag(HistoryView.class);

private static final boolean ACCOUNTS_UI_ENABLED = false;

private HistoryBinding mBinding;
private Accounts mAccounts;
private HistoryAdapter mHistoryAdapter;
Expand Down Expand Up @@ -108,8 +110,10 @@ private void initialize(Context aContext) {
mBinding.setIsLoading(true);

mAccounts = ((VRBrowserApplication)getContext().getApplicationContext()).getAccounts();
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);
if (ACCOUNTS_UI_ENABLED) {
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);
}

mBinding.setIsSignedIn(mAccounts.isSignedIn());
boolean isSyncEnabled = mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE);
Expand All @@ -119,6 +123,7 @@ private void initialize(Context aContext) {
mBinding.setIsSyncing(mAccounts.isSyncing());
}
mBinding.setIsNarrow(false);
mBinding.setIsAccountsUIEnabled(ACCOUNTS_UI_ENABLED);
mBinding.executePendingBindings();

updateHistory();
Expand All @@ -134,8 +139,11 @@ private void initialize(Context aContext) {

public void onDestroy() {
SessionStore.get().getHistoryStore().removeListener(this);
mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);

if (ACCOUNTS_UI_ENABLED) {
mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);
}
}

public void onShow() {
Expand Down Expand Up @@ -246,7 +254,7 @@ public void onIdle() {
updateSyncBindings(false);

// This shouldn't be necessary but for some reason the buttons stays hovered after the sync.
// I guess Android is after enabling it it's state is restored to the latest one (hovered)
// I guess Android restoring it to the latest state (hovered) before being disabled
// Probably an Android bindings bug.
mBinding.historyNarrow.syncButton.setHovered(false);
mBinding.historyWide.syncButton.setHovered(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@ public void setFooterButtonVisibility(int visibility) {
mButton.setVisibility(visibility);
}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);

mButton.setEnabled(enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.databinding.DataBindingUtil;

Expand Down Expand Up @@ -40,6 +41,8 @@ class FxAAccountOptionsView extends SettingsView {
private OptionsFxaAccountBinding mBinding;
private Accounts mAccounts;
private Executor mUIThreadExecutor;
private boolean mInitialBookmarksState;
private boolean mInitialHistoryState;

public FxAAccountOptionsView(Context aContext, WidgetManagerDelegate aWidgetManager) {
super(aContext, aWidgetManager);
Expand All @@ -62,8 +65,12 @@ private void initialize(Context aContext) {
mUIThreadExecutor = ((VRBrowserApplication)getContext().getApplicationContext()).getExecutors().mainThread();

mBinding.signButton.setOnClickListener(view -> mAccounts.logoutAsync());
mBinding.bookmarksSyncSwitch.setOnCheckedChangeListener(mBookmarksSyncListener);
mBinding.syncButton.setOnClickListener(this::sync);

mBinding.setIsSyncing(mAccounts.isSyncing());
mBinding.setLastSync(mAccounts.lastSync());

mBinding.bookmarksSyncSwitch.setOnCheckedChangeListener(mBookmarksSyncListener);
mBinding.historySyncSwitch.setOnCheckedChangeListener(mHistorySyncListener);

updateCurrentAccountState();
Expand All @@ -79,10 +86,13 @@ public void onShown() {
mAccounts.addAccountListener(mAccountListener);
mAccounts.addSyncListener(mSyncListener);

mBinding.bookmarksSyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false);
mBinding.historySyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE), false);
mInitialBookmarksState = mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE);
mInitialHistoryState = mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE);

mBinding.setIsSyncing(mAccounts.isSyncing());
mBinding.bookmarksSyncSwitch.setValue(mInitialBookmarksState, false);
mBinding.historySyncSwitch.setValue(mInitialHistoryState, false);

updateSyncBindings(mAccounts.isSyncing());
}

@Override
Expand All @@ -91,50 +101,60 @@ public void onHidden() {

mAccounts.removeAccountListener(mAccountListener);
mAccounts.removeSyncListener(mSyncListener);

// We only sync engines in case the user has changed the sync engines since previous sync
if (mBinding.bookmarksSyncSwitch.isChecked() != mInitialBookmarksState ||
mBinding.historySyncSwitch.isChecked() != mInitialHistoryState) {
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
}
}

private SwitchSetting.OnCheckedChangeListener mBookmarksSyncListener = (compoundButton, value, apply) -> {
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, value);
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
};

private SwitchSetting.OnCheckedChangeListener mHistorySyncListener = (compoundButton, value, apply) -> {
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, value);
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
};

private void resetOptions() {
mAccounts.setSyncStatus(SyncEngine.Bookmarks.INSTANCE, SettingsStore.BOOKMARKS_SYNC_DEFAULT);
mAccounts.setSyncStatus(SyncEngine.History.INSTANCE, SettingsStore.HISTORY_SYNC_DEFAULT);
mAccounts.syncNowAsync(SyncReason.EngineChange.INSTANCE, false);
mBinding.bookmarksSyncSwitch.setValue(SettingsStore.BOOKMARKS_SYNC_DEFAULT, true);
mBinding.historySyncSwitch.setValue(SettingsStore.HISTORY_SYNC_DEFAULT, true);
}

private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
@Override
public void onStarted() {
mBinding.setIsSyncing(true);
updateSyncBindings(true);
}

@Override
public void onIdle() {
mBinding.setIsSyncing(false);
updateSyncBindings(false);

mBinding.bookmarksSyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.Bookmarks.INSTANCE), false);
mBinding.historySyncSwitch.setValue(mAccounts.isEngineEnabled(SyncEngine.History.INSTANCE), false);

// This shouldn't be necessary but for some reason the buttons stays hovered after the sync.
// I guess Android is after enabling it it's state is restored to the latest one (hovered)
// I guess Android restoring it to the latest state (hovered) before being disabled
// Probably an Android bindings bug.
mBinding.bookmarksSyncSwitch.setHovered(false);
mBinding.historySyncSwitch.setHovered(false);
mBinding.syncButton.setHovered(false);
}

@Override
public void onError(@Nullable Exception e) {
mBinding.setIsSyncing(false);
updateSyncBindings(false);
}
};

private void updateSyncBindings(boolean isSyncing) {
mBinding.setIsSyncing(isSyncing);
mBinding.setLastSync(mAccounts.lastSync());
mBinding.executePendingBindings();
}

void updateCurrentAccountState() {
switch(mAccounts.getAccountStatus()) {
case NEEDS_RECONNECT:
Expand Down Expand Up @@ -173,6 +193,10 @@ private void updateProfile(Profile profile) {
}
}

private void sync(View view) {
mAccounts.syncNowAsync(SyncReason.User.INSTANCE, false);
}

private AccountObserver mAccountListener = new AccountObserver() {

@Override
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/main_button_text_color.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@integer/ui_fadeAnimTime"
android:exitFadeDuration="@integer/ui_fadeAnimTime">
<item android:state_enabled="false"
android:color="@color/rhino"/>
<item android:state_pressed="true"
android:color="@color/fog"/>
<item android:state_hovered="true"
android:color="@color/asphalt"/>
<item android:color="@color/fog"/>
</selector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/rectangle_button_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="@color/asphalt" />
<solid android:color="@color/iron" />
</shape>
</item>
<item android:state_pressed="true">
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/layout/bookmarks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<data>
<import type="android.view.View"/>

<variable
name="isAccountsUIEnabled"
type="boolean" />

<variable
name="isLoading"
type="boolean" />
Expand Down Expand Up @@ -54,6 +58,7 @@
android:layout_marginStart="30dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="20dp"
app:isAccountsUIEnabled="@{isAccountsUIEnabled}"
app:isEmpty="@{isEmpty}"
app:isSignedIn="@{isSignedIn}"
app:isSyncEnabled="@{isSyncEnabled}"
Expand All @@ -69,6 +74,7 @@
android:layout_marginStart="30dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="20dp"
app:isAccountsUIEnabled="@{isAccountsUIEnabled}"
app:isEmpty="@{isEmpty}"
app:isSignedIn="@{isSignedIn}"
app:isSyncEnabled="@{isSyncEnabled}"
Expand All @@ -85,8 +91,8 @@
app:visibleGone="@{!isLoading &amp;&amp; isEmpty}">

<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_width="@{isNarrow ? @dimen/library_icon_size_narrow : @dimen/library_icon_size_wide, default=wrap_content}"
android:layout_height="@{isNarrow ? @dimen/library_icon_size_narrow : @dimen/library_icon_size_wide, default=wrap_content}"
android:src="@drawable/ic_icon_bookmarks"
app:srcCompat="@drawable/ic_icon_bookmarks" />

Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/layout/bookmarks_narrow.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
<variable
name="isAccountsUIEnabled"
type="boolean" />

<variable
name="isEmpty"
type="boolean" />
Expand Down Expand Up @@ -71,7 +75,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
android:gravity="center_vertical"
visibleGone="@{isAccountsUIEnabled}">

<FrameLayout
android:id="@+id/buttons_layout"
Expand Down
Loading

0 comments on commit ebc151c

Please sign in to comment.