@@ -17,102 +17,99 @@ class DefaultReactionDetailController: UITableViewController {
1717 @IBOutlet var confusedCell : UITableViewCell !
1818 @IBOutlet var heartCell : UITableViewCell !
1919 @IBOutlet var enabledSwitch : UISwitch !
20-
20+
2121 override func viewDidLoad( ) {
2222 super. viewDidLoad ( )
2323 checkCurrentDefault ( )
2424 tableView. reloadData ( )
2525 }
26-
26+
2727 override func numberOfSections( in tableView: UITableView ) -> Int {
28- return enabledSwitch. isOn ? 2 : 1
28+ return enabledSwitch. isOn ? 2 : 1
2929 }
30-
30+
3131 private func checkCurrentDefault( ) {
32- switch ( ReactionContent . defaultReaction)
33- {
34- case ReactionContent . thumbsUp:
35- updateCells ( cell: thumbsUpCell)
36- case ReactionContent . thumbsDown:
37- updateCells ( cell: thumbsDownCell)
38- case ReactionContent . laugh:
39- updateCells ( cell: laughCell)
40- case ReactionContent . hooray:
41- updateCells ( cell: hoorayCell)
42- case ReactionContent . confused:
43- updateCells ( cell: confusedCell)
44- case ReactionContent . heart:
45- updateCells ( cell: heartCell)
46- case ReactionContent . __unknown ( " Disabled " ) :
47- enabledSwitch. isOn = false
48- default :
49- updateCells ( cell: thumbsUpCell)
50- }
51-
32+ guard let reaction = ReactionContent . defaultReaction else {
33+ enabledSwitch. isOn = false
34+ return
35+ }
36+
37+ let cell : UITableViewCell
38+ switch ( reaction) {
39+ case . thumbsUp, . __unknown: cell = thumbsUpCell
40+ case . thumbsDown: cell = thumbsDownCell
41+ case . laugh: cell = laughCell
42+ case . hooray: cell = hoorayCell
43+ case . confused: cell = confusedCell
44+ case . heart: cell = heartCell
45+ }
46+ updateCells ( cell: cell)
5247 }
53-
48+
5449 private func updateCells( cell: UITableViewCell ) {
55-
56- rz_smoothlyDeselectRows ( tableView: self . tableView)
57-
58- // Reset all to none
59- thumbsUpCell. accessoryType = . none
60- thumbsDownCell. accessoryType = . none
61- laughCell. accessoryType = . none
62- hoorayCell. accessoryType = . none
63- confusedCell. accessoryType = . none
64- heartCell. accessoryType = . none
65-
66- // Set proper cell to check
67- cell. accessoryType = . checkmark
50+ rz_smoothlyDeselectRows ( tableView: self . tableView)
51+
52+ // Reset all to none
53+ thumbsUpCell. accessoryType = . none
54+ thumbsDownCell. accessoryType = . none
55+ laughCell. accessoryType = . none
56+ hoorayCell. accessoryType = . none
57+ confusedCell. accessoryType = . none
58+ heartCell. accessoryType = . none
6859
60+ // Set proper cell to check
61+ cell. accessoryType = . checkmark
6962 }
70-
63+
7164 override func tableView( _ tableView: UITableView , didSelectRowAt indexPath: IndexPath ) {
72-
73- tableView. deselectRow ( at: indexPath, animated: trueUnlessReduceMotionEnabled)
74- let cell = tableView. cellForRow ( at: indexPath)
75-
76- switch cell {
77- case thumbsUpCell:
78- updateDefaultReaction ( . thumbsUp)
79- case thumbsDownCell:
80- updateDefaultReaction ( . thumbsDown)
81- case laughCell:
82- updateDefaultReaction ( . laugh)
83- case hoorayCell:
84- updateDefaultReaction ( . hooray)
85- case confusedCell:
86- updateDefaultReaction ( . confused)
87- case heartCell:
88- updateDefaultReaction ( . heart)
89- default :
90- break
91- }
65+ tableView. deselectRow ( at: indexPath, animated: trueUnlessReduceMotionEnabled)
66+ let cell = tableView. cellForRow ( at: indexPath)
67+
68+ switch cell {
69+ case thumbsUpCell:
70+ updateDefault ( reaction: . thumbsUp)
71+ case thumbsDownCell:
72+ updateDefault ( reaction: . thumbsDown)
73+ case laughCell:
74+ updateDefault ( reaction: . laugh)
75+ case hoorayCell:
76+ updateDefault ( reaction: . hooray)
77+ case confusedCell:
78+ updateDefault ( reaction: . confused)
79+ case heartCell:
80+ updateDefault ( reaction: . heart)
81+ default :
82+ break
83+ }
9284 }
93-
85+
9486 @IBAction func toggleDefaultReaction( _ sender: Any ) {
9587 if ( enabledSwitch. isOn) {
96- updateDefaultReaction ( . thumbsUp)
88+ updateDefault ( reaction : . thumbsUp)
9789 } else {
98- updateDefaultReaction ( . __unknown ( " Disabled " ) )
90+ disableReaction ( )
9991 }
10092 updateSections ( )
10193 }
102-
103- private func updateDefaultReaction( _ reaction: ReactionContent ) {
104- UserDefaults . setDefault ( reaction: reaction)
105- checkCurrentDefault ( )
94+
95+ private func updateDefault( reaction: ReactionContent ) {
96+ UserDefaults . standard. setDefault ( reaction: reaction)
97+ checkCurrentDefault ( )
98+ }
99+
100+ private func disableReaction( ) {
101+ UserDefaults . standard. disableReaction ( )
106102 }
107-
103+
108104 private func updateSections( ) {
109- tableView. performBatchUpdates ( {
110- if ( enabledSwitch. isOn) {
111- self . tableView. insertSections ( IndexSet ( integer: 1 ) , with: . top)
112- } else {
113- self . tableView. deleteSections ( IndexSet ( integer: 1 ) , with: . top)
114- }
115- } , completion: nil )
105+ tableView. performBatchUpdates ( {
106+ if ( enabledSwitch. isOn) {
107+ self . tableView. insertSections ( IndexSet ( integer: 1 ) , with: . top)
108+ } else {
109+ self . tableView. deleteSections ( IndexSet ( integer: 1 ) , with: . top)
110+ }
111+ } , completion: nil )
116112 }
117113}
118114
115+
0 commit comments