Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Popular subreddit to prepopulated subreddits list #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Beam/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
do {
var subreddits = try objectContext.fetch(fetchRequest)
subreddits.append(try Subreddit.frontpageSubreddit())
subreddits.append(try Subreddit.popularSubreddit())
subreddits.append(try Subreddit.allSubreddit())

let searchableItems = subreddits.compactMap({ (subreddit) -> CSSearchableItem? in
Expand Down Expand Up @@ -449,7 +450,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
do {
let fetchRequest = NSFetchRequest<Subreddit>(entityName: Subreddit.entityName())
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true), NSSortDescriptor(key: "displayName", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:))), NSSortDescriptor(key: "identifier", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "isBookmarked == YES && NOT (identifier IN %@)", [Subreddit.frontpageIdentifier, Subreddit.allIdentifier])
fetchRequest.predicate = NSPredicate(format: "isBookmarked == YES && NOT (identifier IN %@)", [Subreddit.frontpageIdentifier, Subreddit.popularIdentifier, Subreddit.allIdentifier])
fetchRequest.fetchLimit = 3
subreddits = try objectContext.fetch(fetchRequest)
let frontpage = try Subreddit.frontpageSubreddit()
Expand Down Expand Up @@ -478,12 +479,14 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
self.changeActiveTabContent(AppTabContent.MessagesNavigation)
case AppLaunchView.Profile:
self.changeActiveTabContent(AppTabContent.ProfileNavigation)
case AppLaunchView.Frontpage, AppLaunchView.All, AppLaunchView.LastVisitedSubreddit:
case AppLaunchView.Frontpage, AppLaunchView.Popular, AppLaunchView.All, AppLaunchView.LastVisitedSubreddit:
self.changeActiveTabContent(AppTabContent.SubscriptionsNavigation)
var subreddit: Subreddit?
do {
if appOpenView == AppLaunchView.Frontpage {
subreddit = try Subreddit.frontpageSubreddit()
} else if appOpenView == AppLaunchView.Popular {
subreddit = try Subreddit.popularSubreddit()
} else if appOpenView == AppLaunchView.All {
subreddit = try Subreddit.allSubreddit()
} else if appOpenView == AppLaunchView.LastVisitedSubreddit {
Expand Down
2 changes: 1 addition & 1 deletion Beam/Controllers/RedditActivityController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RedditActivityController: NSObject {
return [Subreddit]()
}
let fetchRequest = NSFetchRequest<Subreddit>(entityName: Subreddit.entityName())
fetchRequest.predicate = NSPredicate(format: "lastVisitDate != nil && NOT(identifier IN %@)", [Subreddit.allIdentifier, Subreddit.frontpageIdentifier])
fetchRequest.predicate = NSPredicate(format: "lastVisitDate != nil && NOT(identifier IN %@)", [Subreddit.allIdentifier, Subreddit.popularIdentifier, Subreddit.frontpageIdentifier])
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastVisitDate", ascending: false), NSSortDescriptor(key: "displayName", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))]
fetchRequest.fetchLimit = 5

Expand Down
2 changes: 2 additions & 0 deletions Beam/In-App Settings/AppLaunchOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum AppLaunchView: String {
case Messages = "messages"
case Profile = "profile"
case Frontpage = "frontpage"
case Popular = "popular"
case All = "All"
case LastVisitedSubreddit = "last_visited_subreddit"
}
Expand Down Expand Up @@ -49,6 +50,7 @@ public struct AppLaunchOption: Equatable {
"subreddit": [
AppLaunchOption(title: AWKLocalizedString("app-open-option-last-visited-subreddit"), view: AppLaunchView.LastVisitedSubreddit),
AppLaunchOption(title: AWKLocalizedString("app-open-option-frontpage"), view: AppLaunchView.Frontpage),
AppLaunchOption(title: AWKLocalizedString("app-open-option-popular"), view: AppLaunchView.Popular),
AppLaunchOption(title: AWKLocalizedString("app-open-option-all"), view: AppLaunchView.All)
]
]
Expand Down
7 changes: 7 additions & 0 deletions Beam/Localization/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@

"frontpage-description" = "All subscriptions combined";

"popular-description" = "Most popular posts on Reddit";

"all-description" = "Everything on reddit";

"random-description" = "Gives a random subreddit";
Expand Down Expand Up @@ -859,6 +861,9 @@
/* The name for the subreddit "Frontpage" on reddit. PLEASE USE THE REDDIT TERM FOR THIS! */
"subreddit-frontpage" = "Frontpage";

/* The name for the subreddit "Popular" on reddit. PLEASE USE THE REDDIT TERM FOR THIS! */
"subreddit-popular" = "Popular";

/* Multireddits
The following strings have a relation to multiredditsA message displayed when the app couldn't download your list of multireddits */
"could-not-fetch-multireddits" = "Could not fetch multireddits";
Expand Down Expand Up @@ -1317,6 +1322,8 @@

"app-open-option-frontpage" = "Frontpage";

"app-open-option-popular" = "Popular";

"app-open-option-all" = "All";

/* WelcomeThe connect with reddit button on the welcome view */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "subreddit_icon_star.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "subreddit_icon_star@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "subreddit_icon_star@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Beam/UI/Search/SubredditListTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class SubredditListTableViewCell: BeamTableViewCell {
var subtitle: String?
if self.subreddit?.identifier == Subreddit.frontpageIdentifier {
subtitle = "\n\(AWKLocalizedString("frontpage-description"))"
} else if self.subreddit?.identifier == Subreddit.popularIdentifier {
subtitle = "\n\(AWKLocalizedString("popular-description"))"
} else if self.subreddit?.identifier == Subreddit.allIdentifier {
subtitle = "\n\(AWKLocalizedString("all-description"))"
} else if self.subreddit?.isUserAuthorized != true {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class SubredditMediaOverviewViewController: BeamViewController, SubredditTabItem
toolbar?.toolbarView.delegate = self
toolbar?.metadataView.delegate = self

let shouldShowSubreddit = (self.subreddit is Multireddit || self.subreddit?.identifier == Subreddit.frontpageIdentifier || self.subreddit?.identifier == Subreddit.allIdentifier) && UserSettings[.showPostMetadataSubreddit] && UserSettings[.showPostMetadata]
let shouldShowSubreddit = (self.subreddit is Multireddit || self.subreddit?.identifier == Subreddit.frontpageIdentifier || self.subreddit?.identifier == Subreddit.allIdentifier || self.subreddit?.identifier == Subreddit.popularIdentifier) && UserSettings[.showPostMetadataSubreddit] && UserSettings[.showPostMetadata]
toolbar?.shouldShowSubreddit = shouldShowSubreddit

gallery.bottomView = toolbar
Expand Down
6 changes: 3 additions & 3 deletions Beam/UI/Subreddits/Posts Stream/StreamViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class StreamViewController: BeamTableViewController, PostMetadataViewDelegate, B
guard let content: [Content] = list?.array as? [Content] else {
return [Content]()
}
let shouldFilterSubreddits: Bool = subreddit.identifier == Subreddit.allIdentifier || subreddit.identifier == Subreddit.frontpageIdentifier
let shouldFilterSubreddits: Bool = subreddit.identifier == Subreddit.allIdentifier || subreddit.identifier == Subreddit.frontpageIdentifier || subreddit.identifier == Subreddit.popularIdentifier
let filteredContent: [Content] = content.filter { (content: Content) -> Bool in
var postTitle: String?
var subredditName: String?
Expand Down Expand Up @@ -531,9 +531,9 @@ class StreamViewController: BeamTableViewController, PostMetadataViewDelegate, B
return false
}
if let subreddit = self.visibleSubreddit {
return subreddit is Multireddit || subreddit.identifier == Subreddit.frontpageIdentifier || subreddit.identifier == Subreddit.allIdentifier
return subreddit is Multireddit || subreddit.identifier == Subreddit.frontpageIdentifier || subreddit.identifier == Subreddit.allIdentifier || subreddit.identifier == Subreddit.popularIdentifier
} else if let collection = self.collection as? PostCollection {
return collection.subreddit is Multireddit || collection.subreddit?.identifier == Subreddit.frontpageIdentifier || collection.subreddit?.identifier == Subreddit.allIdentifier || self.subreddit == nil
return collection.subreddit is Multireddit || collection.subreddit?.identifier == Subreddit.frontpageIdentifier || collection.subreddit?.identifier == Subreddit.allIdentifier || collection.subreddit?.identifier == Subreddit.popularIdentifier || self.subreddit == nil
}
return UserSettings[.showPostMetadataSubreddit]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SubredditFilteringViewController: BeamViewController {
fileprivate var keywordsChanged: Bool = false

fileprivate var canFilterSubreddits: Bool {
return self.subreddit.identifier == Subreddit.allIdentifier || self.subreddit.identifier == Subreddit.frontpageIdentifier
return self.subreddit.identifier == Subreddit.allIdentifier || self.subreddit.identifier == Subreddit.frontpageIdentifier || self.subreddit.identifier == Subreddit.popularIdentifier
}

fileprivate var filteringType: SubredditFilteringType = SubredditFilteringType.keywords {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class SubredditStreamViewController: BeamViewController, SubredditTabItemViewCon
subredditName = "Frontpage"
} else if self.subreddit?.identifier == Subreddit.allIdentifier {
subredditName = "All"
} else if self.subreddit?.identifier == Subreddit.popularIdentifier {
subredditName = "Popular"
} else {

}
Expand Down
2 changes: 2 additions & 0 deletions Beam/UI/Subscriptions/SubredditPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class SubredditPreviewView: BeamView {
}
if self.subreddit?.identifier == Subreddit.frontpageIdentifier {
self.imageView.image = #imageLiteral(resourceName: "subreddit_icon_frontpage")
} else if self.subreddit?.identifier == Subreddit.popularIdentifier {
self.imageView.image = #imageLiteral(resourceName: "subreddit_icon_popular")
} else if self.subreddit?.identifier == Subreddit.allIdentifier {
self.imageView.image = #imageLiteral(resourceName: "subreddit_icon_all")
} else {
Expand Down
4 changes: 3 additions & 1 deletion Beam/UI/Subscriptions/SubredditTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ final class SubredditTableViewCell: BeamTableViewCell {
guard self.allowPromimentDisplay else {
return false
}
return self.subreddit?.isBookmarked.boolValue == true || self.subreddit?.identifier == Subreddit.frontpageIdentifier || self.subreddit?.identifier == Subreddit.allIdentifier || self.subreddit is Multireddit
return self.subreddit?.isBookmarked.boolValue == true || self.subreddit?.identifier == Subreddit.frontpageIdentifier || self.subreddit?.identifier == Subreddit.allIdentifier || self.subreddit?.identifier == Subreddit.popularIdentifier || self.subreddit is Multireddit
}

private var starButtonEnabled: Bool {
Expand Down Expand Up @@ -126,6 +126,8 @@ final class SubredditTableViewCell: BeamTableViewCell {
self.subtitleLabel.text = "\(multireddit.subreddits?.count ?? 0) subreddits"
} else if self.subreddit?.identifier == Subreddit.frontpageIdentifier {
self.subtitleLabel.text = AWKLocalizedString("frontpage-description")
} else if self.subreddit?.identifier == Subreddit.popularIdentifier {
self.subtitleLabel.text = AWKLocalizedString("popular-description")
} else if self.subreddit?.identifier == Subreddit.allIdentifier {
self.subtitleLabel.text = AWKLocalizedString("all-description")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public final class SubredditsCollectionQuery: CollectionQuery {
}

let frontpage = try Subreddit.frontpageSubreddit()

let popular = try Subreddit.popularSubreddit()
let all = try Subreddit.allSubreddit()

return try super.prepopulate(context) + [frontpage, all]
return try super.prepopulate(context) + [frontpage, popular, all]
}

override func contentPredicates() -> [NSPredicate] {
Expand Down
52 changes: 46 additions & 6 deletions Snoo/Core Data/Subreddit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ open class Subreddit: SyncObject {
open static var frontpageIdentifier: String {
return "snoo-frontpage"
}

open static var popularIdentifier: String {
return "snoo-popular"
}

open static var allIdentifier: String {
return "snoo-all"
}

open var isPrepopulated: Bool {
if let identifier = self.identifier {
return [Subreddit.frontpageIdentifier, Subreddit.allIdentifier].contains(identifier)
return [Subreddit.frontpageIdentifier, Subreddit.allIdentifier, Subreddit.popularIdentifier].contains(identifier)
}
return false
}
Expand All @@ -95,7 +99,7 @@ open class Subreddit: SyncObject {
if let submissionTypeString = self.submissionTypeString, let submissionType = SubredditSubmissionType(rawValue: submissionTypeString) {
return submissionType
}
if self.identifier == Subreddit.frontpageIdentifier || self.identifier == Subreddit.allIdentifier || self is Multireddit {
if self.identifier == Subreddit.frontpageIdentifier || self.identifier == Subreddit.allIdentifier || self.identifier == Subreddit.popularIdentifier || self is Multireddit {
return SubredditSubmissionType.none
}
//In general subreddits you visit will allow both
Expand Down Expand Up @@ -206,12 +210,48 @@ open class Subreddit: SyncObject {
if let thrownError = thrownError {
throw thrownError
}
subreddit.title = NSLocalizedString("subreddit-frontpage", comment: "The title used for the frontpage secction on reddit. This is a collection of your subbreddits when logged in")
subreddit.displayName = NSLocalizedString("subreddit-frontpage", comment: "The title used for the frontpage secction on reddit. This is a collection of your subbreddits when logged in")
subreddit.title = NSLocalizedString("subreddit-frontpage", comment: "The title used for the frontpage section on reddit. This is a collection of your subbreddits when logged in")
subreddit.displayName = NSLocalizedString("subreddit-frontpage", comment: "The title used for the frontpage section on reddit. This is a collection of your subbreddits when logged in")
subreddit.isBookmarked = NSNumber(value: true as Bool)

return subreddit
}

//Returns the popular subreddit. If it doesn't already exist in the context it will be created. This method is always done on the DataController's private context!
open class func popularSubreddit() throws -> Subreddit {
let context: NSManagedObjectContext! = DataController.shared.privateContext
var subreddit: Subreddit!
var thrownError: Error?
context.performAndWait {
do {
if let existingSubreddit = try Subreddit.fetchObjectWithIdentifier(Subreddit.popularIdentifier, context: context) as? Subreddit {
//We already have a all subreddit, update it below
subreddit = existingSubreddit
} else {
//We don't already have a all subreddit, create it and update it below
subreddit = try Subreddit.objectWithIdentifier(Subreddit.popularIdentifier, cache: nil, context: context) as! Subreddit
subreddit.permalink = "/r/popular"
subreddit.sectionName = ""
if subreddit.objectID.isTemporaryID {
subreddit.order = NSNumber(value: 1 as Int)
}

try context?.obtainPermanentIDs(for: [subreddit])
}
} catch {
thrownError = error
}

}
if let thrownError = thrownError {
throw thrownError
}
subreddit.title = NSLocalizedString("subreddit-popular", comment: "The title used for the popular section on reddit. This is a collection of popular subbreddits")
subreddit.displayName = NSLocalizedString("subreddit-popular", comment: "The title used for the popular section on reddit. This is a collection of popular subbreddits")
subreddit.isBookmarked = NSNumber(value: true as Bool)

return subreddit
}

//Returns the /r/all subreddit. If it doesn't already exist in the context it will be created. This method is always done on the DataController's private context!
open class func allSubreddit() throws -> Subreddit {
Expand All @@ -229,7 +269,7 @@ open class Subreddit: SyncObject {
subreddit.permalink = "/r/all"
subreddit.sectionName = ""
if subreddit.objectID.isTemporaryID {
subreddit.order = NSNumber(value: 1 as Int)
subreddit.order = NSNumber(value: 2 as Int)
}

try context?.obtainPermanentIDs(for: [subreddit])
Expand All @@ -243,7 +283,7 @@ open class Subreddit: SyncObject {
throw thrownError
}
subreddit.title = NSLocalizedString("subreddit-all", comment: "The title used for the all section on reddit. This is a collection of all subbreddits")
subreddit.displayName = NSLocalizedString("subreddit-all", comment: "The title used for the all scction on reddit. This is a collection of all subbreddits")
subreddit.displayName = NSLocalizedString("subreddit-all", comment: "The title used for the all section on reddit. This is a collection of all subbreddits")
subreddit.isBookmarked = NSNumber(value: true as Bool)

return subreddit
Expand Down
2 changes: 1 addition & 1 deletion Snoo/Data Operations/ClearUserRelationsOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class ClearUserRelationsOperation: DataOperation {

fileprivate func predicateForEntityName(_ name: String) -> NSPredicate? {
if name == Subreddit.entityName() {
return NSPredicate(format: "NOT (identifier IN %@)", [Subreddit.frontpageIdentifier, Subreddit.allIdentifier])
return NSPredicate(format: "NOT (identifier IN %@)", [Subreddit.frontpageIdentifier, Subreddit.allIdentifier, Subreddit.popularIdentifier])
}
return nil
}
Expand Down