KSBaseViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // KSBaseViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/17.
  6. //
  7. #import "KSBaseViewController.h"
  8. #import "Reachability.h"
  9. #import <UIButton+EnlargeEdge.h>
  10. #import <UIView+Hints.h>
  11. #import "LoginViewController.h"
  12. #import "CustomNavViewController.h"
  13. #import <DiskFreeSpaceManager.h>
  14. #import "KSTabBarViewController.h"
  15. #import "KSPublicAlertView.h"
  16. #define PROMPT_TIME 1.5f
  17. @interface KSBaseViewController ()
  18. @property (nonatomic, strong) UIView *promptPlaceView;
  19. @property (nonatomic, strong) KSPublicAlertView *checkAlert;
  20. @end
  21. @implementation KSBaseViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. self.view.backgroundColor = HexRGB(0xf6f8f9);
  26. [[self.navigationController navigationBar] setBarTintColor:[UIColor whiteColor]];
  27. [[self.navigationController navigationBar] setTranslucent:NO];
  28. [self defaultMessage]; // 设置默认刷新数据
  29. _scrollView = [[UIScrollView alloc] init];
  30. [self.view addSubview:_scrollView];
  31. [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.left.right.mas_equalTo(self.view);
  33. make.top.mas_equalTo(self.view.mas_top);
  34. make.bottom.mas_equalTo(self.view.mas_bottom);
  35. }];
  36. _scrollView.backgroundColor = HexRGB(0xf6f8f9);
  37. _scrollView.showsHorizontalScrollIndicator = NO;
  38. _scrollView.showsVerticalScrollIndicator = NO;
  39. if (@available(iOS 11.0, *)) {
  40. self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  41. } else {
  42. // Fallback on earlier versions
  43. self.automaticallyAdjustsScrollViewInsets = NO;
  44. }
  45. if (self.navigationController && self != self.navigationController.viewControllers[0]) {
  46. if (_hideBackButton == NO) {
  47. [self leftbutton];
  48. }
  49. }
  50. }
  51. - (BOOL)networkAvaiable {
  52. return [self checkNetworkAvaiable];
  53. }
  54. - (void)defaultMessage {
  55. self.pages = 1;
  56. self.rows = 10;
  57. self.isLoadMore = YES;
  58. }
  59. - (void)selectBarHomeWithIndex:(NSInteger)index {
  60. self.navigationController.tabBarController.hidesBottomBarWhenPushed = NO;
  61. [(KSTabBarViewController *)self.tabBarController tabBarSelectedWithIndex:index];
  62. }
  63. - (void)freeSizeCheck {
  64. CGFloat freeSize = [DiskFreeSpaceManager QueryDiskSpaceFree];
  65. if (freeSize < 0.8) {
  66. [self showFreeSizeHardTips];
  67. }
  68. }
  69. - (void)showFreeSizeHardTips {
  70. MJWeakSelf;
  71. self.checkAlert = [KSPublicAlertView shareInstanceWithTitle:@"提示" descMessage:@"可用存储空间不足,可能会影响您的正常使用。您可以在设置-通用-储存空间中管理储存空间" leftTitle:@"取消" rightTitle:@"设置" cancelAction:^{
  72. } sureAction:^{
  73. [weakSelf openSettingView];
  74. }];
  75. }
  76. - (void)addfeedBackGenertor {
  77. if (@available(iOS 10.0, *)) {
  78. UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
  79. [feedBackGenertor impactOccurred];
  80. } else {
  81. // Fallback on earlier versions
  82. }
  83. }
  84. - (void)openSettingView {
  85. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  86. }
  87. - (void)allocTitle:(NSString *)titleStr {
  88. UILabel *navText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
  89. navText.backgroundColor = [UIColor clearColor];
  90. navText.textAlignment = NSTextAlignmentCenter;
  91. navText.font = [UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium];
  92. navText.textColor = HexRGB(0x000000);
  93. navText.text = titleStr;
  94. self.navigationItem.titleView = navText;
  95. }
  96. - (void)allocTitle:(NSString *)Ntitle withColor:(UIColor *)color {
  97. UILabel *natext= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
  98. natext.textAlignment = NSTextAlignmentCenter;
  99. natext.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium];
  100. natext.textColor = color;
  101. natext.text = Ntitle;
  102. self.navigationItem.titleView = natext;
  103. }
  104. - (void)leftbutton {
  105. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_black"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
  106. leftButton.imageInsets = UIEdgeInsetsMake(0, -5, 0, 0);
  107. leftButton.tintColor = [UIColor blackColor];
  108. self.navigationItem.leftBarButtonItem = leftButton;
  109. }
  110. - (void)leftButtonWithImageName:(NSString *)imageName {
  111. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
  112. leftButton.imageInsets = UIEdgeInsetsMake(0, -5, 0, 0);
  113. leftButton.tintColor = HexRGB(0x000000);
  114. self.navigationItem.leftBarButtonItem = leftButton;
  115. }
  116. // 判断当前是否Wi-Fi环境
  117. - (BOOL)isWiFiEnable {
  118. BOOL isWiFi = NO;
  119. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  120. switch ([reach currentReachabilityStatus]) {
  121. case NotReachable:
  122. isWiFi = NO;
  123. //NSLog(@"notReachable");
  124. break;
  125. case ReachableViaWiFi:
  126. isWiFi = YES;
  127. //NSLog(@"WIFI");
  128. break;
  129. case ReachableViaWWAN:
  130. isWiFi = NO;
  131. //NSLog(@"3G");
  132. break;
  133. }
  134. return isWiFi;
  135. }
  136. - (void)backAction {
  137. [self.view endEditing:YES];
  138. // 根据需要返回到不同页面
  139. [self.navigationController popViewControllerAnimated:YES];
  140. }
  141. -(void)rightButton:(UIImage*)image {
  142. UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
  143. rightBt.frame =CGRectMake(0, 0, 80, 40);
  144. rightBt.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -60);
  145. [rightBt setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
  146. rightBt.titleLabel.font = [UIFont systemFontOfSize:16];
  147. [rightBt setTitleColor:HexRGB(0x000000) forState:UIControlStateNormal];
  148. [rightBt setImage:image forState:UIControlStateNormal];
  149. [rightBt addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
  150. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBt];
  151. self.navigationItem.rightBarButtonItem = rightItem;
  152. }
  153. - (void)rightButtonTitle:(NSString *)title {
  154. [self rightButtonTitle:title color:HexRGB(0x000000)];
  155. }
  156. - (void)rightButtonTitle:(NSString *)title color:(UIColor *)titleColor {
  157. UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
  158. rightBt.frame =CGRectMake(0, 0, 80, 40);
  159. rightBt.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -30);
  160. [rightBt setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
  161. rightBt.titleLabel.font = [UIFont systemFontOfSize:16];
  162. [rightBt setTitleColor:titleColor forState:UIControlStateNormal];
  163. [rightBt setTitle:title forState:UIControlStateNormal];
  164. [rightBt addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
  165. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBt];
  166. self.navigationItem.rightBarButtonItem = rightItem;
  167. }
  168. //结束页面编辑
  169. - (void)endViewEditing {
  170. [self.view endEditing:YES];
  171. }
  172. - (void)rightBtnClick{
  173. }
  174. -(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
  175. UIImage *newImage = nil;
  176. CGSize imageSize = sourceImage.size;
  177. CGFloat width = imageSize.width;
  178. CGFloat height = imageSize.height;
  179. CGFloat targetWidth = size.width;
  180. CGFloat targetHeight = size.height;
  181. CGFloat scaleFactor = 0.0;
  182. CGFloat scaledWidth = targetWidth;
  183. CGFloat scaledHeight = targetHeight;
  184. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  185. if (CGSizeEqualToSize(imageSize, size) == NO)
  186. {
  187. CGFloat widthFactor = targetWidth / width;
  188. CGFloat heightFactor = targetHeight / height;
  189. if (widthFactor > heightFactor)
  190. scaleFactor = widthFactor; // scale to fit height
  191. else
  192. scaleFactor = heightFactor; // scale to fit width
  193. scaledWidth= width * scaleFactor;
  194. scaledHeight = height * scaleFactor;
  195. // center the image
  196. if (widthFactor > heightFactor)
  197. {
  198. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  199. }
  200. else if (widthFactor < heightFactor)
  201. {
  202. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  203. }
  204. }
  205. UIGraphicsBeginImageContext(size); // this will crop
  206. CGRect thumbnailRect = CGRectZero;
  207. thumbnailRect.origin = thumbnailPoint;
  208. thumbnailRect.size.width= scaledWidth;
  209. thumbnailRect.size.height = scaledHeight;
  210. [sourceImage drawInRect:thumbnailRect];
  211. newImage = UIGraphicsGetImageFromCurrentImageContext();
  212. if(newImage == nil)
  213. NSLog(@"could not scale image");
  214. //pop the context to get back to the default
  215. UIGraphicsEndImageContext();
  216. return newImage;
  217. }
  218. #pragma mark ---- 网络连接状态判断
  219. -(BOOL) isConnectionAvailable{
  220. BOOL isExistenceNetwork = YES;
  221. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  222. switch ([reach currentReachabilityStatus]) {
  223. case NotReachable:
  224. isExistenceNetwork = NO;
  225. //NSLog(@"notReachable");
  226. break;
  227. case ReachableViaWiFi:
  228. isExistenceNetwork = YES;
  229. //NSLog(@"WIFI");
  230. break;
  231. case ReachableViaWWAN:
  232. isExistenceNetwork = YES;
  233. //NSLog(@"3G");
  234. break;
  235. }
  236. if (!isExistenceNetwork) {
  237. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"当前没有网络,请先连接网络"];
  238. return NO;
  239. }
  240. return isExistenceNetwork;
  241. }
  242. - (BOOL)checkNetworkAvaiable {
  243. BOOL isExistenceNetwork = YES;
  244. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  245. switch ([reach currentReachabilityStatus]) {
  246. case NotReachable:
  247. isExistenceNetwork = NO;
  248. //NSLog(@"notReachable");
  249. break;
  250. case ReachableViaWiFi:
  251. isExistenceNetwork = YES;
  252. //NSLog(@"WIFI");
  253. break;
  254. case ReachableViaWWAN:
  255. isExistenceNetwork = YES;
  256. //NSLog(@"3G");
  257. break;
  258. }
  259. return isExistenceNetwork;
  260. }
  261. //打电话
  262. - (void)CallPhoneWith:(NSString*)phoneNumber {
  263. phoneNumber = [phoneNumber replaceAll:@" " WithString:@""];
  264. NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];
  265. //方法一
  266. [[UIApplication sharedApplication] openURL:phoneURL options:@{} completionHandler:nil]; //拨号
  267. }
  268. #pragma mark --- 无数据 显示
  269. /**
  270. 设置没有数据时的显示
  271. @param promptString 提示语
  272. @param imgName 图片名称
  273. @param view 显示在什么地方
  274. */
  275. - (void)setPromptString:(NSString *)promptString imageName:(NSString *)imgName inView:(UIView *)view {
  276. if (self.promptView != nil) {
  277. [self.promptView removeFromSuperview];
  278. }
  279. else {
  280. self.promptView = [[StateView alloc]init];
  281. }
  282. _promptPlaceView = view;
  283. //当请求不到数据时 ,自定义提示view 将会出现;
  284. self.promptView.imageName = imgName;
  285. self.promptView.alpha = 0.0f;
  286. [self.promptView setText:promptString];
  287. [view addSubview:self.promptView];
  288. [self.promptView mas_makeConstraints:^(MASConstraintMaker *make) {
  289. make.edges.mas_equalTo(view);
  290. make.width.mas_equalTo(view);
  291. make.height.mas_equalTo(view);
  292. }];
  293. }
  294. - (KSStatusView *)setActionPromptString:(NSString *)promptString buttonTitlle:(NSString *)buttonTitle imageName:(NSString *)imgName inView:(UIView *)view actionBlock:(void (^)(void))actionBlock {
  295. if (self.actionStatusView != nil) {
  296. [self.actionStatusView removeFromSuperview];
  297. }
  298. else {
  299. self.actionStatusView = [[KSStatusView alloc]init];
  300. }
  301. self.actionStatusView.imageName = imgName;
  302. self.actionStatusView.alpha = 1.0f;
  303. [self.actionStatusView setText:promptString];
  304. [self.actionStatusView setButtonTitle:buttonTitle];
  305. [self.actionStatusView buttonActionCallback:^{
  306. actionBlock();
  307. }];
  308. [view addSubview:self.actionStatusView];
  309. [self.actionStatusView mas_makeConstraints:^(MASConstraintMaker *make) {
  310. make.edges.mas_equalTo(view);
  311. make.width.mas_equalTo(view);
  312. make.height.mas_equalTo(view);
  313. }];
  314. return self.actionStatusView;
  315. }
  316. - (void)changeActionPromptLabelState {
  317. NSInteger count;
  318. if (self.dataArray.count) {
  319. count = self.dataArray.count;
  320. } else {
  321. count = 0;
  322. }
  323. [UIView animateWithDuration:0.1 animations:^{
  324. [[self actionStatusView] setAlpha:count ? 0.0f :1.0f ] ;
  325. }] ;
  326. }
  327. // 结束刷新后调用方法
  328. - (void)changePromptLabelState {
  329. NSInteger count;
  330. if (self.dataArray.count) {
  331. count = self.dataArray.count;
  332. } else {
  333. count = 0;
  334. }
  335. [UIView animateWithDuration:0.1 animations:^{
  336. [[self promptView] setAlpha:count ? 0.0f :1.0f ] ;
  337. }] ;
  338. }
  339. - (void)resetParamenter {
  340. self.pages = 1;
  341. self.isLoadMore = YES;
  342. self.dataArray = [NSMutableArray array];
  343. }
  344. #pragma 检测 和跳转登录界面
  345. - (BOOL)checkIsLoginToLoginView:(BOOL)toLogin {
  346. NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:TokenKey];
  347. if (token.length == 0 || token == nil) { // 未登录
  348. _isLogin = NO;
  349. if (toLogin) { // 需要去登录页面
  350. }
  351. }
  352. else { // 登录状态
  353. _isLogin = YES;
  354. }
  355. return _isLogin;
  356. }
  357. // 首页去登录页面
  358. - (void)showLoginView {
  359. LoginViewController *logonVC = [[LoginViewController alloc] init];
  360. CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:logonVC];
  361. // logonVC.homeLogin = YES;
  362. navCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
  363. [self presentViewController:navCtrl animated:YES completion:nil];
  364. }
  365. #pragma mark -- lazying
  366. - (NSMutableArray *)dataArray{
  367. if (!_dataArray) {
  368. _dataArray =[NSMutableArray array];
  369. }
  370. return _dataArray;
  371. }
  372. // 移除通知
  373. - (void)removeNotifier {
  374. [[NSNotificationCenter defaultCenter] removeObserver:self];
  375. }
  376. /// 获取上一个视图控制器
  377. - (KSBaseViewController *)getPreViewController {
  378. NSInteger currentIndex = [self.navigationController.viewControllers indexOfObject:self];
  379. if (currentIndex > 0 && currentIndex < self.navigationController.viewControllers.count && currentIndex != NSNotFound) {
  380. // 获取上一个视图控制器
  381. return [self.navigationController.viewControllers objectAtIndex:currentIndex - 1];
  382. }
  383. return nil;
  384. }
  385. /// 获取下一个视图控制器
  386. - (KSBaseViewController *)getNextViewController {
  387. NSArray *viewControllers = self.navigationController.viewControllers;//获取当前的视图控制其
  388. if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) { //当前视图控制器在栈中,故为push操作
  389. return [self.navigationController.viewControllers objectAtIndex:viewControllers.count - 1];
  390. }
  391. else {
  392. return [self getPreViewController];
  393. }
  394. }
  395. // viewWillDisappear 使用
  396. - (BOOL)isViewPopDismiss {
  397. if (self.is_poppingToRoot) {
  398. return YES;
  399. }
  400. NSArray *viewControllers = self.navigationController.viewControllers;//获取当前的视图控制其
  401. if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) { //当前视图控制器在栈中,故为push操作
  402. return NO;
  403. } else if ([viewControllers indexOfObject:self] == NSNotFound) { //当前视图控制器不在栈中,故为pop操作
  404. return YES;
  405. }
  406. return NO;
  407. }
  408. - (void)viewDidAppear:(BOOL)animated {
  409. [super viewDidAppear:animated];
  410. self.preCtrl = [self getPreViewController];
  411. }
  412. - (void)configStatusViewColorWhite:(BOOL)isWhite {
  413. if (isWhite) {
  414. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  415. }
  416. else {
  417. if (@available(iOS 13.0, *)) {
  418. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDarkContent;
  419. } else {
  420. // Fallback on earlier versions
  421. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  422. }
  423. }
  424. }
  425. /*
  426. #pragma mark - Navigation
  427. // In a storyboard-based application, you will often want to do a little preparation before navigation
  428. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  429. // Get the new view controller using [segue destinationViewController].
  430. // Pass the selected object to the new view controller.
  431. }
  432. */
  433. @end