// // 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 "EQNPastquakes.h" #import "EQNSisma.h" #if NOTIFICATION_CONTENT #import "EQNNotificationContent-Swift.h" #endif @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; // check for minutes, hours or days if (timeDifference < 60) { format = NSLocalizedString(@"manual_minutes_ago", nil); } else if (timeDifference < 1440) { finalValue = timeDifference / 60.0; format = NSLocalizedString(@"manual_hours_ago", nil); } else { finalValue = timeDifference / 1440.0; format = NSLocalizedString(@"manual_days_ago", nil); } return [NSString localizedStringWithFormat:format, 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; } #pragma mark - Store/load data + (void)storeArray:(NSArray *)array toUserDefaultForKey:(NSString *)keyName { [self storeRootObject:array 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; } #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 == nil) { lastUserLocation = EQNUserData.sharedData.lastLocation; } 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