You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
6.6 KiB
211 lines
6.6 KiB
4 years ago
|
//
|
||
|
// TFPromptManager.m
|
||
|
// TFReader
|
||
|
//
|
||
|
// Created by 谢腾飞 on 2020/12/10.
|
||
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "TFPromptManager.h"
|
||
|
#import "TFReaderSettingHelper.h"
|
||
|
#import "UIImage+Color.h"
|
||
|
|
||
|
#define Alert_Duration 1.0f
|
||
|
|
||
|
static TFPromptView *_promptView = nil;
|
||
|
@implementation TFPromptManager
|
||
|
|
||
|
static NSDictionary *_errorInfo;
|
||
|
|
||
|
+ (void)showPromptWithError:(NSError *)error defaultText:(NSString * _Nullable)text
|
||
|
{
|
||
|
_errorInfo = _errorInfo ?: [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ErrorCode" ofType:@"plist"]];
|
||
|
|
||
|
NSString *_text = _errorInfo[[NSString stringWithFormat:@"%zd", error.code]];
|
||
|
_text = _text ?: (error.localizedDescription ?: (text ?: TFLocalizedString(@"请求失败")));
|
||
|
|
||
|
[self showPromptViewWithStatus:TFPromptStatusError promptTitle:_text];
|
||
|
}
|
||
|
|
||
|
+ (void)showPromptViewWithStatus:(TFPromptStatus)status promptTitle:(NSString *)promptTitle
|
||
|
{
|
||
|
[self showPromptViewWithStatus:status promptTitle:promptTitle duration:Alert_Duration];
|
||
|
}
|
||
|
|
||
|
+ (void)showLimitPromptViewWithStatus:(TFPromptStatus)status promptTitle:(NSString *)promptTitle
|
||
|
{
|
||
|
[self showPromptViewWithStatus:status promptTitle:promptTitle duration:Alert_Duration];
|
||
|
}
|
||
|
|
||
|
+ (void)showPromptViewWithStatus:(TFPromptStatus)status promptTitle:(NSString *)promptTitle duration:(CGFloat)duration
|
||
|
{
|
||
|
[self showPromptViewWithStatus:status promptTitle:promptTitle duration:duration completionHandler:nil];
|
||
|
}
|
||
|
|
||
|
+ (void)showPromptViewWithStatus:(TFPromptStatus)status promptTitle:(NSString *)promptTitle completionHandler:(TFPromptDissmissBlock)completionHandler
|
||
|
{
|
||
|
[self showPromptViewWithStatus:status promptTitle:promptTitle duration:Alert_Duration completionHandler:completionHandler];
|
||
|
}
|
||
|
|
||
|
+ (void)showPromptViewWithStatus:(TFPromptStatus)status promptTitle:(NSString *)promptTitle duration:(CGFloat)duration completionHandler:(TFPromptDissmissBlock)completionHandler
|
||
|
{
|
||
|
if (!promptTitle || promptTitle.length == 0) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ([_promptView.promptTitle isEqualToString:promptTitle]) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (_promptView) {
|
||
|
[_promptView hiddenPromptView];
|
||
|
}
|
||
|
|
||
|
if (status == TFPromptStatusLoading) {
|
||
|
duration = 30;
|
||
|
}
|
||
|
|
||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
|
||
|
WS(weakSelf)
|
||
|
_promptView = [[TFPromptView alloc] init];
|
||
|
_promptView.promptTitle = promptTitle;
|
||
|
_promptView.promptStatus = status;
|
||
|
_promptView.alertDuration = duration;
|
||
|
_promptView.alertDissmissBlock = ^{
|
||
|
|
||
|
[weakSelf hiddenAlert];
|
||
|
if (completionHandler) {
|
||
|
completionHandler();
|
||
|
}
|
||
|
};
|
||
|
[_promptView showPromptView];
|
||
|
});
|
||
|
}
|
||
|
|
||
|
static UIActivityIndicatorView *activityIndicator;
|
||
|
UIView *maskView;
|
||
|
|
||
|
+ (void)showPromptViewWithPromptMaskType:(TFPromptMaskType)type loadingStyle:(TFPromptStyle)style
|
||
|
{
|
||
|
if (@available(iOS 13.0, *)) {
|
||
|
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
|
||
|
} else {
|
||
|
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
|
||
|
}
|
||
|
activityIndicator.center = [self currentWindow].center;
|
||
|
|
||
|
// 设置动画颜色
|
||
|
switch (style) {
|
||
|
case TFPromptStyleLight: {
|
||
|
activityIndicator.color = [UIColor whiteColor];
|
||
|
}
|
||
|
break;
|
||
|
case TFPromptStyleDark: {
|
||
|
activityIndicator.color = [UIColor darkGrayColor];
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
// 设置动画的蒙层
|
||
|
switch (type) {
|
||
|
case TFPromptMaskTypeNone: {
|
||
|
[[self currentWindow] addSubview:activityIndicator];
|
||
|
}
|
||
|
break;
|
||
|
case TFPromptMaskTypeBlack:
|
||
|
case TFPromptMaskTypeClear: {
|
||
|
maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
|
||
|
maskView.backgroundColor = type == TFPromptMaskTypeBlack ? [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5] : [UIColor clearColor];
|
||
|
[[self currentWindow] addSubview:maskView];
|
||
|
[[self currentWindow] addSubview:activityIndicator];
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
[activityIndicator startAnimating];
|
||
|
}
|
||
|
|
||
|
+ (void)stopAnimating
|
||
|
{
|
||
|
[activityIndicator stopAnimating];
|
||
|
|
||
|
if (maskView.superview) {
|
||
|
[maskView removeFromSuperview];
|
||
|
maskView = nil;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
+ (void)hiddenAlert
|
||
|
{
|
||
|
if (_promptView.isShowing) {
|
||
|
[_promptView hiddenPromptView];
|
||
|
_promptView = nil;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
+ (UIWindow *)currentWindow
|
||
|
{
|
||
|
if (__IPHONE_13_0) {
|
||
|
return [UIApplication sharedApplication].windows.firstObject;
|
||
|
} else {
|
||
|
return [UIApplication sharedApplication].keyWindow;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static UIView *_loadingView;
|
||
|
+ (UIView *)showLoading:(UIView *)rootView
|
||
|
{
|
||
|
UIView *mainView = [[UIView alloc] init];
|
||
|
_loadingView = mainView;
|
||
|
mainView.backgroundColor = kColorRGBA(0, 0, 0, 0);
|
||
|
|
||
|
if (rootView) {
|
||
|
[rootView addSubview:mainView];
|
||
|
} else {
|
||
|
[kMainWindow addSubview:mainView];
|
||
|
}
|
||
|
|
||
|
[mainView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.edges.equalTo(mainView.superview);
|
||
|
}];
|
||
|
|
||
|
NSMutableArray<YYImage *> *arr = [NSMutableArray array];
|
||
|
for (int i = 0; i < 47; i++) {
|
||
|
YYImage *image = [YYImage imageNamed:[NSString stringWithFormat:@"%@%d", @"loading", i]];
|
||
|
image = [image imageWithColor:[[[TFReaderSettingHelper sharedManager] getReaderTextColor] colorWithAlphaComponent:0.75]];
|
||
|
[arr addObject:image];
|
||
|
}
|
||
|
|
||
|
YYAnimatedImageView *loadingView = [[YYAnimatedImageView alloc] init];
|
||
|
loadingView.backgroundColor = [UIColor clearColor];
|
||
|
loadingView.animationImages = arr;
|
||
|
loadingView.animationDuration = 2.0;
|
||
|
[loadingView startAnimating];
|
||
|
[mainView addSubview:loadingView];
|
||
|
|
||
|
[loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.center.equalTo(mainView);
|
||
|
make.width.mas_equalTo(90);
|
||
|
make.height.equalTo(loadingView.mas_width).multipliedBy(150.0 / 240.0);
|
||
|
}];
|
||
|
|
||
|
__weak typeof(loadingView) weakView = loadingView;
|
||
|
[NSTimer scheduledTimerWithTimeInterval:loadingView.animationDuration block:^(NSTimer * _Nonnull timer) {
|
||
|
|
||
|
if (!weakView.superview) {
|
||
|
[timer invalidate];
|
||
|
timer = nil;
|
||
|
}
|
||
|
NSArray *t_arr = [[loadingView.animationImages reverseObjectEnumerator] allObjects];
|
||
|
weakView.animationImages = t_arr;
|
||
|
} repeats:YES];
|
||
|
return mainView;
|
||
|
}
|
||
|
|
||
|
+ (void)hideLoading
|
||
|
{
|
||
|
[_loadingView removeFromSuperview];
|
||
|
}
|
||
|
@end
|