[android] Added authorizer in bookmark catalog controller

This commit is contained in:
Александр Зацепин 2018-10-29 20:00:14 +03:00 committed by yoksnod
parent 76678e160a
commit 444cde73d4
2 changed files with 58 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package com.mapswithme.maps.bookmarks;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
@ -20,7 +21,9 @@ import android.widget.Toast;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.auth.Authorizer;
import com.mapswithme.maps.auth.BaseWebViewMwmFragment;
import com.mapswithme.maps.auth.TargetFragmentCallback;
import com.mapswithme.maps.metrics.UserActionsLogger;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.UiUtils;
@ -33,6 +36,7 @@ import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
implements TargetFragmentCallback
{
public static final String EXTRA_BOOKMARKS_CATALOG_URL = "bookmarks_catalog_url";
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
@ -58,12 +62,17 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
@NonNull
private BookmarkCatalogController mController;
@SuppressWarnings("NullableProblems")
@NonNull
private Authorizer mAuthorizer;
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mController = new DefaultBookmarkCatalogController(new CatalogListenerDecorator(this));
mAuthorizer = new Authorizer(this);
mController = new DefaultBookmarkCatalogController(mAuthorizer,
new CatalogListenerDecorator(this));
}
@Override
@ -152,6 +161,18 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
}
}
@Override
public void onTargetFragmentResult(int resultCode, @Nullable Intent data)
{
mAuthorizer.onTargetFragmentResult(resultCode, data);
}
@Override
public boolean isTargetAdded()
{
return isAdded();
}
private static class WebViewBookmarksCatalogClient extends WebViewClient
{
private final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);

View file

@ -5,12 +5,13 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;
import com.mapswithme.maps.auth.Authorizer;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import java.net.MalformedURLException;
public class DefaultBookmarkCatalogController
implements BookmarkCatalogController, BookmarkDownloadHandler
implements BookmarkCatalogController, BookmarkDownloadHandler, Authorizer.Callback
{
@Nullable
private Activity mActivity;
@ -18,10 +19,13 @@ public class DefaultBookmarkCatalogController
private final BookmarkDownloadReceiver mDownloadCompleteReceiver = new BookmarkDownloadReceiver();
@NonNull
private final BookmarkManager.BookmarksCatalogListener mCatalogListener;
@NonNull
private final Authorizer mAuthorizer;
DefaultBookmarkCatalogController(@NonNull BookmarkManager.BookmarksCatalogListener
catalogListener)
DefaultBookmarkCatalogController(@NonNull Authorizer authorizer,
@NonNull BookmarkManager.BookmarksCatalogListener catalogListener)
{
mAuthorizer = authorizer;
mCatalogListener = catalogListener;
}
@ -42,6 +46,7 @@ public class DefaultBookmarkCatalogController
throw new AssertionError("Already attached! Call detach.");
mActivity = activity;
mAuthorizer.attach(this);
mDownloadCompleteReceiver.attach(this);
mDownloadCompleteReceiver.register(mActivity.getApplication());
BookmarkManager.INSTANCE.addCatalogListener(mCatalogListener);
@ -53,6 +58,7 @@ public class DefaultBookmarkCatalogController
if (mActivity == null)
throw new AssertionError("Already detached! Call attach.");
mAuthorizer.detach();
mDownloadCompleteReceiver.detach();
mDownloadCompleteReceiver.unregister(mActivity.getApplication());
BookmarkManager.INSTANCE.removeCatalogListener(mCatalogListener);
@ -62,12 +68,7 @@ public class DefaultBookmarkCatalogController
@Override
public void onAuthorizationRequired()
{
if (mActivity == null)
return;
Toast.makeText(mActivity, "Authorization required. Ui coming soon!",
Toast.LENGTH_SHORT).show();
mAuthorizer.authorize();
}
@Override
@ -80,4 +81,30 @@ public class DefaultBookmarkCatalogController
Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthorizationFinish(boolean success)
{
Toast.makeText(mActivity, "Authorization completed, success = " + success,
Toast.LENGTH_SHORT).show();
// TODO: repeat previous download.
}
@Override
public void onAuthorizationStart()
{
// Do nothing by default.
}
@Override
public void onSocialAuthenticationCancel(int type)
{
// Do nothing by default.
}
@Override
public void onSocialAuthenticationError(int type, @Nullable String error)
{
// Do nothing by default.
}
}