|
| 1 | +// |
| 2 | +// LLHtmlConfigViewController.m |
| 3 | +// LLDebugToolDemo |
| 4 | +// |
| 5 | +// Created by admin10000 on 2019/10/11. |
| 6 | +// Copyright © 2019 li. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "LLHtmlConfigViewController.h" |
| 10 | +#import "LLTitleCellCategoryModel.h" |
| 11 | +#import "LLSettingManager.h" |
| 12 | +#import "LLFactory.h" |
| 13 | +#import "LLMacros.h" |
| 14 | +#import "LLConst.h" |
| 15 | +#import "UIView+LL_Utils.h" |
| 16 | +#import "LLThemeManager.h" |
| 17 | +#import "LLToastUtils.h" |
| 18 | +#import "LLHtmlViewController.h" |
| 19 | +#import "LLHtmlUIWebViewController.h" |
| 20 | +#import "LLHtmlWkWebViewController.h" |
| 21 | +#import "LLConfig.h" |
| 22 | +#import <WebKit/WebKit.h> |
| 23 | + |
| 24 | +@interface LLHtmlConfigViewController () <UITextFieldDelegate> |
| 25 | + |
| 26 | +@property (nonatomic, copy) NSString *webViewClass; |
| 27 | + |
| 28 | +@property (nonatomic, strong) UIView *headerView; |
| 29 | + |
| 30 | +@property (nonatomic, strong) UITextField *headerTextField; |
| 31 | + |
| 32 | +@end |
| 33 | + |
| 34 | +@implementation LLHtmlConfigViewController |
| 35 | + |
| 36 | +#pragma mark - Life cycle |
| 37 | +- (void)viewDidLoad { |
| 38 | + [super viewDidLoad]; |
| 39 | + [self setUpUI]; |
| 40 | + [self loadData]; |
| 41 | +} |
| 42 | + |
| 43 | +- (void)viewWillDisappear:(BOOL)animated { |
| 44 | + if ([self.headerTextField isFirstResponder]) { |
| 45 | + [self.headerTextField resignFirstResponder]; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#pragma mark - Over write |
| 50 | +- (void)rightItemClick:(UIButton *)sender { |
| 51 | + NSString *urlString = [self currentUrlString]; |
| 52 | + if (!urlString) { |
| 53 | + [[LLToastUtils shared] toastMessage:@"Empty URL"]; |
| 54 | + return; |
| 55 | + } |
| 56 | + if (![urlString.lowercaseString hasPrefix:@"https://"] && ![urlString.lowercaseString hasPrefix:@"http://"]) { |
| 57 | + [[LLToastUtils shared] toastMessage:@"URL must has prefix with https:// or http://"]; |
| 58 | + return; |
| 59 | + } |
| 60 | + Class cls = NSClassFromString(self.webViewClass); |
| 61 | +#pragma clang diagnostic push |
| 62 | +#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 63 | + if (cls != [UIWebView class] && cls != [WKWebView class]) { |
| 64 | +#pragma clang diagnostic pop |
| 65 | + [[LLToastUtils shared] toastMessage:@"Invalid webView class"]; |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + [LLSettingManager shared].lastWebViewUrl = urlString; |
| 70 | + |
| 71 | + LLHtmlViewController *vc = nil; |
| 72 | +#pragma clang diagnostic push |
| 73 | +#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 74 | + if (cls == [UIWebView class]) { |
| 75 | +#pragma clang diagnostic pop |
| 76 | + vc = [[LLHtmlUIWebViewController alloc] init]; |
| 77 | + } else { |
| 78 | + vc = [[LLHtmlWkWebViewController alloc] init]; |
| 79 | + } |
| 80 | + vc.webViewClass = self.webViewClass; |
| 81 | + vc.urlString = [self currentUrlString]; |
| 82 | + [self.navigationController pushViewController:vc animated:YES]; |
| 83 | +} |
| 84 | + |
| 85 | +#pragma mark - UITextFieldDelegate |
| 86 | +- (BOOL)textFieldShouldReturn:(UITextField *)textField { |
| 87 | + [textField resignFirstResponder]; |
| 88 | + return YES; |
| 89 | +} |
| 90 | + |
| 91 | +#pragma mark - Primary |
| 92 | +- (void)setUpUI { |
| 93 | + self.title = @"WebView Config"; |
| 94 | + [self initNavigationItemWithTitle:@"Go" imageName:nil isLeft:NO]; |
| 95 | + |
| 96 | + self.webViewClass = [LLSettingManager shared].webViewClass ?: NSStringFromClass([WKWebView class]); |
| 97 | + |
| 98 | + self.tableView.tableHeaderView = self.headerView; |
| 99 | +} |
| 100 | + |
| 101 | +- (void)loadData { |
| 102 | + NSMutableArray *settings = [[NSMutableArray alloc] init]; |
| 103 | + |
| 104 | + // Short Cut |
| 105 | + [settings addObject:[self getWebViewStyleModel]]; |
| 106 | + LLTitleCellCategoryModel *category0 = [[LLTitleCellCategoryModel alloc] initWithTitle:nil items:settings]; |
| 107 | + [settings removeAllObjects]; |
| 108 | + |
| 109 | + [self.dataArray removeAllObjects]; |
| 110 | + [self.dataArray addObjectsFromArray:@[category0]]; |
| 111 | + [self.tableView reloadData]; |
| 112 | +} |
| 113 | + |
| 114 | +- (LLTitleCellModel *)getWebViewStyleModel { |
| 115 | + LLTitleCellModel *model = [[LLTitleCellModel alloc] initWithTitle:@"Style" detailTitleSelector:self.webViewClass]; |
| 116 | + __weak typeof(self) weakSelf = self; |
| 117 | + model.block = ^{ |
| 118 | + [weakSelf showWebViewClassAlert]; |
| 119 | + }; |
| 120 | + return model; |
| 121 | +} |
| 122 | + |
| 123 | +- (void)showWebViewClassAlert { |
| 124 | + __block NSMutableArray *actions = [[NSMutableArray alloc] init]; |
| 125 | + [actions addObject:NSStringFromClass([WKWebView class])]; |
| 126 | +#pragma clang diagnostic push |
| 127 | +#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 128 | + [actions addObject:NSStringFromClass([UIWebView class])]; |
| 129 | +#pragma clang diagnostic pop |
| 130 | + __weak typeof(self) weakSelf = self; |
| 131 | + [self showActionSheetWithTitle:@"Web View Style" actions:actions currentAction:self.webViewClass completion:^(NSInteger index) { |
| 132 | + [weakSelf setNewWebViewClass:actions[index]]; |
| 133 | + }]; |
| 134 | +} |
| 135 | + |
| 136 | +- (void)setNewWebViewClass:(NSString *)aClass { |
| 137 | + self.webViewClass = aClass; |
| 138 | + [LLSettingManager shared].webViewClass = aClass; |
| 139 | + [self loadData]; |
| 140 | +} |
| 141 | + |
| 142 | +- (NSString *)currentUrlString { |
| 143 | + NSString *text = self.headerTextField.text; |
| 144 | + text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
| 145 | + if (!text || text.length == 0) { |
| 146 | + return nil; |
| 147 | + } |
| 148 | + return text; |
| 149 | +} |
| 150 | + |
| 151 | +#pragma mark - Getters and setters |
| 152 | +- (UIView *)headerView { |
| 153 | + if (!_headerView) { |
| 154 | + _headerView = [LLFactory getView]; |
| 155 | + _headerView.frame = CGRectMake(0, 0, LL_SCREEN_WIDTH, 60); |
| 156 | + [_headerView addSubview:self.headerTextField]; |
| 157 | + self.headerTextField.frame = CGRectMake(kLLGeneralMargin, kLLGeneralMargin, _headerView.LL_width - kLLGeneralMargin * 2, _headerView.LL_height - kLLGeneralMargin * 2); |
| 158 | + } |
| 159 | + return _headerView; |
| 160 | +} |
| 161 | + |
| 162 | +- (UITextField *)headerTextField { |
| 163 | + if (!_headerTextField) { |
| 164 | + _headerTextField = [LLFactory getTextField]; |
| 165 | + _headerTextField.tintColor = [LLThemeManager shared].primaryColor; |
| 166 | + _headerTextField.backgroundColor = [LLThemeManager shared].backgroundColor; |
| 167 | + [_headerTextField LL_setBorderColor:[LLThemeManager shared].primaryColor borderWidth:1]; |
| 168 | + [_headerTextField LL_setCornerRadius:5]; |
| 169 | + _headerTextField.font = [UIFont systemFontOfSize:14]; |
| 170 | + _headerTextField.textColor = [LLThemeManager shared].primaryColor; |
| 171 | + _headerTextField.delegate = self; |
| 172 | + _headerTextField.clearButtonMode = UITextFieldViewModeWhileEditing; |
| 173 | + _headerTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Pleace input url" attributes:@{NSForegroundColorAttributeName : [LLThemeManager shared].placeHolderColor}]; |
| 174 | + _headerTextField.text = [LLSettingManager shared].lastWebViewUrl ?: ([LLConfig shared].defaultHtmlUrl ?: @"https://"); |
| 175 | + UIView *leftView = [LLFactory getView]; |
| 176 | + leftView.frame = CGRectMake(0, 0, 10, 1); |
| 177 | + _headerTextField.leftView = leftView; |
| 178 | + _headerTextField.leftViewMode = UITextFieldViewModeAlways; |
| 179 | + } |
| 180 | + return _headerTextField; |
| 181 | +} |
| 182 | + |
| 183 | +@end |
0 commit comments