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

ChooseBookmarksSortingTypeFragment.java « bookmarks « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70417c405744cdca83d03541abf91a91c1469bb6 (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
package com.mapswithme.maps.bookmarks;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmDialogFragment;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.statistics.Statistics;

public class ChooseBookmarksSortingTypeFragment extends BaseMwmDialogFragment
    implements RadioGroup.OnCheckedChangeListener
{
  private static final String EXTRA_SORTING_TYPES = "sorting_types";
  private static final String EXTRA_CURRENT_SORT_TYPE = "current_sort_type";

  @Nullable
  private ChooseSortingTypeListener mListener;

  public interface ChooseSortingTypeListener
  {
    void onResetSorting();
    void onSort(@BookmarkManager.SortingType int sortingType);
  }

  public static void chooseSortingType(@NonNull @BookmarkManager.SortingType int[] availableTypes,
                                       int currentType, @NonNull Context context,
                                       @NonNull FragmentManager manager)
  {
    Bundle args = new Bundle();
    args.putIntArray(EXTRA_SORTING_TYPES, availableTypes);
    args.putInt(EXTRA_CURRENT_SORT_TYPE, currentType);
    String name = ChooseBookmarksSortingTypeFragment.class.getName();
    final ChooseBookmarksSortingTypeFragment fragment =
        (ChooseBookmarksSortingTypeFragment) Fragment.instantiate(context, name, args);
    fragment.setArguments(args);
    fragment.show(manager, name);
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState)
  {
    return inflater.inflate(R.layout.dialog_sorting_types, container, false);
  }

  @Override
  protected int getStyle()
  {
    return STYLE_NO_TITLE;
  }

  @IdRes
  private int getViewId(int sortingType)
  {
    if (sortingType >= 0)
    {
      switch (sortingType)
      {
        case BookmarkManager.SORT_BY_TYPE:
          return R.id.sort_by_type;
        case BookmarkManager.SORT_BY_DISTANCE:
          return R.id.sort_by_distance;
        case BookmarkManager.SORT_BY_TIME:
          return R.id.sort_by_time;
      }
    }
    return R.id.sort_by_default;
  }

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
  {
    super.onViewCreated(view, savedInstanceState);

    final Bundle args = getArguments();
    if (args == null)
      throw new AssertionError("Arguments of choose sorting type view can't be null.");

    UiUtils.hide(view, R.id.sort_by_type, R.id.sort_by_distance, R.id.sort_by_time);

    @BookmarkManager.SortingType
    int[] availableSortingTypes = args.getIntArray(EXTRA_SORTING_TYPES);
    if (availableSortingTypes == null)
      throw new AssertionError("Available sorting types can't be null.");

    for (@BookmarkManager.SortingType int type : availableSortingTypes)
      UiUtils.show(view.findViewById(getViewId(type)));

    int currentType = args.getInt(EXTRA_CURRENT_SORT_TYPE);

    RadioGroup radioGroup = view.findViewById(R.id.sorting_types);
    radioGroup.clearCheck();
    radioGroup.check(getViewId(currentType));
    radioGroup.setOnCheckedChangeListener(this);
  }

  @Override
  public void onAttach(Context context)
  {
    super.onAttach(context);
    onAttachInternal();
  }

  private void onAttachInternal()
  {
    mListener = (ChooseSortingTypeListener) (getParentFragment() == null ? getTargetFragment()
                                                                         : getParentFragment());
  }

  @Override
  public void onDetach()
  {
    super.onDetach();
    mListener = null;
  }

  private void resetSorting()
  {
    if (mListener != null)
      mListener.onResetSorting();
    trackBookmarksListResetSort();
    dismiss();
  }

  private void setSortingType(@BookmarkManager.SortingType int sortingType)
  {
    if (mListener != null)
      mListener.onSort(sortingType);
    trackBookmarksListSort(sortingType);
    dismiss();
  }

  @Override
  public void onCheckedChanged(RadioGroup group, @IdRes int id)
  {
    switch (id)
    {
      case R.id.sort_by_default:
        resetSorting();
        break;
      case R.id.sort_by_type:
        setSortingType(BookmarkManager.SORT_BY_TYPE);
        break;
      case R.id.sort_by_distance:
        setSortingType(BookmarkManager.SORT_BY_DISTANCE);
        break;
      case R.id.sort_by_time:
        setSortingType(BookmarkManager.SORT_BY_TIME);
        break;
    }
  }

  private static void trackBookmarksListSort(@BookmarkManager.SortingType int sortingType)
  {
    Statistics.INSTANCE.trackBookmarksListSort(sortingType);
  }

  private static void trackBookmarksListResetSort()
  {
    Statistics.INSTANCE.trackBookmarksListResetSort();
  }
}