diff --git a/android/src/com/mapswithme/maps/base/BaseAsyncOperationFragment.java b/android/src/com/mapswithme/maps/base/BaseAsyncOperationFragment.java new file mode 100644 index 0000000000..7c07c4a266 --- /dev/null +++ b/android/src/com/mapswithme/maps/base/BaseAsyncOperationFragment.java @@ -0,0 +1,41 @@ +package com.mapswithme.maps.base; + +import android.os.Bundle; +import android.support.annotation.StringRes; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentManager; + +import com.mapswithme.maps.R; +import com.mapswithme.maps.dialog.ProgressDialogFragment; + +public abstract class BaseAsyncOperationFragment extends BaseMwmFragment +{ + private static final String PROGRESS_DIALOG_TAG = "base_progress_dialog"; + public static final String TITLE = "title"; + + protected void showProgress() + { + Bundle arguments = getArguments(); + int resId = arguments.getInt(TITLE, getProgressDialogTitle()); + String title = getString(resId); + ProgressDialogFragment dialog = ProgressDialogFragment.newInstance(title); + getFragmentManager() + .beginTransaction() + .add(dialog, PROGRESS_DIALOG_TAG) + .commitAllowingStateLoss(); + } + + @StringRes + protected int getProgressDialogTitle() + { + return R.string.downloading; + } + + protected void hideProgress() + { + FragmentManager fm = getFragmentManager(); + DialogFragment frag = (DialogFragment) fm.findFragmentByTag(PROGRESS_DIALOG_TAG); + if (frag != null) + frag.dismissAllowingStateLoss(); + } +} diff --git a/android/src/com/mapswithme/maps/base/BaseMwmToolbarFragment.java b/android/src/com/mapswithme/maps/base/BaseMwmToolbarFragment.java index 3c9881eb7b..1bdf255c91 100644 --- a/android/src/com/mapswithme/maps/base/BaseMwmToolbarFragment.java +++ b/android/src/com/mapswithme/maps/base/BaseMwmToolbarFragment.java @@ -7,7 +7,7 @@ import android.view.View; import com.mapswithme.maps.widget.ToolbarController; -public class BaseMwmToolbarFragment extends BaseMwmFragment +public class BaseMwmToolbarFragment extends BaseAsyncOperationFragment { @SuppressWarnings("NullableProblems") @NonNull diff --git a/android/src/com/mapswithme/maps/base/NoToolbarBaseMwmAuthFragment.java b/android/src/com/mapswithme/maps/base/NoToolbarBaseMwmAuthFragment.java new file mode 100644 index 0000000000..fb866c2863 --- /dev/null +++ b/android/src/com/mapswithme/maps/base/NoToolbarBaseMwmAuthFragment.java @@ -0,0 +1,55 @@ +package com.mapswithme.maps.base; + +import android.content.Context; +import android.content.Intent; +import android.support.annotation.CallSuper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.mapswithme.maps.auth.Authorizer; +import com.mapswithme.maps.auth.TargetFragmentCallback; + +public abstract class NoToolbarBaseMwmAuthFragment extends BaseAsyncOperationFragment + implements Authorizer.Callback, TargetFragmentCallback +{ + @NonNull + private final Authorizer mAuthorizer = new Authorizer(this); + + protected void authorize() + { + mAuthorizer.authorize(); + } + + @Override + @CallSuper + public void onAttach(Context context) + { + super.onAttach(context); + mAuthorizer.attach(this); + } + + @Override + @CallSuper + public void onDestroyView() + { + super.onDestroyView(); + mAuthorizer.detach(); + } + + @Override + public void onTargetFragmentResult(int resultCode, @Nullable Intent data) + { + mAuthorizer.onTargetFragmentResult(resultCode, data); + } + + @Override + public boolean isTargetAdded() + { + return isAdded(); + } + + protected boolean isAuthorized() + { + return mAuthorizer.isAuthorized(); + } +} diff --git a/android/src/com/mapswithme/maps/dialog/AlertDialog.java b/android/src/com/mapswithme/maps/dialog/AlertDialog.java index bb8dd858f1..14ef41d0b7 100644 --- a/android/src/com/mapswithme/maps/dialog/AlertDialog.java +++ b/android/src/com/mapswithme/maps/dialog/AlertDialog.java @@ -117,12 +117,14 @@ public class AlertDialog extends BaseMwmDialogFragment { if (mTargetCallback != null) mTargetCallback.onAlertDialogPositiveClick(getArguments().getInt(ARG_REQ_CODE), which); + dismissAllowingStateLoss(); } private void onNegativeClicked(int which) { if (mTargetCallback != null) mTargetCallback.onAlertDialogNegativeClick(getArguments().getInt(ARG_REQ_CODE), which); + dismissAllowingStateLoss(); } @Override diff --git a/android/src/com/mapswithme/maps/ugc/routes/SendLinkPlaceholderFragment.java b/android/src/com/mapswithme/maps/ugc/routes/SendLinkPlaceholderFragment.java index f66afb13db..b8b4ed88ea 100644 --- a/android/src/com/mapswithme/maps/ugc/routes/SendLinkPlaceholderFragment.java +++ b/android/src/com/mapswithme/maps/ugc/routes/SendLinkPlaceholderFragment.java @@ -3,26 +3,35 @@ package com.mapswithme.maps.ugc.routes; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; +import android.support.annotation.StringRes; import android.support.v4.app.ShareCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.mapswithme.maps.R; +import com.mapswithme.maps.base.NoToolbarBaseMwmAuthFragment; +import com.mapswithme.maps.bookmarks.data.BookmarkCategory; +import com.mapswithme.maps.bookmarks.data.BookmarkManager; +import com.mapswithme.maps.bookmarks.data.CatalogCustomProperty; +import com.mapswithme.maps.bookmarks.data.CatalogTagsGroup; +import com.mapswithme.maps.dialog.AlertDialog; import com.mapswithme.util.sharing.TargetUtils; import com.mapswithme.util.statistics.Statistics; +import java.util.List; import java.util.Objects; -public class SendLinkPlaceholderFragment extends Fragment +public class SendLinkPlaceholderFragment extends NoToolbarBaseMwmAuthFragment implements BookmarkManager.BookmarksCatalogListener { - public static final String EXTRA_SHARED_LINK = "shared_link"; + public static final String EXTRA_CATEGORY = "bookmarks_category"; private static final String BODY_STRINGS_SEPARATOR = "\n\n"; + private static final String ERROR_EDITED_ON_WEB_DIALOG_REQ_TAG = "error_edited_on_web_dialog"; + private static final int REQ_CODE_ERROR_EDITED_ON_WEB_DIALOG = 105; @SuppressWarnings("NullableProblems") @NonNull - private String mSharedLink; + private BookmarkCategory mCategory; @Override public void onCreate(@Nullable Bundle savedInstanceState) @@ -32,7 +41,7 @@ public class SendLinkPlaceholderFragment extends Fragment if (args == null) throw new IllegalArgumentException("Please, setup arguments"); - mSharedLink = Objects.requireNonNull(args.getString(EXTRA_SHARED_LINK)); + mCategory = Objects.requireNonNull(args.getParcelable(EXTRA_CATEGORY)); } @Nullable @@ -44,14 +53,28 @@ public class SendLinkPlaceholderFragment extends Fragment View closeBtn = root.findViewById(R.id.close_btn); closeBtn.setOnClickListener(v -> getActivity().finish()); View sendMeLinkBtn = root.findViewById(R.id.send_me_link_btn); - sendMeLinkBtn.setOnClickListener(v -> shareLink()); + sendMeLinkBtn.setOnClickListener(v -> onSendMeLinkBtnClicked()); return root; } + private void onSendMeLinkBtnClicked() + { + if (mCategory.getAccessRules() == BookmarkCategory.AccessRules.ACCESS_RULES_LOCAL) + requestUpload(); + else + shareLink(); + } + + private void requestUpload() + { + showProgress(); + BookmarkManager.INSTANCE.uploadToCatalog(BookmarkCategory.AccessRules.ACCESS_RULES_AUTHOR_ONLY, mCategory); + } + private void shareLink() { String emailBody = getString(R.string.edit_your_guide_email_body) + BODY_STRINGS_SEPARATOR + - mSharedLink; + BookmarkManager.INSTANCE.getCatalogDeeplink(mCategory.getId()); ShareCompat.IntentBuilder.from(getActivity()) .setType(TargetUtils.TYPE_TEXT_PLAIN) @@ -61,4 +84,125 @@ public class SendLinkPlaceholderFragment extends Fragment .startChooser(); Statistics.INSTANCE.trackEvent(Statistics.EventName.BM_EDIT_ON_WEB_CLICK); } + + @Override + public void onUploadFinished(@NonNull BookmarkManager.UploadResult uploadResult, @NonNull + String description, long originCategoryId, long resultCategoryId) + { + if (uploadResult == BookmarkManager.UploadResult.UPLOAD_RESULT_SUCCESS) + onUploadSucceeded(); + else if (uploadResult == BookmarkManager.UploadResult.UPLOAD_RESULT_AUTH_ERROR) + onAuthError(); + else + onUploadFailed(); + + hideProgress(); + } + + private void onAuthError() + { + authorize(); + } + + private void onUploadFailed() + { + showErrorDialog(); + } + + private void showErrorDialog() + { + /* FIXME text*/ + showErrorDialog(R.string.unable_upload_error_subtitle_edited, + R.string.unable_upload_error_subtitle_edited, + REQ_CODE_ERROR_EDITED_ON_WEB_DIALOG, + ERROR_EDITED_ON_WEB_DIALOG_REQ_TAG); + } + + private void showErrorDialog(@StringRes int title, @StringRes int subtitle, int reqCode, + @NonNull String tag) + { + AlertDialog dialog = new AlertDialog.Builder() + .setTitleId(title) + .setMessageId(subtitle) + .setPositiveBtnId(R.string.ok) + .setReqCode(reqCode) + .setFragManagerStrategyType(AlertDialog.FragManagerStrategyType.ACTIVITY_FRAGMENT_MANAGER) + .build(); + dialog.setTargetFragment(this, reqCode); + dialog.show(this, tag); + } + + private void onUploadSucceeded() + { + mCategory = BookmarkManager.INSTANCE.getAllCategoriesSnapshot().refresh(mCategory); + } + + @Override + public void onStart() + { + super.onStart(); + BookmarkManager.INSTANCE.addCatalogListener(this); + } + + @Override + public void onStop() + { + super.onStop(); + BookmarkManager.INSTANCE.removeCatalogListener(this); + } + + @Override + public void onImportStarted(@NonNull String serverId) + { + /* do noting by default */ + } + + @Override + public void onImportFinished(@NonNull String serverId, long catId, boolean successful) + { + /* do noting by default */ + } + + @Override + public void onTagsReceived(boolean successful, @NonNull List tagsGroups) + { + /* do noting by default */ + } + + @Override + public void onCustomPropertiesReceived(boolean successful, @NonNull List properties) + { + /* do noting by default */ + } + + @Override + public void onUploadStarted(long originCategoryId) + { + /* do noting by default */ + } + + @Override + public void onAuthorizationFinish(boolean success) + { + if (success) + requestUpload(); + } + + @Override + public void onAuthorizationStart() + { + /* do noting by default */ + } + + @Override + public void onSocialAuthenticationCancel(int type) + { + /* do noting by default */ + } + + @Override + public void onSocialAuthenticationError(int type, @Nullable String error) + { + /* do noting by default */ + } } diff --git a/android/src/com/mapswithme/maps/ugc/routes/UgcSharingOptionsFragment.java b/android/src/com/mapswithme/maps/ugc/routes/UgcSharingOptionsFragment.java index 588f734e9c..3833d70c27 100644 --- a/android/src/com/mapswithme/maps/ugc/routes/UgcSharingOptionsFragment.java +++ b/android/src/com/mapswithme/maps/ugc/routes/UgcSharingOptionsFragment.java @@ -49,22 +49,19 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl public static final int REQ_CODE_CUSTOM_PROPERTIES = 101; private static final int REQ_CODE_NO_NETWORK_CONNECTION_DIALOG = 103; private static final int REQ_CODE_ERROR_BROKEN_FILE_DIALOG = 104; - private static final int REQ_CODE_ERROR_EDITED_ON_WEB_DIALOG = 105; private static final int REQ_CODE_ERROR_COMMON = 106; private static final int REQ_CODE_ERROR_NOT_ENOUGH_BOOKMARKS = 107; private static final int REQ_CODE_UPLOAD_CONFIRMATION_DIALOG = 108; private static final int REQ_CODE_ERROR_HTML_FORMATTING_DIALOG = 109; private static final String BUNDLE_CURRENT_MODE = "current_mode"; - private static final String UPLOADING_PROGRESS_DIALOG_TAG = "uploading_progress_dialog"; private static final String NO_NETWORK_CONNECTION_DIALOG_TAG = "no_network_connection_dialog"; private static final String NOT_ENOUGH_BOOKMARKS_DIALOG_TAG = "not_enough_bookmarks_dialog"; private static final String ERROR_BROKEN_FILE_DIALOG_TAG = "error_broken_file_dialog"; - private static final String ERROR_EDITED_ON_WEB_DIALOG_REQ_TAG = "error_edited_on_web_dialog"; private static final String ERROR_COMMON_DIALOG_TAG = "error_common_dialog"; private static final String UPLOAD_CONFIRMATION_DIALOG_TAG = "upload_confirmation_dialog"; private static final String ERROR_HTML_FORMATTING_DIALOG_TAG = "error_html_formatting_dialog"; - + private static final int MIN_REQUIRED_CATEGORY_SIZE = 3; @SuppressWarnings("NullableProblems") @NonNull @@ -231,9 +228,8 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl private void onEditOnWebClicked() { - String deepLink = BookmarkManager.INSTANCE.getCatalogDeeplink(mCategory.getId()); Intent intent = new Intent(getContext(), SendLinkPlaceholderActivity.class) - .putExtra(SendLinkPlaceholderFragment.EXTRA_SHARED_LINK, deepLink); + .putExtra(SendLinkPlaceholderFragment.EXTRA_CATEGORY, mCategory); startActivity(intent); Statistics.INSTANCE.trackSharingOptionsClick(Statistics.ParamValue.EDIT_ON_WEB); } @@ -291,6 +287,12 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl private void onUploadAndPublishBtnClicked() { +/* if (mCategory.size() < MIN_REQUIRED_CATEGORY_SIZE) + { + showNotEnoughBookmarksDialog(); + return; + }*/ + mCurrentMode = BookmarkCategory.AccessRules.ACCESS_RULES_PUBLIC; onUploadBtnClicked(); Statistics.INSTANCE.trackSharingOptionsClick(Statistics.ParamValue.PUBLIC); @@ -299,7 +301,7 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl private void onGetDirectLinkClicked() { mCurrentMode = BookmarkCategory.AccessRules.ACCESS_RULES_DIRECT_LINK; - onUploadBtnClicked(); + requestUpload(); Statistics.INSTANCE.trackSharingOptionsClick(Statistics.ParamValue.PRIVATE); } @@ -311,6 +313,11 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl return; } + showUploadCatalogConfirmationDialog(); + } + + private void requestUpload() + { if (isAuthorized()) onPostAuthCompleted(); else @@ -338,25 +345,6 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl BookmarkManager.INSTANCE.uploadRoutes(mCurrentMode.ordinal(), mCategory); } - private void showProgress() - { - String title = getString(R.string.upload_and_publish_progress_text); - ProgressDialogFragment dialog = ProgressDialogFragment.newInstance(title); - getFragmentManager() - .beginTransaction() - .add(dialog, UPLOADING_PROGRESS_DIALOG_TAG) - .commitAllowingStateLoss(); - } - - - private void hideProgress() - { - FragmentManager fm = getFragmentManager(); - DialogFragment frag = (DialogFragment) fm.findFragmentByTag(UPLOADING_PROGRESS_DIALOG_TAG); - if (frag != null) - frag.dismissAllowingStateLoss(); - } - @Override public void onSaveInstanceState(Bundle outState) { @@ -486,7 +474,7 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl if (uploadResult == BookmarkManager.UploadResult.UPLOAD_RESULT_ACCESS_ERROR) { - showErrorEditedOnWebDialog(); + showUnresolvedConflictsErrorDialog(); return; } @@ -522,15 +510,6 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl " Current value = " + mCategory.getAccessRules()); } - - private void showErrorEditedOnWebDialog() - { - showUploadErrorDialog(R.string.unable_upload_error_subtitle_edited, - REQ_CODE_ERROR_EDITED_ON_WEB_DIALOG, - ERROR_EDITED_ON_WEB_DIALOG_REQ_TAG); - } - - private void showErrorBrokenFileDialog() { showUploadErrorDialog(R.string.unable_upload_error_subtitle_broken, @@ -574,7 +553,8 @@ public class UgcSharingOptionsFragment extends BaseMwmAuthorizationFragment impl { if (requestCode == REQ_CODE_NO_NETWORK_CONNECTION_DIALOG) Utils.showSystemSettings(getContext()); - + else if (requestCode == REQ_CODE_UPLOAD_CONFIRMATION_DIALOG) + requestUpload(); } @Override