forked from organicmaps/organicmaps
[android] Refactored passport authorization logic to make it reusable for bookmarks fragment
This commit is contained in:
parent
4ea02c36fe
commit
37fabd489d
4 changed files with 109 additions and 72 deletions
66
android/src/com/mapswithme/maps/auth/Authorizer.java
Normal file
66
android/src/com/mapswithme/maps/auth/Authorizer.java
Normal file
|
@ -0,0 +1,66 @@
|
|||
package com.mapswithme.maps.auth;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.mapswithme.maps.Framework;
|
||||
import com.mapswithme.util.ConnectionState;
|
||||
|
||||
public class Authorizer
|
||||
{
|
||||
@NonNull
|
||||
private final Fragment mFragment;
|
||||
@Nullable
|
||||
private final Callback mCallback;
|
||||
|
||||
public Authorizer(@NonNull Fragment fragment, @NonNull Callback callback)
|
||||
{
|
||||
mFragment = fragment;
|
||||
mCallback = callback;
|
||||
}
|
||||
|
||||
public final void authorize()
|
||||
{
|
||||
if (Framework.nativeIsUserAuthenticated() || !ConnectionState.isConnected())
|
||||
{
|
||||
if (mCallback != null)
|
||||
mCallback.onAuthorizationFinish();
|
||||
return;
|
||||
}
|
||||
|
||||
String name = SocialAuthDialogFragment.class.getName();
|
||||
DialogFragment fragment = (DialogFragment) Fragment.instantiate(mFragment.getContext(), name);
|
||||
fragment.setTargetFragment(mFragment, Constants.REQ_CODE_GET_SOCIAL_TOKEN);
|
||||
fragment.show(mFragment.getActivity().getSupportFragmentManager(), name);
|
||||
}
|
||||
|
||||
public final void onActivityResult(int requestCode, int resultCode, Intent data)
|
||||
{
|
||||
if (requestCode != Constants.REQ_CODE_GET_SOCIAL_TOKEN
|
||||
|| resultCode != Activity.RESULT_OK || data == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String socialToken = data.getStringExtra(Constants.EXTRA_SOCIAL_TOKEN);
|
||||
if (!TextUtils.isEmpty(socialToken))
|
||||
{
|
||||
@Framework.SocialTokenType
|
||||
int type = data.getIntExtra(Constants.EXTRA_TOKEN_TYPE, -1);
|
||||
Framework.nativeAuthenticateUser(socialToken, type);
|
||||
}
|
||||
|
||||
if (mCallback != null)
|
||||
mCallback.onAuthorizationFinish();
|
||||
}
|
||||
|
||||
public interface Callback
|
||||
{
|
||||
void onAuthorizationFinish();
|
||||
}
|
||||
}
|
|
@ -1,20 +1,9 @@
|
|||
package com.mapswithme.maps.auth;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.MainThread;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
|
||||
import com.mapswithme.maps.Framework;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseMwmToolbarFragment;
|
||||
import com.mapswithme.util.ConnectionState;
|
||||
|
||||
/**
|
||||
* A base toolbar fragment which is responsible for the <b>authorization flow</b>,
|
||||
|
@ -22,69 +11,28 @@ import com.mapswithme.util.ConnectionState;
|
|||
* to get user authorized for the MapsMe server (Passport).
|
||||
*/
|
||||
public abstract class BaseMwmAuthorizationFragment extends BaseMwmToolbarFragment
|
||||
implements Authorizer.Callback
|
||||
{
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
|
||||
@NonNull
|
||||
private final Authorizer mAuthorizer = new Authorizer(this, this);
|
||||
|
||||
protected void authorize()
|
||||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
View submitButton = mToolbarController.findViewById(R.id.submit);
|
||||
if (submitButton == null)
|
||||
throw new AssertionError("Descendant of BaseMwmAuthorizationFragment must have authorize " +
|
||||
"button in toolbar!");
|
||||
|
||||
submitButton.setOnClickListener(new View.OnClickListener()
|
||||
{
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
onSubmitButtonClick();
|
||||
authorize();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void authorize()
|
||||
{
|
||||
if (Framework.nativeIsUserAuthenticated() || !ConnectionState.isConnected())
|
||||
{
|
||||
finishActivity();
|
||||
return;
|
||||
}
|
||||
|
||||
String name = SocialAuthDialogFragment.class.getName();
|
||||
DialogFragment fragment = (DialogFragment) Fragment.instantiate(getContext(), name);
|
||||
fragment.setTargetFragment(this, Constants.REQ_CODE_GET_SOCIAL_TOKEN);
|
||||
fragment.show(getActivity().getSupportFragmentManager(), name);
|
||||
mAuthorizer.authorize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data)
|
||||
{
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
if (requestCode != Constants.REQ_CODE_GET_SOCIAL_TOKEN
|
||||
|| resultCode != Activity.RESULT_OK || data == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String socialToken = data.getStringExtra(Constants.EXTRA_SOCIAL_TOKEN);
|
||||
if (!TextUtils.isEmpty(socialToken))
|
||||
{
|
||||
@Framework.SocialTokenType
|
||||
int type = data.getIntExtra(Constants.EXTRA_TOKEN_TYPE, -1);
|
||||
Framework.nativeAuthenticateUser(socialToken, type);
|
||||
}
|
||||
|
||||
finishActivity();
|
||||
mAuthorizer.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
private void finishActivity()
|
||||
protected abstract void onAuthorized();
|
||||
|
||||
@Override
|
||||
public void onAuthorizationFinish()
|
||||
{
|
||||
if (isAdded())
|
||||
getActivity().finish();
|
||||
onAuthorized();
|
||||
}
|
||||
|
||||
@MainThread
|
||||
protected abstract void onSubmitButtonClick();
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import android.view.MenuItem;
|
|||
import android.view.View;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.auth.Authorizer;
|
||||
import com.mapswithme.maps.base.BaseMwmRecyclerFragment;
|
||||
import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
|
||||
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
|
||||
|
@ -23,16 +24,20 @@ import com.mapswithme.util.UiUtils;
|
|||
import com.mapswithme.util.sharing.SharingHelper;
|
||||
|
||||
public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
||||
implements EditTextDialogFragment.OnTextSaveListener,
|
||||
MenuItem.OnMenuItemClickListener,
|
||||
RecyclerClickListener,
|
||||
RecyclerLongClickListener,
|
||||
BookmarkManager.BookmarksLoadingListener
|
||||
implements EditTextDialogFragment.OnTextSaveListener,
|
||||
MenuItem.OnMenuItemClickListener,
|
||||
RecyclerClickListener,
|
||||
RecyclerLongClickListener,
|
||||
BookmarkManager.BookmarksLoadingListener,
|
||||
Authorizer.Callback
|
||||
{
|
||||
private int mSelectedPosition;
|
||||
@Nullable
|
||||
private View mLoadingPlaceholder;
|
||||
|
||||
@NonNull
|
||||
private final Authorizer mAuthorizer = new Authorizer(this, this);
|
||||
|
||||
@Override
|
||||
protected @LayoutRes int getLayoutRes()
|
||||
{
|
||||
|
@ -223,4 +228,10 @@ public class BookmarkCategoriesFragment extends BaseMwmRecyclerFragment
|
|||
{
|
||||
// Do nothing here.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthorizationFinish()
|
||||
{
|
||||
// TODO: coming soon.
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ import com.mapswithme.maps.R;
|
|||
import com.mapswithme.maps.auth.BaseMwmAuthorizationFragment;
|
||||
import com.mapswithme.maps.bookmarks.data.FeatureId;
|
||||
import com.mapswithme.maps.widget.ToolbarController;
|
||||
import com.mapswithme.util.CrashlyticsUtils;
|
||||
import com.mapswithme.util.Language;
|
||||
import com.mapswithme.util.UiUtils;
|
||||
import com.mapswithme.util.statistics.Statistics;
|
||||
|
@ -39,7 +38,8 @@ public class UGCEditorFragment extends BaseMwmAuthorizationFragment
|
|||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
View root = inflater.inflate(R.layout.fragment_ugc_editor, container, false);
|
||||
mReviewEditText = (EditText) root.findViewById(R.id.review);
|
||||
|
@ -74,6 +74,12 @@ public class UGCEditorFragment extends BaseMwmAuthorizationFragment
|
|||
{
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
mToolbarController.setTitle(getArguments().getString(ARG_TITLE));
|
||||
View submitButton = mToolbarController.findViewById(R.id.submit);
|
||||
submitButton.setOnClickListener(v ->
|
||||
{
|
||||
onSubmitButtonClick();
|
||||
authorize();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -91,7 +97,13 @@ public class UGCEditorFragment extends BaseMwmAuthorizationFragment
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void onSubmitButtonClick()
|
||||
protected void onAuthorized()
|
||||
{
|
||||
if (isAdded())
|
||||
getActivity().finish();
|
||||
}
|
||||
|
||||
private void onSubmitButtonClick()
|
||||
{
|
||||
List<UGC.Rating> modifiedRatings = mUGCRatingAdapter.getItems();
|
||||
UGC.Rating[] ratings = new UGC.Rating[modifiedRatings.size()];
|
||||
|
|
Loading…
Add table
Reference in a new issue