From 7068ad90bbbe1cea65777068dd29169574b61e11 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Fri, 23 Dec 2022 15:53:02 +0100 Subject: [PATCH] Share + Fixed logged out mode --- IceCubesApp/App/Tabs/TimelineTab.swift | 14 +++++----- Packages/Models/Sources/Models/Status.swift | 18 ++++++++----- .../Status/Row/StatusActionsView.swift | 26 ++++++++++++------- .../Status/Row/StatusRowViewModel.swift | 26 +++++++++++++------ .../Sources/Timeline/TimelineView.swift | 2 +- 5 files changed, 54 insertions(+), 32 deletions(-) diff --git a/IceCubesApp/App/Tabs/TimelineTab.swift b/IceCubesApp/App/Tabs/TimelineTab.swift index c656b57f..e1e62ccb 100644 --- a/IceCubesApp/App/Tabs/TimelineTab.swift +++ b/IceCubesApp/App/Tabs/TimelineTab.swift @@ -4,6 +4,7 @@ import Env import Network struct TimelineTab: View { + @EnvironmentObject private var client: Client @StateObject private var routeurPath = RouterPath() var body: some View { @@ -12,13 +13,14 @@ struct TimelineTab: View { .withAppRouteur() .withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet) .toolbar { - ToolbarItem(placement: .navigationBarLeading) { - Button { - routeurPath.presentedSheet = .statusEditor(replyToStatus: nil) - } label: { - Image(systemName: "square.and.pencil") + if client.isAuth { + ToolbarItem(placement: .navigationBarLeading) { + Button { + routeurPath.presentedSheet = .statusEditor(replyToStatus: nil) + } label: { + Image(systemName: "square.and.pencil") + } } - } } } diff --git a/Packages/Models/Sources/Models/Status.swift b/Packages/Models/Sources/Models/Status.swift index ed623c81..c69bee64 100644 --- a/Packages/Models/Sources/Models/Status.swift +++ b/Packages/Models/Sources/Models/Status.swift @@ -11,10 +11,11 @@ public protocol AnyStatus { var reblogsCount: Int { get } var favouritesCount: Int { get } var card: Card? { get } - var favourited: Bool { get } - var reblogged: Bool { get } + var favourited: Bool? { get } + var reblogged: Bool? { get } var pinned: Bool? { get } var emojis: [Emoji] { get } + var url: URL? { get } } public struct Status: AnyStatus, Codable, Identifiable { @@ -29,10 +30,11 @@ public struct Status: AnyStatus, Codable, Identifiable { public let reblogsCount: Int public let favouritesCount: Int public let card: Card? - public let favourited: Bool - public let reblogged: Bool + public let favourited: Bool? + public let reblogged: Bool? public let pinned: Bool? public let emojis: [Emoji] + public let url: URL? public static func placeholder() -> Status { .init(id: UUID().uuidString, @@ -49,7 +51,8 @@ public struct Status: AnyStatus, Codable, Identifiable { favourited: false, reblogged: false, pinned: false, - emojis: []) + emojis: [], + url: nil) } public static func placeholders() -> [Status] { @@ -68,8 +71,9 @@ public struct ReblogStatus: AnyStatus, Codable, Identifiable { public let reblogsCount: Int public let favouritesCount: Int public let card: Card? - public let favourited: Bool - public let reblogged: Bool + public let favourited: Bool? + public let reblogged: Bool? public let pinned: Bool? public let emojis: [Emoji] + public let url: URL? } diff --git a/Packages/Status/Sources/Status/Row/StatusActionsView.swift b/Packages/Status/Sources/Status/Row/StatusActionsView.swift index a3890b97..1ac3ccb1 100644 --- a/Packages/Status/Sources/Status/Row/StatusActionsView.swift +++ b/Packages/Status/Sources/Status/Row/StatusActionsView.swift @@ -41,18 +41,24 @@ struct StatusActionsView: View { var body: some View { HStack { ForEach(Actions.allCases, id: \.self) { action in - Button { - handleAction(action: action) - } label: { - HStack(spacing: 2) { - Image(systemName: action.iconName(viewModel: viewModel)) - if let count = action.count(viewModel: viewModel) { - Text("\(count)") - .font(.footnote) + if action == .share { + if let url = viewModel.status.reblog?.url ?? viewModel.status.url { + ShareLink(item: url) { + Image(systemName: action.iconName(viewModel: viewModel)) + } + } + } else { + Button { + handleAction(action: action) + } label: { + HStack(spacing: 2) { + Image(systemName: action.iconName(viewModel: viewModel)) + if let count = action.count(viewModel: viewModel) { + Text("\(count)") + .font(.footnote) + } } } - } - if action != .share { Spacer() } } diff --git a/Packages/Status/Sources/Status/Row/StatusRowViewModel.swift b/Packages/Status/Sources/Status/Row/StatusRowViewModel.swift index e04c1b64..03975794 100644 --- a/Packages/Status/Sources/Status/Row/StatusRowViewModel.swift +++ b/Packages/Status/Sources/Status/Row/StatusRowViewModel.swift @@ -18,15 +18,20 @@ public class StatusRowViewModel: ObservableObject { public init(status: Status, isEmbed: Bool) { self.status = status self.isEmbed = isEmbed - self.isFavourited = status.reblog?.favourited ?? status.favourited + if let reblog = status.reblog { + self.isFavourited = reblog.favourited == true + self.isReblogged = reblog.reblogged == true + } else { + self.isFavourited = status.favourited == true + self.isReblogged = status.reblogged == true + } self.favouritesCount = status.reblog?.favouritesCount ?? status.favouritesCount - self.isReblogged = status.reblog?.reblogged ?? status.reblogged self.reblogsCount = status.reblog?.reblogsCount ?? status.reblogsCount self.repliesCount = status.reblog?.repliesCount ?? status.repliesCount } func favourite() async { - guard let client else { return } + guard let client, client.isAuth else { return } isFavourited = true favouritesCount += 1 do { @@ -39,7 +44,7 @@ public class StatusRowViewModel: ObservableObject { } func unFavourite() async { - guard let client else { return } + guard let client, client.isAuth else { return } isFavourited = false favouritesCount -= 1 do { @@ -52,7 +57,7 @@ public class StatusRowViewModel: ObservableObject { } func reblog() async { - guard let client else { return } + guard let client, client.isAuth else { return } isReblogged = true reblogsCount += 1 do { @@ -65,7 +70,7 @@ public class StatusRowViewModel: ObservableObject { } func unReblog() async { - guard let client else { return } + guard let client, client.isAuth else { return } isReblogged = false reblogsCount -= 1 do { @@ -78,9 +83,14 @@ public class StatusRowViewModel: ObservableObject { } private func updateFromStatus(status: Status) { - isFavourited = status.reblog?.favourited ?? status.favourited + if let reblog = status.reblog { + isFavourited = reblog.favourited == true + isReblogged = reblog.reblogged == true + } else { + isFavourited = status.favourited == true + isReblogged = status.reblogged == true + } favouritesCount = status.reblog?.favouritesCount ?? status.favouritesCount - isReblogged = status.reblog?.reblogged ?? status.reblogged reblogsCount = status.reblog?.reblogsCount ?? status.reblogsCount repliesCount = status.reblog?.repliesCount ?? status.repliesCount } diff --git a/Packages/Timeline/Sources/Timeline/TimelineView.swift b/Packages/Timeline/Sources/Timeline/TimelineView.swift index ee8025ac..17cfe2a2 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineView.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineView.swift @@ -27,7 +27,7 @@ public struct TimelineView: View { .navigationTitle(filter?.title() ?? viewModel.timeline.title()) .navigationBarTitleDisplayMode(.inline) .toolbar { - if filter == nil { + if filter == nil, client.isAuth { ToolbarItem(placement: .navigationBarTrailing) { timelineFilterButton }