Skip to content

Commit 8598245

Browse files
committed
Fix two or more buttons open same link in article
1 parent 5138098 commit 8598245

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

ForPDA/Sources/Modules/Article/ArticleBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ final class ArticleBuilder {
162162
// MARK: - Buttons
163163

164164
/// Returns UIView container with UIButton inside
165-
static func addButton(text: String, url: String, completion: @escaping () -> Void) -> UIView {
166-
let action = UIAction { _ in completion() }
165+
static func addButton(text: String, url: String, completion: @escaping (String) -> Void) -> UIView {
166+
let action = UIAction { _ in completion(url) }
167167
let button = UIButton(type: .system, primaryAction: action)
168168
button.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
169169
button.setTitle(text, for: .normal)

ForPDA/Sources/Modules/Article/ArticleVC.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ final class ArticleVC: PDAViewController<ArticleView> {
2424
@Injected(\.analyticsService) var analyticsService
2525

2626
private let viewModel: ArticleVMProtocol
27-
28-
private var externalLinkUrl: String?
2927

3028
// MARK: - Lifecycle
3129

@@ -98,10 +96,10 @@ final class ArticleVC: PDAViewController<ArticleView> {
9896
}
9997
}
10098

101-
private func externalLinkButtonTapped() {
102-
if let externalLinkUrl, let url = URL(string: externalLinkUrl), UIApplication.shared.canOpenURL(url) {
99+
private func externalLinkButtonTapped(_ link: String) {
100+
if let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
103101
UIApplication.shared.open(url)
104-
analyticsService.clickButtonInArticle(currentUrl: viewModel.article.url, targetUrl: externalLinkUrl)
102+
analyticsService.clickButtonInArticle(currentUrl: viewModel.article.url, targetUrl: url.absoluteString)
105103
}
106104
}
107105

@@ -133,9 +131,8 @@ final class ArticleVC: PDAViewController<ArticleView> {
133131
articleElement = ArticleBuilder.addGif(url: item.url)
134132

135133
case let item as ButtonElement:
136-
externalLinkUrl = item.url
137-
articleElement = ArticleBuilder.addButton(text: item.text, url: item.url) {
138-
self.externalLinkButtonTapped()
134+
articleElement = ArticleBuilder.addButton(text: item.text, url: item.url) { [weak self] link in
135+
self?.externalLinkButtonTapped(link)
139136
}
140137

141138
case let item as BulletListParentElement:

ForPDA/Sources/Services/Parsing/ParsingService.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ final class ParsingService {
186186
let text = try! element.text()
187187
articleElements.append(TextElement(text: text, isHeader: true))
188188
} else if try! element.iS("dl") {
189-
let charters = parseCharters(element)
190-
articleElements.append(BulletListParentElement(elements: charters))
189+
let bulletList = parseBulletList(element)
190+
articleElements.append(BulletListParentElement(elements: bulletList))
191191
} else if try! element.iS("ul") {
192192
let galCont = try! element.select("a[data-lightbox]")
193193
for gal in galCont {
@@ -228,19 +228,18 @@ final class ParsingService {
228228
}
229229
}
230230
} else if try! element.iS("dl") {
231-
let charters = parseCharters(element)
231+
let charters = parseBulletList(element)
232232
articleElements.append(BulletListParentElement(elements: charters))
233233
} else if try! element.iS("a") {
234234
let text = try! element.text()
235235
let url = try! element.attr("href")
236-
print("ADDING BUTTON 2")
237236
articleElements.append(ButtonElement(text: text, url: url))
238237
}
239238
}
240239
return articleElements
241240
}
242241

243-
private func parseCharters(_ element: Element) -> [BulletListElement] {
242+
private func parseBulletList(_ element: Element) -> [BulletListElement] {
244243
var charterElements: [BulletListElement] = []
245244
let elements = try! element.select("dd, dt")
246245

0 commit comments

Comments
 (0)