Skip to content

Commit

Permalink
Merge pull request wikimedia#3769 from wikimedia/T267849
Browse files Browse the repository at this point in the history
AaaLD - Navigate to particular talk page topic if possible
  • Loading branch information
tonisevener authored Nov 19, 2020
2 parents 93a38c4 + 6cbd044 commit f3ec62f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public extension ArticleAsLivingDocViewModel {

public enum ButtonsToDisplay {
case thankAndViewChanges(userId: UInt, revisionId: UInt)
case viewDiscussion(sectionName: String)
case viewDiscussion(sectionName: String?)
}

public let typedEvent: SignificantEvents.TypedEvent
Expand Down Expand Up @@ -599,7 +599,13 @@ public extension ArticleAsLivingDocViewModel {
case .newTalkPageTopic(let newTalkPageTopic):
self.userId = newTalkPageTopic.userId
userGroups = newTalkPageTopic.userGroups
self.buttonsToDisplay = .viewDiscussion(sectionName: newTalkPageTopic.section)

if let talkPageSection = newTalkPageTopic.section {
self.buttonsToDisplay = .viewDiscussion(sectionName: Self.sectionTitleWithWikitextStripped(originalTitle: talkPageSection))
} else {
self.buttonsToDisplay = .viewDiscussion(sectionName: nil)
}

case .large(let largeChange):
self.userId = largeChange.userId
userGroups = largeChange.userGroups
Expand Down Expand Up @@ -637,7 +643,6 @@ public extension ArticleAsLivingDocViewModel {
revId = 0
parentId = 0
}

}

public static func == (lhs: ArticleAsLivingDocViewModel.Event.Large, rhs: ArticleAsLivingDocViewModel.Event.Large) -> Bool {
Expand Down Expand Up @@ -905,17 +910,25 @@ public extension ArticleAsLivingDocViewModel.Event.Large {
}

//strip == signs from all section titles
let finalSet = set.map { (section) -> String in
if let match = section.range(of: "(?<==)[^=]+", options: .regularExpression) {
return String(section[match])
}

return section
}
let finalSet = set.map { Self.sectionTitleWithWikitextStripped(originalTitle: $0) }

return Set(finalSet)
}

//remove one or more equal signs and zero or more spaces on either side of the title text
private static func sectionTitleWithWikitextStripped(originalTitle: String) -> String {
var loopTitle = originalTitle

let regex = "^=+\\s*|\\s*=+$"
var maybeMatch = loopTitle.range(of: regex, options: .regularExpression)
while let match = maybeMatch {
loopTitle.removeSubrange(match)
maybeMatch = loopTitle.range(of: regex, options: .regularExpression)
}

return loopTitle
}

private func localizedStringFromSections(sections: [String]) -> String? {
var localizedString: String
switch sections.count {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public extension SignificantEvents {
public let timestampString: String
public let user: String
public let userId: UInt
public let section: String
public let section: String?
public let snippet: String
public let userGroups: [String]?
public let userEditCount: UInt?
Expand All @@ -242,7 +242,6 @@ public extension SignificantEvents {
let timestampString = untypedEvent.timestampString,
let user = untypedEvent.user,
let userId = untypedEvent.userId,
let section = untypedEvent.section,
let snippet = untypedEvent.snippet else {
return nil
}
Expand All @@ -253,7 +252,7 @@ public extension SignificantEvents {
self.timestampString = timestampString
self.user = user
self.userId = userId
self.section = section
self.section = untypedEvent.section
self.snippet = snippet
self.userGroups = untypedEvent.userGroups
self.userEditCount = untypedEvent.userEditCount
Expand Down
6 changes: 6 additions & 0 deletions WMF Framework/String+LinkParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public extension String {
return replacingOccurrences(of: " ", with: "_").precomposedStringWithCanonicalMapping
}

var asTalkPageFragment: String? {
let denormalizedName = replacingOccurrences(of: " ", with: "_")
let unlinkedName = denormalizedName.replacingOccurrences(of: "[[", with: "").replacingOccurrences(of: "]]", with: "")
return unlinkedName.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.wmf_encodeURIComponentAllowed())
}

//assumes string is already normalized
var googleFormPercentEncodedPageTitle: String? {
return googleFormPageTitle?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,17 @@ class ArticleAsLivingDocLargeEventCollectionViewCell: CollectionViewCell {
ArticleAsLivingDocFunnel.shared.logModalViewDiscussionButtonTapped(position: loggingPosition)
}

articleDelegate?.showTalkPage()
guard let largeEvent = largeEvent else {
return
}

switch largeEvent.buttonsToDisplay {
case .viewDiscussion(let sectionName):
articleDelegate?.showTalkPageWithSectionName(sectionName)
default:
assertionFailure("Unexpected button type")
articleDelegate?.showTalkPageWithSectionName(nil)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ protocol ArticleAsLivingDocViewControllerDelegate: class {
func fetchNextPage(nextRvStartId: UInt, theme: Theme)
func showEditHistory()
func handleLink(with href: String)
func showTalkPage()
func livingDocViewWillAppear()
func livingDocViewWillPush()
}

protocol ArticleDetailsShowing: class {
func goToHistory()
func goToDiff(revisionId: UInt, parentId: UInt, diffType: DiffContainerViewModel.DiffType)
func showTalkPage()
func showTalkPageWithSectionName(_ sectionName: String?)
func thankButtonTapped(for revisionID: Int, isUserAnonymous: Bool, livingDocLoggingValues: ArticleAsLivingDocLoggingValues)
}

Expand Down Expand Up @@ -442,11 +441,19 @@ extension ArticleAsLivingDocViewController: ArticleAsLivingDocHorizontallyScroll

@available(iOS 13.0, *)
extension ArticleAsLivingDocViewController: ArticleDetailsShowing {
func showTalkPage() {
guard let talkPageURL = delegate?.articleURL.articleTalkPage else {
func showTalkPageWithSectionName(_ sectionName: String?) {

var maybeTalkPageURL = delegate?.articleURL.articleTalkPage
if let convertedSectionName = sectionName?.asTalkPageFragment,
let talkPageURL = maybeTalkPageURL {
maybeTalkPageURL = URL(string: talkPageURL.absoluteString + "#" + convertedSectionName)
}

guard let talkPageURL = maybeTalkPageURL else {
showGenericError()
return
}

navigate(to: talkPageURL)
}

Expand Down

0 comments on commit f3ec62f

Please sign in to comment.