205 lines
8.0 KiB
Objective-C
Executable File
205 lines
8.0 KiB
Objective-C
Executable File
//
|
||
// SessionUtilOC.m
|
||
// LegendTeam
|
||
//
|
||
// Created by 梁博 on 20/12/21.
|
||
//
|
||
|
||
#import "SessionUtilOC.h"
|
||
|
||
@implementation SessionUtilOC
|
||
|
||
+ (BOOL)shouldReloadWhenInsert:(NSArray<NSIndexPath *> *)indexPaths tableView:(UITableView *)tableView {
|
||
// 如果插入数据后,中间有空档,则不能直接插入,需要全量重新加载
|
||
NSMutableDictionary * sectionCurrentCount = [NSMutableDictionary dictionary];
|
||
NSMutableDictionary * sectionMaxCount = [NSMutableDictionary dictionary];
|
||
NSMutableDictionary * sectionInsertingCount = [NSMutableDictionary dictionary];
|
||
|
||
for(NSIndexPath * indexPath in indexPaths)
|
||
{
|
||
NSInteger section = indexPath.section;
|
||
NSInteger count = [tableView numberOfRowsInSection:section];
|
||
sectionCurrentCount[@(section)] = @(count);
|
||
}
|
||
|
||
for(NSIndexPath * indexPath in indexPaths)
|
||
{
|
||
NSInteger section = indexPath.section;
|
||
NSInteger row = indexPath.row;
|
||
NSInteger count = [sectionCurrentCount[@(section)] integerValue];
|
||
NSInteger sectionMaxNum = [sectionMaxCount[@(section)] integerValue];
|
||
NSInteger max = 0;
|
||
if (row <= count)
|
||
{
|
||
sectionCurrentCount[@(section)] = @(count+1);
|
||
max = count + 1;
|
||
}
|
||
else
|
||
{
|
||
max = row + 1;
|
||
}
|
||
max = MAX(max, sectionMaxNum);
|
||
sectionMaxCount[@(section)] = @(max);
|
||
|
||
NSInteger sectionCurrentCount = [sectionInsertingCount[@(section)] integerValue];
|
||
sectionInsertingCount[@(section)] = @(++ sectionCurrentCount);
|
||
}
|
||
|
||
for(NSNumber * sectionKey in sectionMaxCount.allKeys)
|
||
{
|
||
NSInteger maxCount = [sectionMaxCount[sectionKey] integerValue];
|
||
NSInteger currentCount = [sectionInsertingCount[sectionKey] integerValue];
|
||
NSInteger section = [sectionKey integerValue];
|
||
NSInteger count = [tableView numberOfRowsInSection:section];
|
||
if (maxCount > count + currentCount)
|
||
{
|
||
return YES;
|
||
}
|
||
}
|
||
|
||
return NO;
|
||
}
|
||
|
||
|
||
+ (NSArray<NSIndexPath *> *)getIndexPathsWith:(NSArray <NSNumber *>*)indexs {
|
||
NSMutableArray *addIndexPathes = [NSMutableArray array];
|
||
[indexs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[obj integerValue] inSection:0];
|
||
[addIndexPathes addObject:indexPath];
|
||
}];
|
||
return [addIndexPathes copy];
|
||
}
|
||
|
||
+ (CGSize)sizeWithImageOriginSize:(CGSize)originSize
|
||
minSize:(CGSize)imageMinSize
|
||
maxSize:(CGSize)imageMaxSiz{
|
||
|
||
NSInteger imageWidth = originSize.width ,imageHeight = originSize.height;
|
||
imageWidth = imageWidth == 0 ? 1 : imageWidth;
|
||
imageHeight = imageHeight == 0 ? 1 : imageHeight;
|
||
NSInteger imageMinWidth = imageMinSize.width, imageMinHeight = imageMinSize.height;
|
||
NSInteger imageMaxWidth = imageMaxSiz.width, imageMaxHeight = imageMaxSiz.height;
|
||
|
||
CGFloat newWidth = imageWidth;
|
||
CGFloat newHeight = imageHeight;
|
||
|
||
if (imageWidth > imageHeight) //宽图
|
||
{
|
||
if (imageWidth > imageMaxWidth) {
|
||
// 宽度大于最大宽度时, 取最大宽度 100 * 50 60
|
||
newWidth = imageMaxWidth;
|
||
// 根据宽度比例获取高度
|
||
newHeight = imageHeight * imageMaxWidth / imageWidth;
|
||
} else if (imageWidth < imageMinWidth) {
|
||
// 宽度小于于最小宽度时, 取最小宽度 20 * 10 30
|
||
newWidth = imageMinWidth;
|
||
// 根据宽度比例获取高度
|
||
newHeight = imageHeight * imageMinWidth / imageWidth;
|
||
}
|
||
}
|
||
else if(imageWidth < imageHeight)//高图
|
||
{
|
||
if (imageHeight > imageMaxHeight) {
|
||
// 高度大于最大高度时, 取最大高度 50 * 100 60
|
||
newHeight = imageMaxHeight;
|
||
newWidth = imageWidth * imageMaxHeight / imageHeight;
|
||
} else if (imageHeight < imageMinHeight) {
|
||
// 高度小于最小高度时, 取最大高度 10 * 20 30
|
||
newHeight = imageMinHeight;
|
||
newWidth = imageWidth * imageMinHeight / imageHeight;
|
||
}
|
||
}
|
||
|
||
|
||
// 控制宽度最大最小显示逻辑
|
||
if (newWidth > imageMaxWidth) {
|
||
newWidth = imageMaxWidth;
|
||
}
|
||
if (newWidth < imageMinWidth) {
|
||
newWidth = imageMinWidth;
|
||
}
|
||
|
||
// 控制高度最大最小显示逻辑
|
||
if (newHeight > imageMaxHeight) {
|
||
newHeight = imageMaxHeight;
|
||
}
|
||
if (newHeight < imageMinHeight) {
|
||
newHeight = imageMinHeight;
|
||
}
|
||
|
||
return CGSizeMake(newWidth, newHeight);
|
||
}
|
||
|
||
|
||
|
||
+ (NSString*)showTime:(NSTimeInterval)msglastTime showDetail:(BOOL)showDetail {
|
||
//今天的时间
|
||
NSDate *nowDate = [NSDate date];
|
||
NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
|
||
NSTimeInterval nowTimeInterval = [nowDate timeIntervalSince1970];
|
||
|
||
NSString *result = nil;
|
||
NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
|
||
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
|
||
NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
|
||
|
||
double OnedayTimeIntervalValue = 24*60*60; //一天的秒数
|
||
// NSInteger hour = msgDateComponents.hour;
|
||
// if (hour > 12)
|
||
// {
|
||
// hour = hour - 12;
|
||
// }
|
||
BOOL isSameYer = nowDateComponents.year == msgDateComponents.year;
|
||
BOOL isSameMonth = isSameYer && (nowDateComponents.month == msgDateComponents.month);
|
||
BOOL isSameDay = isSameYer && isSameMonth && (nowDateComponents.day == msgDateComponents.day);
|
||
// 如果要求强制显示AM、需要通过日历控件自己计算
|
||
NSInteger reduceInterval = nowTimeInterval - msglastTime;
|
||
if (showDetail) {
|
||
if (reduceInterval < OnedayTimeIntervalValue) {
|
||
// 24小时内 显示 XX以前
|
||
result =[NSString stringWithFormat:@"%@", [self stringFromDate:msgDate format:@"h:mm aa"]];
|
||
} else if (isSameMonth && (nowDateComponents.day == (msgDateComponents.day + 1))) {
|
||
// 昨天
|
||
result =[NSString stringWithFormat:@"Yesterday %@", [self stringFromDate:msgDate format:@"h:mm aa"]];
|
||
} else if (isSameYer) {
|
||
// 当年内
|
||
result =[NSString stringWithFormat:@"%@", [self stringFromDate:msgDate format:@"MMM d, h:mm aa"]];
|
||
} else {
|
||
result =[NSString stringWithFormat:@"%@", [self stringFromDate:msgDate format:@"MMM d, yyyy h:mm aa"]];
|
||
}
|
||
} else {
|
||
if (isSameDay) {
|
||
// 今天
|
||
result =[NSString stringWithFormat:@"%@", [self stringFromDate:msgDate format:@"h:mm aa"]];
|
||
} else if (isSameMonth && (nowDateComponents.day == (msgDateComponents.day + 1))) {
|
||
// 昨天
|
||
result =[NSString stringWithFormat:@"Yesterday"];
|
||
} else if (isSameYer) {
|
||
// 当年内
|
||
result =[NSString stringWithFormat:@"%@", [self stringFromDate:msgDate format:@"MMM d"]];
|
||
} else {
|
||
result =[NSString stringWithFormat:@"%@", [self stringFromDate:msgDate format:@"MMM d, yyyy"]];
|
||
}
|
||
}
|
||
|
||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||
[formatter setDateFormat:@"hh:mm aa"];
|
||
NSDate *tempDate = [formatter dateFromString:@"16:20"];
|
||
// 24小时制,需要处理一下显示
|
||
if (tempDate && ([result containsString:@"AM"] || [result containsString:@"PM"])) {
|
||
NSString *string1 = [result stringByReplacingOccurrencesOfString:@"AM" withString:@""];
|
||
NSString *string2 = [string1 stringByReplacingOccurrencesOfString:@"PM" withString:@""];
|
||
result = string2;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
+ (NSString *)stringFromDate:(NSDate *)date format:(NSString *)format {
|
||
NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
|
||
[fomatter setTimeZone:[NSTimeZone localTimeZone]];
|
||
[fomatter setDateFormat:format];
|
||
return [fomatter stringFromDate:date];
|
||
}
|
||
|
||
@end
|