239 lines
8.8 KiB
Objective-C
239 lines
8.8 KiB
Objective-C
//
|
|
// EQNUtility.m
|
|
// Earthquake Network
|
|
//
|
|
// Created by Luca Beretta on 02/11/18.
|
|
// Copyright © 2018 Luca Beretta. All rights reserved.
|
|
//
|
|
|
|
#import "EQNUtility.h"
|
|
#import "EQNSegnalazione.h"
|
|
#import "EQNSegnalazione.h"
|
|
#import "EQNPastquakes.h"
|
|
#import "EQNSisma.h"
|
|
|
|
@implementation EQNUtility
|
|
|
|
+ (NSDate *)getDateFromString:(NSString *)dateString
|
|
{
|
|
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
|
formatter.timeZone = [NSTimeZone timeZoneWithName:@"Europe/Rome"];
|
|
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
|
|
return [formatter dateFromString:dateString];
|
|
}
|
|
|
|
+ (NSString *)formattedStringForTimeDifference:(NSInteger)timeDifference
|
|
{
|
|
NSString *format = @"";
|
|
NSInteger finalValue = timeDifference;
|
|
|
|
if (timeDifference == 1) {
|
|
format = NSLocalizedString(@"minutes_one", comment: nil);
|
|
} else if (timeDifference < 60) {
|
|
format = NSLocalizedString(@"minutes_other", comment: nil);
|
|
} else if (timeDifference > 60 && timeDifference < 120) {
|
|
finalValue = timeDifference / 60.0;
|
|
format = NSLocalizedString(@"hours_one", comment: nil);
|
|
} else if (timeDifference > 60 && timeDifference < 1440) {
|
|
finalValue = timeDifference / 60.0;
|
|
format = NSLocalizedString(@"hours_other", comment: nil);
|
|
} else if (timeDifference > 1400 && timeDifference < 2800) {
|
|
finalValue = timeDifference / 1400.0;
|
|
format = NSLocalizedString(@"days_one", comment: nil);
|
|
} else {
|
|
finalValue = timeDifference / 1400.0;
|
|
format = NSLocalizedString(@"days_other", comment: nil);
|
|
}
|
|
|
|
return [NSString stringWithFormat:format, (long)finalValue];
|
|
}
|
|
|
|
+ (NSArray *)applicaFiltroWithTime:(EQNFiltroMappa )filtro withList:(NSArray *)list
|
|
{
|
|
NSDate *date = [self impostaFiltroWithTime:filtro];
|
|
NSMutableArray *temp = [NSMutableArray array];
|
|
|
|
for (id object in list) {
|
|
if ([object isKindOfClass:[EQNPastquakes class]]) {
|
|
EQNPastquakes *anPasq = (EQNPastquakes *)object;
|
|
if ([anPasq.date compare:date] == NSOrderedDescending) {
|
|
[temp addObject:anPasq];
|
|
NSLog(@"\n\naggiunto %@ filtro %@", anPasq.date, date);
|
|
} else {
|
|
NSLog(@"\n\nNon aggiunto %@ filtro %@", anPasq.date, date);
|
|
}
|
|
} else if ([object isKindOfClass:[EQNSegnalazione class]]){
|
|
EQNSegnalazione *anSegn = (EQNSegnalazione *)object;
|
|
if ([anSegn.date compare:date] == NSOrderedDescending) {
|
|
[temp addObject:anSegn];
|
|
NSLog(@"\n\naggiunto %@ filtro %@", anSegn.date, date);
|
|
} else {
|
|
NSLog(@"\n\nNon aggiunto %@ filtro %@", anSegn.date, date);
|
|
}
|
|
}
|
|
}
|
|
|
|
NSLog(@"filtrati %lu tutti %lu", (unsigned long)temp.count, (unsigned long)list.count);
|
|
return temp;
|
|
}
|
|
|
|
+ (NSDate *)impostaFiltroWithTime:(EQNFiltroMappa)filtro
|
|
{
|
|
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
|
|
|
|
NSDate *now = [NSDate date];
|
|
NSCalendar *gregorian = [NSCalendar currentCalendar];
|
|
NSDateComponents *comps = [gregorian components:unitFlags fromDate:now];
|
|
switch (filtro) {
|
|
case EQNFiltroMappaUnAnno:
|
|
[comps setYear:[comps year] - 1];
|
|
break;
|
|
case EQNFiltroMappaUnMese:
|
|
[comps setMonth:[comps month] - 1];
|
|
break;
|
|
case EQNFiltroMappaUnaSettimana:
|
|
[comps setDay:[comps day] - 7];
|
|
break;
|
|
case EQNFiltroMappaUnGiorno:
|
|
[comps setDay:[comps day] - 1];
|
|
break;
|
|
case EQNFiltroMappaDodiciOre:
|
|
[comps setHour:[comps hour] - 10];
|
|
break;
|
|
case EQNFiltroMappaDueOre:
|
|
[comps setHour:[comps hour] - 2];
|
|
break;
|
|
case EQNFiltroMappaUnOra:
|
|
[comps setHour:[comps hour] - 1];
|
|
break;
|
|
case EQNFiltroMappaDieciMinuti:
|
|
[comps setMinute:[comps minute] - 10];
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
return [gregorian dateFromComponents:comps];
|
|
}
|
|
|
|
+ (NSString *)clearStringMessaggi:(NSString *)messaggio
|
|
{
|
|
NSString *clearString = [messaggio stringByReplacingOccurrencesOfString:@" " withString:@""];
|
|
clearString = [clearString stringByReplacingOccurrencesOfString:@"[" withString:@""];
|
|
clearString = [clearString stringByReplacingOccurrencesOfString:@"]" withString:@""];
|
|
clearString = [clearString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
|
|
clearString = [clearString stringByReplacingOccurrencesOfString:@"<" withString:@""];
|
|
clearString = [clearString stringByReplacingOccurrencesOfString:@">" withString:@""];
|
|
clearString = [clearString stringByReplacingOccurrencesOfString:@"/" withString:@""];
|
|
clearString = [clearString stringByReplacingOccurrencesOfString:@"span" withString:@""];
|
|
return clearString;
|
|
}
|
|
|
|
+ (NSInteger)getDifferenceMinute:(NSDate *)date
|
|
{
|
|
NSDate *now = [NSDate date];
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
|
|
NSDateComponents *components = [calendar components:NSCalendarUnitMinute
|
|
fromDate:date
|
|
toDate:now
|
|
options:0];
|
|
|
|
return components.minute;
|
|
}
|
|
|
|
#pragma mark - Store/load data
|
|
|
|
+ (void)storeArray:(NSArray *)array toUserDefaultForKey:(NSString *)keyName
|
|
{
|
|
[self storeRootObject:array toUserDefaultForKey:keyName];
|
|
}
|
|
|
|
+ (void)storeDictionary:(NSDictionary *)dictionary toUserDefaultForKey:(NSString *)keyName
|
|
{
|
|
[self storeRootObject:dictionary toUserDefaultForKey:keyName];
|
|
}
|
|
|
|
+ (void)storeRootObject:(id)rootObject toUserDefaultForKey:(NSString *)keyName
|
|
{
|
|
NSError *error;
|
|
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rootObject requiringSecureCoding:NO error:nil];
|
|
if (error) {
|
|
NSLog(@"[EQNUtility] Error when storing object for key '%@' (error: %@)", keyName, error.localizedDescription);
|
|
return;
|
|
}
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
[defaults setObject:data forKey:keyName];
|
|
}
|
|
|
|
+ (NSArray *)loadArrayOfClass:(Class)class fromUserDefaultsForKey:(NSString *)keyName
|
|
{
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
NSData *data = [defaults objectForKey:keyName];
|
|
|
|
NSError *error;
|
|
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error];
|
|
unarchiver.requiresSecureCoding = NO;
|
|
NSArray *array = [unarchiver decodeObjectOfClass:class forKey:NSKeyedArchiveRootObjectKey];
|
|
|
|
if (error) {
|
|
NSLog(@"[EQNUtility] Unable to unarchive object for key '%@' (error: %@)", keyName, error.localizedDescription);
|
|
return nil;
|
|
}
|
|
return array;
|
|
}
|
|
|
|
+ (NSDictionary *)loadDictionaryFromUserDefaultsForKey:(NSString *)keyName
|
|
{
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
NSData *data = [defaults objectForKey:keyName];
|
|
|
|
NSError *error;
|
|
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error];
|
|
unarchiver.requiresSecureCoding = NO;
|
|
NSDictionary *dictionary = [unarchiver decodeObjectOfClass:[NSDictionary class] forKey:NSKeyedArchiveRootObjectKey];
|
|
|
|
if (error) {
|
|
NSLog(@"[EQNUtility] Unable to unarchive object for key '%@' (error: %@)", keyName, error.localizedDescription);
|
|
return nil;
|
|
}
|
|
return dictionary;
|
|
}
|
|
|
|
#pragma mark - Notifications
|
|
|
|
+ (nullable NSDate *)calculateUserSeismicTimestampFromUserInfo:(NSDictionary *)info
|
|
{
|
|
// dobbiamo calcolare l'ora in cui il sisma arriverà nella posizione dell'utente
|
|
// per fare questo, il calcolo sarà:
|
|
// timestamp sisma + warning time
|
|
|
|
// ultima posizione nota dell'utente
|
|
CLLocationManager *manager = [[CLLocationManager alloc] init];
|
|
CLLocation *lastUserLocation = manager.location;
|
|
if (!lastUserLocation) {
|
|
return nil;
|
|
}
|
|
|
|
// posizione sisma
|
|
double latitude = [info[@"latitude"] doubleValue];
|
|
double longitude = [info[@"longitude"] doubleValue];
|
|
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
|
|
|
|
// distanza tra sisma e utente (in km)
|
|
CLLocationDistance distance = [lastUserLocation distanceFromLocation:location];
|
|
double userDistance = distance/1000;
|
|
|
|
// calcoliamo warning_time
|
|
double waveSpeed = [info[@"wave_speed"] doubleValue];
|
|
double warningTime = round(userDistance/waveSpeed - 5);
|
|
|
|
// aggiungiamo il warning time al timestamp del sisma
|
|
NSDate *seismicDate = [EQNUtility getDateFromString:info[@"datetime"]];
|
|
NSDate *userSeismicDate = [seismicDate dateByAddingTimeInterval:warningTime];
|
|
|
|
return userSeismicDate;
|
|
}
|
|
|
|
@end
|