From c970c4238af119b83d976e5073db65dd41629ae2 Mon Sep 17 00:00:00 2001 From: Manish Reddy <47571755+mani-sh-reddy@users.noreply.github.com> Date: Sat, 4 Nov 2023 00:49:56 +0000 Subject: [PATCH] re-added community subscription logic and label on postview --- Lunar/DataLoaders/PostsFetcher.swift | 1 + Lunar/DataModels/RealmPost.swift | 3 + Lunar/Feed Views/FeedView.swift | 16 ++-- .../SubscribedCommunitiesView.swift | 12 +-- .../Feed Views/SubscribedFeedQuicklink.swift | 7 -- Lunar/Post Views/CompactPostItem.swift | 2 +- Lunar/Post Views/PostItem.swift | 79 +++++++++++++++++-- Lunar/Post Views/PostsView.swift | 1 + Lunar/Tools/RealmThawFunctions.swift | 9 +++ 9 files changed, 97 insertions(+), 33 deletions(-) diff --git a/Lunar/DataLoaders/PostsFetcher.swift b/Lunar/DataLoaders/PostsFetcher.swift index eafac1b7..86593138 100644 --- a/Lunar/DataLoaders/PostsFetcher.swift +++ b/Lunar/DataLoaders/PostsFetcher.swift @@ -211,6 +211,7 @@ class PostsFetcher: ObservableObject { communityIcon: post.community.icon, communityBanner: post.community.banner, communityUpdated: post.community.updated, + communitySubscribed: post.subscribed, postScore: post.counts.postScore, postCommentCount: post.counts.comments, upvotes: post.counts.upvotes, diff --git a/Lunar/DataModels/RealmPost.swift b/Lunar/DataModels/RealmPost.swift index 15efd774..e5053f0c 100644 --- a/Lunar/DataModels/RealmPost.swift +++ b/Lunar/DataModels/RealmPost.swift @@ -43,6 +43,7 @@ class RealmPost: Object, ObjectKeyIdentifiable { @Persisted var communityIcon: String? @Persisted var communityBanner: String? @Persisted var communityUpdated: String? + @Persisted var communitySubscribed: SubscribedState? // MARK: - Counts @@ -96,6 +97,7 @@ class RealmPost: Object, ObjectKeyIdentifiable { communityIcon: String?, communityBanner: String?, communityUpdated: String?, + communitySubscribed: SubscribedState?, postScore: Int?, postCommentCount: Int?, upvotes: Int?, @@ -134,6 +136,7 @@ class RealmPost: Object, ObjectKeyIdentifiable { self.communityIcon = communityIcon self.communityBanner = communityBanner self.communityUpdated = communityUpdated + self.communitySubscribed = communitySubscribed self.postScore = postScore self.postCommentCount = postCommentCount diff --git a/Lunar/Feed Views/FeedView.swift b/Lunar/Feed Views/FeedView.swift index c8e0cddd..f1935189 100644 --- a/Lunar/Feed Views/FeedView.swift +++ b/Lunar/Feed Views/FeedView.swift @@ -30,13 +30,13 @@ struct FeedView: View { @State var currentlyDownloadingPage: Int = 1 @State var downloaderCommunityID: Int = 0 -// var subscribedCommunityListHeading: String { -// if !activeAccount.actorID.isEmpty { -// "\(URLParser.extractUsername(from: activeAccount.actorID))'s Subscribed Communities" -// } else { -// "Subscribed Communities" -// } -// } + var subscribedCommunityListHeading: String { + if !activeAccount.actorID.isEmpty { + "\(URLParser.extractUsername(from: activeAccount.actorID))'s Communities" + } else { + "Subscribed Communities" + } + } var body: some View { NavigationView { @@ -162,6 +162,8 @@ struct FeedView: View { var subscribedSection: some View { Section { SubscribedCommunitiesSectionView() + } header: { + Text(subscribedCommunityListHeading) } } diff --git a/Lunar/Feed Views/SubscribedCommunitiesView.swift b/Lunar/Feed Views/SubscribedCommunitiesView.swift index c3bc8584..12a6e183 100644 --- a/Lunar/Feed Views/SubscribedCommunitiesView.swift +++ b/Lunar/Feed Views/SubscribedCommunitiesView.swift @@ -26,14 +26,6 @@ struct SubscribedCommunitiesSectionView: View { let realm = try! Realm() - var subscribedCommunityListHeading: String { - if !activeAccount.actorID.isEmpty { - "\(activeAccount.name)'s Communities" - } else { - "Subscribed Communities" - } - } - var body: some View { if !activeAccount.actorID.isEmpty { DisclosureGroup { @@ -86,13 +78,13 @@ struct SubscribedCommunitiesSectionView: View { var userSubscriptionsDisclosureGroupLabel: some View { HStack { - Image(systemSymbol: .personCircleFill) + Image(systemSymbol: .person2CircleFill) .resizable() .frame(width: 30, height: 30) .symbolRenderingMode(.hierarchical) .foregroundColor(.orange) - Text(subscribedCommunityListHeading) + Text("Subscribed Communities") .padding(.horizontal, 10) .foregroundColor(.primary) } diff --git a/Lunar/Feed Views/SubscribedFeedQuicklink.swift b/Lunar/Feed Views/SubscribedFeedQuicklink.swift index 3c870545..ceeaf9c1 100644 --- a/Lunar/Feed Views/SubscribedFeedQuicklink.swift +++ b/Lunar/Feed Views/SubscribedFeedQuicklink.swift @@ -56,13 +56,6 @@ struct SubscribedFeedQuicklink: View { .padding(.horizontal, 10) .foregroundColor(.primary) } -// GeneralCommunityQuicklinkButton( -// image: subscribedPostsQuicklink.icon, -// hexColor: subscribedPostsQuicklink.iconColor, -// title: subscribedPostsQuicklink.title, -// brightness: subscribedPostsQuicklink.brightness, -// saturation: subscribedPostsQuicklink.saturation -// ) } } } diff --git a/Lunar/Post Views/CompactPostItem.swift b/Lunar/Post Views/CompactPostItem.swift index bb94dae9..12974b47 100644 --- a/Lunar/Post Views/CompactPostItem.swift +++ b/Lunar/Post Views/CompactPostItem.swift @@ -322,7 +322,7 @@ struct CompactPostsView_Previews: PreviewProvider { "This is a sample community description. It provides information about the community.", communityIcon: "https://example.com/community-icon.jpg", communityBanner: "https://example.com/community-banner.jpg", - communityUpdated: "October 16, 2023", + communityUpdated: "October 16, 2023", communitySubscribed: .subscribed, postScore: 42, postCommentCount: 10, upvotes: 30, diff --git a/Lunar/Post Views/PostItem.swift b/Lunar/Post Views/PostItem.swift index 69b42bea..c4b8eec2 100644 --- a/Lunar/Post Views/PostItem.swift +++ b/Lunar/Post Views/PostItem.swift @@ -18,6 +18,7 @@ struct PostItem: View { @ObservedRealmObject var post: RealmPost @State var showSafari: Bool = false + @State var subscribeAlertPresented: Bool = false let hapticsSoft = UIImpactFeedbackGenerator(style: .soft) let hapticsLight = UIImpactFeedbackGenerator(style: .light) @@ -156,15 +157,39 @@ struct PostItem: View { } var postCommunityLabel: some View { - Text(communityLabel) - .textCase(.lowercase) - .foregroundColor(.secondary) - .font(.caption) - .highPriorityGesture( - TapGesture().onEnded { - hapticsLight.impactOccurred(intensity: 0.5) + HStack(spacing: 3) { + Text(communityLabel) + if post.communitySubscribed != nil, post.communitySubscribed == .subscribed { + Image(systemSymbol: .bookmark) + } else if post.communitySubscribed != nil, post.communitySubscribed == .pending { + Image(systemSymbol: .clock) + } + } + .textCase(.lowercase) + .foregroundColor(.secondary) + .font(.caption) + .highPriorityGesture( + TapGesture().onEnded { + hapticsLight.impactOccurred() + subscribeAlertPresented = true + } + ) + .confirmationDialog("", isPresented: $subscribeAlertPresented) { + if post.communitySubscribed == .notSubscribed { + Button("Subscribe") { + sendSubscribeAction(subscribeAction: true) } - ) + } else { + Button("Unsubscribe") { + sendSubscribeAction(subscribeAction: false) + } + } + Button("Dismiss", role: .cancel) { + subscribeAlertPresented = false + } + } message: { + Text("\(post.communityName)@\(URLParser.extractDomain(from: post.communityActorID))") + } } var postTitle: some View { @@ -287,4 +312,42 @@ struct PostItem: View { print("RETURNED /post/like \(String(describing: postID)):\(voteSubmittedSuccessfully)") } } + + func sendSubscribeAction(subscribeAction: Bool) { + let notificationHaptics = UINotificationFeedbackGenerator() + if let communityID = post.communityID { + SubscriptionActionSender( + communityID: communityID, + asActorID: activeAccount.actorID, + subscribeAction: subscribeAction + ).fetchSubscribeInfo { _, subscribeResponse, _ in + let realm = try! Realm() + if subscribeResponse != nil { + notificationHaptics.notificationOccurred(.success) + try! realm.write { + let community = RealmCommunity( + id: communityID, + name: post.communityName, + title: post.communityTitle, + actorID: post.communityActorID, + instanceID: post.communityInstanceID, + descriptionText: post.communityDescription, + icon: post.communityIcon, + banner: post.communityBanner, + postingRestrictedToMods: false, + published: "", + subscribers: nil, + posts: nil, + comments: nil, + subscribed: subscribeResponse ?? .notSubscribed + ) + realm.add(community, update: .modified) + } + RealmThawFunctions().subscribe(post: post, subscribedState: subscribeResponse ?? .notSubscribed) + } else { + notificationHaptics.notificationOccurred(.error) + } + } + } + } } diff --git a/Lunar/Post Views/PostsView.swift b/Lunar/Post Views/PostsView.swift index 4dbaeb77..3556bf47 100644 --- a/Lunar/Post Views/PostsView.swift +++ b/Lunar/Post Views/PostsView.swift @@ -306,6 +306,7 @@ struct PostsView_Previews: PreviewProvider { communityIcon: "https://example.com/community-icon.jpg", communityBanner: "https://example.com/community-banner.jpg", communityUpdated: "October 16, 2023", + communitySubscribed: .subscribed, postScore: 42, postCommentCount: 10, upvotes: 30, diff --git a/Lunar/Tools/RealmThawFunctions.swift b/Lunar/Tools/RealmThawFunctions.swift index b60bb735..4911def0 100644 --- a/Lunar/Tools/RealmThawFunctions.swift +++ b/Lunar/Tools/RealmThawFunctions.swift @@ -13,6 +13,15 @@ class RealmThawFunctions { let hapticsLight = UIImpactFeedbackGenerator(style: .light) let hapticsSoft = UIImpactFeedbackGenerator(style: .soft) + func subscribe(post: RealmPost, subscribedState: SubscribedState) { + let realm = try! Realm() + try! realm.write { + if let thawedPost = post.thaw() { + thawedPost.communitySubscribed = subscribedState + } + } + } + func hideAction(post: RealmPost) { let realm = try! Realm() try! realm.write {