Skip to content

Commit

Permalink
Added sort functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorChernyshov committed Oct 11, 2021
1 parent ef25cdb commit 3b630ef
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
42 changes: 26 additions & 16 deletions Sticky Links/Controllers/CategoryViewController.swift
Expand Up @@ -10,8 +10,7 @@ import CoreData

class CategoryViewController: UITableViewController {


var categoryArray = [Category]()
var categories = [Category]()
var filteredCategoryData: [Category] = []
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let request : NSFetchRequest<Category> = Category.fetchRequest()
Expand All @@ -38,7 +37,7 @@ class CategoryViewController: UITableViewController {
outletSwitch.isOn = UserDefaults.standard.value(forKey: "DarkMode") as? Bool ?? false
searchBar.delegate = self
searchBar.autocapitalizationType = .none
filteredCategoryData = categoryArray
filteredCategoryData = categories
}
}

Expand All @@ -51,7 +50,7 @@ extension CategoryViewController{
if searchInProgress == true {
return filteredCategoryData.count
} else {
return categoryArray.count
return categories.count
}

}
Expand All @@ -61,7 +60,7 @@ extension CategoryViewController{
if searchInProgress == true {
cell.textLabel?.text = filteredCategoryData[indexPath.row].name
} else {
cell.textLabel?.text = categoryArray[indexPath.row].name
cell.textLabel?.text = categories[indexPath.row].name
}
return cell
}
Expand All @@ -77,7 +76,7 @@ extension CategoryViewController{
if searchInProgress == true {
destinationVC.selectedProperty = filteredCategoryData[tableView.indexPathForSelectedRow!.row]
} else {
destinationVC.selectedProperty = categoryArray[tableView.indexPathForSelectedRow!.row]
destinationVC.selectedProperty = categories[tableView.indexPathForSelectedRow!.row]
}
}

Expand All @@ -87,20 +86,21 @@ extension CategoryViewController{
}

private func deleteContextualAction(forRowat indexPath: IndexPath) -> UIContextualAction {
let action = UIContextualAction(style: .destructive, title: "Delete") { action, view, completionHandler in
let action = UIContextualAction(style: .destructive, title: "Delete") { [weak self] action, view, completionHandler in
guard let self = self else { return }
var name = ""
if self.searchInProgress == true {
name = self.filteredCategoryData[indexPath.row].name!
} else {
name = self.categoryArray[indexPath.row].name!
name = self.categories[indexPath.row].name!
}

let alert = UIAlertController(title: "Are you sure you want to delete this item?", message: "\(name) will be deleted and can't be retrived afterwards", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .destructive, handler: { _ in
let category = self.categoryArray[indexPath.row]
let category = self.categories[indexPath.row]
self.context.delete(category)
self.categoryArray.remove(at: indexPath.row)
self.filteredCategoryData = self.categoryArray
self.categories.remove(at: indexPath.row)
self.filteredCategoryData = self.categories
self.tableView.deleteRows(at: [indexPath], with: .fade)
self.saveCategory()
}))
Expand All @@ -122,11 +122,14 @@ extension CategoryViewController{
var textField = UITextField()

let alert = UIAlertController(title: "Add Category", message: "", preferredStyle: .alert)
let addAction = UIAlertAction(title: "Add", style: .default) { (action) in
let addAction = UIAlertAction(title: "Add", style: .default) { [weak self] (action) in
guard let self = self else { return }
guard let newCategoryName = textField.text else { return }
let categoryName = Category(context: self.context)
categoryName.name = textField.text!
self.categoryArray.append(categoryName)
categoryName.name = newCategoryName
self.categories.append(categoryName)
self.filteredCategoryData.append(categoryName)
self.sortCategories()
self.saveCategory()
}

Expand All @@ -153,7 +156,13 @@ extension CategoryViewController{
//MARK: SORTING
extension CategoryViewController{
@IBAction func sortCategory(_ sender: UIBarButtonItem) {
categories.reverse()
tableView.reloadData()
}

private func sortCategories() {
categories.sort { $0.name ?? "" < $1.name ?? "" }
}
}

//MARK: SEARCH BAR
Expand All @@ -167,7 +176,7 @@ extension CategoryViewController: UISearchBarDelegate{
searchInProgress = true
searchBar.showsCancelButton = true
let caseInsensitiveText = searchText.lowercased()
filteredCategoryData = searchText.isEmpty ? categoryArray : categoryArray.filter ({ $0.name!.lowercased().contains(caseInsensitiveText)})
filteredCategoryData = searchText.isEmpty ? categories : categories.filter ({ $0.name!.lowercased().contains(caseInsensitiveText)})
tableView.reloadData()
}

Expand Down Expand Up @@ -205,7 +214,8 @@ extension CategoryViewController{

func loadCategory(){
do{
categoryArray = try context.fetch(request)
categories = try context.fetch(request)
sortCategories()
}catch{
print("\(error)")
}
Expand Down
34 changes: 21 additions & 13 deletions Sticky Links/Controllers/LinksViewController.swift
Expand Up @@ -7,9 +7,10 @@

import UIKit
import CoreData

class LinksViewController: UITableViewController {
var links=[Items]()

var links = [Items]()
var filteredLinksData: [Items] = []
var searchInProgress: Bool = false
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Expand All @@ -21,11 +22,7 @@ class LinksViewController: UITableViewController {
}

@IBOutlet weak var searchBar: UISearchBar!


// override func viewWillAppear(_ animated: Bool) {
//
// }

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Expand Down Expand Up @@ -77,7 +74,8 @@ extension LinksViewController{
}

private func deleteContextualAction(forRowat indexPath: IndexPath) -> UIContextualAction {
let action = UIContextualAction(style: .destructive, title: "Delete") { action, view, completionHandler in
let action = UIContextualAction(style: .destructive, title: "Delete") { [weak self] action, view, completionHandler in
guard let self = self else { return }
let title = self.links[indexPath.row].title!
let alert = UIAlertController(title: "Are you sure you want to delete this item?", message: "\(title) will be deleted and can't be retrived afterwards", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .destructive, handler: { _ in
Expand All @@ -103,12 +101,15 @@ extension LinksViewController{
var textField = UITextField()
var linktextField = UITextField()
let alert = UIAlertController(title: "Add your favourite Webpages", message: "", preferredStyle: .alert)
let addAction = UIAlertAction(title: "Add", style: .default) { (action) in
let addAction = UIAlertAction(title: "Add", style: .default) { [weak self] (action) in
guard let self = self else { return }
guard let pageTitle = textField.text, let pageLink = linktextField.text else { return }
let newLink = Items(context: self.context)
newLink.title = textField.text!
newLink.link = linktextField.text!
newLink.title = pageTitle
newLink.link = pageLink
newLink.parentCategory = self.selectedProperty
self.links.append(newLink)
self.sortLinks()
self.saveLink()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
Expand All @@ -135,7 +136,13 @@ extension LinksViewController{
//MARK: SORTING
extension LinksViewController{
@IBAction func sortLinksButton(_ sender: UIBarButtonItem) {
links.reverse()
tableView.reloadData()
}

private func sortLinks() {
links.sort { $0.title ?? "" < $1.title ?? "" }
}
}

//MARK: SEARCH BAR
Expand All @@ -162,8 +169,8 @@ extension LinksViewController: UISearchBarDelegate{
searchInProgress = false
searchBar.text = ""
searchBar.resignFirstResponder()
self.tableView.resignFirstResponder()
self.searchBar.showsCancelButton = false
tableView.resignFirstResponder()
searchBar.showsCancelButton = false
tableView.reloadData()
}

Expand All @@ -188,6 +195,7 @@ extension LinksViewController{
func loadLink(){
do{
links = try context.fetch(request)
sortLinks()
}catch{
print("\(error)")
}
Expand Down

0 comments on commit 3b630ef

Please sign in to comment.