KSBaseViewController.m 16 KB

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