forked from amirdew/CollectionViewPagingLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutDesignerCodePreviewViewController.swift
More file actions
121 lines (93 loc) · 3.9 KB
/
LayoutDesignerCodePreviewViewController.swift
File metadata and controls
121 lines (93 loc) · 3.9 KB
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
//
// LayoutDesignerCodePreviewViewController.swift
// PagingLayoutSamples
//
// Created by Amir on 26/06/2020.
// Copyright © 2020 Amir Khorsandi. All rights reserved.
//
import Foundation
import UIKit
import Splash
protocol LayoutDesignerCodePreviewViewControllerDelegate: AnyObject {
func layoutDesignerCodePreviewViewController(_ vc: LayoutDesignerCodePreviewViewController, onHelpButtonTouched button: UIButton)
}
class LayoutDesignerCodePreviewViewController: UIViewController, NibBased, ViewModelBased {
// MARK: Properties
var viewModel: LayoutDesignerCodePreviewViewModel! {
didSet {
refreshViews()
}
}
weak var delegate: LayoutDesignerCodePreviewViewControllerDelegate?
@IBOutlet private weak var codeTextView: UITextView!
@IBOutlet private weak var copyButton: UIButton!
@IBOutlet private weak var saveButton: UIButton!
@IBOutlet private weak var helpButton: UIButton!
@IBOutlet private weak var codeModeSegmentedControl: UISegmentedControl!
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
configureViews()
}
// MARK: Listener
@IBAction private func copyButtonTouched() {
let pasteBoard = UIPasteboard.general
pasteBoard.string = codeTextView.text
}
@IBAction private func saveButtonTouched() {
guard let exportURL = viewModel.sampleProjectTempURL else { return }
viewModel.generateSampleProject()
let controller = UIDocumentPickerViewController(forExporting: [exportURL])
controller.delegate = self
present(controller, animated: true)
}
@IBAction private func onHelpButtonTouched() {
delegate?.layoutDesignerCodePreviewViewController(self, onHelpButtonTouched: helpButton)
}
@IBAction private func codeTypeChanged() {
refreshViews()
}
// MARK: Private functions
private func configureViews() {
configureTextView()
configureButtons()
configureCodeTypeSegmentedControl()
}
private func configureButtons() {
[saveButton, copyButton, helpButton].forEach {
$0?.layer.cornerRadius = 8
}
}
private func configureCodeTypeSegmentedControl() {
codeModeSegmentedControl.backgroundColor = UIColor.black.withAlphaComponent(0.4)
codeModeSegmentedControl.selectedSegmentTintColor = UIColor.white.withAlphaComponent(0.4)
codeModeSegmentedControl.setTitleTextAttributes(
[.foregroundColor: UIColor.white, .font: UIFont.systemFont(ofSize: 12)],
for: .normal)
codeModeSegmentedControl.setTitleTextAttributes(
[.foregroundColor: UIColor.black.withAlphaComponent(0.6), .font: UIFont.systemFont(ofSize: 12)],
for: .selected)
codeModeSegmentedControl.selectedSegmentIndex = 0
}
private func configureTextView() {
codeTextView.backgroundColor = .clear
codeTextView.isEditable = false
}
private func refreshViews() {
codeTextView.attributedText = viewModel?.getHighlightedText(
addViewControllerInCode: codeModeSegmentedControl.selectedSegmentIndex == 0
)
codeTextView.contentInset = .init(top: 40 + view.safeAreaInsets.top, left: 0, bottom: 200 + view.safeAreaInsets.bottom, right: 0)
codeTextView.contentOffset = .init(x: 0, y: -codeTextView.contentInset.top)
}
}
extension LayoutDesignerCodePreviewViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
controller.dismiss(animated: true)
viewModel.removeSampleProject()
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true)
viewModel.removeSampleProject()
}
}