<commit_before>//
//  CustomForumManagerTableViewController.swift
//  KomicaViewer
//
//  Created by <NAME> on 11/09/2016.
//  Copyright © 2016 Craig. All rights reserved.
//

import UIKit

class CustomForumManagerTableViewController: UITableViewController {

    private struct CellIdentifier {
        static let customForum = "customForumCell"
    }
    
    private struct SegueIdentifier {
        static let addForum = "addForum"
        static let scanQRCode = "scanQRCode"
        static let showForum = "showForum"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Forums.customForumGroup.forums?.count ?? 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier.customForum, forIndexPath: indexPath)

        if let forum = Forums.customForumGroup.forums?[indexPath.row] {
            cell.textLabel?.text = forum.name
            if let indexURLString = forum.indexURL,
                let indexURL = NSURL(string: indexURLString)
            {
                cell.detailTextLabel?.text = indexURL.host ?? ""
            } else {
                cell.detailTextLabel?.text = ""
            }
        }
        return cell
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == SegueIdentifier.showForum,
            let destinationViewController = segue.destinationViewController as? AddForumTableViewController,
            let tableViewCell = sender as? UITableViewCell,
            let indexPath = tableView.indexPathForCell(tableViewCell),
            let customForum = Forums.customForumGroup.forums?[indexPath.row]
        {
            destinationViewController.displayType = .readonly
            destinationViewController.newForum = customForum
        }
    }

}


// MARK: UI actions.
extension CustomForumManagerTableViewController {
    
    @IBAction func editButtonAction(sender: AnyObject) {
        tableView.setEditing(!tableView.editing, animated: true)
    }<commit_msg>Added an edit button to CustomForumManagerTableViewController, which toggles tableView.editing. And handle deleting event.<commit_after>//
//  CustomForumManagerTableViewController.swift
//  KomicaViewer
//
//  Created by <NAME> on 11/09/2016.
//  Copyright © 2016 Craig. All rights reserved.
//

import UIKit

class CustomForumManagerTableViewController: UITableViewController {

    private struct CellIdentifier {
        static let customForum = "customForumCell"
    }
    
    private struct SegueIdentifier {
        static let addForum = "addForum"
        static let scanQRCode = "scanQRCode"
        static let showForum = "showForum"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setToolbarHidden(false, animated: animated)
        // Colours.
        self.navigationController?.toolbar.barTintColor = self.navigationController?.navigationBar.barTintColor
        self.navigationController?.toolbar.tintColor = self.navigationController?.navigationBar.tintColor
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setToolbarHidden(true, animated: animated)
    }

    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Forums.customForumGroup.forums?.count ?? 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier.customForum, forIndexPath: indexPath)

        if let forum = Forums.customForumGroup.forums?[indexPath.row] {
            cell.textLabel?.text = forum.name
            if let indexURLString = forum.indexURL,
                let indexURL = NSURL(string: indexURLString)
            {
                cell.detailTextLabel?.text = indexURL.host ?? ""
            } else {
                cell.detailTextLabel?.text = ""
            }
        }
        return cell
    }
    
    // MARK: UITableView delegate
    
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == 0 && editingStyle == .Delete {
            Forums.customForumGroup.forums?.removeAtIndex(indexPath.row)
            Forums.saveCustomForums()
            tableView.reloadData()
        }
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == SegueIdentifier.showForum,
            let destinationViewController = segue.destinationViewController as? AddForumTableViewController,
            let tableViewCell = sender as? UITableViewCell,
            let indexPath = tableView.indexPathForCell(tableViewCell),
            let customForum = Forums.customForumGroup.forums?[indexPath.row]
        {
            destinationViewController.displayType = .readonly
            destinationViewController.newForum = customForum
        }
    }

}


// MARK: UI actions.
extension CustomForumManagerTableViewController {
    
    @IBAction func editButtonAction(sender: AnyObject) {
        tableView.setEditing(!tableView.editing, animated: true)
    }
{'fraction_non_alphanumeric': 0.07980484404948597, 'fraction_numerical': 0.004704652378463147, 'mean_word_length': 2.7369791666666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '143670', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1516, 'n_words': 326}