Utilities.m 4.02 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"

@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];
  }
}

+ (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 {
}
@end