From d8cc935b068bfd9fb6386a29b24b60b86dde19f2 Mon Sep 17 00:00:00 2001 From: Dwayne Harris Date: Sat, 17 Nov 2018 18:50:04 -0800 Subject: [PATCH] Fix attachment cropping and spoiler text rendering, improve input views --- Frameworks/Kingfisher | 2 +- elpha-ios.xcodeproj/project.pbxproj | 20 ++++++- elpha-ios/AttachmentInputView.swift | 30 +++++++++++ elpha-ios/AttachmentInputView.xib | 77 +++++++++++++++++++++++++++ elpha-ios/AttachmentManager.swift | 23 ++++---- elpha-ios/ComposeViewController.swift | 45 ++++++++++------ elpha-ios/MastodonAPI.swift | 4 +- elpha-ios/StatusView.swift | 70 ++++++++++++------------ elpha-ios/VisibilityInputView.swift | 58 ++++++++++++++++++++ elpha-ios/VisibilityInputView.xib | 50 +++++++++++++++++ 10 files changed, 313 insertions(+), 66 deletions(-) create mode 100644 elpha-ios/AttachmentInputView.swift create mode 100644 elpha-ios/AttachmentInputView.xib create mode 100644 elpha-ios/VisibilityInputView.swift create mode 100644 elpha-ios/VisibilityInputView.xib diff --git a/Frameworks/Kingfisher b/Frameworks/Kingfisher index fbf8067..696a876 160000 --- a/Frameworks/Kingfisher +++ b/Frameworks/Kingfisher @@ -1 +1 @@ -Subproject commit fbf8067218a358b470baad99a1ed3836365540f5 +Subproject commit 696a8762a81af2acffc916c1753a22a5a1381d0f diff --git a/elpha-ios.xcodeproj/project.pbxproj b/elpha-ios.xcodeproj/project.pbxproj index 2d19053..71dd5b6 100644 --- a/elpha-ios.xcodeproj/project.pbxproj +++ b/elpha-ios.xcodeproj/project.pbxproj @@ -24,6 +24,10 @@ 152FBCF2219818AD0079B3E8 /* FieldTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 152FBCF1219818AD0079B3E8 /* FieldTableViewCell.swift */; }; 15341866219FF29D002F5F8A /* SettingsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15341865219FF29D002F5F8A /* SettingsManager.swift */; }; 15341880219FF40F002F5F8A /* UIImageView+Effects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1534187F219FF40F002F5F8A /* UIImageView+Effects.swift */; }; + 1534189E21A0E19F002F5F8A /* VisibilityInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1534189D21A0E19F002F5F8A /* VisibilityInputView.swift */; }; + 153418A021A0E25A002F5F8A /* VisibilityInputView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1534189F21A0E25A002F5F8A /* VisibilityInputView.xib */; }; + 153418A221A0EEAC002F5F8A /* AttachmentInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 153418A121A0EEAC002F5F8A /* AttachmentInputView.swift */; }; + 153418A421A0EEED002F5F8A /* AttachmentInputView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 153418A321A0EEED002F5F8A /* AttachmentInputView.xib */; }; 1539509121894A38009BA6E7 /* AlertManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1539509021894A38009BA6E7 /* AlertManager.swift */; }; 156370BD219FE24200D51D42 /* Kingfisher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 156370B0219FE22E00D51D42 /* Kingfisher.framework */; }; 156370BE219FE24200D51D42 /* Kingfisher.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 156370B0219FE22E00D51D42 /* Kingfisher.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -304,6 +308,10 @@ 152FBCF1219818AD0079B3E8 /* FieldTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FieldTableViewCell.swift; sourceTree = ""; }; 15341865219FF29D002F5F8A /* SettingsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsManager.swift; sourceTree = ""; }; 1534187F219FF40F002F5F8A /* UIImageView+Effects.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+Effects.swift"; sourceTree = ""; }; + 1534189D21A0E19F002F5F8A /* VisibilityInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisibilityInputView.swift; sourceTree = ""; }; + 1534189F21A0E25A002F5F8A /* VisibilityInputView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = VisibilityInputView.xib; sourceTree = ""; }; + 153418A121A0EEAC002F5F8A /* AttachmentInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentInputView.swift; sourceTree = ""; }; + 153418A321A0EEED002F5F8A /* AttachmentInputView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AttachmentInputView.xib; sourceTree = ""; }; 1539509021894A38009BA6E7 /* AlertManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertManager.swift; sourceTree = ""; }; 15637094219FE22E00D51D42 /* Kingfisher.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Kingfisher.xcodeproj; path = Frameworks/Kingfisher/Kingfisher.xcodeproj; sourceTree = ""; }; 1569029F219A7D75002BF61F /* ComposeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeViewController.swift; sourceTree = ""; }; @@ -353,10 +361,10 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 156370BD219FE24200D51D42 /* Kingfisher.framework in Frameworks */, - 15A79B2E215C63B6007A326E /* AlamofireImage.framework in Frameworks */, 157405D1215890D700EEAAEB /* Alamofire.framework in Frameworks */, + 15A79B2E215C63B6007A326E /* AlamofireImage.framework in Frameworks */, EAD1445C219A6CAF002C5338 /* AlamofireNetworkActivityIndicator.framework in Frameworks */, + 156370BD219FE24200D51D42 /* Kingfisher.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -411,6 +419,10 @@ 159B5539219D11E600964AC0 /* ComposeAccessoryView.xib */, 15131EF1216D8D570092B252 /* StatusView.swift */, 15131ED7216D8C680092B252 /* StatusView.xib */, + 1534189D21A0E19F002F5F8A /* VisibilityInputView.swift */, + 1534189F21A0E25A002F5F8A /* VisibilityInputView.xib */, + 153418A121A0EEAC002F5F8A /* AttachmentInputView.swift */, + 153418A321A0EEED002F5F8A /* AttachmentInputView.xib */, ); name = "Reusable Views"; sourceTree = ""; @@ -830,8 +842,10 @@ buildActionMask = 2147483647; files = ( 15960E67213145E200C38CE9 /* LaunchScreen.storyboard in Resources */, + 153418A421A0EEED002F5F8A /* AttachmentInputView.xib in Resources */, 159B553A219D11E600964AC0 /* ComposeAccessoryView.xib in Resources */, 15C91A02216AB2D600D97DC3 /* AlertView.xib in Resources */, + 153418A021A0E25A002F5F8A /* VisibilityInputView.xib in Resources */, 15960E64213145E200C38CE9 /* Assets.xcassets in Resources */, 157405B12151A5DA00EEAAEB /* README.md in Resources */, 15960E62213145E100C38CE9 /* Main.storyboard in Resources */, @@ -857,6 +871,7 @@ 156FF0312174797E0074D9CA /* StatusTableViewController.swift in Sources */, 152FB0F8218ADC1A001D6574 /* AttachmentPageViewController.swift in Sources */, 15131EF4216DB8B90092B252 /* AccountTableViewController.swift in Sources */, + 1534189E21A0E19F002F5F8A /* VisibilityInputView.swift in Sources */, 156902A0219A7D75002BF61F /* ComposeViewController.swift in Sources */, 15F998352162C0E8009E58DA /* MastodonDataManager.swift in Sources */, 15222807219D37A500D2E5A6 /* ComposeAccessoryView.swift in Sources */, @@ -869,6 +884,7 @@ 15131EF2216D8D570092B252 /* StatusView.swift in Sources */, 152734D22186DC74003DB3C8 /* TimelinesViewController.swift in Sources */, 15B127A32192486200F4EF1D /* UIColor+HexString.swift in Sources */, + 153418A221A0EEAC002F5F8A /* AttachmentInputView.swift in Sources */, 15341880219FF40F002F5F8A /* UIImageView+Effects.swift in Sources */, 156FF07221779C650074D9CA /* InstanceRequest.swift in Sources */, 15960E7721322C6F00C38CE9 /* Configuration.swift in Sources */, diff --git a/elpha-ios/AttachmentInputView.swift b/elpha-ios/AttachmentInputView.swift new file mode 100644 index 0000000..215fe77 --- /dev/null +++ b/elpha-ios/AttachmentInputView.swift @@ -0,0 +1,30 @@ +// +// AttachmentInputView.swift +// elpha-ios +// +// Created by Dwayne Harris on 11/17/18. +// Copyright © 2018 Elpha. All rights reserved. +// + +import UIKit + +class AttachmentInputView: UIView { + @IBOutlet var contentView: UIView! + + override init(frame: CGRect) { + super.init(frame: frame) + setup() + } + + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + setup() + } + + private func setup() { + Bundle.main.loadNibNamed("AttachmentInputView", owner: self, options: nil) + addSubview(contentView) + contentView.frame = self.bounds + contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + } +} diff --git a/elpha-ios/AttachmentInputView.xib b/elpha-ios/AttachmentInputView.xib new file mode 100644 index 0000000..5063ccb --- /dev/null +++ b/elpha-ios/AttachmentInputView.xib @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/elpha-ios/AttachmentManager.swift b/elpha-ios/AttachmentManager.swift index 5be32aa..211dd1b 100644 --- a/elpha-ios/AttachmentManager.swift +++ b/elpha-ios/AttachmentManager.swift @@ -31,9 +31,13 @@ class AttachmentManager: NSObject { let placeholder = UIImage(named: "Help") let halfWidth = (view.frame.width / 2) - 1 - let fullAttachmentSize = CGSize(width: view.frame.width, height: view.frame.width) - let tallAttachmentSize = CGSize(width: halfWidth, height: view.frame.width) - let smallAttachmentSize = CGSize(width: halfWidth, height: halfWidth) + let fullSize = CGSize(width: view.frame.width, height: view.frame.width) + let tallSize = CGSize(width: halfWidth, height: view.frame.width) + let smallSize = CGSize(width: halfWidth, height: halfWidth) + + let fullResizingProcessor = ResizingImageProcessor(referenceSize: fullSize, mode: .aspectFill) >> CroppingImageProcessor(size: fullSize) + let tallResizingProcessor = ResizingImageProcessor(referenceSize: tallSize, mode: .aspectFill) >> CroppingImageProcessor(size: tallSize) + let smallResizingProcessor = ResizingImageProcessor(referenceSize: smallSize, mode: .aspectFill) >> CroppingImageProcessor(size: smallSize) for subview in view.subviews as [UIView] { subview.removeFromSuperview() @@ -44,12 +48,11 @@ class AttachmentManager: NSObject { return case 1: let attachment = attachments.firstObject as! AttachmentMO - let processor = ResizingImageProcessor(referenceSize: fullAttachmentSize, mode: .aspectFill) let imageView = UIImageView() imageView.contentMode = UIImageView.ContentMode.scaleAspectFill imageView.isUserInteractionEnabled = true - imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(processor)]) + imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(fullResizingProcessor)]) let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped)) tapGestureRecognizer.delegate = self @@ -76,11 +79,10 @@ class AttachmentManager: NSObject { for (index, imageView) in imageViews.enumerated() { let attachment = attachments[index] as! AttachmentMO - let processor = ResizingImageProcessor(referenceSize: tallAttachmentSize, mode: .aspectFill) imageView.contentMode = UIImageView.ContentMode.scaleAspectFill imageView.isUserInteractionEnabled = true - imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(processor)]) + imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(tallResizingProcessor)]) let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped)) tapGestureRecognizer.delegate = self @@ -116,9 +118,9 @@ class AttachmentManager: NSObject { var processor: ImageProcessor if index == 0 { - processor = ResizingImageProcessor(referenceSize: tallAttachmentSize, mode: .aspectFill) + processor = tallResizingProcessor } else { - processor = ResizingImageProcessor(referenceSize: smallAttachmentSize, mode: .aspectFill) + processor = smallResizingProcessor } imageView.contentMode = UIImageView.ContentMode.scaleAspectFill @@ -166,11 +168,10 @@ class AttachmentManager: NSObject { for (index, imageView) in imageViews.enumerated() { let attachment = attachments[index] as! AttachmentMO - let processor = ResizingImageProcessor(referenceSize: smallAttachmentSize, mode: .aspectFill) imageView.contentMode = UIImageView.ContentMode.scaleAspectFill imageView.isUserInteractionEnabled = true - imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(processor)]) + imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(smallResizingProcessor)]) let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped)) tapGestureRecognizer.delegate = self diff --git a/elpha-ios/ComposeViewController.swift b/elpha-ios/ComposeViewController.swift index 7f374ad..b786afc 100644 --- a/elpha-ios/ComposeViewController.swift +++ b/elpha-ios/ComposeViewController.swift @@ -25,6 +25,10 @@ class ComposeViewController: UIViewController { var feedbackGenerator: UINotificationFeedbackGenerator? = nil var replyToStatus: StatusMO? = nil var composeAccessoryView: ComposeAccessoryView? = nil + var selectedVisibility: StatusVisibility = .public + + var attachmentInputView: AttachmentInputView? = nil + var visibilityInputView: VisibilityInputView? = nil override func viewDidLoad() { super.viewDidLoad() @@ -33,12 +37,18 @@ class ComposeViewController: UIViewController { statusTextView.layer.cornerRadius = 10 statusTextView.layer.masksToBounds = true + statusTextView.textContainerInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) replyToAvatarImageView.setRoundedCorners() - composeAccessoryView = ComposeAccessoryView(frame: CGRect(x: 0.0, y: 0.0, width: self.view.bounds.size.width, height: composeAccessoryViewHeight)) + composeAccessoryView = ComposeAccessoryView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: composeAccessoryViewHeight)) composeAccessoryView!.delegate = self - composeAccessoryView!.selectedVisibility = .public + composeAccessoryView!.selectedVisibility = selectedVisibility + + visibilityInputView = VisibilityInputView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 250)) + visibilityInputView!.delegate = self + + attachmentInputView = AttachmentInputView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 400)) statusTextView.delegate = self statusTextView.inputAccessoryView = composeAccessoryView @@ -79,15 +89,8 @@ class ComposeViewController: UIViewController { } @objc func keyboardNotification(notification: NSNotification) { - if let userInfo = notification.userInfo { - let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue - let endFrameY = endFrame?.origin.y ?? 0 - - if endFrameY >= UIScreen.main.bounds.size.height { - bottomConstraint.constant = 0 - } else { - bottomConstraint.constant = (endFrame?.size.height ?? 0.0) + 10 - } + if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { + bottomConstraint.constant = keyboardSize.height + 10 } } @@ -103,13 +106,15 @@ extension ComposeViewController: UITextViewDelegate { } } -extension ComposeViewController: ComposeAccessoryViewDelegate { +extension ComposeViewController: ComposeAccessoryViewDelegate, VisibilityInputViewDelegate { func attachmentTapped() { - AlertManager.shared.show(message: "Attachment support coming soon", category: .normal) + statusTextView.inputView = statusTextView.inputView == nil ? attachmentInputView : nil + statusTextView.reloadInputViews() } func visibilityTapped() { - print("Visibility tapped") + statusTextView.inputView = statusTextView.inputView == nil ? visibilityInputView : nil + statusTextView.reloadInputViews() } func tootTapped() { @@ -117,7 +122,7 @@ extension ComposeViewController: ComposeAccessoryViewDelegate { if !content.isEmpty && content.count <= characterLimit { feedbackGenerator?.prepare() - MastodonAPI.postStatus(content: content, replyToID: replyToStatus?.id, spoilerText: contentWarningTextField.text) { error in + MastodonAPI.postStatus(content: content, replyToID: replyToStatus?.id, spoilerText: contentWarningTextField.text, visibility: selectedVisibility) { error in guard error == nil else { self.feedbackGenerator?.notificationOccurred(.error) AlertManager.shared.show(message: "Couldn't toot!", category: .error) @@ -125,7 +130,7 @@ extension ComposeViewController: ComposeAccessoryViewDelegate { } self.feedbackGenerator?.notificationOccurred(.success) - AlertManager.shared.show(message: "Toot!") + AlertManager.shared.show(message: "Toot!", category: .normal) self.statusTextView.text = "" self.contentWarningTextField.text = "" @@ -134,4 +139,12 @@ extension ComposeViewController: ComposeAccessoryViewDelegate { } } } + + func visibilitySelected(visibility: StatusVisibility) { + selectedVisibility = visibility + composeAccessoryView?.selectedVisibility = visibility + + statusTextView.inputView = nil + statusTextView.reloadInputViews() + } } diff --git a/elpha-ios/MastodonAPI.swift b/elpha-ios/MastodonAPI.swift index 6c5f42b..dfa6ad0 100644 --- a/elpha-ios/MastodonAPI.swift +++ b/elpha-ios/MastodonAPI.swift @@ -29,8 +29,8 @@ enum AttachmentType: String { case unknown, image, gifv, video } -enum StatusVisibility: String { - case direct, `private`, unlisted, `public` +enum StatusVisibility: String, CaseIterable { + case `public`, unlisted, `private`, direct } public class PaginationItem: NSObject, NSCoding { diff --git a/elpha-ios/StatusView.swift b/elpha-ios/StatusView.swift index 7a61195..151fc61 100644 --- a/elpha-ios/StatusView.swift +++ b/elpha-ios/StatusView.swift @@ -269,40 +269,42 @@ class StatusView: UIView { if let content = status.content { contentTextView.attributedText = content.htmlAttributed(size: 15.0) - if status.hidden && spoilerView == nil { - let spoilerText = UILabel(frame: contentTextView.frame) - spoilerText.textColor = UIColor(named: "Primary") - spoilerText.font = UIFont.systemFont(ofSize: 15, weight: .bold) - spoilerText.textAlignment = .center - spoilerText.text = status.spoilerText - spoilerText.numberOfLines = 0 - spoilerText.translatesAutoresizingMaskIntoConstraints = false - - let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.light)) - blurEffectView.translatesAutoresizingMaskIntoConstraints = false - blurEffectView.contentView.addSubview(spoilerText) - - NSLayoutConstraint.activate([ - spoilerText.leadingAnchor.constraint(equalTo: blurEffectView.leadingAnchor), - spoilerText.trailingAnchor.constraint(equalTo: blurEffectView.trailingAnchor), - spoilerText.centerYAnchor.constraint(equalTo: blurEffectView.centerYAnchor), - ]) - - let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.reveal)) - tapGesture.numberOfTapsRequired = 1 - tapGesture.numberOfTouchesRequired = 1 - blurEffectView.addGestureRecognizer(tapGesture) - blurEffectView.isUserInteractionEnabled = true - - self.spoilerView = blurEffectView - self.contentView.addSubview(blurEffectView) - - NSLayoutConstraint.activate([ - blurEffectView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), - blurEffectView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), - blurEffectView.topAnchor.constraint(equalTo: contentTextView.topAnchor), - blurEffectView.bottomAnchor.constraint(equalTo: detailsView.topAnchor), - ]) + if status.hidden { + if spoilerView == nil { + let spoilerText = UILabel(frame: contentTextView.frame) + spoilerText.textColor = UIColor(named: "Primary") + spoilerText.font = UIFont.systemFont(ofSize: 15, weight: .bold) + spoilerText.textAlignment = .center + spoilerText.text = status.spoilerText + spoilerText.numberOfLines = 0 + spoilerText.translatesAutoresizingMaskIntoConstraints = false + + let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.light)) + blurEffectView.translatesAutoresizingMaskIntoConstraints = false + blurEffectView.contentView.addSubview(spoilerText) + + NSLayoutConstraint.activate([ + spoilerText.leadingAnchor.constraint(equalTo: blurEffectView.leadingAnchor), + spoilerText.trailingAnchor.constraint(equalTo: blurEffectView.trailingAnchor), + spoilerText.centerYAnchor.constraint(equalTo: blurEffectView.centerYAnchor), + ]) + + let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.reveal)) + tapGesture.numberOfTapsRequired = 1 + tapGesture.numberOfTouchesRequired = 1 + blurEffectView.addGestureRecognizer(tapGesture) + blurEffectView.isUserInteractionEnabled = true + + self.spoilerView = blurEffectView + self.contentView.addSubview(blurEffectView) + + NSLayoutConstraint.activate([ + blurEffectView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), + blurEffectView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), + blurEffectView.topAnchor.constraint(equalTo: contentTextView.topAnchor), + blurEffectView.bottomAnchor.constraint(equalTo: detailsView.topAnchor), + ]) + } } else { if let spoilerView = spoilerView { spoilerView.removeFromSuperview() diff --git a/elpha-ios/VisibilityInputView.swift b/elpha-ios/VisibilityInputView.swift new file mode 100644 index 0000000..8ea3a09 --- /dev/null +++ b/elpha-ios/VisibilityInputView.swift @@ -0,0 +1,58 @@ +// +// VisibilityInputView.swift +// elpha-ios +// +// Created by Dwayne Harris on 11/17/18. +// Copyright © 2018 Elpha. All rights reserved. +// + +import UIKit + +protocol VisibilityInputViewDelegate { + func visibilitySelected(visibility: StatusVisibility) +} + +class VisibilityInputView: UIView { + @IBOutlet var contentView: UIView! + @IBOutlet var visibilityPickerView: UIPickerView! + + public var delegate: VisibilityInputViewDelegate? = nil + + override init(frame: CGRect) { + super.init(frame: frame) + setup() + } + + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + setup() + } + + private func setup() { + Bundle.main.loadNibNamed("VisibilityInputView", owner: self, options: nil) + addSubview(contentView) + contentView.frame = self.bounds + contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + + visibilityPickerView.delegate = self + visibilityPickerView.dataSource = self + } +} + +extension VisibilityInputView: UIPickerViewDelegate, UIPickerViewDataSource { + func numberOfComponents(in pickerView: UIPickerView) -> Int { + return 1 + } + + func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { + return StatusVisibility.allCases.count + } + + func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { + return StatusVisibility.allCases[row].rawValue.capitalized + } + + func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { + delegate?.visibilitySelected(visibility: StatusVisibility.allCases[row]) + } +} diff --git a/elpha-ios/VisibilityInputView.xib b/elpha-ios/VisibilityInputView.xib new file mode 100644 index 0000000..84e5d07 --- /dev/null +++ b/elpha-ios/VisibilityInputView.xib @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +