245 lines
8.7 KiB
Objective-C
245 lines
8.7 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];
|
|
}
|
|
|
|
+(NSDate *)impostaFiltroWithTime:(EQNFiltriMappa )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 unAnno:
|
|
[comps setYear:[comps year] - 1];
|
|
break;
|
|
case unMese:
|
|
[comps setMonth:[comps month] - 1];
|
|
break;
|
|
case unaSettimana:
|
|
[comps setDay:[comps day] - 7];
|
|
break;
|
|
case unGiorno:
|
|
[comps setDay:[comps day] - 1];
|
|
break;
|
|
case dodiciOre:
|
|
[comps setHour:[comps hour] - 10];
|
|
break;
|
|
case dueOre:
|
|
[comps setHour:[comps hour] - 2];
|
|
break;
|
|
case unOra:
|
|
[comps setHour:[comps hour] - 1];
|
|
break;
|
|
case dieciMinuti:
|
|
[comps setMinute:[comps minute] - 10];
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
return [gregorian dateFromComponents:comps];
|
|
|
|
}
|
|
|
|
+(NSArray *)applicaFiltroWithTime:(EQNFiltriMappa )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;
|
|
}
|
|
|
|
+(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;
|
|
|
|
}
|
|
|
|
+ (GADBannerView *)ottieniBannerWithController:(UIViewController *)controller position:(BOOL)isButton
|
|
{
|
|
if ([EQNPurchaseUtility isProVersionEnabled]) {
|
|
return nil;
|
|
}
|
|
|
|
GADBannerView *bannerView = [[GADBannerView alloc]
|
|
initWithAdSize:kGADAdSizeBanner];
|
|
bannerView.adUnitID = EQN_ADMOB_ANNUNCIO_IDA;
|
|
bannerView.rootViewController = controller;
|
|
bannerView.backgroundColor = [UIColor whiteColor];
|
|
[bannerView loadRequest:[GADRequest request]];
|
|
[self addBanner:bannerView toController:controller isTopAnchored:isButton];
|
|
|
|
return bannerView;
|
|
}
|
|
|
|
+ (void)addBanner:(UIView *)bannerView toController:(UIViewController *)controller isTopAnchored:(BOOL)isButton
|
|
{
|
|
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
|
|
[controller.view addSubview:bannerView];
|
|
|
|
[[bannerView.centerXAnchor constraintEqualToAnchor:controller.view.centerXAnchor] setActive:YES];
|
|
NSLayoutAnchor *parentAnchor = isButton ? controller.view.safeAreaLayoutGuide.bottomAnchor : controller.view.safeAreaLayoutGuide.topAnchor;
|
|
NSLayoutAnchor *bannerAnchor = isButton ? bannerView.bottomAnchor : bannerView.topAnchor;
|
|
[[bannerAnchor constraintEqualToAnchor:parentAnchor] setActive:YES];
|
|
}
|
|
|
|
#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;
|
|
}
|
|
|
|
@end
|