Skip to content

Commit d61e178

Browse files
authored
Reader: Add Favorites (#23815)
* Add support for adding favorites * Add analytics * Save in background * Fix
1 parent 7ab94fd commit d61e178

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

WordPress/Classes/Models/ReaderAbstractTopic.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CoreData
1111
@NSManaged open var following: Bool
1212
@NSManaged open var lastSynced: Date?
1313
@NSManaged open var path: String
14+
/// Repurposed for "isFavorite".
1415
@NSManaged open var showInMenu: Bool
1516
@NSManaged open var title: String
1617
@NSManaged open var type: String

WordPress/Classes/Services/ReaderTopicService.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,6 @@ - (ReaderSiteTopic *)siteTopicForRemoteSiteInfo:(RemoteReaderSiteInfo *)siteInfo
735735
topic.organizationID = [siteInfo.organizationID integerValue];
736736
topic.path = siteInfo.postsEndpoint;
737737
topic.postCount = siteInfo.postCount;
738-
topic.showInMenu = NO;
739738
topic.siteBlavatar = siteInfo.siteBlavatar;
740739
topic.siteDescription = siteInfo.siteDescription;
741740
topic.siteID = siteInfo.siteID;

WordPress/Classes/Utility/Analytics/WPAnalyticsEvent.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ import Foundation
116116
case readerCommentTextHighlighted
117117
case readerCommentTextCopied
118118
case readerPostContextMenuButtonTapped
119+
case readerAddSiteToFavoritesTapped
119120

120121
// Stats - Empty Stats nudges
121122
case statsPublicizeNudgeShown
@@ -816,6 +817,8 @@ import Foundation
816817
return "reader_comment_text_copied"
817818
case .readerPostContextMenuButtonTapped:
818819
return "reader_post_context_menu_button_tapped"
820+
case .readerAddSiteToFavoritesTapped:
821+
return "reader_add_site_to_favorites_tapped"
819822

820823
// Stats - Empty Stats nudges
821824
case .statsPublicizeNudgeShown:

WordPress/Classes/ViewRelated/Reader/Sidebar/ReaderSidebarSubscriptionsSection.swift

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,8 @@ struct ReaderSidebarSubscriptionsSection: View {
1313
private var subscriptions: FetchedResults<ReaderSiteTopic>
1414

1515
var body: some View {
16-
ForEach(subscriptions, id: \.self) { site in
17-
Label {
18-
Text(site.title)
19-
} icon: {
20-
ReaderSiteIconView(site: site, size: .small)
21-
}
22-
.lineLimit(1)
23-
.tag(ReaderSidebarItem.subscription(TaggedManagedObjectID(site)))
24-
.swipeActions(edge: .trailing) {
25-
Button(SharedStrings.Reader.unfollow, role: .destructive) {
26-
ReaderSubscriptionHelper().unfollow(site)
27-
}.tint(.red)
28-
}
16+
ForEach(subscriptions, id: \.self) {
17+
ReaderSidebarSubscriptionCell(site: $0)
2918
}
3019
.onDelete(perform: delete)
3120
}
@@ -37,3 +26,42 @@ struct ReaderSidebarSubscriptionsSection: View {
3726
}
3827
}
3928
}
29+
30+
struct ReaderSidebarSubscriptionCell: View {
31+
@ObservedObject var site: ReaderSiteTopic
32+
@Environment(\.editMode) var editMode
33+
34+
var body: some View {
35+
HStack {
36+
Label {
37+
Text(site.title)
38+
} icon: {
39+
ReaderSiteIconView(site: site, size: .small)
40+
}
41+
if editMode?.wrappedValue.isEditing == true {
42+
Spacer()
43+
Button {
44+
if !site.showInMenu {
45+
WPAnalytics.track(.readerAddSiteToFavoritesTapped)
46+
}
47+
48+
let siteObjectID = TaggedManagedObjectID(site)
49+
ContextManager.shared.performAndSave({ managedObjectContext in
50+
let site = try managedObjectContext.existingObject(with: siteObjectID)
51+
site.showInMenu.toggle()
52+
}, completion: nil, on: DispatchQueue.main)
53+
} label: {
54+
Image(systemName: site.showInMenu ? "star.fill" : "star")
55+
.foregroundStyle(site.showInMenu ? .pink : .secondary)
56+
}.buttonStyle(.plain)
57+
}
58+
}
59+
.lineLimit(1)
60+
.tag(ReaderSidebarItem.subscription(TaggedManagedObjectID(site)))
61+
.swipeActions(edge: .trailing) {
62+
Button(SharedStrings.Reader.unfollow, role: .destructive) {
63+
ReaderSubscriptionHelper().unfollow(site)
64+
}.tint(.red)
65+
}
66+
}
67+
}

WordPress/Classes/ViewRelated/Reader/Sidebar/ReaderSidebarViewController.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,20 @@ private struct ReaderSidebarView: View {
5050
@ObservedObject var viewModel: ReaderSidebarViewModel
5151

5252
@AppStorage("reader_sidebar_organization_expanded") var isSectionOrganizationExpanded = true
53+
@AppStorage("reader_sidebar_favorites_expanded") var isSectionFavoritesExpanded = true
5354
@AppStorage("reader_sidebar_subscriptions_expanded") var isSectionSubscriptionsExpanded = true
5455
@AppStorage("reader_sidebar_lists_expanded") var isSectionListsExpanded = true
5556
@AppStorage("reader_sidebar_tags_expanded") var isSectionTagsExpanded = true
5657

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

61+
@FetchRequest(
62+
sortDescriptors: [SortDescriptor(\.title, order: .forward)],
63+
predicate: NSPredicate(format: "following = YES AND showInMenu = YES")
64+
)
65+
private var favorites: FetchedResults<ReaderSiteTopic>
66+
6067
@Environment(\.layoutDirection) var layoutDirection
6168
@Environment(\.editMode) var editMode
6269

@@ -101,7 +108,13 @@ private struct ReaderSidebarView: View {
101108
.withDisabledSelection(isEditing)
102109
}
103110
}
104-
111+
if !favorites.isEmpty || isEditing {
112+
makeSection(Strings.favorites, isExpanded: $isSectionFavoritesExpanded) {
113+
ForEach(favorites, id: \.self) {
114+
ReaderSidebarSubscriptionCell(site: $0)
115+
}
116+
}
117+
}
105118
if !teams.isEmpty {
106119
makeSection(Strings.organization, isExpanded: $isSectionOrganizationExpanded) {
107120
ReaderSidebarOrganizationSection(viewModel: viewModel, teams: teams)
@@ -213,6 +226,7 @@ private extension View {
213226
private struct Strings {
214227
static let reader = SharedStrings.Reader.title
215228
static let subscriptions = NSLocalizedString("reader.sidebar.section.subscriptions.title", value: "Subscriptions", comment: "Reader sidebar section title")
229+
static let favorites = NSLocalizedString("reader.sidebar.section.favorites.title", value: "Favorites", comment: "Reader sidebar section title")
216230
static let lists = NSLocalizedString("reader.sidebar.section.lists.title", value: "Lists", comment: "Reader sidebar section title")
217231
static let tags = NSLocalizedString("reader.sidebar.section.tags.title", value: "Tags", comment: "Reader sidebar section title")
218232
static let organization = NSLocalizedString("reader.sidebar.section.organization.title", value: "Organization", comment: "Reader sidebar section title")

0 commit comments

Comments
 (0)