// // EPMomentViewController.m // YuMi // // Created by AI on 2025-10-09. // Copyright © 2025 YuMi. All rights reserved. // #import "EPMomentViewController.h" #import #import #import "EPMomentCell.h" #import "EPMomentListView.h" #import "EPMomentPublishViewController.h" #import "YUMIMacroUitls.h" @interface EPMomentViewController () // MARK: - UI Components /// 列表视图(MVVM:View) @property (nonatomic, strong) EPMomentListView *listView; /// 顶部图标 @property (nonatomic, strong) UIImageView *topIconImageView; /// 顶部固定文案 @property (nonatomic, strong) UILabel *topTipLabel; @end @implementation EPMomentViewController // MARK: - Lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Enjoy your Life Time"; // 设置 title 为白色 [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName: [UIColor whiteColor] }]; [self setupUI]; [self.listView reloadFirstPage]; // 监听发布成功通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMomentPublishSuccess:) name:EPMomentPublishSuccessNotification object:nil]; // ✅ 新增:冷启动时延迟检查数据,如果没有数据则自动刷新一次 [self scheduleAutoRefreshIfNeeded]; NSLog(@"[EPMomentViewController] 页面加载完成"); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } // MARK: - Setup UI - (void)setupUI { UIImageView *bgImageView = [[UIImageView alloc] initWithImage:kImage(@"vc_bg")]; bgImageView.contentMode = UIViewContentModeScaleAspectFill; bgImageView.clipsToBounds = YES; [self.view addSubview:bgImageView]; [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); }]; // 顶部图标 [self.view addSubview:self.topIconImageView]; [self.topIconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view); make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(14); make.size.mas_equalTo(CGSizeMake(56, 41)); }]; // 顶部固定文案 [self.view addSubview:self.topTipLabel]; [self.topTipLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.topIconImageView.mas_bottom).offset(14); make.leading.trailing.equalTo(self.view).inset(20); }]; // 列表视图 [self.view addSubview:self.listView]; [self.listView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.bottom.equalTo(self.view); make.top.equalTo(self.topTipLabel.mas_bottom).offset(8); }]; // 右上角发布按钮 UIImage *addIcon = [UIImage imageNamed:@"icon_moment_add"]; UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom]; publishButton.contentMode = UIViewContentModeScaleAspectFit; [publishButton setImage:addIcon forState:UIControlStateNormal]; publishButton.frame = CGRectMake(0, 0, 40, 40); [publishButton addTarget:self action:@selector(onPublishButtonTapped) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *publishItem = [[UIBarButtonItem alloc] initWithCustomView:publishButton]; self.navigationItem.rightBarButtonItem = publishItem; NSLog(@"[EPMomentViewController] UI 设置完成"); } // 不再在 VC 内部直接发请求/维护分页 // MARK: - Auto Refresh /// 延迟检查数据,如果没有数据则自动刷新(解决冷启动数据未加载问题) - (void)scheduleAutoRefreshIfNeeded { __weak typeof(self) weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ __strong typeof(weakSelf) self = weakSelf; if (!self) return; // 检查是否有数据 if (self.listView.rawList.count == 0) { NSLog(@"[EPMomentViewController] ⚠️ 冷启动 1 秒后检测到无数据,自动刷新一次"); [self.listView reloadFirstPage]; } else { NSLog(@"[EPMomentViewController] ✅ 冷启动 1 秒后检测到已有 %lu 条数据,无需刷新", (unsigned long)self.listView.rawList.count); } }); } // MARK: - Actions - (void)onPublishButtonTapped { NSLog(@"[EPMomentViewController] 发布按钮点击"); EPMomentPublishViewController *vc = [[EPMomentPublishViewController alloc] init]; vc.modalPresentationStyle = UIModalPresentationFullScreen; [self.navigationController presentViewController:vc animated:YES completion:nil]; } - (void)showAlertWithMessage:(NSString *)message { UIAlertController *alert = [UIAlertController alertControllerWithTitle:YMLocalizedString(@"common.tips") message:message preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:YMLocalizedString(@"common.confirm") style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alert animated:YES completion:nil]; } - (void)onMomentPublishSuccess:(NSNotification *)notification { NSLog(@"[EPMomentViewController] 收到发布成功通知,刷新列表"); [self.listView reloadFirstPage]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } // 列表点击回调由 listView 暴露 // MARK: - Lazy Loading - (EPMomentListView *)listView { if (!_listView) { _listView = [[EPMomentListView alloc] initWithFrame:CGRectZero]; __weak typeof(self) weakSelf = self; _listView.onSelectMoment = ^(NSInteger index) { __strong typeof(weakSelf) self = weakSelf; // [self showAlertWithMessage:[NSString stringWithFormat:YMLocalizedString(@"moment.item_clicked"), (long)index]]; }; } return _listView; } - (UIImageView *)topIconImageView { if (!_topIconImageView) { _topIconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_moment_Volume"]]; _topIconImageView.contentMode = UIViewContentModeScaleAspectFit; } return _topIconImageView; } - (UILabel *)topTipLabel { if (!_topTipLabel) { _topTipLabel = [UILabel new]; _topTipLabel.numberOfLines = 0; _topTipLabel.textColor = [UIColor whiteColor]; _topTipLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular]; _topTipLabel.text = @"In the quiet gallery of the heart, we learn to see the colors of emotion. And in the shared silence between souls, we begin to find the sound of resonance. This is more than an app—it's a space where your inner world is both a masterpiece and a melody."; } return _topTipLabel; } // 无数据源属性 @end