Skip to content

Commit 018ea16

Browse files
committed
Update loading related settings
1 parent 0b1afcc commit 018ea16

File tree

11 files changed

+171
-53
lines changed

11 files changed

+171
-53
lines changed

ForPDA/Resources/en.lproj/Localizable.strings

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@
7272
"background.dark" = "Dark";
7373
"background.black" = "Black";
7474
"advanced" = "Advanced";
75-
"fast.loading.system" = "Fast loading";
76-
"fast.loading.system.warning" = "After activiting this option some of last news may not load, also amount of likes and comments can be wrong";
75+
"fast.loading.news" = "Fast loading news";
76+
"fast.loading.news.warning" = "After activiting this option some of the last news may not load and/or show wrong amount of comments";
77+
"fast.loading.article" = "Fast loading article";
78+
"fast.loading.article.warning" = "After activiting this option you won't see likes on the comments automatically";
7779
"comments.show.likes" = "Show likes";
78-
"comments.show.likes.warning" = "After activiting this option you may see increase of internet traffic from this app, also likes may update with some delay";
80+
"comments.show.likes.warning" = "After activiting this option and \"Fast loading article\" you may see increase of traffic usage";
7981
"account" = "Account";
8082
"sign.out" = "Sign Out";
8183
"about.app" = "About App";

ForPDA/Resources/ru.lproj/Localizable.strings

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@
7272
"background.dark" = "Темный";
7373
"background.black" = "Черный";
7474
"advanced" = "Продвинутые";
75-
"fast.loading.system" = "Быстрая загрузка";
76-
"fast.loading.system.warning" = "При активации этой функции последние статьи могут не отображаться, а также количество лайков и комментариев может не соответствовать реальности";
75+
"fast.loading.news" = "Быстрая загрузка новостей";
76+
"fast.loading.news.warning" = "При активации этой функции последние статьи могут не отображаться, а количество лайков не соответствовать действительности";
77+
"fast.loading.article" = "Быстрая загрузка новости";
78+
"fast.loading.article.warning" = "После активации этой функции, количество лайков на комментариях не будет отображаться";
7779
"comments.show.likes" = "Отображать лайки";
78-
"comments.show.likes.warning" = "При активации этой функции может быть увеличеный расход трафика в приложении, также лайки могут отображаться с задержкой";
80+
"comments.show.likes.warning" = "При активации этой функции вместе с \"Быстрая загрузка новости\" может быть увеличенный расход трафика";
7981
"account" = "Аккаунт";
8082
"sign.out" = "Выйти";
8183
"about.app" = "О приложении";

ForPDA/Sources/Modules/Article/ArticlePresenter.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final class ArticlePresenter: ArticlePresenterProtocol {
2121

2222
@Injected(\.newsService) private var newsService
2323
@Injected(\.parsingService) private var parsingService
24+
@Injected(\.settingsService) private var settingsService
2425

2526
weak var view: ArticleVCProtocol?
2627

@@ -51,6 +52,8 @@ final class ArticlePresenter: ArticlePresenterProtocol {
5152
let elements = parsingService.parseArticle(from: response)
5253
view?.configureArticle(with: elements)
5354
view?.makeComments(from: response)
55+
56+
await showLikesIfNeeded()
5457
} catch {
5558
view?.showError()
5659
}
@@ -59,10 +62,21 @@ final class ArticlePresenter: ArticlePresenterProtocol {
5962
@MainActor
6063
func updateComments() async {
6164
do {
62-
let response = try await newsService.article(path: path)
65+
let response = try await newsService.comments(path: path)
6366
view?.updateComments(with: response)
6467
} catch {
6568
view?.showError()
6669
}
6770
}
71+
72+
// MARK: - Private Functions
73+
74+
private func showLikesIfNeeded() async {
75+
let articleUsesFLS = settingsService.getArticleFLS()
76+
let showLikes = settingsService.getShowLikesInComments()
77+
78+
if articleUsesFLS && showLikes {
79+
await updateComments()
80+
}
81+
}
6882
}

ForPDA/Sources/Modules/Settings/Cells/SettingsSwitchCell.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ final class SettingsSwitchCell: UITableViewCell {
5454
mySwitch.isOn = model.isOn
5555
}
5656

57+
func forceSwitch(to state: Bool) {
58+
mySwitch.isOn = state
59+
}
60+
5761
// MARK: - Layout
5862

5963
private func addSubviews() {

ForPDA/Sources/Modules/Settings/SettingsPresenter.swift

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ protocol SettingsPresenterProtocol {
1313

1414
func changeTheme(to theme: AppTheme)
1515
func changeDarkThemeBackgroundColor(to color: AppDarkThemeBackgroundColor)
16-
func showFastLoadingSystemSwitchTapped(isOn: Bool)
16+
func showNewsFLSSwitchTapped(isOn: Bool)
17+
func showArticleFLSSwitchTapped(isOn: Bool)
1718
func showLikesInCommentsSwitchTapped(isOn: Bool)
1819
}
1920

@@ -51,8 +52,12 @@ final class SettingsPresenter: SettingsPresenterProtocol {
5152
settingsService.getShowLikesInComments()
5253
}
5354

54-
private var currentLoadingSystemIsFast: Bool {
55-
settingsService.getFastLoadingSystem()
55+
private var newsUsesFLS: Bool {
56+
settingsService.getNewsFLS()
57+
}
58+
59+
private var articleUsesFLS: Bool {
60+
settingsService.getArticleFLS()
5661
}
5762

5863
private let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0"
@@ -76,15 +81,23 @@ final class SettingsPresenter: SettingsPresenterProtocol {
7681
handler: changeDarkThemeBackgroundColor))
7782
]),
7883

79-
// Comments
80-
MenuSection(title: R.string.localizable.advanced(), options: [
81-
.switchCell(model: SwitchOption(title: R.string.localizable.fastLoadingSystem(),
82-
isOn: currentLoadingSystemIsFast,
83-
handler: {})),
84-
.switchCell(model: SwitchOption(title: R.string.localizable.commentsShowLikes(),
85-
isOn: currentShowLikesInComments,
86-
handler: {}))
87-
]),
84+
// News & Comments
85+
MenuSection(
86+
title: R.string.localizable.advanced(),
87+
options: [
88+
.switchCell(model: SwitchOption(
89+
title: R.string.localizable.fastLoadingNews(),
90+
isOn: newsUsesFLS, handler: {})
91+
),
92+
.switchCell(model: SwitchOption(
93+
title: R.string.localizable.fastLoadingArticle(),
94+
isOn: articleUsesFLS, handler: {})
95+
),
96+
.switchCell(model: SwitchOption(
97+
title: R.string.localizable.commentsShowLikes(),
98+
isOn: currentShowLikesInComments, handler: {})
99+
)
100+
]),
88101

89102
// Account
90103
MenuSection(title: R.string.localizable.account(), options: [
@@ -112,8 +125,13 @@ final class SettingsPresenter: SettingsPresenterProtocol {
112125
reloadData()
113126
}
114127

115-
func showFastLoadingSystemSwitchTapped(isOn: Bool) {
116-
settingsService.setFastLoadingSystem(to: isOn)
128+
func showNewsFLSSwitchTapped(isOn: Bool) {
129+
settingsService.setNewsFLS(to: isOn)
130+
reloadData(forceUpdate: false)
131+
}
132+
133+
func showArticleFLSSwitchTapped(isOn: Bool) {
134+
settingsService.setArticleFLS(to: isOn)
117135
reloadData(forceUpdate: false)
118136
}
119137

@@ -162,16 +180,22 @@ final class SettingsPresenter: SettingsPresenterProtocol {
162180
sections[1].options[1] = .descriptionCell(model: model2)
163181

164182
let model3 = SwitchOption(
165-
title: R.string.localizable.fastLoadingSystem(),
166-
isOn: settingsService.getFastLoadingSystem(),
183+
title: R.string.localizable.fastLoadingNews(),
184+
isOn: settingsService.getNewsFLS(),
167185
handler: {})
168186
sections[2].options[0] = .switchCell(model: model3)
169187

170188
let model4 = SwitchOption(
189+
title: R.string.localizable.fastLoadingArticle(),
190+
isOn: settingsService.getArticleFLS(),
191+
handler: {})
192+
sections[2].options[1] = .switchCell(model: model4)
193+
194+
let model5 = SwitchOption(
171195
title: R.string.localizable.commentsShowLikes(),
172196
isOn: settingsService.getShowLikesInComments(),
173197
handler: {})
174-
sections[2].options[1] = .switchCell(model: model4)
198+
sections[2].options[2] = .switchCell(model: model5)
175199

176200
if forceUpdate {
177201
view?.reloadData()

ForPDA/Sources/Modules/Settings/SettingsVC.swift

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ extension SettingsVC: UITableViewDelegate, UITableViewDataSource {
6868
return 46
6969
}
7070

71-
// Refactor this
71+
// Refactor this (todo)
7272
// swiftlint:disable cyclomatic_complexity function_body_length
7373
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
7474
let model = presenter.sections[indexPath.section].options[indexPath.row]
@@ -91,18 +91,29 @@ extension SettingsVC: UITableViewDelegate, UITableViewDataSource {
9191
cell.switchTapped = { [weak self] isOn in
9292
guard let self else { return }
9393

94+
// When switching off
9495
if !isOn {
95-
if indexPath.row == 0 {
96-
self.presenter.showFastLoadingSystemSwitchTapped(isOn: isOn)
97-
} else {
98-
self.presenter.showLikesInCommentsSwitchTapped(isOn: isOn)
96+
switch indexPath.row {
97+
case 0:
98+
presenter.showNewsFLSSwitchTapped(isOn: isOn)
99+
case 1:
100+
presenter.showArticleFLSSwitchTapped(isOn: isOn)
101+
// Workaround for always working comments on article sls
102+
if let cell = tableView.cellForRow(at: IndexPath(row: 2, section: 2)) as? SettingsSwitchCell {
103+
cell.forceSwitch(to: true)
104+
presenter.showLikesInCommentsSwitchTapped(isOn: true)
105+
}
106+
default:
107+
presenter.showLikesInCommentsSwitchTapped(isOn: isOn)
99108
}
100109
return
101110
}
102111

103112
var message = ""
104-
if model.title.contains("Быстрая") {
105-
message = R.string.localizable.fastLoadingSystemWarning()
113+
if model.title.contains("новостей") {
114+
message = R.string.localizable.fastLoadingNewsWarning()
115+
} else if model.title.contains("новости") {
116+
message = R.string.localizable.fastLoadingArticleWarning()
106117
} else {
107118
message = R.string.localizable.commentsShowLikesWarning()
108119
}
@@ -113,19 +124,27 @@ extension SettingsVC: UITableViewDelegate, UITableViewDataSource {
113124
preferredStyle: .alert
114125
)
115126

116-
let okAction = UIAlertAction(title: R.string.localizable.ok(), style: .default) { _ in
117-
if indexPath.row == 0 {
118-
self.presenter.showFastLoadingSystemSwitchTapped(isOn: isOn)
119-
} else {
120-
self.presenter.showLikesInCommentsSwitchTapped(isOn: isOn)
127+
let okAction = UIAlertAction(title: R.string.localizable.ok(), style: .default) { [weak self] _ in
128+
guard let self else { return }
129+
switch indexPath.row {
130+
case 0:
131+
presenter.showNewsFLSSwitchTapped(isOn: isOn)
132+
case 1:
133+
presenter.showArticleFLSSwitchTapped(isOn: isOn)
134+
default:
135+
presenter.showLikesInCommentsSwitchTapped(isOn: isOn)
121136
}
122137
}
123-
let cancelAction = UIAlertAction(title: R.string.localizable.cancel(), style: .default) { _ in
138+
let cancelAction = UIAlertAction(title: R.string.localizable.cancel(), style: .default) { [weak self] _ in
139+
guard let self else { return }
124140
cell.set(with: model)
125-
if indexPath.row == 0 {
126-
self.presenter.showFastLoadingSystemSwitchTapped(isOn: !isOn)
127-
} else {
128-
self.presenter.showLikesInCommentsSwitchTapped(isOn: !isOn)
141+
switch indexPath.row {
142+
case 0:
143+
presenter.showNewsFLSSwitchTapped(isOn: !isOn)
144+
case 1:
145+
presenter.showArticleFLSSwitchTapped(isOn: !isOn)
146+
default:
147+
presenter.showLikesInCommentsSwitchTapped(isOn: !isOn)
129148
}
130149
}
131150

ForPDA/Sources/Services/Network/Base/Endpoint.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protocol Endpoint {
2121

2222
extension Endpoint {
2323
/// Forces Fast Loading System if not stated otherwise (e.g. news)
24+
/// Has sort of overriding in HTTPClient request function
2425
var forceFLS: Bool {
2526
return true
2627
}

ForPDA/Sources/Services/Network/Base/HTTPClient.swift

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,40 @@ extension HTTPClient {
4646
return try await fastRequest(endpoint: endpoint)
4747
}
4848

49-
if settingsService.getFastLoadingSystem() || endpoint.forceFLS {
50-
return try await fastRequest(endpoint: endpoint)
51-
} else {
52-
return try await slowRequest(endpoint: endpoint)
49+
// Test later (todo)
50+
// if (endpoint as? NewsEndpoint != nil) && settingsService.getIsDeeplinking() {
51+
// return try await fastRequest(endpoint: endpoint)
52+
// }
53+
54+
// Totally needs refactoring (todo)
55+
switch endpoint {
56+
case let newsEndpoint as NewsEndpoint:
57+
switch newsEndpoint {
58+
case .news:
59+
if settingsService.getNewsFLS() {
60+
return try await fastRequest(endpoint: endpoint)
61+
} else {
62+
return try await slowRequest(endpoint: endpoint)
63+
}
64+
case .article:
65+
if settingsService.getArticleFLS() {
66+
return try await fastRequest(endpoint: endpoint)
67+
} else {
68+
return try await slowRequest(endpoint: endpoint)
69+
}
70+
case .comments:
71+
if settingsService.getShowLikesInComments() {
72+
return try await slowRequest(endpoint: endpoint)
73+
} else {
74+
return try await fastRequest(endpoint: endpoint)
75+
}
76+
}
77+
default:
78+
if endpoint.forceFLS {
79+
return try await fastRequest(endpoint: endpoint)
80+
} else {
81+
return try await slowRequest(endpoint: endpoint)
82+
}
5383
}
5484
}
5585

ForPDA/Sources/Services/Network/News/NewsEndpoint.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
enum NewsEndpoint {
1111
case news(page: Int)
1212
case article(path: [String])
13+
case comments(path: [String])
1314
}
1415

1516
extension NewsEndpoint: Endpoint {
@@ -24,6 +25,9 @@ extension NewsEndpoint: Endpoint {
2425

2526
case .article(path: let path):
2627
return path.joined(separator: "/").removing(prefix: "/")
28+
29+
case .comments(path: let path):
30+
return path.joined(separator: "/").removing(prefix: "/")
2731
}
2832
}
2933

ForPDA/Sources/Services/Network/News/NewsService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
protocol NewsServicable {
1111
func news(page: Int) async throws -> String
1212
func article(path: [String]) async throws -> String
13+
func comments(path: [String]) async throws -> String
1314
}
1415

1516
final class NewsService: HTTPClient, NewsServicable {
@@ -21,4 +22,8 @@ final class NewsService: HTTPClient, NewsServicable {
2122
func article(path: [String]) async throws -> String {
2223
return try await request(endpoint: NewsEndpoint.article(path: path))
2324
}
25+
26+
func comments(path: [String]) async throws -> String {
27+
return try await request(endpoint: NewsEndpoint.comments(path: path))
28+
}
2429
}

0 commit comments

Comments
 (0)