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

Use template where clause to avoid a runtime type test. #14

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f1ee537
Use template where clause to avoid a runtime type test.
jackpal Aug 1, 2020
7766031
Beta 4 fixes
Dimillian Aug 5, 2020
10bf0a3
Proper settings view
Dimillian Aug 5, 2020
8e43c8a
WIP quick search + display awards
Dimillian Aug 5, 2020
3194a45
Settings + WIP search
Dimillian Aug 9, 2020
124533e
Rich text suppor for flair + flair & awards for comments.
Dimillian Aug 10, 2020
67e3a9d
About community + subreddit icon in toolbar
Dimillian Aug 12, 2020
e16f776
New commnts system + share picker
Dimillian Aug 12, 2020
de011fe
New menu shortcuts
Dimillian Aug 12, 2020
4004482
Better route handling
Dimillian Aug 13, 2020
54959d9
Subscribe / Favorite from about popover
Dimillian Aug 18, 2020
343d75a
WIP
Dimillian Aug 19, 2020
ddec324
Fixes on toolbar and post row
Dimillian Sep 4, 2020
11e74fb
Replace SDWebImage with KingFisher
Dimillian Sep 4, 2020
eacc4c3
Cleanup
Dimillian Sep 8, 2020
0fa0475
Big Sur Beta 7 update
Dimillian Sep 22, 2020
fb9c638
Cleanup profile + improve search
Dimillian Sep 30, 2020
dcb7102
Fixes
Dimillian Sep 30, 2020
a2bcd59
Various improvements + update KinfFisher
Dimillian Oct 1, 2020
42c0021
Color fixes
Dimillian Oct 6, 2020
ab54c18
Update README.md
Dimillian Nov 13, 2020
914cd1b
Fix tests
Dimillian Dec 1, 2020
da6c201
Fixed typo on line 07
kant Nov 19, 2020
840810b
Various fixes
Dimillian Dec 16, 2020
79368dd
added clear instructions of how to add secrets.plist
rursache Dec 5, 2020
2b37ee4
Update README.md
Dimillian Dec 31, 2020
46aa0e0
Introduce url extension to help avoid force unwrapping for static url…
DanKorkelia Jan 1, 2021
aa2687f
Restore development team name in project file
DanKorkelia Jan 1, 2021
81fd956
Update function signiture
DanKorkelia Jan 1, 2021
31f9ad3
Improve call site usage
DanKorkelia Jan 1, 2021
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
19 changes: 19 additions & 0 deletions Packages/Backend/Sources/Backend/Extensions/URL+StaticString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// URL+StaticString.swift
//
//
// Created by Dan Korkelia on 01/01/2021.
//

import Foundation

extension URL {
/// Use this init for static URL strings to avoid using force unwrap or doing redundant error handling
/// - Parameter string: static url ie https://www.example.com/privacy/
init(staticString: StaticString) {
guard let url = URL(string: "\(staticString)") else {
fatalError("URL is illegal: \(staticString)")
}
self = url
}
}
24 changes: 24 additions & 0 deletions Packages/Backend/Sources/Backend/Models/Award.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// File.swift
//
//
// Created by Thomas Ricouard on 05/08/2020.
//

import Foundation

public struct Award: Decodable, Identifiable {
public let id: String
public let name: String
public let staticIconUrl: URL
public let description: String
public let count: Int
public let coinPrice: Int

public static let `default` = Award(id: "award",
name: "Awesome",
staticIconUrl: URL(staticString: "https://i.redd.it/award_images/t5_22cerq/5smbysczm1w41_Hugz.png"),
description: "Awesome reward",
count: 5,
coinPrice: 200)
}
29 changes: 26 additions & 3 deletions Packages/Backend/Sources/Backend/Models/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,27 @@ public struct Comment: Decodable, Identifiable {
public let id: String
public let name: String
public let body: String?
public let isSubmitter: Bool?
public let author: String?
public let lindId: String?
public let created: Date?
public let createdUtc: Date?
public let replies: Replies?
public let score: Int?
public var score: Int?
public var likes: Bool?
public let allAwardings: [Award]?
public var saved: Bool?

public let permalink: String?
public var permalinkURL: URL? {
guard let permalink = permalink else { return nil }
return URL(string: "https://reddit.com\(permalink)")
}

public let authorFlairRichtext: [FlairRichText]?
public let authorFlairText: String?
public let authorFlairTextColor: String?
public let authorFlairBackgroundColor: String?

public var repliesComments: [Comment]? {
if let replies = replies {
Expand Down Expand Up @@ -53,11 +68,19 @@ public enum Replies: Decodable {

public let static_comment = Comment(id: UUID().uuidString,
name: "t1_id",
body: "Comment text with a long line of text \n and another line.",
body: "Comment text with a long line of text\nThis is another line.",
isSubmitter: false,
author: "TestUser",
lindId: "",
created: Date(),
createdUtc: Date(),
replies: .none(""),
score: 2500)
score: 2500,
allAwardings: [],
saved: false,
permalink: "",
authorFlairRichtext: nil,
authorFlairText: nil,
authorFlairTextColor: nil,
authorFlairBackgroundColor: nil)
public let static_comments = [static_comment, static_comment, static_comment]
23 changes: 23 additions & 0 deletions Packages/Backend/Sources/Backend/Models/FlairRichText.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// File.swift
//
//
// Created by Thomas Ricouard on 10/08/2020.
//

import Foundation

public struct FlairRichText: Decodable, Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(e)
hasher.combine(u)
hasher.combine(t)
}

/// type
public let e: String
/// image URL
public let u: URL?
/// text
public let t: String?
}
22 changes: 16 additions & 6 deletions Packages/Backend/Sources/Backend/Models/Listing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ public struct ListingHolder<T: Decodable>: Decodable {
case kind, data
}

public init(from decoder: Decoder) throws {
public init(from decoder: Decoder) throws
where T == GenericListingContent
{
let container = try decoder.container(keyedBy: CodingKeys.self)
kind = try container.decode(String.self, forKey: .kind)
if T.self == GenericListingContent.self {
data = try GenericListingContent(from: decoder) as! T
} else {
data = try container.decode(T.self, forKey: .data)
}
data = try T(from: decoder)
}

public init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
kind = try container.decode(String.self, forKey: .kind)
// This assert can be removed before merging the
// patch. It's just here to verify that
// the specialized init, above, is used for
// when T is a GenericListingContent.
assert(T.self != GenericListingContent.self)
data = try container.decode(T.self, forKey: .data)
}
}

Expand Down
52 changes: 52 additions & 0 deletions Packages/Backend/Sources/Backend/Models/Media.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// File.swift
//
//
// Created by Thomas Ricouard on 05/08/2020.
//

import Foundation

public struct SecureMedia: Decodable {
public let redditVideo: RedditVideo?
public let oembed: Oembed?

public var video: Video? {
if let video = redditVideo {
return Video(url: video.fallbackUrl, width: video.width, height: video.height)
} else if oembed?.type == "video",
let oembed = oembed,
let url = oembed.url,
let width = oembed.width,
let height = oembed.height {
return Video(url: url, width: width, height: height)
}
return nil
}
}

public struct RedditVideo: Decodable {
public let fallbackUrl: URL
public let height: Int
public let width: Int
}

public struct Oembed: Decodable {
public let providerUrl: URL?
public let thumbnailUrl: String?
public var thumbnailUrlAsURL: URL? {
thumbnailUrl != nil ? URL(string: thumbnailUrl!) : nil
}
public let url: URL?
public let width: Int?
public let height: Int?
public let thumbnailWidth: Int?
public let thumbnailHeight: Int?
public let type: String?
}

public struct Video {
public let url: URL
public let width: Int
public let height: Int
}
23 changes: 23 additions & 0 deletions Packages/Backend/Sources/Backend/Models/Subreddit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation

public struct Subreddit: Codable, Identifiable {
public let id: String
public let name: String
public let displayName: String
public let title: String
public let publicDescription: String?
Expand All @@ -12,4 +13,26 @@ public struct Subreddit: Codable, Identifiable {
public let bannerImg: String?
public let subscribers: Int?
public let accountsActive: Int?
public let createdUtc: Date
public let url: String
public var redditURL: URL {
URL(string: "https://reddit.com\(url)")!
}
public var userIsSubscriber: Bool?
}

public let static_subreddit_full = Subreddit(id: "games",
name: "t3_fjfj",
displayName: "games",
title: "games",
publicDescription: "a description",
primaryColor: "#545452",
keyColor: "#545452",
bannerBackgroundColor: "#545452",
iconImg: "https://a.thumbs.redditmedia.com/8hr1PTpJ9iWLNWP67vZN0w3IEP8uI3eAQ1kE4XLRg88.png",
bannerImg: "https://a.thumbs.redditmedia.com/8hr1PTpJ9iWLNWP67vZN0w3IEP8uI3eAQ1kE4XLRg88.png",
subscribers: 1000,
accountsActive: 500,
createdUtc: Date(),
url: "/r/games",
userIsSubscriber: false)
63 changes: 18 additions & 45 deletions Packages/Backend/Sources/Backend/Models/SubredditPost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import Foundation

public struct SubredditPost: Decodable, Identifiable, Hashable {
public static func == (lhs: SubredditPost, rhs: SubredditPost) -> Bool {
lhs.id == rhs.id && lhs.likes == rhs.likes
lhs.id == rhs.id &&
lhs.likes == rhs.likes &&
lhs.saved == rhs.saved
}

public func hash(into hasher: inout Hasher) {
Expand Down Expand Up @@ -39,9 +41,18 @@ public struct SubredditPost: Decodable, Identifiable, Hashable {
public let secureMedia: SecureMedia?
public let url: String?
public let permalink: String?

public let linkFlairText: String?
public let linkFlairRichtext: [FlairRichText]?
public let linkFlairBackgroundColor: String?
public let linkFlairTextColor: String?

public let authorFlairRichtext: [FlairRichText]?
public let authorFlairText: String?
public let authorFlairTextColor: String?
public let authorFlairBackgroundColor: String?

public let allAwardings: [Award]
public var visited: Bool
public var saved: Bool
public var redditURL: URL? {
Expand All @@ -53,50 +64,6 @@ public struct SubredditPost: Decodable, Identifiable, Hashable {
public var likes: Bool?
}

public struct SecureMedia: Decodable {
public let redditVideo: RedditVideo?
public let oembed: Oembed?

public var video: Video? {
if let video = redditVideo {
return Video(url: video.fallbackUrl, width: video.width, height: video.height)
} else if oembed?.type == "video",
let oembed = oembed,
let url = oembed.url,
let width = oembed.width,
let height = oembed.height {
return Video(url: url, width: width, height: height)
}
return nil
}
}

public struct RedditVideo: Decodable {
public let fallbackUrl: URL
public let height: Int
public let width: Int
}

public struct Oembed: Decodable {
public let providerUrl: URL?
public let thumbnailUrl: String?
public var thumbnailUrlAsURL: URL? {
thumbnailUrl != nil ? URL(string: thumbnailUrl!) : nil
}
public let url: URL?
public let width: Int?
public let height: Int?
public let thumbnailWidth: Int?
public let thumbnailHeight: Int?
public let type: String?
}

public struct Video {
public let url: URL
public let width: Int
public let height: Int
}

public let static_listing = SubredditPost(id: "0",
name: "t3_0",
title: "A very long title to be able to debug the UI correctly as it should be displayed on mutliple lines.",
Expand All @@ -114,8 +81,14 @@ public let static_listing = SubredditPost(id: "0",
url: "https://test.com",
permalink: nil,
linkFlairText: nil,
linkFlairRichtext: nil,
linkFlairBackgroundColor: nil,
linkFlairTextColor: nil,
authorFlairRichtext: nil,
authorFlairText: nil,
authorFlairTextColor: nil,
authorFlairBackgroundColor: nil,
allAwardings: [],
visited: false,
saved: false,
likes: nil)
4 changes: 4 additions & 0 deletions Packages/Backend/Sources/Backend/Models/SubredditSmall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public struct SubredditResponse: Decodable {
}

public struct SubredditSmall: Codable, Identifiable, Equatable, Hashable {
public static func == (lhs: SubredditSmall, rhs: SubredditSmall) -> Bool {
lhs.id == rhs.id
}

public var id: String { name }
public let name: String
public let subscriberCount: Int
Expand Down
12 changes: 12 additions & 0 deletions Packages/Backend/Sources/Backend/Models/Vote.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Thomas Ricouard on 12/08/2020.
//

import Foundation

public enum Vote: Int {
case upvote = 1, downvote = -1, neutral = 0
}
3 changes: 3 additions & 0 deletions Packages/Backend/Sources/Backend/Network/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
public enum Endpoint {
case subreddit(name: String, sort: String?)
case subredditAbout(name: String)
case subscribe
case searchSubreddit
case comments(name: String, id: String)
case accessToken
Expand All @@ -26,6 +27,8 @@ public enum Endpoint {
}
case .searchSubreddit:
return "api/search_subreddits"
case .subscribe:
return "api/subscribe"
case let .comments(name, id):
return "r/\(name)/comments/\(id)"
case .accessToken:
Expand Down
Loading