forked from organicmaps/organicmaps
[android] Added progress view to bookmark auhtorization
This commit is contained in:
parent
ac48fce063
commit
9edf4b4bd7
5 changed files with 129 additions and 61 deletions
|
@ -40,21 +40,34 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:textAppearance="@style/MwmTextAppearance.Body3"
|
||||
tools:text="@string/bookmarks_message_unbackuped_user"/>
|
||||
<Button
|
||||
android:id="@+id/button"
|
||||
style="@style/MwmWidget.Button.Start"
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="@dimen/nav_button_height"
|
||||
android:paddingBottom="@dimen/margin_half_plus"
|
||||
android:paddingTop="@dimen/margin_half_plus"
|
||||
android:paddingStart="@dimen/margin_half"
|
||||
android:paddingLeft="@dimen/margin_half"
|
||||
android:paddingEnd="@dimen/margin_half"
|
||||
android:paddingRight="@dimen/margin_half"
|
||||
android:layout_marginTop="@dimen/margin_base"
|
||||
android:layout_marginBottom="@dimen/margin_half"
|
||||
android:layout_gravity="center"
|
||||
tools:text="@string/authorization_button_sign_in"/>
|
||||
android:layout_gravity="center">
|
||||
<Button
|
||||
android:id="@+id/button"
|
||||
style="@style/MwmWidget.Button.Start"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:minHeight="@dimen/nav_button_height"
|
||||
android:layout_marginTop="@dimen/margin_base"
|
||||
android:layout_marginBottom="@dimen/margin_half"
|
||||
android:paddingBottom="@dimen/margin_half_plus"
|
||||
android:paddingTop="@dimen/margin_half_plus"
|
||||
android:paddingStart="@dimen/margin_half"
|
||||
android:paddingLeft="@dimen/margin_half"
|
||||
android:paddingEnd="@dimen/margin_half"
|
||||
android:paddingRight="@dimen/margin_half"
|
||||
tools:text="@string/authorization_button_sign_in"/>
|
||||
<ProgressBar
|
||||
android:id="@+id/progress"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/margin_base"
|
||||
android:layout_marginBottom="@dimen/margin_half"
|
||||
android:layout_gravity="center"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible"/>
|
||||
</FrameLayout>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
|
|
@ -16,17 +16,33 @@ public class Authorizer implements AuthorizationListener
|
|||
@NonNull
|
||||
private final Fragment mFragment;
|
||||
@Nullable
|
||||
private final Callback mCallback;
|
||||
private Callback mCallback;
|
||||
private boolean mInProgress;
|
||||
|
||||
public Authorizer(@NonNull Fragment fragment, @NonNull Callback callback)
|
||||
public Authorizer(@NonNull Fragment fragment)
|
||||
{
|
||||
this(fragment, null);
|
||||
}
|
||||
|
||||
Authorizer(@NonNull Fragment fragment, @Nullable Callback callback)
|
||||
{
|
||||
mFragment = fragment;
|
||||
}
|
||||
|
||||
|
||||
public void attach(@NonNull Callback callback)
|
||||
{
|
||||
mCallback = callback;
|
||||
}
|
||||
|
||||
public void detach()
|
||||
{
|
||||
mCallback = null;
|
||||
}
|
||||
|
||||
public final void authorize()
|
||||
{
|
||||
if (Framework.nativeIsUserAuthenticated() || !ConnectionState.isConnected())
|
||||
if (isAuthorized() || !ConnectionState.isConnected())
|
||||
{
|
||||
if (mCallback != null)
|
||||
mCallback.onAuthorizationFinish(true);
|
||||
|
@ -52,19 +68,31 @@ public class Authorizer implements AuthorizationListener
|
|||
{
|
||||
@Framework.SocialTokenType
|
||||
int type = data.getIntExtra(Constants.EXTRA_TOKEN_TYPE, -1);
|
||||
Framework.nativeAuthenticateUser(socialToken, type, this);
|
||||
mInProgress = true;
|
||||
if (mCallback != null)
|
||||
mCallback.onAuthorizationStart();
|
||||
Framework.nativeAuthenticateUser(socialToken, type, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorized(boolean success)
|
||||
{
|
||||
mInProgress = false;
|
||||
if (mCallback != null)
|
||||
mCallback.onAuthorizationFinish(success);
|
||||
}
|
||||
|
||||
public boolean isInProgress()
|
||||
{
|
||||
return mInProgress;
|
||||
}
|
||||
|
||||
public boolean isAuthorized()
|
||||
{
|
||||
return Framework.nativeIsUserAuthenticated();
|
||||
}
|
||||
|
||||
public interface Callback
|
||||
{
|
||||
void onAuthorizationFinish(boolean success);
|
||||
|
|
|
@ -28,16 +28,12 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
MenuItem.OnMenuItemClickListener,
|
||||
RecyclerClickListener,
|
||||
RecyclerLongClickListener,
|
||||
BookmarkManager.BookmarksLoadingListener,
|
||||
Authorizer.Callback, BookmarkBackupController.BackupListener
|
||||
BookmarkManager.BookmarksLoadingListener
|
||||
{
|
||||
private long mSelectedCatId;
|
||||
@Nullable
|
||||
private View mLoadingPlaceholder;
|
||||
|
||||
@NonNull
|
||||
private final Authorizer mAuthorizer = new Authorizer(this, this);
|
||||
|
||||
@Nullable
|
||||
private BookmarkBackupController mBackupController;
|
||||
|
||||
|
@ -69,8 +65,7 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
|
||||
mLoadingPlaceholder = view.findViewById(R.id.placeholder_loading);
|
||||
mBackupController = new BookmarkBackupController(view.findViewById(R.id.backup));
|
||||
mBackupController.setListener(this);
|
||||
|
||||
mBackupController.setAuthorizer(new Authorizer(this));
|
||||
if (getAdapter() != null)
|
||||
{
|
||||
getAdapter().setOnClickListener(this);
|
||||
|
@ -116,6 +111,8 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
{
|
||||
super.onStart();
|
||||
BookmarkManager.INSTANCE.addListener(this);
|
||||
if (mBackupController != null)
|
||||
mBackupController.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -123,6 +120,8 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
{
|
||||
super.onStop();
|
||||
BookmarkManager.INSTANCE.removeListener(this);
|
||||
if (mBackupController != null)
|
||||
mBackupController.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -235,29 +234,11 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
// Do nothing here.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorizationFinish(boolean success)
|
||||
{
|
||||
if (mBackupController != null)
|
||||
mBackupController.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorizationStart()
|
||||
{
|
||||
// TODO: show progress
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSignInClick()
|
||||
{
|
||||
mAuthorizer.authorize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data)
|
||||
{
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
mAuthorizer.onActivityResult(requestCode, resultCode, data);
|
||||
if (mBackupController != null)
|
||||
mBackupController.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +1,29 @@
|
|||
package com.mapswithme.maps.bookmarks.data;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.View;
|
||||
|
||||
import com.mapswithme.maps.Framework;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.auth.Authorizer;
|
||||
import com.mapswithme.maps.widget.BookmarkBackupView;
|
||||
import com.mapswithme.util.DateUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class BookmarkBackupController
|
||||
public class BookmarkBackupController implements Authorizer.Callback
|
||||
{
|
||||
@NonNull
|
||||
private final BookmarkBackupView mBackupView;
|
||||
@Nullable
|
||||
private BackupListener mListener;
|
||||
private Authorizer mAuthorizer;
|
||||
@NonNull
|
||||
private final View.OnClickListener mSignInClickListener = v ->
|
||||
{
|
||||
if (mListener != null)
|
||||
mListener.onSignInClick();
|
||||
if (mAuthorizer != null)
|
||||
mAuthorizer.authorize();
|
||||
};
|
||||
@NonNull
|
||||
private final View.OnClickListener mEnableClickListener = v ->
|
||||
|
@ -31,33 +32,40 @@ public class BookmarkBackupController
|
|||
update();
|
||||
};
|
||||
|
||||
|
||||
public BookmarkBackupController(@NonNull BookmarkBackupView backupView)
|
||||
{
|
||||
mBackupView = backupView;
|
||||
update();
|
||||
}
|
||||
|
||||
public void setListener(@Nullable BackupListener listener)
|
||||
public void setAuthorizer(@Nullable Authorizer authorizer)
|
||||
{
|
||||
mListener = listener;
|
||||
mAuthorizer = authorizer;
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
final Context context = mBackupView.getContext();
|
||||
boolean isAuthorized = Framework.nativeIsUserAuthenticated();
|
||||
if (!isAuthorized)
|
||||
if (mAuthorizer != null && !mAuthorizer.isAuthorized())
|
||||
{
|
||||
mBackupView.setMessage(context.getString(R.string.bookmarks_message_unauthorized_user));
|
||||
mBackupView.setButtonLabel(context.getString(R.string.authorization_button_sign_in));
|
||||
mBackupView.setClickListener(mSignInClickListener);
|
||||
mBackupView.showButton();
|
||||
if (mAuthorizer.isInProgress())
|
||||
{
|
||||
mBackupView.showProgressBar();
|
||||
mBackupView.hideButton();
|
||||
}
|
||||
else
|
||||
{
|
||||
mBackupView.hideProgressBar();
|
||||
mBackupView.setClickListener(mSignInClickListener);
|
||||
mBackupView.showButton();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isEnabled = BookmarkManager.INSTANCE.isCloudEnabled();
|
||||
mBackupView.hideProgressBar();
|
||||
|
||||
boolean isEnabled = BookmarkManager.INSTANCE.isCloudEnabled();
|
||||
if (isEnabled)
|
||||
{
|
||||
long backupTime = BookmarkManager.INSTANCE.getLastSynchronizationTimestampInMs();
|
||||
|
@ -83,8 +91,36 @@ public class BookmarkBackupController
|
|||
mBackupView.showButton();
|
||||
}
|
||||
|
||||
public interface BackupListener
|
||||
public void onStart()
|
||||
{
|
||||
void onSignInClick();
|
||||
if (mAuthorizer != null)
|
||||
mAuthorizer.attach(this);
|
||||
update();
|
||||
}
|
||||
|
||||
public void onStop()
|
||||
{
|
||||
if (mAuthorizer != null)
|
||||
mAuthorizer.detach();
|
||||
}
|
||||
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data)
|
||||
{
|
||||
if (mAuthorizer != null)
|
||||
mAuthorizer.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorizationStart()
|
||||
{
|
||||
update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorizationFinish(boolean success /* it's ignored for a while.*/)
|
||||
{
|
||||
if (success)
|
||||
BookmarkManager.INSTANCE.setCloudEnabled(true);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,6 +149,16 @@ public class BookmarkBackupView extends LinearLayout
|
|||
UiUtils.show(mContentLayout, R.id.button);
|
||||
}
|
||||
|
||||
public void hideProgressBar()
|
||||
{
|
||||
UiUtils.hide(mContentLayout, R.id.progress);
|
||||
}
|
||||
|
||||
public void showProgressBar()
|
||||
{
|
||||
UiUtils.show(mContentLayout, R.id.progress);
|
||||
}
|
||||
|
||||
public void setClickListener(@Nullable OnClickListener listener)
|
||||
{
|
||||
mContentLayout.findViewById(R.id.button).setOnClickListener(listener);
|
||||
|
|
Loading…
Add table
Reference in a new issue