Skip to content

Commit

Permalink
Reader: Add Favorites (#23815)
Browse files Browse the repository at this point in the history
* Add support for adding favorites

* Add analytics

* Save in background

* Fix
  • Loading branch information
kean authored Nov 19, 2024
1 parent 7ab94fd commit d61e178
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
1 change: 1 addition & 0 deletions WordPress/Classes/Models/ReaderAbstractTopic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CoreData
@NSManaged open var following: Bool
@NSManaged open var lastSynced: Date?
@NSManaged open var path: String
/// Repurposed for "isFavorite".
@NSManaged open var showInMenu: Bool
@NSManaged open var title: String
@NSManaged open var type: String
Expand Down
1 change: 0 additions & 1 deletion WordPress/Classes/Services/ReaderTopicService.m
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,6 @@ - (ReaderSiteTopic *)siteTopicForRemoteSiteInfo:(RemoteReaderSiteInfo *)siteInfo
topic.organizationID = [siteInfo.organizationID integerValue];
topic.path = siteInfo.postsEndpoint;
topic.postCount = siteInfo.postCount;
topic.showInMenu = NO;
topic.siteBlavatar = siteInfo.siteBlavatar;
topic.siteDescription = siteInfo.siteDescription;
topic.siteID = siteInfo.siteID;
Expand Down
3 changes: 3 additions & 0 deletions WordPress/Classes/Utility/Analytics/WPAnalyticsEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import Foundation
case readerCommentTextHighlighted
case readerCommentTextCopied
case readerPostContextMenuButtonTapped
case readerAddSiteToFavoritesTapped

// Stats - Empty Stats nudges
case statsPublicizeNudgeShown
Expand Down Expand Up @@ -816,6 +817,8 @@ import Foundation
return "reader_comment_text_copied"
case .readerPostContextMenuButtonTapped:
return "reader_post_context_menu_button_tapped"
case .readerAddSiteToFavoritesTapped:
return "reader_add_site_to_favorites_tapped"

// Stats - Empty Stats nudges
case .statsPublicizeNudgeShown:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,8 @@ struct ReaderSidebarSubscriptionsSection: View {
private var subscriptions: FetchedResults<ReaderSiteTopic>

var body: some View {
ForEach(subscriptions, id: \.self) { site in
Label {
Text(site.title)
} icon: {
ReaderSiteIconView(site: site, size: .small)
}
.lineLimit(1)
.tag(ReaderSidebarItem.subscription(TaggedManagedObjectID(site)))
.swipeActions(edge: .trailing) {
Button(SharedStrings.Reader.unfollow, role: .destructive) {
ReaderSubscriptionHelper().unfollow(site)
}.tint(.red)
}
ForEach(subscriptions, id: \.self) {
ReaderSidebarSubscriptionCell(site: $0)
}
.onDelete(perform: delete)
}
Expand All @@ -37,3 +26,42 @@ struct ReaderSidebarSubscriptionsSection: View {
}
}
}

struct ReaderSidebarSubscriptionCell: View {
@ObservedObject var site: ReaderSiteTopic
@Environment(\.editMode) var editMode

var body: some View {
HStack {
Label {
Text(site.title)
} icon: {
ReaderSiteIconView(site: site, size: .small)
}
if editMode?.wrappedValue.isEditing == true {
Spacer()
Button {
if !site.showInMenu {
WPAnalytics.track(.readerAddSiteToFavoritesTapped)
}

let siteObjectID = TaggedManagedObjectID(site)
ContextManager.shared.performAndSave({ managedObjectContext in
let site = try managedObjectContext.existingObject(with: siteObjectID)
site.showInMenu.toggle()
}, completion: nil, on: DispatchQueue.main)
} label: {
Image(systemName: site.showInMenu ? "star.fill" : "star")
.foregroundStyle(site.showInMenu ? .pink : .secondary)
}.buttonStyle(.plain)
}
}
.lineLimit(1)
.tag(ReaderSidebarItem.subscription(TaggedManagedObjectID(site)))
.swipeActions(edge: .trailing) {
Button(SharedStrings.Reader.unfollow, role: .destructive) {
ReaderSubscriptionHelper().unfollow(site)
}.tint(.red)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ private struct ReaderSidebarView: View {
@ObservedObject var viewModel: ReaderSidebarViewModel

@AppStorage("reader_sidebar_organization_expanded") var isSectionOrganizationExpanded = true
@AppStorage("reader_sidebar_favorites_expanded") var isSectionFavoritesExpanded = true
@AppStorage("reader_sidebar_subscriptions_expanded") var isSectionSubscriptionsExpanded = true
@AppStorage("reader_sidebar_lists_expanded") var isSectionListsExpanded = true
@AppStorage("reader_sidebar_tags_expanded") var isSectionTagsExpanded = true

@FetchRequest(sortDescriptors: [SortDescriptor(\.title, order: .forward)])
private var teams: FetchedResults<ReaderTeamTopic>

@FetchRequest(
sortDescriptors: [SortDescriptor(\.title, order: .forward)],
predicate: NSPredicate(format: "following = YES AND showInMenu = YES")
)
private var favorites: FetchedResults<ReaderSiteTopic>

@Environment(\.layoutDirection) var layoutDirection
@Environment(\.editMode) var editMode

Expand Down Expand Up @@ -101,7 +108,13 @@ private struct ReaderSidebarView: View {
.withDisabledSelection(isEditing)
}
}

if !favorites.isEmpty || isEditing {
makeSection(Strings.favorites, isExpanded: $isSectionFavoritesExpanded) {
ForEach(favorites, id: \.self) {
ReaderSidebarSubscriptionCell(site: $0)
}
}
}
if !teams.isEmpty {
makeSection(Strings.organization, isExpanded: $isSectionOrganizationExpanded) {
ReaderSidebarOrganizationSection(viewModel: viewModel, teams: teams)
Expand Down Expand Up @@ -213,6 +226,7 @@ private extension View {
private struct Strings {
static let reader = SharedStrings.Reader.title
static let subscriptions = NSLocalizedString("reader.sidebar.section.subscriptions.title", value: "Subscriptions", comment: "Reader sidebar section title")
static let favorites = NSLocalizedString("reader.sidebar.section.favorites.title", value: "Favorites", comment: "Reader sidebar section title")
static let lists = NSLocalizedString("reader.sidebar.section.lists.title", value: "Lists", comment: "Reader sidebar section title")
static let tags = NSLocalizedString("reader.sidebar.section.tags.title", value: "Tags", comment: "Reader sidebar section title")
static let organization = NSLocalizedString("reader.sidebar.section.organization.title", value: "Organization", comment: "Reader sidebar section title")
Expand Down

0 comments on commit d61e178

Please sign in to comment.