Visual_Novel_iOS/crush/Crush/Src/Modules/Chat/Util/SessionUtilOC.m

205 lines
8.0 KiB
Mathematica
Raw Normal View History

2025-10-09 10:29:35 +00:00
//
// 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