From 2a15f2ea733084354230b496c7ebe5d608330b40 Mon Sep 17 00:00:00 2001 From: Wilhelm Oks Date: Thu, 12 Dec 2024 12:24:35 +0100 Subject: [PATCH] Models WIP: Weekly, RantFeed --- .../SwiftDevRant/Models/RantFeed.News.swift | 68 +++++++++++++++++++ Sources/SwiftDevRant/Models/RantFeed.swift | 47 +++++++++++++ Sources/SwiftDevRant/Models/User.swift | 5 ++ Sources/SwiftDevRant/Models/Weekly.swift | 47 +++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 Sources/SwiftDevRant/Models/RantFeed.News.swift create mode 100644 Sources/SwiftDevRant/Models/RantFeed.swift create mode 100644 Sources/SwiftDevRant/Models/Weekly.swift diff --git a/Sources/SwiftDevRant/Models/RantFeed.News.swift b/Sources/SwiftDevRant/Models/RantFeed.News.swift new file mode 100644 index 0000000..29de217 --- /dev/null +++ b/Sources/SwiftDevRant/Models/RantFeed.News.swift @@ -0,0 +1,68 @@ +public extension RantFeed { + /// Contains information about news given in rant feeds. + /// - note: This is mostly used for weekly group rants. + struct News: Hashable, Identifiable { + public enum Action: String { + case groupRant = "grouprant" + case none = "none" + case rant = "rant" + } + + public let id: Int + + /// Most of the time this is equal to the value `intlink`, this specifies the type of news. + /// This should be an enum but it's unknown what the other values are and weekly news are dead anyway. + public let type: String + + /// The headline text of the news. + public let headlineText: String + + /// The contents of the news. + public let text: String + + /// The footer text of the news. + public let footerText: String + + /// The height of the news view on the screen. + public let height: Int + + /// The action that should be performed when the user taps/clicks on the news. + public let action: Action + + public init(id: Int, type: String, headlineText: String, text: String, footerText: String, height: Int, action: Action) { + self.id = id + self.type = type + self.headlineText = headlineText + self.text = text + self.footerText = footerText + self.height = height + self.action = action + } + } +} + +extension RantFeed.News { + struct CodingData: Codable { + let id: Int + let type: String + let headline: String + let body: String? + let footer: String + let height: Int + let action: String + } +} + +extension RantFeed.News.CodingData { + var decoded: RantFeed.News { + .init( + id: id, + type: type, + headlineText: headline, + text: body ?? "", + footerText: footer, + height: height, + action: .init(rawValue: action) ?? .none + ) + } +} diff --git a/Sources/SwiftDevRant/Models/RantFeed.swift b/Sources/SwiftDevRant/Models/RantFeed.swift new file mode 100644 index 0000000..85eb892 --- /dev/null +++ b/Sources/SwiftDevRant/Models/RantFeed.swift @@ -0,0 +1,47 @@ +/// Contains the list of rants for the logged in user and other random things. +public struct RantFeed: Hashable { + public var rants: [Rant] + + /// The notification settings for the logged-in user. + //public let settings: Settings + + public let sessionHash: String? + + /// The weekly group rant week number. + public let weeklyRantWeek: Int? + + /// True if the logged in user is subscribed to devRant++. + public let devRantSupporter: Bool + //public let isUserDPP: Int + + public let numberOfUnreadNotifications: Int + + public let news: News? +} + +public extension RantFeed { + enum Sort { + /// The devRant algorithm decides what rants appear in the feed. + case algorithm + + /// The most recent rants appear in the feed. + case recent + + /// The top rated rants appear in the feed. + case top(range: Range) + } + + enum Range { + /// Rants from the one day. + case day + + /// Rants from the one week. + case week + + /// Rants from the one month. + case month + + /// Rants from all time. + case all + } +} diff --git a/Sources/SwiftDevRant/Models/User.swift b/Sources/SwiftDevRant/Models/User.swift index e9662e6..97f0d41 100644 --- a/Sources/SwiftDevRant/Models/User.swift +++ b/Sources/SwiftDevRant/Models/User.swift @@ -1,7 +1,12 @@ public struct User: Identifiable, Hashable { public let id: Int + public let name: String + + /// The number of upvotes from other users. public let score: Int + + /// True if the logged in user is subscribed to devRant++. public let devRantSupporter: Bool /// A small avatar for the rant views and comment views. diff --git a/Sources/SwiftDevRant/Models/Weekly.swift b/Sources/SwiftDevRant/Models/Weekly.swift new file mode 100644 index 0000000..aed2b63 --- /dev/null +++ b/Sources/SwiftDevRant/Models/Weekly.swift @@ -0,0 +1,47 @@ +/// The weekly item data for the list of weeklies. +public struct Weekly: Hashable, Identifiable { + /// The number of the week. The first week starts with 1. + public let week: Int + + /// The weekly subject/topic. + public let topic: String + + /// The US formatted date the weekly. + public let formattedDate: String + + /// How many rants have beeon posted for this weekly. + public let numberOfRants: Int + + public var id: Int { week } + + public init(week: Int, topic: String, formattedDate: String, numberOfRants: Int) { + self.week = week + self.topic = topic + self.formattedDate = formattedDate + self.numberOfRants = numberOfRants + } +} + +extension Weekly { + struct CodingData: Codable { + let week: Int + let prompt: String + let date: String + let num_rants: Int + + struct List: Codable { + let weeks: [CodingData] + } + } +} + +extension Weekly.CodingData { + var decoded: Weekly { + .init( + week: week, + topic: prompt, + formattedDate: date, + numberOfRants: num_rants + ) + } +}