337 lines
13 KiB
Objective-C
337 lines
13 KiB
Objective-C
//
|
|
// XPSessionMainViewController.m
|
|
// xplan-ios
|
|
//
|
|
// Created by duoban on 2022/10/12.
|
|
//
|
|
|
|
#import "XPSessionMainViewController.h"
|
|
///Tool
|
|
#import <JXPagingView/JXPagerView.h>
|
|
#import <JXPagingView/JXPagerListRefreshView.h>
|
|
#import <JXCategoryView/JXCategoryView.h>
|
|
#import "QEmotionHelper.h"
|
|
///vc
|
|
#import "XPMineAttentionViewController.h"
|
|
#import "SessionListViewController.h"
|
|
#import "XPMineFriendViewController.h"
|
|
#import "XPMineFansViewController.h"
|
|
#import "MessagePresenter.h"
|
|
#import "MessageProtocol.h"
|
|
|
|
@interface XPSessionMainViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
|
|
|
|
@property (nonatomic, strong) NSArray<NSString *> *titles;
|
|
@property (nonatomic, strong) UIPageViewController *pageViewController;
|
|
@property (nonatomic, strong) NSArray<UIViewController *> *viewControllers;
|
|
@property (nonatomic, assign) NSInteger currentIndex;
|
|
@property (nonatomic, strong) UIStackView *buttonStackView;
|
|
@property (nonatomic, strong) NSMutableArray<UIButton *> *segmentButtons;
|
|
@property (nonatomic, strong) UIScrollView *scrollView; // 添加 scrollView 属性
|
|
|
|
@property (nonatomic,strong) SessionListViewController *sessionListVC;
|
|
///好友
|
|
@property (nonatomic,strong) XPMineFriendViewController *friendVC;
|
|
///关注
|
|
@property (nonatomic,strong) XPMineAttentionViewController *attentionVC;
|
|
///粉丝
|
|
@property (nonatomic,strong) XPMineFansViewController *fansVC;
|
|
///清除已读
|
|
@property (nonatomic,strong) UIButton * allCleanBtn;
|
|
|
|
@end
|
|
|
|
@implementation XPSessionMainViewController
|
|
- (MessagePresenter *)createPresenter {
|
|
return [[MessagePresenter alloc] init];
|
|
}
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
[self setupTopTheme];
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
}
|
|
- (BOOL)isHiddenNavBar {
|
|
return YES;
|
|
}
|
|
-(void)dealloc{
|
|
[[NSNotificationCenter defaultCenter]removeObserver:self];
|
|
}
|
|
|
|
#pragma mark - Private Method
|
|
- (void)initSubViews {
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
selector:@selector(getTabBarItemBadge:)
|
|
name:@"kGetTabBarItemBadge"
|
|
object:nil];
|
|
|
|
self.view.backgroundColor = [[ClientConfig shareConfig] bgColor];
|
|
|
|
// 初始化分页控制器
|
|
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
|
|
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
|
|
options:nil];
|
|
self.pageViewController.dataSource = self;
|
|
self.pageViewController.delegate = self;
|
|
[self addChildViewController:self.pageViewController];
|
|
[self.view addSubview:self.pageViewController.view];
|
|
[self.pageViewController didMoveToParentViewController:self];
|
|
|
|
// 初始化顶部按钮
|
|
[self setupTopButtons];
|
|
|
|
// 设置初始页面
|
|
self.viewControllers = @[self.sessionListVC, self.friendVC, self.attentionVC, self.fansVC];
|
|
[self.pageViewController setViewControllers:@[self.viewControllers[0]]
|
|
direction:UIPageViewControllerNavigationDirectionForward
|
|
animated:NO
|
|
completion:nil];
|
|
}
|
|
- (void)initSubViewConstraints {
|
|
UIView *lineView = [UIView new];
|
|
lineView.backgroundColor = UIColorFromRGB(0xF5F6FA);
|
|
[self.view addSubview:lineView];
|
|
[lineView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.bottom.leading.trailing.equalTo(self.view);
|
|
make.height.mas_equalTo(1);
|
|
}];
|
|
|
|
// 设置页面控制器约束
|
|
[self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.equalTo(self.buttonStackView.mas_bottom).offset(10);
|
|
make.left.right.bottom.equalTo(self.view);
|
|
}];
|
|
}
|
|
|
|
- (void)setupTopButtons {
|
|
self.segmentButtons = [NSMutableArray array];
|
|
self.buttonStackView = [[UIStackView alloc] init];
|
|
self.buttonStackView.axis = UILayoutConstraintAxisHorizontal;
|
|
self.buttonStackView.spacing = (isMSZH() || isMSZH()) ? 16 : 4;
|
|
self.buttonStackView.distribution = UIStackViewDistributionFill;//UIStackViewDistributionFillEqually;
|
|
self.buttonStackView.semanticContentAttribute = isMSRTL() ? UISemanticContentAttributeForceRightToLeft : UISemanticContentAttributeForceLeftToRight;
|
|
|
|
|
|
if (isMSZH() || isMSRTL()) {
|
|
[self.view addSubview:self.buttonStackView];
|
|
// 设置stackView约束
|
|
[self.buttonStackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo([UIApplication sharedApplication].keyWindow.safeAreaInsets.top);
|
|
make.leading.mas_equalTo(12);
|
|
}];
|
|
} else {
|
|
// 创建滚动视图
|
|
self.scrollView = [[UIScrollView alloc] init];
|
|
self.scrollView.showsHorizontalScrollIndicator = NO;
|
|
self.scrollView.showsVerticalScrollIndicator = NO;
|
|
self.scrollView.bounces = NO;
|
|
[self.view addSubview:self.scrollView];
|
|
// 设置scrollView约束
|
|
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.mas_equalTo([UIApplication sharedApplication].keyWindow.safeAreaInsets.top);
|
|
make.height.mas_equalTo(44); // 设置一个固定高度
|
|
make.leading.mas_equalTo(12);
|
|
make.trailing.mas_equalTo(-40);
|
|
}];
|
|
[self.scrollView addSubview:self.buttonStackView];
|
|
// 设置stackView约束
|
|
[self.buttonStackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self.scrollView);
|
|
make.height.equalTo(self.scrollView);
|
|
}];
|
|
}
|
|
|
|
for (NSInteger i = 0; i < self.titles.count; i++) {
|
|
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[button setTitle:self.titles[i] forState:UIControlStateNormal];
|
|
[button setTitleColor:UIColorFromRGB(0x313131) forState:UIControlStateNormal];
|
|
[button setTitleColor:UIColorFromRGB(0x313131) forState:UIControlStateSelected];
|
|
button.titleLabel.font = kFontRegular(16);
|
|
button.tag = i;
|
|
[button addTarget:self action:@selector(segmentButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
|
|
[self.segmentButtons addObject:button];
|
|
[self.buttonStackView addArrangedSubview:button];
|
|
}
|
|
|
|
// 设置初始选中状态
|
|
[self updateSegmentButtonSelection:0];
|
|
|
|
// 设置清除按钮
|
|
[self.view addSubview:self.allCleanBtn];
|
|
[self.allCleanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.height.mas_equalTo(30);
|
|
make.centerY.mas_equalTo(self.buttonStackView);
|
|
make.trailing.mas_equalTo(-8);
|
|
}];
|
|
}
|
|
|
|
- (void)segmentButtonTapped:(UIButton *)sender {
|
|
NSInteger index = sender.tag;
|
|
if (index == self.currentIndex) return;
|
|
|
|
UIViewController *targetVC = self.viewControllers[index];
|
|
UIPageViewControllerNavigationDirection direction = index > self.currentIndex ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
|
|
|
|
[self.pageViewController setViewControllers:@[targetVC]
|
|
direction:direction
|
|
animated:YES
|
|
completion:nil];
|
|
|
|
[self updateSegmentButtonSelection:index];
|
|
}
|
|
|
|
- (void)updateSegmentButtonSelection:(NSInteger)index {
|
|
self.currentIndex = index;
|
|
[self.segmentButtons enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL *stop) {
|
|
button.selected = (idx == index);
|
|
button.titleLabel.font = button.selected ? kFontSemibold(20) : kFontRegular(16);
|
|
}];
|
|
// 获取选中按钮
|
|
UIButton *selectedButton = self.segmentButtons[index];
|
|
CGRect buttonFrame = selectedButton.frame;
|
|
CGFloat scrollViewWidth = self.scrollView.bounds.size.width;
|
|
CGFloat currentOffset = self.scrollView.contentOffset.x;
|
|
|
|
// 计算按钮的可见区域
|
|
CGFloat buttonLeftEdge = buttonFrame.origin.x;
|
|
CGFloat buttonRightEdge = buttonFrame.origin.x + buttonFrame.size.width;
|
|
CGFloat visibleLeftEdge = currentOffset;
|
|
CGFloat visibleRightEdge = currentOffset + scrollViewWidth;
|
|
|
|
// 只有当按钮不完全可见时才滚动
|
|
// if (buttonLeftEdge < visibleLeftEdge) {
|
|
// // 如果按钮被左边遮挡,滚动到让按钮左对齐
|
|
// [self.scrollView setContentOffset:CGPointMake(buttonLeftEdge, 0) animated:YES];
|
|
// } else if (buttonRightEdge > visibleRightEdge) {
|
|
// // 如果按钮被右边遮挡,滚动最小距离使按钮完全可见
|
|
// CGFloat newOffset = buttonRightEdge - scrollViewWidth;
|
|
// [self.scrollView setContentOffset:CGPointMake(newOffset, 0) animated:YES];
|
|
// }
|
|
|
|
|
|
if (isMSRTL()) {
|
|
// RTL 布局时的滚动逻辑
|
|
if (buttonRightEdge > visibleRightEdge) {
|
|
// 如果按钮被右边遮挡,滚动到让按钮右对齐
|
|
[self.scrollView setContentOffset:CGPointMake(buttonRightEdge - scrollViewWidth, 0) animated:YES];
|
|
} else if (buttonLeftEdge < visibleLeftEdge) {
|
|
// 如果按钮被左边遮挡,滚动最小距离使按钮完全可见
|
|
[self.scrollView setContentOffset:CGPointMake(buttonLeftEdge, 0) animated:YES];
|
|
}
|
|
} else {
|
|
// LTR 布局时的滚动逻辑(保持原来的逻辑)
|
|
if (buttonLeftEdge < visibleLeftEdge) {
|
|
[self.scrollView setContentOffset:CGPointMake(buttonLeftEdge, 0) animated:YES];
|
|
} else if (buttonRightEdge > visibleRightEdge) {
|
|
CGFloat newOffset = buttonRightEdge - scrollViewWidth;
|
|
[self.scrollView setContentOffset:CGPointMake(newOffset, 0) animated:YES];
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark - SessionListViewController通知
|
|
-(void)getTabBarItemBadge:(NSNotification *)not{
|
|
NSInteger badgeValue = [not.userInfo[@"BadgeValue"] integerValue];
|
|
if (badgeValue > 0) {
|
|
[self.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%ld", badgeValue]];
|
|
} else {
|
|
[self.tabBarItem setBadgeValue:nil];
|
|
}
|
|
}
|
|
|
|
- (NSArray<NSString *> *)titles {
|
|
if (!_titles) {
|
|
_titles = @[YMLocalizedString(@"XPSessionMainViewController0"),
|
|
YMLocalizedString(@"XPSessionMainViewController1"),
|
|
YMLocalizedString(@"XPSessionMainViewController2"),
|
|
YMLocalizedString(@"XPSessionMainViewController3")];
|
|
}
|
|
return _titles;
|
|
}
|
|
#pragma mark- 清除消息
|
|
- (void)allReadButtonClick:(UIButton *)sender {
|
|
[[NSNotificationCenter defaultCenter]postNotificationName:@"kAllReadAction" object:nil];
|
|
}
|
|
|
|
-(NSTimeInterval)curTime {
|
|
static double time = 0;
|
|
time += 0.1 ;
|
|
return time;
|
|
}
|
|
|
|
#pragma mark - UIPageViewControllerDataSource
|
|
|
|
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
|
|
NSInteger index = [self.viewControllers indexOfObject:viewController];
|
|
if (index == 0 || index == NSNotFound) {
|
|
return nil;
|
|
}
|
|
return self.viewControllers[index - 1];
|
|
}
|
|
|
|
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
|
|
NSInteger index = [self.viewControllers indexOfObject:viewController];
|
|
if (index == self.viewControllers.count - 1 || index == NSNotFound) {
|
|
return nil;
|
|
}
|
|
return self.viewControllers[index + 1];
|
|
}
|
|
|
|
#pragma mark - UIPageViewControllerDelegate
|
|
|
|
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
|
|
if (completed) {
|
|
UIViewController *currentVC = pageViewController.viewControllers.firstObject;
|
|
NSInteger index = [self.viewControllers indexOfObject:currentVC];
|
|
[self updateSegmentButtonSelection:index];
|
|
}
|
|
}
|
|
|
|
#pragma mark -懒加载
|
|
- (XPMineAttentionViewController *)attentionVC {
|
|
if (!_attentionVC) {
|
|
_attentionVC = [[XPMineAttentionViewController alloc] init];
|
|
_attentionVC.type = ContactUseingType_In_Session;
|
|
_attentionVC.isClearColor = YES;
|
|
}
|
|
return _attentionVC;
|
|
}
|
|
|
|
- (XPMineFriendViewController *)friendVC {
|
|
if (!_friendVC) {
|
|
_friendVC = [[XPMineFriendViewController alloc] init];
|
|
_friendVC.isClearColor = YES;
|
|
_friendVC.type = ContactUseingType_In_Session;
|
|
|
|
}
|
|
return _friendVC;
|
|
}
|
|
|
|
- (XPMineFansViewController *)fansVC {
|
|
if (!_fansVC) {
|
|
_fansVC = [[XPMineFansViewController alloc] init];
|
|
_fansVC.type = ContactUseingType_In_Session;
|
|
_fansVC.isClearColor = YES;
|
|
|
|
}
|
|
return _fansVC;
|
|
}
|
|
- (SessionListViewController *)sessionListVC{
|
|
if (!_sessionListVC){
|
|
_sessionListVC = [[SessionListViewController alloc]init];
|
|
}
|
|
return _sessionListVC;
|
|
}
|
|
- (UIButton *)allCleanBtn{
|
|
if (!_allCleanBtn){
|
|
_allCleanBtn = [UIButton new];
|
|
[_allCleanBtn setCornerWithLeftTopCorner:10 rightTopCorner:10 bottomLeftCorner:10 bottomRightCorner:10 size:CGSizeMake(30, 30)];
|
|
[_allCleanBtn setImage:[UIImage imageNamed:@"sessionList_clear"] forState:UIControlStateNormal];
|
|
[_allCleanBtn addTarget:self action:@selector(allReadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _allCleanBtn;
|
|
}
|
|
|
|
@end
|