-
-
Notifications
You must be signed in to change notification settings - Fork 721
/
InsertMediaAdvancedSettingsViewController.swift
154 lines (133 loc) · 6.99 KB
/
InsertMediaAdvancedSettingsViewController.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import UIKit
final class InsertMediaAdvancedSettingsViewController: ViewController {
static let title = WMFLocalizedString("advanced-settings-title", value: "Advanced settings", comment: "Title for advanced settings screen")
private let tableView = UITableView()
typealias AdvancedSettings = InsertMediaSettings.Advanced
var advancedSettings: AdvancedSettings {
return AdvancedSettings(wrapTextAroundImage: textWrappingSwitch.isOn, imagePosition: imagePositionSettingsViewController.selectedImagePosition(isTextWrappingEnabled: textWrappingSwitch.isOn), imageType: imageTypeSettingsViewController.selectedImageType, imageSize: imageSizeSettingsViewController.selectedImageSize)
}
struct ViewModel {
let title: String
let detailText: String?
let accessoryView: UIView?
let accessoryType: UITableViewCell.AccessoryType
let isEnabled: Bool
let selectionStyle: UITableViewCell.SelectionStyle
let onSelection: (() -> Void)?
init(title: String, detailText: String? = nil, accessoryView: UIView? = nil, accessoryType: UITableViewCell.AccessoryType = .disclosureIndicator, isEnabled: Bool = true, selectionStyle: UITableViewCell.SelectionStyle = .default, onSelection: (() -> Void)? = nil) {
self.title = title
self.detailText = detailText
self.accessoryView = accessoryView
self.accessoryType = accessoryType
self.isEnabled = isEnabled
self.selectionStyle = selectionStyle
self.onSelection = onSelection
}
}
private lazy var textWrappingSwitch: UISwitch = {
let textWrappingSwitch = UISwitch()
textWrappingSwitch.isOn = true
textWrappingSwitch.addTarget(self, action: #selector(toggleImagePositionEnabledState(_:)), for: .valueChanged)
return textWrappingSwitch
}()
private lazy var imagePositionSettingsViewController = InsertMediaImagePositionSettingsViewController()
private lazy var imageTypeSettingsViewController = InsertMediaImageTypeSettingsViewController()
private lazy var imageSizeSettingsViewController = InsertMediaImageSizeSettingsViewController()
private var viewModels: [ViewModel] {
let textWrappingViewModel = ViewModel(title: WMFLocalizedString("insert-media-image-text-wrapping-setting", value: "Wrap text around image", comment: "Title for image setting that wraps text around image"), accessoryView: textWrappingSwitch, accessoryType: .none, selectionStyle: .none)
let imagePositionViewModel = ViewModel(title: AdvancedSettings.ImagePosition.displayTitle, detailText: imagePositionSettingsViewController.selectedImagePosition(isTextWrappingEnabled: textWrappingSwitch.isOn).displayTitle, isEnabled: textWrappingSwitch.isOn) { [weak self] in
guard let self = self else {
return
}
self.push(self.imagePositionSettingsViewController)
}
let imageTypeViewModel = ViewModel(title: AdvancedSettings.ImageType.displayTitle, detailText: imageTypeSettingsViewController.selectedImageType.displayTitle) { [weak self] in
guard let self = self else {
return
}
self.push(self.imageTypeSettingsViewController)
}
let imageSizeViewModel = ViewModel(title: AdvancedSettings.ImageSize.displayTitle, detailText: imageSizeSettingsViewController.selectedImageSize.displayTitle) { [weak self] in
guard let self = self else {
return
}
self.push(self.imageSizeSettingsViewController)
}
return [textWrappingViewModel, imagePositionViewModel, imageTypeViewModel, imageSizeViewModel]
}
private func push(_ viewController: UIViewController & Themeable) {
viewController.apply(theme: theme)
navigationController?.pushViewController(viewController, animated: true)
}
@objc private func toggleImagePositionEnabledState(_ sender: UISwitch) {
tableView.reloadRows(at: [IndexPath(row: 1, section: 0)], with: .automatic)
}
override func viewDidLoad() {
scrollView = tableView
super.viewDidLoad()
navigationBar.isBarHidingEnabled = false
tableView.dataSource = self
tableView.delegate = self
view.wmf_addSubviewWithConstraintsToEdges(tableView)
tableView.separatorInset = .zero
tableView.tableFooterView = UIView()
title = InsertMediaAdvancedSettingsViewController.title
apply(theme: theme)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
defer {
isFirstAppearance = false
}
guard !isFirstAppearance else {
return
}
tableView.reloadData()
}
// MARK: - Themeable
override func apply(theme: Theme) {
super.apply(theme: theme)
view.backgroundColor = theme.colors.paperBackground
tableView.backgroundColor = view.backgroundColor
tableView.separatorColor = theme.colors.border
tableView.reloadData()
}
}
// MARK: - Table view data source
extension InsertMediaAdvancedSettingsViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.identifier) ?? UITableViewCell(style: .value1, reuseIdentifier: UITableViewCell.identifier)
let viewModel = viewModels[indexPath.row]
cell.textLabel?.text = viewModel.title
cell.accessoryView = viewModel.accessoryView
cell.accessoryType = viewModel.accessoryType
cell.isUserInteractionEnabled = viewModel.isEnabled
cell.detailTextLabel?.textAlignment = .right
cell.detailTextLabel?.text = viewModel.detailText
cell.selectionStyle = cell.isUserInteractionEnabled ? viewModel.selectionStyle : .none
apply(theme: theme, to: cell)
return cell
}
private func apply(theme: Theme, to cell: UITableViewCell) {
cell.backgroundColor = theme.colors.paperBackground
cell.contentView.backgroundColor = theme.colors.paperBackground
let selectedBackgroundView = UIView()
selectedBackgroundView.backgroundColor = theme.colors.midBackground
cell.selectedBackgroundView = selectedBackgroundView
cell.textLabel?.textColor = cell.isUserInteractionEnabled ? theme.colors.primaryText : theme.colors.secondaryText
cell.detailTextLabel?.textColor = theme.colors.secondaryText
}
}
// MARK: - Table view delegate
extension InsertMediaAdvancedSettingsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewModel = viewModels[indexPath.row]
viewModel.onSelection?()
}
}