Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UgcRoutePropertiesFragment.java « routes « ugc « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8f525df8675bfd624e89ccc459401e78a1fc236c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.mapswithme.maps.ugc.routes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmFragment;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.CatalogCustomProperty;
import com.mapswithme.maps.bookmarks.data.CatalogCustomPropertyOption;
import com.mapswithme.maps.bookmarks.data.CatalogPropertyOptionAndKey;
import com.mapswithme.maps.bookmarks.data.CatalogTagsGroup;
import com.mapswithme.maps.dialog.AlertDialog;
import com.mapswithme.maps.dialog.AlertDialogCallback;
import com.mapswithme.util.UiUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class UgcRoutePropertiesFragment extends BaseMwmFragment implements BookmarkManager.BookmarksCatalogListener, AlertDialogCallback
{
  public static final String EXTRA_TAGS_ACTIVITY_RESULT = "tags_activity_result";
  public static final String EXTRA_CATEGORY_OPTIONS = "category_options";
  public static final int REQ_CODE_TAGS_ACTIVITY = 102;
  private static final int REQ_CODE_LOAD_FAILED = 101;
  private static final int FIRST_OPTION_INDEX = 0;
  private static final int SECOND_OPTION_INDEX = 1;
  private static final String BUNDLE_SELECTED_OPTION = "selected_property";
  private static final String BUNDLE_CUSTOM_PROPS = "custom_props";
  private static final String ERROR_LOADING_DIALOG_TAG = "error_loading_dialog";

  @NonNull
  private List<CatalogCustomProperty> mProps = Collections.emptyList();

  @Nullable
  private CatalogPropertyOptionAndKey mSelectedOption;

  @SuppressWarnings("NullableProblems")
  @NonNull
  private View mProgress;

  @SuppressWarnings("NullableProblems")
  @NonNull
  private View mPropsContainer;

  @SuppressWarnings("NullableProblems")
  @NonNull
  private Button mLeftBtn;

  @SuppressWarnings("NullableProblems")
  @NonNull
  private Button mRightBtn;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState)
  {
    View root = inflater.inflate(R.layout.fragment_ugc_routes_properties, container, false);
    initPropsAndOptions(savedInstanceState);
    initViews(root);
    if (mProps.isEmpty())
      BookmarkManager.INSTANCE.requestCustomProperties();
    return root;
  }

  private void initPropsAndOptions(@Nullable Bundle savedInstanceState)
  {
    if (savedInstanceState == null)
      return;
    mProps =  Objects.requireNonNull(savedInstanceState.getParcelableArrayList(BUNDLE_CUSTOM_PROPS));
    mSelectedOption = savedInstanceState.getParcelable(BUNDLE_SELECTED_OPTION);
  }

  private void initViews(@NonNull View root)
  {
    mLeftBtn = root.findViewById(R.id.left_btn);
    mLeftBtn.setOnClickListener(v -> onLeftBtnClicked());
    mRightBtn = root.findViewById(R.id.right_btn);
    mRightBtn.setOnClickListener(v -> onRightBtnClicked());
    mPropsContainer = root.findViewById(R.id.properties_container);
    UiUtils.hideIf(mProps.isEmpty(), mPropsContainer);
    mProgress = root.findViewById(R.id.progress);
    UiUtils.showIf(mProps.isEmpty(), mProgress);
    if (mProps.isEmpty())
      return;

    initButtons();
  }

  private void initButtons()
  {
    CatalogCustomProperty property = mProps.get(0);
    CatalogCustomPropertyOption firstOption = property.getOptions().get(FIRST_OPTION_INDEX);
    CatalogCustomPropertyOption secondOption = property.getOptions().get(SECOND_OPTION_INDEX);
    mLeftBtn.setText(firstOption.getLocalizedName());
    mRightBtn.setText(secondOption.getLocalizedName());
  }

  @Override
  public void onSaveInstanceState(Bundle outState)
  {
    super.onSaveInstanceState(outState);
    if (mSelectedOption != null)
      outState.putParcelable(BUNDLE_SELECTED_OPTION, mSelectedOption);
    outState.putParcelableArrayList(BUNDLE_CUSTOM_PROPS, new ArrayList<>(mProps));
  }

  private void onBtnClicked(int index)
  {
    CatalogCustomProperty property = mProps.get(0);
    CatalogCustomPropertyOption option = property.getOptions().get(index);
    mSelectedOption = new CatalogPropertyOptionAndKey(property.getKey(), option);
    Intent intent = new Intent(getContext(), UgcRouteTagsActivity.class);
    startActivityForResult(intent, REQ_CODE_TAGS_ACTIVITY);
  }

  private void onRightBtnClicked()
  {
    onBtnClicked(SECOND_OPTION_INDEX);
  }

  private void onLeftBtnClicked()
  {
    onBtnClicked(FIRST_OPTION_INDEX);
  }

  @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<CatalogTagsGroup> tagsGroups,
                             int tagsLimit)
  {
    /* Do noting by default */
  }

  @Override
  public void onCustomPropertiesReceived(boolean successful,
                                         @NonNull List<CatalogCustomProperty> properties)
  {
    if (!successful)
    {
      onLoadFailed();
      return;
    }

    if (properties.isEmpty())
    {
      onLoadFailed();
      return;
    }

    CatalogCustomProperty property = properties.iterator().next();
    if (property == null)
    {
      onLoadFailed();
      return;
    }

    List<CatalogCustomPropertyOption> options = property.getOptions();
    if (options.size() <= SECOND_OPTION_INDEX)
    {
      onLoadFailed();
      return;
    }

    onLoadSuccess(properties);
  }

  private void onLoadFailed()
  {
    showLoadFailedDialog();
    UiUtils.hide(mProgress, mPropsContainer);
  }

  private void showLoadFailedDialog()
  {
    AlertDialog dialog = new AlertDialog.Builder()
        .setTitleId(R.string.discovery_button_viator_error_title)
        .setMessageId(R.string.properties_loading_error_subtitle)
        .setPositiveBtnId(R.string.try_again)
        .setNegativeBtnId(R.string.cancel)
        .setReqCode(REQ_CODE_LOAD_FAILED)
        .setFragManagerStrategyType(AlertDialog.FragManagerStrategyType.ACTIVITY_FRAGMENT_MANAGER)
        .build();
    dialog.setTargetFragment(this, REQ_CODE_LOAD_FAILED);
    dialog.show(this, ERROR_LOADING_DIALOG_TAG);
  }

  private void onLoadSuccess(@NonNull List<CatalogCustomProperty> properties)
  {
    mProps = properties;
    mPropsContainer.setVisibility(View.VISIBLE);
    mProgress.setVisibility(View.GONE);
    initButtons();
  }

  @Override
  public void onUploadStarted(long originCategoryId)
  {
    /* Do noting by default */
  }

  @Override
  public void onUploadFinished(@NonNull BookmarkManager.UploadResult uploadResult,
                               @NonNull String description, long originCategoryId,
                               long resultCategoryId)
  {
    /* Do noting by default */
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE_TAGS_ACTIVITY)
    {
      if (resultCode == Activity.RESULT_OK)
      {
        Intent intent = new Intent();
        ArrayList<CatalogPropertyOptionAndKey> options = new ArrayList<>(prepareSelectedOptions());
        intent.putParcelableArrayListExtra(EXTRA_CATEGORY_OPTIONS, options);
        intent.putExtra(EXTRA_TAGS_ACTIVITY_RESULT, data.getExtras());
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
      }
    }
  }

  @NonNull
  private List<CatalogPropertyOptionAndKey> prepareSelectedOptions()
  {
    return mSelectedOption == null ? Collections.emptyList()
                                   : Collections.singletonList(mSelectedOption);
  }

  @Override
  public void onAlertDialogPositiveClick(int requestCode, int which)
  {
    UiUtils.show(mProgress);
    UiUtils.hide(mPropsContainer);
    BookmarkManager.INSTANCE.requestCustomProperties();
  }

  @Override
  public void onAlertDialogNegativeClick(int requestCode, int which)
  {
    getActivity().setResult(Activity.RESULT_CANCELED);
    getActivity().finish();
  }

  @Override
  public void onAlertDialogCancel(int requestCode)
  {
    getActivity().setResult(Activity.RESULT_CANCELED);
    getActivity().finish();
  }
}