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

MWMToast.mm « Toast « CustomAlert « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3bba346a92206cd785ed2131f63d07083000da6 (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
#import "MWMToast.h"
#import "MWMCommon.h"
#import "SwiftBridge.h"

namespace
{
void * kContext = &kContext;
NSString * const kKeyPath = @"sublayers";
NSUInteger const kWordsPerSecond = 3;
}  // namespace

@interface MWMToast ()

@property(nonatomic) IBOutlet UIView * rootView;
@property(nonatomic) IBOutlet UILabel * label;

@property(nonatomic) NSLayoutConstraint * bottomOffset;

@end

@implementation MWMToast

+ (void)showWithText:(NSString *)text
{
  MWMToast * toast = [self toast];
  toast.label.text = text;
  toast.label.textColor = [UIColor blackPrimaryText];
  toast.rootView.backgroundColor = [UIColor toastBackground];
  [toast show];
}

+ (BOOL)affectsStatusBar { return [self toast].rootView.superview != nil; }
+ (UIStatusBarStyle)preferredStatusBarStyle
{
  setStatusBarBackgroundColor([UIColor clearColor]);
  return [UIColor isNightMode] ? UIStatusBarStyleLightContent : UIStatusBarStyleDefault;
}

+ (MWMToast *)toast
{
  static MWMToast * toast;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    toast = [[super alloc] initToast];
  });
  return toast;
}

- (instancetype)initToast
{
  self = [super init];
  if (self)
  {
    [[NSBundle mainBundle] loadNibNamed:[[self class] className] owner:self options:nil];
    self.rootView.translatesAutoresizingMaskIntoConstraints = NO;
  }
  return self;
}

- (void)configLayout
{
  UIView * sv = self.rootView;
  [sv removeFromSuperview];

  auto tvc = [UIViewController topViewController];
  UIView * ov = tvc.view;
  if (!tvc.navigationController.navigationBarHidden)
    ov = tvc.navigationController.navigationBar.superview;
  [ov addSubview:sv];

  NSLayoutConstraint * topOffset = [NSLayoutConstraint constraintWithItem:sv
                                                                attribute:NSLayoutAttributeTop
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:ov
                                                                attribute:NSLayoutAttributeTop
                                                               multiplier:1
                                                                 constant:0];
  topOffset.priority = UILayoutPriorityDefaultLow;
  self.bottomOffset = [NSLayoutConstraint constraintWithItem:sv
                                                   attribute:NSLayoutAttributeBottom
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:ov
                                                   attribute:NSLayoutAttributeTop
                                                  multiplier:1
                                                    constant:0];
  self.bottomOffset.priority = UILayoutPriorityDefaultHigh;
  NSLayoutConstraint * leadingOffset =
      [NSLayoutConstraint constraintWithItem:sv
                                   attribute:NSLayoutAttributeLeading
                                   relatedBy:NSLayoutRelationEqual
                                      toItem:ov
                                   attribute:NSLayoutAttributeLeading
                                  multiplier:1
                                    constant:0];
  NSLayoutConstraint * trailingOffset =
      [NSLayoutConstraint constraintWithItem:sv
                                   attribute:NSLayoutAttributeTrailing
                                   relatedBy:NSLayoutRelationEqual
                                      toItem:ov
                                   attribute:NSLayoutAttributeTrailing
                                  multiplier:1
                                    constant:0];
  [ov addConstraints:@[ topOffset, self.bottomOffset, leadingOffset, trailingOffset ]];
  [ov setNeedsLayout];
}

- (void)scheduleHide
{
  [NSObject cancelPreviousPerformRequestsWithTarget:self];
  NSString * text = self.label.text;
  NSArray<NSString *> * words = [text componentsSeparatedByString:@" "];
  NSTimeInterval const delay = MAX(2, 1 + words.count / kWordsPerSecond);
  [self performSelector:@selector(hide) withObject:nil afterDelay:delay];
}

- (void)show
{
  [self configLayout];

  UIView * ov = [UIViewController topViewController].view;
  runAsyncOnMainQueue(^{
    [ov layoutIfNeeded];
    self.bottomOffset.priority = UILayoutPriorityFittingSizeLevel;
    [UIView animateWithDuration:kDefaultAnimationDuration
        animations:^{
          [ov layoutIfNeeded];
        }
        completion:^(BOOL finished) {
          [self subscribe];
          [[UIViewController topViewController] setNeedsStatusBarAppearanceUpdate];
          [self scheduleHide];
        }];
  });
}

- (void)hide
{
  [self unsubscribe];

  UIView * ov = [UIViewController topViewController].view;
  [ov layoutIfNeeded];
  self.bottomOffset.priority = UILayoutPriorityDefaultHigh;
  [UIView animateWithDuration:kDefaultAnimationDuration
      animations:^{
        [ov layoutIfNeeded];
      }
      completion:^(BOOL finished) {
        [self.rootView removeFromSuperview];
        [[UIViewController topViewController] setNeedsStatusBarAppearanceUpdate];
      }];
}

- (void)subscribe
{
  UIView * sv = self.rootView;
  UIView * ov = sv.superview;
  CALayer * ol = ov.layer;
  [ol addObserver:self forKeyPath:kKeyPath options:NSKeyValueObservingOptionNew context:kContext];
}

- (void)unsubscribe
{
  UIView * sv = self.rootView;
  UIView * ov = sv.superview;
  CALayer * ol = ov.layer;
  [ol removeObserver:self forKeyPath:kKeyPath context:kContext];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
  if (context == kContext)
  {
    UIView * sv = self.rootView;
    UIView * ov = sv.superview;
    [ov bringSubviewToFront:sv];
    return;
  }
  [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
@end