Skip to content

Commit

Permalink
re-added community subscription logic and label on postview
Browse files Browse the repository at this point in the history
  • Loading branch information
mani-sh-reddy committed Nov 4, 2023
1 parent 5834c62 commit c970c42
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 33 deletions.
1 change: 1 addition & 0 deletions Lunar/DataLoaders/PostsFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions Lunar/DataModels/RealmPost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -96,6 +97,7 @@ class RealmPost: Object, ObjectKeyIdentifiable {
communityIcon: String?,
communityBanner: String?,
communityUpdated: String?,
communitySubscribed: SubscribedState?,
postScore: Int?,
postCommentCount: Int?,
upvotes: Int?,
Expand Down Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions Lunar/Feed Views/FeedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -162,6 +162,8 @@ struct FeedView: View {
var subscribedSection: some View {
Section {
SubscribedCommunitiesSectionView()
} header: {
Text(subscribedCommunityListHeading)
}
}

Expand Down
12 changes: 2 additions & 10 deletions Lunar/Feed Views/SubscribedCommunitiesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 0 additions & 7 deletions Lunar/Feed Views/SubscribedFeedQuicklink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
// )
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Lunar/Post Views/CompactPostItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
79 changes: 71 additions & 8 deletions Lunar/Post Views/PostItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
}
}
1 change: 1 addition & 0 deletions Lunar/Post Views/PostsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions Lunar/Tools/RealmThawFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c970c42

Please sign in to comment.