Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion Modules/Sources/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public struct APIClient: Sendable {

// Bookmarks
public var getBookmarksList: @Sendable () async throws -> [Bookmark]
public var addBookmark: @Sendable (_ parentId: Int, _ name: String, _ format: Bookmark.Format) async throws -> Bool
public var deleteBookmark: @Sendable (_ bookmark: Bookmark) async throws -> Bool
public var modifyBookmark: @Sendable (_ bookmark: Bookmark, _ parentId: Int, _ name: String) async throws -> Bool

// Forum
public var getForumsList: @Sendable (_ policy: CachePolicy) async throws -> AsyncThrowingStream<[ForumInfo], any Error>
Expand Down Expand Up @@ -355,6 +358,21 @@ extension APIClient: DependencyKey {
let response = try await api.send(MemberCommand.Bookmarks.list)
return try await parser.parseBookmarksList(response)
},
addBookmark: { parentId, name, format in
let request = BookmarkRequest(parentId: parentId, name: name, number: 0, format: format.request, isDeleted: false)
let response = try await api.send(MemberCommand.Bookmarks.add(data: request))
return Int(response.getResponseStatus()) == 0
},
deleteBookmark: { bookmark in
let request = BookmarkRequest(bookmark, isDeleted: true)
let response = try await api.send(MemberCommand.Bookmarks.modify(id: bookmark.id, data: request))
return Int(response.getResponseStatus()) == 0
},
modifyBookmark: { bookmark, parentId, name in
let request = BookmarkRequest(bookmark, parentId: parentId, name: name)
let response = try await api.send(MemberCommand.Bookmarks.modify(id: bookmark.id, data: request))
return Int(response.getResponseStatus()) == 0
},

// MARK: - Forum

Expand Down Expand Up @@ -817,7 +835,16 @@ extension APIClient: DependencyKey {
return true
},
getBookmarksList: {
return [.mockArticle, .mockForum, .mockUser]
return [.mockFolder, .mockSubFolder, .mockArticle, .mockForum, .mockUser]
},
addBookmark: { _, _, _ in
return true
},
deleteBookmark: { _ in
return true
},
modifyBookmark: { _, _, _ in
return true
},
getForumsList: { _ in
return .finished()
Expand Down Expand Up @@ -1047,3 +1074,29 @@ extension String {
.components(separatedBy: ",")[number]
}
}

// MARK: - Bookmark Request

private extension BookmarkRequest {
init(_ bookmark: Bookmark, parentId: Int? = nil, name: String? = nil, isDeleted: Bool = false) {
self.init(
parentId: parentId ?? bookmark.parentId,
name: name ?? bookmark.name,
number: bookmark.number,
format: bookmark.format.request,
isDeleted: isDeleted
)
}
}

private extension Bookmark.Format {
/// Site expects absolute links
var request: BookmarkRequest.BookmarkFormat {
switch self {
case .folder:
return .folder
case let .url(url):
return .url(url: (absoluteURL ?? url).absoluteString)
}
}
}
5 changes: 3 additions & 2 deletions Modules/Sources/AnalyticsClient/Events/ForumEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum ForumEvent: Event {
case sectionExpandTapped(String)

// case menuSort
// case menuBookmarks
case menuAddToBookmarks(Int)

case menuOpen(Int)
case menuGoToEnd(Int)
Expand Down Expand Up @@ -61,7 +61,8 @@ public enum ForumEvent: Event {
case let .menuGoToEnd(id):
return ["id": String(id)]

case let .menuEdit(id):
case let .menuEdit(id),
let .menuAddToBookmarks(id):
return ["id": String(id)]

case let .menuStat(id, isForum):
Expand Down
1 change: 1 addition & 0 deletions Modules/Sources/AnalyticsClient/Events/TopicEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum TopicEvent: Event {
case menuOpenInBrowser
case menuGoToEnd
case menuSetFavorite
case menuAddToBookmarks
case menuAboutTopic
case menuEditTopic
case menuWritePost
Expand Down
18 changes: 16 additions & 2 deletions Modules/Sources/AppFeature/AppFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public struct AppFeature: Reducer, Sendable {
public var articlesTab: StackTab.State
public var favoritesTab: StackTab.State
public var forumTab: StackTab.State
public var bookmarksTab: StackTab.State
public var moreTab: StackTab.State

@Presents public var logStore: LogStoreFeature.State?
Expand Down Expand Up @@ -102,6 +103,7 @@ public struct AppFeature: Reducer, Sendable {
articlesTab: StackTab.State = StackTab.State(root: .articles(.articlesList(ArticlesListFeature.State()))),
favoritesTab: StackTab.State = StackTab.State(root: .favorites(FavoritesFeature.State())),
forumTab: StackTab.State = StackTab.State(root: .forum(.forumList(ForumsListFeature.State()))),
bookmarksTab: StackTab.State = StackTab.State(root: .more(.bookmarks(BookmarksFeature.State()))),
moreTab: StackTab.State = StackTab.State(root: .more(.more(MoreFeature.State()))),
alert: AlertState<Never>? = nil,
selectedTab: AppTab = .articles,
Expand All @@ -114,6 +116,7 @@ public struct AppFeature: Reducer, Sendable {
self.articlesTab = articlesTab
self.favoritesTab = favoritesTab
self.forumTab = forumTab
self.bookmarksTab = bookmarksTab
self.moreTab = moreTab

self.alert = alert
Expand All @@ -139,6 +142,7 @@ public struct AppFeature: Reducer, Sendable {
case articlesTab(StackTab.Action)
case favoritesTab(StackTab.Action)
case forumTab(StackTab.Action)
case bookmarksTab(StackTab.Action)
case moreTab(StackTab.Action)

case logStore(PresentationAction<LogStoreFeature.Action>)
Expand Down Expand Up @@ -203,6 +207,10 @@ public struct AppFeature: Reducer, Sendable {
StackTab()
}

Scope(\.bookmarksTab, action: \.bookmarksTab) {
StackTab()
}

Scope(\.moreTab, action: \.moreTab) {
StackTab()
}
Expand Down Expand Up @@ -516,19 +524,21 @@ public struct AppFeature: Reducer, Sendable {
case let .articlesTab(.delegate(.showTabBar(show))),
let .favoritesTab(.delegate(.showTabBar(show))),
let .forumTab(.delegate(.showTabBar(show))),
let .bookmarksTab(.delegate(.showTabBar(show))),
let .moreTab(.delegate(.showTabBar(show))):
state.showTabBar = show
return .none

case let .articlesTab(.delegate(.switchTab(to: tab))),
let .favoritesTab(.delegate(.switchTab(to: tab))),
let .forumTab(.delegate(.switchTab(to: tab))),
let .bookmarksTab(.delegate(.switchTab(to: tab))),
let .moreTab(.delegate(.switchTab(to: tab))):
state.previousTab = state.selectedTab
state.selectedTab = tab
return .none

case .articlesTab, .favoritesTab, .forumTab, .moreTab:
case .articlesTab, .favoritesTab, .forumTab, .bookmarksTab, .moreTab:
return .none

case .appDelegate:
Expand Down Expand Up @@ -558,6 +568,9 @@ public struct AppFeature: Reducer, Sendable {
case .forum:
state.forumTab.path.removeAll()

case .bookmarks:
state.bookmarksTab.path.removeAll()

case .more:
state.moreTab.path.removeAll()
}
Expand All @@ -575,7 +588,7 @@ public struct AppFeature: Reducer, Sendable {
private func removeNotifications(_ state: inout State) -> Effect<Action> {
return .run { [tab = state.selectedTab] _ in
switch tab {
case .articles, .forum, .more:
case .articles, .forum, .bookmarks, .more:
break
case .favorites:
break
Expand All @@ -594,6 +607,7 @@ public struct AppFeature: Reducer, Sendable {
case .articles: return .send(.articlesTab(action))
case .favorites: return .send(.favoritesTab(action))
case .forum: return .send(.forumTab(action))
case .bookmarks: return .send(.bookmarksTab(action))
case .more: return .send(.moreTab(action))
}
}
Expand Down
15 changes: 14 additions & 1 deletion Modules/Sources/AppFeature/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ struct LiquidTabView: View {
}
.badge(store.favoritesBadges)

Tab(
AppTab.bookmarks.title,
systemSymbol: AppTab.bookmarks.iconSymbol,
value: .bookmarks
) {
StackTabView(store: store.scope(\.bookmarksTab, action: \.bookmarksTab))
}

Tab(
AppTab.forum.title,
systemSymbol: AppTab.forum.iconSymbol,
Expand Down Expand Up @@ -202,7 +210,7 @@ struct LiquidTabView: View {
struct OldTabView: View {

@Perception.Bindable var store: StoreOf<AppFeature>
@State private var shouldAnimatedTabItem: [Bool] = [false, false, false, false]
@State private var shouldAnimatedTabItem: [Bool] = Array(repeating: false, count: AppTab.allCases.count)
@Environment(\.backgroundColors) private var backgroundColors

var body: some View {
Expand All @@ -214,6 +222,9 @@ struct OldTabView: View {
StackTabView(store: store.scope(\.favoritesTab, action: \.favoritesTab))
.tag(AppTab.favorites)

StackTabView(store: store.scope(\.bookmarksTab, action: \.bookmarksTab))
.tag(AppTab.bookmarks)

StackTabView(store: store.scope(\.forumTab, action: \.forumTab))
.tag(AppTab.forum)

Expand Down Expand Up @@ -431,6 +442,8 @@ extension LiquidTabView {
return getPage(for: store.scope(\.favoritesTab, action: \.favoritesTab))
case .forum:
return getPage(for: store.scope(\.forumTab, action: \.forumTab))
case .bookmarks:
return getPage(for: store.scope(\.bookmarksTab, action: \.bookmarksTab))
case .more:
return getPage(for: store.scope(\.moreTab, action: \.moreTab))
}
Expand Down
6 changes: 6 additions & 0 deletions Modules/Sources/AppFeature/Navigation/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ComposableArchitecture
import AnnouncementFeature
import ArticleFeature
import ArticlesListFeature
import BookmarksFeature
import DeveloperFeature
import DeviceSpecificationsFeature
import DeviceTypeFeature
Expand Down Expand Up @@ -64,6 +65,7 @@ public enum Path {
case more(MoreFeature)
case profile(ProfileFeature)
case history(HistoryFeature)
case bookmarks(BookmarksFeature)
case mentions(MentionsFeature)
case reputation(ReputationFeature)
}
Expand Down Expand Up @@ -193,6 +195,10 @@ extension Path {
HistoryScreen(store: store)
.tracking(for: HistoryScreen.self)

case let .bookmarks(store):
BookmarksScreen(store: store)
.tracking(for: BookmarksScreen.self)

case let .mentions(store):
MentionsScreen(store: store)
.tracking(for: MentionsScreen.self)
Expand Down
7 changes: 7 additions & 0 deletions Modules/Sources/AppFeature/Navigation/StackTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation

import AnalyticsClient
import BookmarksFeature
import DeeplinkHandler
import Models
import TCAExtensions
Expand Down Expand Up @@ -411,6 +412,12 @@ public struct StackTab: Reducer, Sendable {
case let .history(.delegate(.openTopic(id: id, name: name, goTo: goTo))):
state.path.append(.forum(.topic(TopicFeature.State(topicId: id, topicName: name, goTo: goTo))))

case let .bookmarks(.delegate(.openFolder(id: id, name: name))):
state.path.append(.more(.bookmarks(BookmarksFeature.State(folderId: id, folderName: name))))

case let .bookmarks(.delegate(.openURL(url))):
return handleInnerDeeplink(url: url, state: &state)

case let .mentions(.delegate(.openArticle(sourceId: sourceId, targetId: targetId))):
let articlePreview = ArticlePreview.innerDeeplink(id: sourceId)
state.path.append(.articles(.article(ArticleFeature.State.init(articlePreview: articlePreview, scrollToId: targetId))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ extension ArticleFeature {
analytics.log(ArticleEvent.linkCopied(state.articlePreview.url))
case .shareLink:
analytics.log(ArticleEvent.linkShareOpened(state.articlePreview.url))
case .addToBookmarks:
analytics.log(ArticleEvent.bookmarkButtonTapped(state.articlePreview.url))
}

case let .linkShared(success, url):
Expand Down
13 changes: 13 additions & 0 deletions Modules/Sources/ArticleFeature/ArticleFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public struct ArticleFeature: Reducer, Sendable {
public enum ArticleMenuAction: Sendable {
case copyLink
case shareLink
case addToBookmarks
}

// MARK: - Localizations
Expand Down Expand Up @@ -168,6 +169,7 @@ public struct ArticleFeature: Reducer, Sendable {
@Dependency(\.analyticsClient) var analyticsClient
@Dependency(\.pasteboardClient) var pasteboardClient
@Dependency(\.notificationsClient) var notificationsClient
@Dependency(\.notificationCenter) var notificationCenter
@Dependency(\.openURL) var openURL
@Dependency(\.dismiss) var dismiss
@Dependency(\.continuousClock) var clock
Expand Down Expand Up @@ -474,6 +476,17 @@ public struct ArticleFeature: Reducer, Sendable {

case .shareLink:
state.destination = .share(state.articlePreview.url)

case .addToBookmarks:
let title = state.article?.title ?? state.articlePreview.title
return .run { [url = state.articlePreview.url, title] _ in
let status = try await apiClient.addBookmark(0, title, .url(url: url))
await toastClient.showToast(status ? .addedToBookmarks : .whoopsSomethingWentWrong)
if status { notificationCenter.post(name: .bookmarksUpdated, object: nil) }
} catch: { error, _ in
analyticsClient.capture(error)
await toastClient.showToast(.whoopsSomethingWentWrong)
}
}

return .none
Expand Down
10 changes: 10 additions & 0 deletions Modules/Sources/ArticleFeature/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
}
}
},
"Add to bookmarks" : {
"localizations" : {
"ru" : {
"stringUnit" : {
"state" : "translated",
"value" : "Добавить в закладки"
}
}
}
},
"Comment has been deleted" : {
"localizations" : {
"ru" : {
Expand Down
8 changes: 8 additions & 0 deletions Modules/Sources/ArticleFeature/Views/ArticleMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public struct ArticleMenu: View {
ContextButton(text: LocalizedStringResource("Share Link", bundle: .module), symbol: .squareAndArrowUp) {
store.send(.menuActionTapped(.shareLink))
}

if store.isAuthorized {
Section {
ContextButton(text: LocalizedStringResource("Add to bookmarks", bundle: .module), symbol: .bookmark) {
store.send(.menuActionTapped(.addToBookmarks))
}
}
}
} label: {
Image(systemSymbol: .ellipsis)
.font(.body)
Expand Down
11 changes: 9 additions & 2 deletions Modules/Sources/ArticlesListFeature/ArticlesListFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public struct ArticlesListFeature: Reducer, Sendable {
@Dependency(\.pasteboardClient) private var pasteboardClient
@Dependency(\.hapticClient) private var hapticClient
@Dependency(\.continuousClock) private var clock
@Dependency(\.notificationCenter) private var notificationCenter

// MARK: - Body

Expand Down Expand Up @@ -252,8 +253,14 @@ public struct ArticlesListFeature: Reducer, Sendable {
return .run { _ in await open(url: article.url) }

case .addToBookmarks:
assertionFailure("Not implemented")
return .run { _ in await hapticClient.play(.rigid) }
return .run { _ in
let status = try await apiClient.addBookmark(0, article.title, .url(url: article.url))
await toastClient.showToast(status ? .addedToBookmarks : .whoopsSomethingWentWrong)
if status { notificationCenter.post(name: .bookmarksUpdated, object: nil) }
} catch: { error, _ in
analyticsClient.capture(error)
await toastClient.showToast(.whoopsSomethingWentWrong)
}
}
return .none
}
Expand Down
Loading