Skip to content

Instantly share code, notes, and snippets.

@dennda
Created May 21, 2018 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennda/acd20cc724ad61ad292bc0df9da74f2d to your computer and use it in GitHub Desktop.
Save dennda/acd20cc724ad61ad292bc0df9da74f2d to your computer and use it in GitHub Desktop.
diff --git a/DuolingoMobile/Sources/Explanations/ExplanationCells.swift b/DuolingoMobile/Sources/Explanations/ExplanationCells.swift
index b29ff18c1..aa34831f2 100644
--- a/DuolingoMobile/Sources/Explanations/ExplanationCells.swift
+++ b/DuolingoMobile/Sources/Explanations/ExplanationCells.swift
@@ -209,7 +209,7 @@ class OptionsCell: ExplanationCell {
let missingButtons = max(0, model.count - buttons.count)
if missingButtons > 0 {
let newButtons = (0..<missingButtons).map { _ in
- OptionsCell.makeButton()
+ OptionsCell.makeButton(target: self)
}
for button in newButtons {
buttons.append(button)
@@ -233,36 +233,38 @@ class OptionsCell: ExplanationCell {
}
}
- var optionTapped: ((Int) -> Void)?
+ var optionTapped: ((OptionsCell, Int) -> Void)?
typealias RadioButton = DUOCheckBoxButton
- private var buttons: [RadioButton] = {
- (0..<2).map { _ in
- OptionsCell.makeButton()
- }
- }()
+ private var buttons: [RadioButton] = []
- private static func makeButton() -> RadioButton {
+ private static func makeButton(target: OptionsCell?) -> RadioButton {
let style = DUOStyle.default()
let radioButton = DUOCheckBoxButton(frame: .zero, style: style)!
radioButton.isRadioButton = true
radioButton.font = style.lightFont
radioButton.textAlignment = .left
radioButton.layOutRightToLeft = false
+ if let target = target {
+ radioButton.addTarget(target, action: #selector(OptionsCell.handleOptionTap(_:)), for: .touchUpInside)
+ }
return radioButton
}
@objc override init(frame: CGRect) {
super.init(frame: frame)
+ buttons = (0..<2).map { _ in
+ OptionsCell.makeButton(target: self)
+ }
for button in buttons {
contentView.addSubview(button)
}
- // XXX don't need this
- let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(OptionsCell.handleTap(_:)))
- contentView.addGestureRecognizer(tapRecognizer)
+// // XXX don't need this
+// let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(OptionsCell.handleTap(_:)))
+// contentView.addGestureRecognizer(tapRecognizer)
}
override func layoutSubviews() {
@@ -330,7 +332,7 @@ class OptionsCell: ExplanationCell {
static func height(forModel model: Model, maxWidth: CGFloat) -> CGFloat {
let buttons: [RadioButton] = model.map {
- let button = self.makeButton()
+ let button = self.makeButton(target: nil)
button.text = $0.text
return button
}
@@ -339,12 +341,16 @@ class OptionsCell: ExplanationCell {
}
@objc
- func handleTap(_ sender: UITapGestureRecognizer) {
+ func handleOptionTap(_ sender: RadioButton) {
guard let currentModel = currentModel else {
return
}
- optionTapped?(0)
+ guard let buttonIndex = buttons.index(of: sender) else {
+ assertionFailure("Button that was tapped doesn't exist?")
+ return
+ }
+ optionTapped?(self, buttonIndex)
}
required init?(coder aDecoder: NSCoder) {
diff --git a/DuolingoMobile/Sources/Explanations/ExplanationsVC.swift b/DuolingoMobile/Sources/Explanations/ExplanationsVC.swift
index 91cc8520f..87ffc348b 100644
--- a/DuolingoMobile/Sources/Explanations/ExplanationsVC.swift
+++ b/DuolingoMobile/Sources/Explanations/ExplanationsVC.swift
@@ -387,14 +387,16 @@ class ExplanationsVC: UIViewController, UICollectionViewDataSource, UICollection
private var images: [URL: PDFImage] = [:]
- private func flattenedElement(atIndex index: Int) -> Either<(Element, isPartOfChallenge: Bool), [Option]> {
+ typealias ChallengeID = String
+
+ private func flattenedElement(atIndex index: Int) -> Either<(Element, isPartOfChallenge: Bool), (identifier: ChallengeID, options: [Option])> {
// XXX maybe don't compute this all the time.
var flattenedElements: [Either<(ExplanationsElement, isPartOfChallenge: Bool), [Option]>] = []
for element in currentElements {
switch element {
case .challenge(let model):
flattenedElements += model.elements.map({ .left(($0, isPartOfChallenge: true)) })
- flattenedElements += [.right(model.options)]
+ flattenedElements += [.right((model.identifier, model.options))]
default:
flattenedElements += [.left((element, isPartOfChallenge: false))]
}
@@ -483,7 +485,7 @@ class ExplanationsVC: UIViewController, UICollectionViewDataSource, UICollection
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OptionsCell.reuseIdentifier(),
for: indexPath) as! OptionsCell
cell.currentModel = options
- cell.optionTapped = { [weak self] optionIndex in
+ cell.optionTapped = { [weak self] (cell, optionIndex) in
guard
let strongSelf = self,
let oldExplanation = strongSelf.currentExplanation
@@ -495,6 +497,11 @@ class ExplanationsVC: UIViewController, UICollectionViewDataSource, UICollection
// Tell the server that this challenge was answered.
// XXX ?
+ // Determine the challenge that was answered.
+ let indexPath = strongSelf.collectionView.indexPath(for: cell)
+ let challengeModel: ChallengeElementModel
+ let element = currentElements[indexPath?.row]
+
// Mark the challenge as answered.
let newChallengeModel = challengeModel.answered
let newElement = ExplanationsElement.challenge(newChallengeModel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment