Files
eqn.ios/Sources/Earthquake Network/Models/EQNUtility.m
T
2022-05-26 18:06:27 +02:00

179 lines
6.6 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];
}
+ (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];
if (!data) {
NSLog(@"[EQNUtility] No array saved for key '%@'", keyName);
return nil;
}
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];
if (!data) {
NSLog(@"[EQNUtility] No dictionary saved for key '%@'", keyName);
return nil;
}
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