Share + Fixed logged out mode

This commit is contained in:
Thomas Ricouard 2022-12-23 15:53:02 +01:00
parent 6e8ed998d4
commit 7068ad90bb
5 changed files with 54 additions and 32 deletions

View file

@ -4,6 +4,7 @@ import Env
import Network import Network
struct TimelineTab: View { struct TimelineTab: View {
@EnvironmentObject private var client: Client
@StateObject private var routeurPath = RouterPath() @StateObject private var routeurPath = RouterPath()
var body: some View { var body: some View {
@ -12,13 +13,14 @@ struct TimelineTab: View {
.withAppRouteur() .withAppRouteur()
.withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet) .withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet)
.toolbar { .toolbar {
ToolbarItem(placement: .navigationBarLeading) { if client.isAuth {
Button { ToolbarItem(placement: .navigationBarLeading) {
routeurPath.presentedSheet = .statusEditor(replyToStatus: nil) Button {
} label: { routeurPath.presentedSheet = .statusEditor(replyToStatus: nil)
Image(systemName: "square.and.pencil") } label: {
Image(systemName: "square.and.pencil")
}
} }
} }
} }
} }

View file

@ -11,10 +11,11 @@ public protocol AnyStatus {
var reblogsCount: Int { get } var reblogsCount: Int { get }
var favouritesCount: Int { get } var favouritesCount: Int { get }
var card: Card? { get } var card: Card? { get }
var favourited: Bool { get } var favourited: Bool? { get }
var reblogged: Bool { get } var reblogged: Bool? { get }
var pinned: Bool? { get } var pinned: Bool? { get }
var emojis: [Emoji] { get } var emojis: [Emoji] { get }
var url: URL? { get }
} }
public struct Status: AnyStatus, Codable, Identifiable { public struct Status: AnyStatus, Codable, Identifiable {
@ -29,10 +30,11 @@ public struct Status: AnyStatus, Codable, Identifiable {
public let reblogsCount: Int public let reblogsCount: Int
public let favouritesCount: Int public let favouritesCount: Int
public let card: Card? public let card: Card?
public let favourited: Bool public let favourited: Bool?
public let reblogged: Bool public let reblogged: Bool?
public let pinned: Bool? public let pinned: Bool?
public let emojis: [Emoji] public let emojis: [Emoji]
public let url: URL?
public static func placeholder() -> Status { public static func placeholder() -> Status {
.init(id: UUID().uuidString, .init(id: UUID().uuidString,
@ -49,7 +51,8 @@ public struct Status: AnyStatus, Codable, Identifiable {
favourited: false, favourited: false,
reblogged: false, reblogged: false,
pinned: false, pinned: false,
emojis: []) emojis: [],
url: nil)
} }
public static func placeholders() -> [Status] { public static func placeholders() -> [Status] {
@ -68,8 +71,9 @@ public struct ReblogStatus: AnyStatus, Codable, Identifiable {
public let reblogsCount: Int public let reblogsCount: Int
public let favouritesCount: Int public let favouritesCount: Int
public let card: Card? public let card: Card?
public let favourited: Bool public let favourited: Bool?
public let reblogged: Bool public let reblogged: Bool?
public let pinned: Bool? public let pinned: Bool?
public let emojis: [Emoji] public let emojis: [Emoji]
public let url: URL?
} }

View file

@ -41,18 +41,24 @@ struct StatusActionsView: View {
var body: some View { var body: some View {
HStack { HStack {
ForEach(Actions.allCases, id: \.self) { action in ForEach(Actions.allCases, id: \.self) { action in
Button { if action == .share {
handleAction(action: action) if let url = viewModel.status.reblog?.url ?? viewModel.status.url {
} label: { ShareLink(item: url) {
HStack(spacing: 2) { Image(systemName: action.iconName(viewModel: viewModel))
Image(systemName: action.iconName(viewModel: viewModel)) }
if let count = action.count(viewModel: viewModel) { }
Text("\(count)") } else {
.font(.footnote) 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() Spacer()
} }
} }

View file

@ -18,15 +18,20 @@ public class StatusRowViewModel: ObservableObject {
public init(status: Status, isEmbed: Bool) { public init(status: Status, isEmbed: Bool) {
self.status = status self.status = status
self.isEmbed = isEmbed 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.favouritesCount = status.reblog?.favouritesCount ?? status.favouritesCount
self.isReblogged = status.reblog?.reblogged ?? status.reblogged
self.reblogsCount = status.reblog?.reblogsCount ?? status.reblogsCount self.reblogsCount = status.reblog?.reblogsCount ?? status.reblogsCount
self.repliesCount = status.reblog?.repliesCount ?? status.repliesCount self.repliesCount = status.reblog?.repliesCount ?? status.repliesCount
} }
func favourite() async { func favourite() async {
guard let client else { return } guard let client, client.isAuth else { return }
isFavourited = true isFavourited = true
favouritesCount += 1 favouritesCount += 1
do { do {
@ -39,7 +44,7 @@ public class StatusRowViewModel: ObservableObject {
} }
func unFavourite() async { func unFavourite() async {
guard let client else { return } guard let client, client.isAuth else { return }
isFavourited = false isFavourited = false
favouritesCount -= 1 favouritesCount -= 1
do { do {
@ -52,7 +57,7 @@ public class StatusRowViewModel: ObservableObject {
} }
func reblog() async { func reblog() async {
guard let client else { return } guard let client, client.isAuth else { return }
isReblogged = true isReblogged = true
reblogsCount += 1 reblogsCount += 1
do { do {
@ -65,7 +70,7 @@ public class StatusRowViewModel: ObservableObject {
} }
func unReblog() async { func unReblog() async {
guard let client else { return } guard let client, client.isAuth else { return }
isReblogged = false isReblogged = false
reblogsCount -= 1 reblogsCount -= 1
do { do {
@ -78,9 +83,14 @@ public class StatusRowViewModel: ObservableObject {
} }
private func updateFromStatus(status: Status) { 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 favouritesCount = status.reblog?.favouritesCount ?? status.favouritesCount
isReblogged = status.reblog?.reblogged ?? status.reblogged
reblogsCount = status.reblog?.reblogsCount ?? status.reblogsCount reblogsCount = status.reblog?.reblogsCount ?? status.reblogsCount
repliesCount = status.reblog?.repliesCount ?? status.repliesCount repliesCount = status.reblog?.repliesCount ?? status.repliesCount
} }

View file

@ -27,7 +27,7 @@ public struct TimelineView: View {
.navigationTitle(filter?.title() ?? viewModel.timeline.title()) .navigationTitle(filter?.title() ?? viewModel.timeline.title())
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.toolbar { .toolbar {
if filter == nil { if filter == nil, client.isAuth {
ToolbarItem(placement: .navigationBarTrailing) { ToolbarItem(placement: .navigationBarTrailing) {
timelineFilterButton timelineFilterButton
} }