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

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

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;

import com.cocosw.bottomsheet.BottomSheet;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.widget.ListShadowController;

import java.lang.ref.WeakReference;

public final class BottomSheetHelper
{
  public static class ShadowedBottomSheet extends BottomSheet
  {
    private ListShadowController mShadowController;


    ShadowedBottomSheet(Context context, int theme)
    {
      super(context, theme);
    }

    @Override
    protected void init(Context context)
    {
      super.init(context);
      mShadowController = new ListShadowController(list);
    }

    @Override
    protected void showFullItems()
    {
      super.showFullItems();
      mShadowController.attach();
    }

    @Override
    protected void showShortItems()
    {
      super.showShortItems();
      mShadowController.detach();
    }
  }


  public static class Builder extends BottomSheet.Builder
  {
    public Builder(@NonNull Activity context)
    {
      super(context);
      setOnDismissListener(null);
    }

    @Override
    protected BottomSheet createDialog(Context context, int theme)
    {
      return new ShadowedBottomSheet(context, theme);
    }

    @Override
    public BottomSheet build()
    {
      free();

      BottomSheet res = super.build();
      sRef = new WeakReference<>(res);
      return res;
    }

    @SuppressWarnings("NullableProblems")
    @Override
    public BottomSheet.Builder setOnDismissListener(final DialogInterface.OnDismissListener listener)
    {
      return super.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog)
        {
          free();
          if (listener != null)
            listener.onDismiss(dialog);
        }
      });
    }
  }


  private static WeakReference<BottomSheet> sRef;


  private BottomSheetHelper()
  {}

  public static BottomSheet getReference()
  {
    if (sRef == null)
      return null;

    return sRef.get();
  }

  public static boolean isShowing()
  {
    BottomSheet bs = getReference();
    return (bs != null && bs.isShowing());
  }

  public static void free()
  {
    BottomSheet ref = getReference();
    if (ref != null)
    {
      if (ref.isShowing())
        ref.dismiss();
      sRef = null;
    }
  }

  public static BottomSheet.Builder create(Activity context)
  {
    free();
    return new Builder(context);
  }

  public static BottomSheet.Builder create(Activity context, @StringRes int title)
  {
    return create(context).title(title);
  }

  public static BottomSheet.Builder createGrid(Activity context, @StringRes int title)
  {
    return create(context, title).grid();
  }

  public static BottomSheet.Builder sheet(BottomSheet.Builder builder, int id, @DrawableRes int iconRes, CharSequence text)
  {
    Drawable icon = ContextCompat.getDrawable(MwmApplication.get(), iconRes);
    return builder.sheet(id, icon, text);
  }
}