-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
87 lines (60 loc) · 2.8 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// ViewController.swift
// MilestoneProject
//
// Created by Machine on 27.01.2022.
//
import UIKit
class ViewController: UITableViewController {
var shoppingList = [String]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Alışveriş Listesi"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addProduct))
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(refresh))
let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareList))
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolbarItems = [spacer, shareButton]
navigationController?.isToolbarHidden = false
}
@objc func shareList(){
let currentList = shoppingList.joined(separator: "\n")
let vc = UIActivityViewController(activityItems: [currentList], applicationActivities: [])
vc.popoverPresentationController?.barButtonItem = toolbarItems?.last
present(vc, animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
return shoppingList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = shoppingList[indexPath.row]
return cell
}
func startAgain() {
title = shoppingList.randomElement()
shoppingList.removeAll(keepingCapacity: true)
tableView.reloadData()
}
@objc func addProduct() {
let ac = UIAlertController(title: "Ne lazım?", message: nil, preferredStyle: .alert)
ac.addTextField()
ac.addAction(UIAlertAction(title: "Kapat", style: .cancel))
ac.addAction(UIAlertAction(title: "Ekle", style: .default) {
[weak self, weak ac] _ in
guard let text = ac?.textFields?[0].text else { return }
self?.addProductToList(item: text)
})
ac.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(ac, animated: true)
}
func addProductToList(item : String) {
shoppingList.insert(item, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
@objc func refresh() {
startAgain()
}
}