@@ -17,102 +17,99 @@ class DefaultReactionDetailController: UITableViewController {
17
17
@IBOutlet var confusedCell : UITableViewCell !
18
18
@IBOutlet var heartCell : UITableViewCell !
19
19
@IBOutlet var enabledSwitch : UISwitch !
20
-
20
+
21
21
override func viewDidLoad( ) {
22
22
super. viewDidLoad ( )
23
23
checkCurrentDefault ( )
24
24
tableView. reloadData ( )
25
25
}
26
-
26
+
27
27
override func numberOfSections( in tableView: UITableView ) -> Int {
28
- return enabledSwitch. isOn ? 2 : 1
28
+ return enabledSwitch. isOn ? 2 : 1
29
29
}
30
-
30
+
31
31
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)
52
47
}
53
-
48
+
54
49
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
68
59
60
+ // Set proper cell to check
61
+ cell. accessoryType = . checkmark
69
62
}
70
-
63
+
71
64
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
+ }
92
84
}
93
-
85
+
94
86
@IBAction func toggleDefaultReaction( _ sender: Any ) {
95
87
if ( enabledSwitch. isOn) {
96
- updateDefaultReaction ( . thumbsUp)
88
+ updateDefault ( reaction : . thumbsUp)
97
89
} else {
98
- updateDefaultReaction ( . __unknown ( " Disabled " ) )
90
+ disableReaction ( )
99
91
}
100
92
updateSections ( )
101
93
}
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 ( )
106
102
}
107
-
103
+
108
104
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 )
116
112
}
117
113
}
118
114
115
+
0 commit comments