EepromTemplateController.m 8.22キロバイト
//
//  EepromTemplateController.m
//  jacket_test_ios
//
//  Created by ドラッサル 亜嵐 on 2017/07/24.
//  Copyright © 2017年 ドラッサル 亜嵐. All rights reserved.
//

#import "EepromTemplateController.h"
#import "EepromTemplateTableViewCell.h"
#import "DBManager.h"

@interface EepromTemplateController ()

@end

@implementation EepromTemplateController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    arrResult = [[NSMutableArray alloc] init];
    
    UINib *nib = [UINib nibWithNibName:@"EepromTemplateTableViewCell" bundle:nil];
    [tblResult registerNib:nib forCellReuseIdentifier:@"mycell"];
    tblResult.delegate = self;
    tblResult.dataSource = self;
    
    [arrResult removeAllObjects];
    
    NSArray *result = [[DBManager sharedInstance] getEepromTemplateData];
    
    for(id key in result) {
        NSString *valueRecordId = [key objectAtIndex:0];
        NSString *valueRecordName = [key objectAtIndex:1];
        NSString *valueDataId = [key objectAtIndex:2];
        NSString *valueDataModel = [key objectAtIndex:3];
        NSString *valueDataType = [key objectAtIndex:4];
        NSString *valueDataOs = [key objectAtIndex:5];
        NSString *valueDataPin = [key objectAtIndex:6];
        
        NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
        [newDict setObject:valueRecordId forKey:@"recordId"];
        [newDict setObject:valueRecordName forKey:@"recordName"];
        [newDict setObject:valueDataId forKey:@"dataId"];
        [newDict setObject:valueDataModel forKey:@"dataModel"];
        [newDict setObject:valueDataType forKey:@"dataType"];
        [newDict setObject:valueDataOs forKey:@"dataOs"];
        [newDict setObject:valueDataPin forKey:@"dataPin"];
        
        [arrResult addObject:newDict];
    }
    [tblResult reloadData];    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancelButtonClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    NSDictionary *dataRecord = [arrResult objectAtIndex:row];

    [self.delegate didChoose:[dataRecord valueForKey:@"recordId"]
                  recordName:[dataRecord valueForKey:@"recordName"]
                      dataId:[dataRecord valueForKey:@"dataId"]
                   dataModel:[dataRecord valueForKey:@"dataModel"]
                    dataType:[dataRecord valueForKey:@"dataType"]
                      dataOs:[dataRecord valueForKey:@"dataOs"]
                     dataPin:[dataRecord valueForKey:@"dataPin"]];
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [arrResult count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"mycell";
    EepromTemplateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    NSInteger row = [indexPath row];
    NSDictionary *dataRecord = [arrResult objectAtIndex:row];
    
    // Configure the cell...
    UIFont *newFont = [UIFont fontWithName:@"Arial" size:17.0];
    cell.lblRecordName.font = newFont;
    cell.lblRecordName.text = [dataRecord valueForKey:@"recordName"];
    
    newFont = [UIFont fontWithName:@"Arial" size:12.0];
    cell.lblRecordId.font = newFont;
    cell.lblRecordId.text = [dataRecord valueForKey:@"recordId"];

    cell.lblDataId.font = newFont;
    cell.lblDataId.text = [dataRecord valueForKey:@"dataId"];
    
    cell.lblDataModel.font = newFont;
    cell.lblDataModel.text = [dataRecord valueForKey:@"dataModel"];
    
    cell.lblDataType.font = newFont;
    cell.lblDataType.text = [dataRecord valueForKey:@"dataType"];
    
    cell.lblDataOs.font = newFont;
    cell.lblDataOs.text = [dataRecord valueForKey:@"dataOs"];
    
    cell.lblDataPin.font = newFont;
    cell.lblDataPin.text = [dataRecord valueForKey:@"dataPin"];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @[
             [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
                                                title:@"削除"
                                              handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
                                                  // own delete action
                                                  
                                                  
                                                  // Distructive button tapped.
                                                  NSLog(@"削除の確認");
                                                  
                                                  NSDictionary * selectedData = [arrResult objectAtIndex:indexPath.row];
                                                  NSString *firmwareTitle = [selectedData valueForKey:@"file"];
                                                  
                                                  UIAlertController *actionSheetConfirm = [UIAlertController alertControllerWithTitle:@"削除の確認" message:[NSString stringWithFormat:@"「%@」を削除します。よろしいですか?",firmwareTitle] preferredStyle:UIAlertControllerStyleAlert];
                                                  
                                                  [actionSheetConfirm addAction:[UIAlertAction actionWithTitle:@"削除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *actionConfirm) {
                                                      
                                                      
                                                      // Get the record ID of the selected name and set it to the recordIDToEdit property.
                                                      NSDictionary * selectedData = [arrResult objectAtIndex:indexPath.row];
                                                      
                                                      NSString *recordId = [selectedData valueForKey:@"recordId"];
                                                      NSLog(@"recordId = %@",recordId);

                                                      [[DBManager sharedInstance] deleteEepromTemplateData:recordId];
                                                      //[self populateDetail];
                                                      //[self.tblResult reloadData];
                                                      [arrResult removeObjectAtIndex:indexPath.row];
                                                      [tblResult deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                                                  }]];
                                                  [actionSheetConfirm addAction:[UIAlertAction actionWithTitle:@"キャンセル" style:UIAlertActionStyleDefault handler:^(UIAlertAction *actionConfirm) {
                                                      // Distructive button tapped.
                                                      NSLog(@"キャンセルの確認");
                                                  }]];
                                                  [self presentViewController:actionSheetConfirm animated:YES completion:nil];
                                              }],
             ];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end