Utilities.m 5.13 KB
//
//  Utilities.m
//  LifeLog
//
//  Created by Nguyen Van Phong on 7/29/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import <Social/Social.h>
#import <LineKit/Line.h>

#import "Utilities.h"
#import "ServerAPI.h"

@implementation Utilities
+ (NSString *)addCommaFromNumber:(NSInteger)number
{
    NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
    [fmt setNumberStyle:NSNumberFormatterDecimalStyle];
    [fmt setMaximumFractionDigits:0];
    NSString *result = [fmt stringFromNumber:@(number)];
    return result;
}

+ (UIColor *)convertHecToColor:(int) hex
{
    return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 \
                           green:((float)((hex & 0xFF00) >> 8))/255.0 \
                            blue:((float)(hex & 0xFF))/255.0 alpha:1.0];
}

+ (void)showErrorMessage:(NSString *)message withViewController:(UIViewController *)vc
{
  if (message.length > 0) {
    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Error"
                                  message:message
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                           [alert dismissViewControllerAnimated:YES completion:nil];
                           
                         }];
    
    [alert addAction:ok];
    
    [vc presentViewController:alert animated:YES completion:nil];
  }
}

+ (NSString *) getImageLink : (NSString *) path {
    NSString * link = kServerAddress;
    return [link stringByAppendingString:path];
}

+ (void) shareFacebook : (NSString *) content withViewController:(UIViewController *)vc {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        
        [composeViewController setInitialText:content];
        [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) {
            
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"canceled");
                    break;
                case SLComposeViewControllerResultDone:
                    NSLog(@"done");
                    break;
                default:
                    break;
            }
        }];
        [vc presentViewController:composeViewController animated:YES completion:nil];
    }
    else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=FACEBOOK"]];
    }
}

+ (void) shareTwitter : (NSString *) content withViewController:(UIViewController *)vc {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        
        [composeViewController setInitialText:content];
        [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) {
            
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"canceled");
                    break;
                case SLComposeViewControllerResultDone:
                    NSLog(@"done");
                    break;
                default:
                    break;
            }
        }];
        [vc presentViewController:composeViewController animated:YES completion:nil];
    }
    else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=TWITTER"]];
    }
}

+ (void) shareLine : (NSString *) content withViewController:(UIViewController *)vc {
    if (![Line isLineInstalled]) {
        [self showErrorMessage:@"Install Line app first" withViewController:vc];
    }
    else {
        [Line shareText:content];
    }
}

+ (void) shareEmail : (NSString *) content withViewController:(UIViewController *)vc {
    NSString *urlEmail = @"mailto:?subject=Share from LifeLog&body=";
    urlEmail = [urlEmail stringByAppendingString:content];
    urlEmail = [urlEmail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [[UIApplication sharedApplication]  openURL: [NSURL URLWithString: urlEmail]];
}

#pragma mark convert date time
+ (NSDate *) dateFromString : (NSString *) dateString withFormat: (NSString *) format {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:format];
    NSDate *date  = [dateFormat dateFromString:dateString];
    if(date == NULL) {
        return [NSDate date];
    }
    return date;
}

+ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:format];
    NSString *dateString  = [dateFormat stringFromDate:date];
    return dateString;
}

@end