<commit_before>//
//  HomeViewModel.swift
//  DesignStudioExpress
//
//  Created by <NAME> on 11/13/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation
import RealmSwift

class HomeViewModel {
    lazy var realm = try! Realm()
    lazy var data: Results<DesignStudio> = self.loadDesignStudios()
    
    private func loadDesignStudios() -> Results<DesignStudio> {
        let realm = try! Realm()
        var designStudios = realm.objects(DesignStudio)
        
        // TODO fix this after demo, this will add test data that we can delete
        if designStudios.count == 0 || designStudios.count < 4 {
            createDefaultDesignStudios()
            designStudios = realm.objects(DesignStudio)
        }
        
        return designStudios
    }

    private func createDefaultDesignStudios() {
        let realm = try! Realm()
        
        realm.beginWrite()
        
        let ds1 = DesignStudio()
        ds1.title = "First template"
        ds1.duration = 45
        realm.add(ds1)
        
        let ds2 = DesignStudio()
        ds2.title = "Second template"
        ds2.duration = 62
        realm.add(ds2)
        
        // TODO remove - test data for demo
        for item in 1...3 {
            let ds = DesignStudio()
            ds.title = "Studio " + "\(item)"
            ds.duration = 90
            realm.add(ds)
        }

        try! realm.commitWrite()
    }
    
    func getTotalRows() -> Int {
        return data.count + 1
    }
    
    
    func isRowEditable(indexPath: NSIndexPath) -> Bool {
        if indexPath.row > 2 {
            return true
        }
        
        return false
    }
    
    // handler for Delete buttonr
    // since we have only one swipe button
    func swipeButtonClicked(indexPath: NSIndexPath) -> Bool {
        var success = false
        try! self.realm.write {
            self.realm.delete(self.data[indexPath.row-1])
            success = true
        }
        
        return success
    }
    
    func getTitle(indexPath: NSIndexPath) -> String {
        if indexPath.row == 0 {
            return "MY DESIGN STUDIOS"
        }
        return data[indexPath.row-1].title
    }
    
    func getDetail(indexPath: NSIndexPath) -> String {
        if indexPath.row == 0 {
            return "Start fast from a template or create a new one"
        }
        // format duration string to 2 decimals if we have a remainder
        let duration = Float(data[indexPath.row-1].duration) / 60
        let stringFormat: String
        
        if (duration % 1) != 0 {
            stringFormat = "%.2f %@"
        } else {
            stringFormat = "%.0f %@"
        }
        let durationString = String.localizedStringWithFormat(stringFormat, duration, "hr")
        return "Duration \(durationString)"
    }
    
    func getImageName(indexPath: NSIndexPath) -> String {
        // big image
        if indexPath.row == 0 {
            return "DS_Home_BG_image"
        }
        // icon for<commit_msg>#3 Duration is now formatted in mins<commit_after>//
//  HomeViewModel.swift
//  DesignStudioExpress
//
//  Created by <NAME> on 11/13/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation
import RealmSwift

class HomeViewModel {
    lazy var realm = try! Realm()
    lazy var data: Results<DesignStudio> = self.loadDesignStudios()
    
    private func loadDesignStudios() -> Results<DesignStudio> {
        let realm = try! Realm()
        var designStudios = realm.objects(DesignStudio)
        
        // TODO fix this after demo, this will add test data that we can delete
        if designStudios.count == 0 || designStudios.count < 4 {
            createDefaultDesignStudios()
            designStudios = realm.objects(DesignStudio)
        }
        
        return designStudios
    }

    private func createDefaultDesignStudios() {
        let realm = try! Realm()
        
        realm.beginWrite()
        
        let ds1 = DesignStudio()
        ds1.title = "First template"
        ds1.duration = 45
        realm.add(ds1)
        
        let ds2 = DesignStudio()
        ds2.title = "Second template"
        ds2.duration = 62
        realm.add(ds2)
        
        // TODO remove - test data for demo
        for item in 1...3 {
            let ds = DesignStudio()
            ds.title = "Studio " + "\(item)"
            ds.duration = 90
            realm.add(ds)
        }

        try! realm.commitWrite()
    }
    
    func getTotalRows() -> Int {
        return data.count + 1
    }
    
    
    func isRowEditable(indexPath: NSIndexPath) -> Bool {
        if indexPath.row > 2 {
            return true
        }
        
        return false
    }
    
    // handler for Delete buttonr
    // since we have only one swipe button
    func swipeButtonClicked(indexPath: NSIndexPath) -> Bool {
        var success = false
        try! self.realm.write {
            self.realm.delete(self.data[indexPath.row-1])
            success = true
        }
        
        return success
    }
    
    func getTitle(indexPath: NSIndexPath) -> String {
        if indexPath.row == 0 {
            return "MY DESIGN STUDIOS"
        }
        return data[indexPath.row-1].title
    }
    
    func getDetail(indexPath: NSIndexPath) -> String {
        if indexPath.row == 0 {
            return "Start fast from a template or create a new one"
        }
        let durationString = String.localizedStringWithFormat("% %@", data[indexPath.row-1].duration, "min")
        return "Duration \(durationString)"
    }
    
    func getImageName(indexPath: NSIndexPath) -> String {
        // big image
        if indexPath.row == 0 {
            return "DS_Home_BG_image"
        }
        // icon for
{'fraction_non_alphanumeric': 0.09424447922100504, 'fraction_numerical': 0.013910624239262736, 'mean_word_length': 1.6555863342566943, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3101386', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1621, 'n_words': 446}