cdts/xdts-ios 3/TreeHole/Code/AppSetup.m

459 lines
20 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// AppSetup.m
// AppScaffold
//
// Created by on 2020/3/14.
// Copyright © 2020 ko1o. All rights reserved.
//
// App
#import "AppSetup.h"
#import "AppDelegate.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import "MTCommonListTable.h"
#import <MJRefresh/MJRefresh.h>
#import "MTCacheManager.h"
#import <CYLTabBarController/CYLTabBarController.h>
#import "MsgListViewController.h"
#import "LaunchViewController.h"
#import <TUILogin.h>
#import <TUIConfig.h>
#import <TUIMessageCellLayout.h>
#import <TUITextMessageCellData.h>
#import <UMCommon/UMCommon.h>
//#import <UMShare/UMShare.h>
//#import "ProfileViewController.h"
#import "BottleViewController.h"
#import <WechatOpenSDK/WXApi.h>
#import <Toast.h>
#import "AlertInputView.h"
#import "BottleNerbyViewController.h"
#import "BottleBaseViewController.h"
#import "MineViewController.h"
#import "InviteCodeViewController.h"
#import "PhoneLoginViewController.h"
#import "THTabBarViewController.h"
//#import "THNavigationController.h"
#import "THHomeViewController.h"
#import "THLanternAnimationController.h"
#import "THNearAndLanternViewController.h"
#import "THDemoMineViewController.h"
@implementation AppSetup
+ (void)initSetupWithWindow:(UIWindow *)window
{
if (!window) {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[(AppDelegate *)[UIApplication sharedApplication].delegate setWindow:window];
}
//
[self setupIQKeyboardManager];
// hud
[self setupSVProgressHUD];
// MMKV
[self setupMMKV];
//
[self setupMJRefresh];
//
[self setupMTCommonListTab];
// IM UI
[self setupIMUI];
/// toast
[self setupToast];
// UM
[self setupUM];
//registerlog, log
[WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) {
NSLog(@"WeChatSDK: %@", log);
}];
//
// //
// [WXApi registerApp:WEIXIN_APP_ID
// universalLink:UNIVERSAL_LINK];
2023-07-27 09:20:00 +08:00
//// //
// [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
// NSLog(@"%@, %u, %@, %@", @(step), result.success, result.errorInfo, result.suggestion);
// }];
// window
[self setupWindowRootVCWithWindow:window isLanuch:YES];
//
[window makeKeyAndVisible];
}
#pragma mark -
+ (void)setupToast {
CSToastStyle *style = [CSToastManager sharedStyle];
style.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.7];
style.messageColor = style.titleColor = COLOR_WITH_RGB(0x040000);
[CSToastManager setSharedStyle:style];
}
+ (void)setupUM {
// // UM App Key
[UMConfigure initWithAppkey:UM_SHARE_APP_KEY channel:@"App Store"];
//
// // QQuniversalLink
// //Universal Link universalLinkDickeyrawInt UMSocialPlatformType.wechatSession.rawInt
// [UMSocialGlobal shareInstance].universalLinkDic =@{
// @(UMSocialPlatformType_WechatSession): UNIVERSAL_LINK,
// @(UMSocialPlatformType_QQ): UNIVERSAL_LINK,
// };
//
// /* appKeyappSecret */
// [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WEIXIN_APP_ID appSecret:WEIXIN_APP_SECRET redirectURL:UM_SHARE_REDIRECT_URL];
//
// /* QQappID
// * U-Share SDKappKeyappSecretQQappIDU-ShareappKey
// */
// [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQ_APP_ID/*QQappID*/ appSecret:QQ_APP_SECRET redirectURL:UM_SHARE_REDIRECT_URL];
}
+ (void)setupIMUI {
TUIConfig *config = [TUIConfig defaultConfig];
config.avatarType = TAvatarTypeRadiusCorner;
config.avatarCornerRadius = 8;
config.defaultAvatarImage = [UIImage imageNamed:@"TH_defalut_avatar"];
TUIMessageCellLayout *outgoingTextMessageLayout = [TUIMessageCellLayout outgoingTextMessageLayout];
outgoingTextMessageLayout.avatarSize = CGSizeMake(FIX_SIZE(40), FIX_SIZE(40));
UIEdgeInsets insets = outgoingTextMessageLayout.avatarInsets;
outgoingTextMessageLayout.avatarInsets = UIEdgeInsetsMake(0, FIX_SIZE(5.5), 0, FIX_SIZE(15));
TUIMessageCellLayout *outgoingVoiceMessageLayout = [TUIMessageCellLayout outgoingVoiceMessageLayout];
outgoingVoiceMessageLayout.avatarSize = outgoingTextMessageLayout.avatarSize;
outgoingVoiceMessageLayout.avatarInsets = outgoingTextMessageLayout.avatarInsets;
TUIMessageCellLayout *outgoingMessageLayout = [TUIMessageCellLayout outgoingMessageLayout];
outgoingMessageLayout.avatarSize = outgoingTextMessageLayout.avatarSize;
outgoingMessageLayout.avatarInsets = outgoingTextMessageLayout.avatarInsets;
TUIMessageCellLayout *incommingTextMessageLayout = [TUIMessageCellLayout incommingTextMessageLayout];
incommingTextMessageLayout.avatarSize = outgoingTextMessageLayout.avatarSize;
incommingTextMessageLayout.avatarInsets = UIEdgeInsetsMake(insets.top, insets.right, insets.bottom, insets.left);
TUIMessageCellLayout *incommingVoiceMessageLayout = [TUIMessageCellLayout incommingVoiceMessageLayout];
incommingVoiceMessageLayout.avatarSize = incommingTextMessageLayout.avatarSize;
incommingVoiceMessageLayout.avatarInsets = incommingTextMessageLayout.avatarInsets;
TUIMessageCellLayout *incommingMessageLayout = [TUIMessageCellLayout incommingMessageLayout];
incommingMessageLayout.avatarSize = incommingTextMessageLayout.avatarSize;
incommingMessageLayout.avatarInsets = incommingTextMessageLayout.avatarInsets;
//
[TUITextMessageCellData setOutgoingTextFont:NORMAL_FONT];
[TUITextMessageCellData setOutgoingTextColor:COLOR_WITH_RGB(0x040000)];
[TUITextMessageCellData setIncommingTextFont:NORMAL_FONT];
[TUITextMessageCellData setIncommingTextColor:UIColor.whiteColor];
}
+ (void)setupWindowRootVCWithWindow:(UIWindow *)window isLanuch:(BOOL)isLanuch
{
// window.rootViewController = [[PYNavigationViewController alloc] initWithRootViewController:[NSClassFromString(@"SetNickNameViewController") new]];
// return;
if (![LoginService token] || ![LoginService isUserSigVaild]) { // Token UserSig
//-.
[[NSUserDefaults standardUserDefaults]setObject:@{} forKey:@"UserNearbyShaiXuanParamDict"];
NSDictionary* UserNearbyShaiXuanDict = @{@"sx_age":@"全部",@"sx_gender":@"全部",@"sx_gjsx":@"全部"};
[[NSUserDefaults standardUserDefaults]setObject:UserNearbyShaiXuanDict forKey:@"UserNearbyShaiXuanDict"];
window.rootViewController = [[PYNavigationViewController alloc] initWithRootViewController:[[PhoneLoginViewController alloc] init]];
// LaunchViewController
return;
}
if (isLanuch == YES && [LoginService mainToken] != nil &&
[UserService currentUser].vip_kind <= 0 && [LoginService isClone] > 0) {
//token
[LoginService cacheToken:[LoginService mainToken]];
}
if (![LoginService isTokenVaild]) { // token token
[LoginService refreshTokenWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) {
if (error) {
[PYAppService logout];
}else{
[AppSetup autoLogin:window isLanuch:isLanuch];
}
}];
}else{
[AppSetup autoLogin:window isLanuch:isLanuch];
}
}
+ (void)autoLogin:(UIWindow *)window isLanuch:(BOOL)isLanuch
{
// IM
[TUILogin login:TIM_APPID userID:[LoginService imUserId] userSig:[LoginService userSig] succ:^{
NSLog(@"-----> 登录成功%@",[LoginService imUserId]);
} fail:^(int code, NSString *msg) {
[PYAppService logout];
[SVProgressHUD showErrorWithStatus:@"IM登录失败"];
}];
//
[UserService getUserInfoWithCompletion:^(User * _Nonnull user) {
//
[UserService loadUserOtherInfosWithUserIDs:@[@(user.ID).stringValue] userCache:YES comoletion:^(id _Nullable rsp, NSError * _Nullable error) {
}];
}];
// tab
[self setupTabViewControllerWithWindow:window];
}
+ (void)setupTabViewControllerWithWindow:(UIWindow *)window{
NSArray *childControllers = @[
// PYNavigationViewController
// [[PYNavigationViewController alloc] initWithRootViewController:[BottleViewController new]],
// [[THNavigationController alloc] initWithRootViewController:[THLanternAnimationController new]],
// [[THNavigationController alloc] initWithRootViewController:[THHomeViewController new]],
[[PYNavigationViewController alloc] initWithRootViewController:[MsgListViewController new]],
[[PYNavigationViewController alloc] initWithRootViewController:[THNearAndLanternViewController new]],
// [[PYNavigationViewController alloc] initWithRootViewController:[BottleNerbyViewController new]],
// [[PYNavigationViewController alloc] initWithRootViewController:[InviteCodeViewController new]],
// [[PYNavigationViewController alloc] initWithRootViewController:[MsgListViewController new]],
// [[PYNavigationViewController alloc] initWithRootViewController:[THDemoMineViewController new]],
[[PYNavigationViewController alloc] initWithRootViewController:[MineViewController new]],
];
NSArray *titles = @[
@"消息",
@"基地",
@"我的",
];
NSArray *iconNames = @[
@"TH_TabBarMsgNormal",
@"TH_TabBarBaseLocalNormal",
@"TH_TabBarMineNormal"
];
NSArray *iconSelectedNames = @[
@"TH_TabBarMsgSelected",
@"TH_TabBarBaseLocalSelected",
@"TH_TabBarMineSelected"
];
NSMutableArray *attrs = [NSMutableArray array];
for (int i = 0; i < titles.count; i++) {
NSMutableDictionary *attrM = [NSMutableDictionary dictionary];
attrM[CYLTabBarItemTitle] = titles[i];
attrM[CYLTabBarItemImage] = [[UIImage imageNamed:iconNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
attrM[CYLTabBarItemSelectedImage] = [[UIImage imageNamed:iconSelectedNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[attrs addObject:attrM];
}
/*
THTabBarViewController *tabBarView = [[THTabBarViewController alloc] init];
for (UINavigationController *nav in childControllers) {
[tabBarView addChildViewController:nav];
}
window.rootViewController = tabBarView;
*/
CYLTabBarController *tabVC = [CYLTabBarController tabBarControllerWithViewControllers:childControllers tabBarItemsAttributes:attrs imageInsets:UIEdgeInsetsZero titlePositionAdjustment:UIOffsetMake(0, HOME_INDICATOR_HEIHT > 0 ? 3 : 0)];
tabVC.tabBar.tintColor = THEME_COLOR;
tabVC.tabBar.unselectedItemTintColor = [UIColor whiteColor];
tabVC.tabBar.translucent = NO;
// tabVC.tabBar.backgroundColor = [COLOR_WITH_RGB(0x151032) colorWithAlphaComponent:0.8];
tabVC.tabBar.backgroundColor = [COLOR_WITH_RGB(0x000000) colorWithAlphaComponent:1];
UIView *lineView = [UIView lineViewWithWidth:SCREEN_WIDTH];
lineView.height = 0.5;
[tabVC.tabBar addSubview:lineView];
[tabVC hideTabBadgeBackgroundSeparator];
BOOL isFromChangeAccount = [window.rootViewController isKindOfClass:CYLTabBarController.class];
window.rootViewController = tabVC;
// app
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// app
[PYHTTPManager postWithPath:@"checkappupdate" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) {
NSString *type = rsp[@"update_type"];
NSString *version = rsp[@"new_version"];
NSString *content = rsp[@"update_content"];
NSString *app_urlStr = rsp[@"app_url"];
// NSLog(@"type:%@\napp_urlStr:%@",type,app_urlStr);
// NSString *type = @"force_update";
// NSString *content = @"https://sourl.cn/9KSTN6";
// NSString *app_urlStr = @"https://sourl.cn/9KSTN6";
if ([type isEqualToString:@"update_remind"]) { //
[self showAppUpdateAlertViewWithVersion:version content:content isForce:NO andDownLoadAddress:app_urlStr];
return;
}
if ([type isEqualToString:@"force_update"]) { //
[self showAppUpdateAlertViewWithVersion:version content:content isForce:YES andDownLoadAddress:app_urlStr];
return;
}
}
}];
});
});
// window.rootViewController = [[PYNavigationViewController alloc] initWithRootViewController:[NSClassFromString(@"VIPViewController") new]];
dispatch_async(dispatch_get_main_queue(), ^{
for (UIView *subView in tabVC.tabBar.subviews) {
if ([NSStringFromClass([subView class]) isEqualToString:@"_UIBarBackground"]) {
subView.hidden = YES;
}
}
if (!isFromChangeAccount) {
[tabVC setSelectedIndex:0]; // Tab
// is_security_on
if ([UserService isSecurityCodeOpen]) {
// NSLog(@" [UserService currentUser].security_code : %@",[UserService currentUser].security_code);
[AlertInputView showCheckPasswordWithTitle:@"验证密码锁" content:nil maxLength:SecurityCodeLength action:^BOOL(NSString * _Nonnull content) {
if (![content isEqualToString:[UserService currentUser].security_code]) {
[ToastUtil showToast:@"密码验证失败" position:CSToastPositionTop];
return NO;
}
return YES;
}];
}
}
});
// tabVC.selectedIndex = 1;
}
+ (void)showAppUpdateAlertViewWithVersion:(NSString *)version content:(NSString *)content isForce:(BOOL)isForce andDownLoadAddress:(NSString *)app_urlStr{
UIView *customView = [[UIView alloc] init];
customView.width = FIX_SIZE(270);
customView.height = FIX_SIZE(494);
customView.backgroundColor = COLOR_WITH_RGB(0x222436);
customView.layer.cornerRadius = FIX_SIZE(10);
customView.clipsToBounds = YES;
MTAlertView *alerView = [MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.customView = customView;
}];
UIImageView *iconView = [[UIImageView alloc] init];
iconView.width = customView.width;
iconView.height = FIX_SIZE(160);
iconView.image = ImageNamed(@"TH_app_update_img");
[customView addSubview:iconView];
UILabel *versionLabel = [[UILabel alloc] init];
versionLabel.text = version;
versionLabel.font = MT_FONT_REGULAR_SIZE(12);
versionLabel.textColor = [UIColor whiteColor];
[versionLabel sizeToFit];
versionLabel.x = FIX_SIZE(25);
versionLabel.centerY = iconView.height * 0.5;
[iconView addSubview:versionLabel];
UIButton *nextButton = [[UIButton alloc] init];
[nextButton setTitle:@"稍后更新" forState:UIControlStateNormal];
[nextButton setTitleColor:CONTENT_COLOR forState:UIControlStateNormal];
[nextButton sizeToFit];
nextButton.width += 10;
nextButton.height = FIX_SIZE(30);
nextButton.titleLabel.font = MT_FONT_REGULAR_SIZE(13);
nextButton.centerX = customView.width * 0.5;
nextButton.bottom = customView.height - FIX_SIZE(15);
nextButton.hidden = isForce;
[customView addSubview:nextButton];
PYThemeButton *button = [PYThemeButton buttonWithTitle:@"立即更新" action:^(UIButton * _Nonnull button) {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/qq/%@?mt=8", APPSTORE_APPID]] options:@{} completionHandler:^(BOOL success) {
//// NSLog(@"to App Store");
// }];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:app_urlStr] options:@{} completionHandler:^(BOOL success) {
// NSLog(@"to App Store");
}];
}];
[nextButton addTouchUpInsideWithAction:^(UIButton * _Nullable button) {
[alerView dismiss];
}];
button.width = FIX_SIZE(180);
button.centerX = customView.width * 0.5;
button.bottom = isForce ? customView.height - FIX_SIZE(20) : nextButton.y - FIX_SIZE(8);
[customView addSubview:button];
UITextView *textView = [[UITextView alloc] init];
textView.x = FIX_SIZE(20);
textView.text = content;
textView.backgroundColor = [UIColor clearColor];
textView.width = customView.width - textView.x * 2;
textView.y = iconView.bottom + FIX_SIZE(20);
textView.height = button.y - FIX_SIZE(20) - textView.y;
textView.textColor = [UIColor whiteColor];
textView.font = MT_FONT_REGULAR_SIZE(15);
[customView addSubview:textView];
}
+ (void)setupIQKeyboardManager
{
IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
keyboardManager.toolbarDoneBarButtonItemText = @"完成";
keyboardManager.toolbarNextBarButtonItemText = nil;
keyboardManager.toolbarPreviousBarButtonItemText = nil;
keyboardManager.previousNextDisplayMode = IQPreviousNextDisplayModeAlwaysHide;
}
+ (void)setupSVProgressHUD
{
//
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
[SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
[SVProgressHUD setMinimumSize:CGSizeMake(FIX_SIZE(360 * 0.5), FIX_SIZE(300 * 0.5))];
[SVProgressHUD setFont:MT_FONT_REGULAR_SIZE(16)];
[SVProgressHUD setForegroundColor:[UIColor whiteColor]];
[SVProgressHUD setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
[SVProgressHUD setCornerRadius:6];
[SVProgressHUD setImageViewSize:CGSizeMake(FIX_SIZE(90 * 0.5), FIX_SIZE(90 * 130 / 120.0 * 0.5))];
[SVProgressHUD setErrorImage:ImageNamed(@"TH_hud_error_icon")];
[SVProgressHUD setSuccessImage:ImageNamed(@"TH_hud_succeed_icon")];
[SVProgressHUD setInfoImage:ImageNamed(@"TH_hud_info_icon")];
[SVProgressHUD setMinimumDismissTimeInterval:1.0];
[SVProgressHUD setMaximumDismissTimeInterval:2.0];
[SVProgressHUD setMaxSupportedWindowLevel:CGFLOAT_MAX];
}
+ (void)setupMMKV
{
// uid
[MTCacheManager setupCacheManager:^NSString * _Nullable{
return @([UserService currentUserID]).stringValue;
}];
}
+ (void)setupMJRefresh
{
}
+ (void)setupMTCommonListTab
{
MTCommonListTableConfig *tableConfig = [MTCommonListTableConfig sharedConfig];
tableConfig.tableBackgroundColor = BG_COLOR;
tableConfig.cellBackgroudColor = [UIColor clearColor];
tableConfig.cellTitleFont = MT_FONT_MEDIUM_SIZE(14);
tableConfig.cellImageTintColor = THEME_COLOR;
tableConfig.cellTitleColor = COLOR_WITH_RGB(0xFFFFFF);
tableConfig.cellImageSize = CGSizeMake(FIX_SIZE(26), FIX_SIZE(26));
tableConfig.cellImageMarginLeft = FIX_SIZE(8);
tableConfig.cellAccessoryDisclosureIndicatorImageName = @"TH_icon_right_black";
tableConfig.cellAccessoryImageSize = CGSizeMake(FIX_SIZE(15), FIX_SIZE(15));
tableConfig.sectionHeaderHeight = FIX_SIZE(10);
tableConfig.cellSeparatorLeadingType = MTCommonListTableCellSeparatorLeadingTypeLeadingTitle;
tableConfig.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableConfig.cellAccessoryTitleFont = MT_FONT_REGULAR_SIZE(11);
tableConfig.cellAccessoryTitleColor = COLOR_WITH_RGB(0xB3B3B3);
tableConfig.cellHeight = FIX_SIZE(48);
tableConfig.cellImageMarginLeft = FIX_SIZE(15.5);
}
@end