refactor: Use proper method to store/load also dictionary

This commit is contained in:
Andrea Busi
2020-10-07 16:14:42 +02:00
parent 509b75e7b5
commit e1bd26db48
4 changed files with 39 additions and 24 deletions
+1 -1
View File
@@ -155,7 +155,7 @@
NSDate *dataRicezione = [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:dataRicezione forKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA];
[EQNUtility writeArrayWithCustomObjToUserDefaults:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA withDict:userInfo];
[EQNUtility storeDictionary:userInfo toUserDefaultForKey:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA];
section = EQNTabBarSectionAllerte;
} else if([userInfo[@"type"] isEqualToString:@"manual"]) {
section = EQNTabBarSectionSegnalazioni;
@@ -222,7 +222,7 @@ typedef NS_ENUM(NSInteger, AllerteTableRow) {
} else if (tableRow == AllerteTableRowSismiRilevati) {
if (self.isNotificaAttiva) {
AlertsSeismicNotificationExpandedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeismicNotificationExpandedCell" forIndexPath:indexPath];
NSDictionary *info = [EQNUtility readDictyWithCustomObjFromUserDefaults:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA];
NSDictionary *info = [EQNUtility loadDictionaryFromUserDefaultsForKey:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA];
cell.notification = info;
__weak AllerteViewController *weakSelf = self;
@@ -29,8 +29,6 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSString *)formattedStringForTimeDifference:(NSInteger)timeDifference;
+(NSArray *)applicaFiltroWithTime:(EQNFiltriMappa )filtro withList:(NSArray *)list;
+(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withDict:(NSDictionary *)myDict;
+(NSDictionary *)readDictyWithCustomObjFromUserDefaults:(NSString*)keyName;
+(NSString *)clearStringMessaggi:(NSString *)messaggio;
+(NSInteger )getDifferenceMinute:(NSDate *)date;
@@ -51,6 +49,15 @@ NS_ASSUME_NONNULL_BEGIN
/// @param keyName Key of NSUserDefault to retrieve
+ (nullable NSArray *)loadArrayOfClass:(Class)class fromUserDefaultsForKey:(NSString* )keyName;
/// Store a dictionary to NSUserDefaults
/// @param dictionary Dictionary to store
/// @param keyName Key of NSUserDefault to use
+ (void)storeDictionary:(NSDictionary *)dictionary toUserDefaultForKey:(NSString *)keyName;
/// Retrieve a saved dictionary from NSUserDefaults.
/// @param keyName Key of NSUserDefault to retrieve
+ (nullable NSDictionary *)loadDictionaryFromUserDefaultsForKey:(NSString *)keyName;
@end
NS_ASSUME_NONNULL_END
+28 -20
View File
@@ -126,25 +126,6 @@
return temp;
}
+(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withDict:(NSDictionary *)myDict
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myDict requiringSecureCoding:NO error:nil];
[defaults setObject:data forKey:keyName];
[defaults synchronize];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+(NSDictionary *)readDictyWithCustomObjFromUserDefaults:(NSString*)keyName
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:keyName];
NSDictionary *myDict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[defaults synchronize];
return myDict;
}
+(NSString *)clearStringMessaggi:(NSString *)messaggio{
NSString *clearString = [messaggio stringByReplacingOccurrencesOfString:@" " withString:@""];
@@ -204,9 +185,19 @@
#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:array requiringSecureCoding:NO error:&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;
@@ -233,4 +224,21 @@
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