跳到主要内容

提示

此功能在 5.12.0 版本开始支持。

好友申请列表页面

好友申请列表页面展示了当前用户设备上的所有好友申请。进入该页面,SDK 就会从数据库中拉取好友申请信息,并按照时间排序。IMKit 提供基于 UIKit UITableView 的好友页面类 RCApplyFriendListViewController

好友请求列表有效期 7 天。超过有效期需要重新发起请求,过期的请求多端不再同步。

好友申请列表页面

好友列申请表页面一般由导航栏,好友申请列表两部分部分组成。

初始化

调用 RCApplyFriendListViewController 类的初始化方法构建好友列表页面。注意,您需要创建一个 RCApplyFriendListViewModel 对象, 作为RCApplyFriendListViewController 的业务逻辑处理模块。 RCApplyFriendSectionItem 对象用于按照时间区间将好友申请信息进行分组。

- (void)showFriendApplyWithController:(UIViewController *)controller {
NSMutableArray *sections = [NSMutableArray array];
RCFriendApplyItemFilterBlock block = ^BOOL(RCApplyFriendCellViewModel *obj, NSInteger start, NSInteger end, BOOL * _Nonnull stop) {
if (obj.application.operationTime >= start && obj.application.operationTime < end) {
return YES;
}
return NO;
};

RCApplyFriendSectionItem *justNow = [[RCApplyFriendSectionItem alloc] initWithFilterBlock:block compareBlock:nil];
justNow.title = @"刚刚";
justNow.timeStart = [self startOfToday];
justNow.timeEnd = [[NSDate date]timeIntervalSince1970] * 1000;
[sections addObject:justNow];

RCApplyFriendSectionItem *oneDays = [[RCApplyFriendSectionItem alloc] initWithFilterBlock:block compareBlock:nil];
oneDays.title = @"近一天";
oneDays.timeStart = [self startTimeOfDaysBefore:-1];
oneDays.timeEnd = [self startOfToday];
[sections addObject:oneDays];

RCApplyFriendSectionItem *threeDays = [[RCApplyFriendSectionItem alloc] initWithFilterBlock:block compareBlock:nil];
threeDays.title = @"近三天";
threeDays.timeStart = [self startTimeOfDaysBefore:-4];
threeDays.timeEnd = [self startTimeOfDaysBefore:-1];
[sections addObject:threeDays];

RCApplyFriendSectionItem *longAgo = [[RCApplyFriendSectionItem alloc] initWithFilterBlock:block compareBlock:nil];
longAgo.title = @"三天之前";
longAgo.timeStart = 0;
longAgo.timeEnd = [self startTimeOfDaysBefore:-4];
[sections addObject:longAgo];

RCApplyFriendListViewModel *vm = [[RCApplyFriendListViewModel alloc] initWithSectionItems:sections option:nil types:@[] status:@[]];
RCApplyFriendListViewController *listVC = [[RCApplyFriendListViewController alloc] initWithViewModel:vm];
[self.navigationController pushViewController:listVC animated:YES];
}

- (NSInteger)startOfToday {
NSDate *now = [NSDate date];
NSDate *date = [[NSCalendar currentCalendar] startOfDayForDate:[NSDate date]];
return [date timeIntervalSince1970] * 1000;
}

- (NSInteger)timeOfDaysBefore:(NSInteger)dayDiff start:(BOOL)start {
NSDate *now = [NSDate date];
NSDate *date = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
value:dayDiff
toDate:now options:NSCalendarMatchStrictly];
NSDate *dateStart = [[NSCalendar currentCalendar] startOfDayForDate:date];

if (start) {
return [dateStart timeIntervalSince1970] * 1000;
} else {
NSDate *dateEnd = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
value:1
toDate:dateStart options:NSCalendarWrapComponents];
return [dateEnd timeIntervalSince1970] * 1000 - 1;
}
}

- (NSInteger)startTimeOfDaysBefore:(NSInteger)dayDiff {
return [self timeOfDaysBefore:dayDiff start:YES];;
}

- (NSInteger)endTimeOfDaysBefore:(NSInteger)dayDiff {
return [self timeOfDaysBefore:dayDiff start:NO];;
}
参数类型说明
viewModelRCApplyFriendListViewModelRCApplyFriendListViewController 的业务逻辑处理模块。 处理页面UI的配置以及好友信息的拉取。
itemsNSArray <RCApplyFriendSectionItem *>*好友申请信息分组model, 根据不同的时间区间对数据进行归类。
optionRCPagingQueryOption查询配置。
types NSArray<NSNumber *> *类型列表。如:@[@(RCFriendApplicationTypeSent),@(RCFriendApplicationTypeReceived)]
status NSArray<NSNumber *> *状态列表。 如: @[@(RCFriendApplicationStatusUnHandled),@(RCFriendApplicationStatusAccepted),@(RCFriendApplicationStatusRefused),@(RCFriendApplicationStatusExpired)]
提示

多个 RCApplyFriendSectionItem 传入的时间段不能交叉,否则可能出现同一数据展示多次的问题。

定制化

IMKit 好友申请列表界面的样式可供自定义修改。

标题栏

IMKit 的 RCApplyFriendListViewController 使用了系统的导航栏,可用于显示好友列表的标题,用户通过继承RCApplyFriendListViewController ,在 viewDidLoad 方法中通过 title 属性设置标题。

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"新标题";
}

自定义cell

RCApplyFriendListViewController 支持对cell自定义,通过设置 RCApplyFriendListViewModeldelegate 属性, 实现相关代理方法,修改cell的样式,以及编辑需要显示的好友信息。

1. 自定义 RCCustomCell

@interface RCCustomCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@end

@implementation
// Cell 绘制
@end

2.自定义 RCCustomCellViewModel

提示

RCApplyFriendCellViewModel 可以Cell的创建, 自定义 CellViewModel 需要继承该类

typedef void(^RCPermanentCellViewModelBlock)(UIViewController *);
@interface RCCustomCellViewModel : RCApplyFriendCellViewModel
/// 初始化
- (instancetype)initWithTitle:(NSString *)title
portrait:(UIImage *)portrait
touchBlock:(RCPermanentCellViewModelBlock)touchBlock;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIImage *portrait;
@property (nonatomic, copy) RCPermanentCellViewModelBlock touchBlock;
@end

@implementation RCCustomCellViewModel

- (instancetype)initWithTitle:(NSString *)title
portrait:(UIImage *)portrait
touchBlock:(RCPermanentCellViewModelBlock)touchBlock{
self = [super init];
if (self) {
self.title = title;
self.portrait = portrait;
self.touchBlock = touchBlock;
}
return self;
}

// 注册并自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"RCCustomCellIdentifier";
// 注册 cell
[tableView registerClass:RCCustomCell.class forCellReuseIdentifier:cellIdentifier];
// 返回 cell
RCCustomCellViewModel *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
//修改UI
cell.titleLabel.text = self.title;
cell.contentLabel.text = self.detail;
return cell;
}

@end

3. 添加代理

初始化时设置 RCApplyFriendListViewModel 代理:

RCApplyFriendListViewModel *viewModel = [[RCApplyFriendListViewModel alloc] initWithSectionItems:sections option:nil types:@[] status:@[]];
viewModel.delegate = self;

4. 修改数据源

实现代理方法:

- (NSArray <RCApplyFriendCellViewModel *> *_Nullable)applyFriendListViewModel:(RCApplyFriendListViewModel *)viewModel
willLoadItemsInDataSource:(NSArray *_Nullable)dataSource{
NSMutableArray *list = dataSource.mutableCopy;
RCCustomCellViewModel *customCellVM = [[RCCustomCellViewModel alloc] initWithTitle:@"title" portrait:[UIImage imageNamed:@"newFriend"] touchBlock:^(UIViewController * vc) {
// 点击事件

}];
[list addObject:@[customCellVM]];
return list;
}

5.点击 cell

/// 配置自定义点击事件,Returns: App是否处理[YES : SDK不再处理, NO: SDK处理]
- (BOOL)applyFriendListViewModel:(RCApplyFriendListViewModel *)viewModel
viewController:(UIViewController*)viewController
tableView:(UITableView *)tableView
didSelectRow:(NSIndexPath *)indexPath
cellViewModel:(RCApplyFriendCellViewModel *)viewModel {

}