Skip to content

Commit

Permalink
Add Wikifeeds featured content models.
Browse files Browse the repository at this point in the history
  • Loading branch information
staykids committed Mar 22, 2023
1 parent 88b5d4c commit 4c47aca
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 59 deletions.
15 changes: 15 additions & 0 deletions WMF Framework/Widget/Models/WidgetContentURL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

public struct WidgetContentURL: Codable {

// MARK: - Nested Types

public struct PageURL: Codable {
public let page: String
}

// MARK: - Properties

public let desktop: PageURL

}
35 changes: 35 additions & 0 deletions WMF Framework/Widget/Models/WidgetFeaturedArticle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation

public struct WidgetFeaturedArticle: Codable {

// MARK: - Nested Types

enum CodingKeys: String, CodingKey {
case displayTitle = "displaytitle"
case description
case extract
case languageCode = "lang"
case languageDirection = "dir"
case contentURL = "content_urls"
case thumbnailImageSource = "thumbnail"
case originalImageSource = "originalimage"
}

// MARK: - Properties

public var displayTitle: String
public let description: String?
public let extract: String
public let languageCode: String
public let languageDirection: String
public let contentURL: WidgetContentURL
public var thumbnailImageSource: WidgetImageSource?
public var originalImageSource: WidgetImageSource?

// MARK: - Computed Properties

public var isRTL: Bool {
return languageDirection.caseInsensitiveCompare("rtl") == .orderedSame
}

}
59 changes: 8 additions & 51 deletions WMF Framework/Widget/Models/WidgetFeaturedContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,19 @@ public struct WidgetFeaturedContent: Codable {

enum CodingKeys: String, CodingKey {
case featuredArticle = "tfa"
case topRead = "mostread"
case onThisDay = "onthisday"
case pictureOfTheDay = "image"
case fetchDate
case fetchedLanguageVariantCode
}

public struct FeaturedArticleContent: Codable {

// MARK: - Featured Article - Nested Types

enum CodingKeys: String, CodingKey {
case displayTitle = "displaytitle"
case description
case extract
case languageCode = "lang"
case languageDirection = "dir"
case contentURL = "content_urls"
case thumbnailImageSource = "thumbnail"
case originalImageSource = "originalimage"
}

public struct ContentURL: Codable {
public struct PageURL: Codable {
public let page: String
}

public let desktop: PageURL
}

public struct ImageSource: Codable {
enum CodingKeys: String, CodingKey {
case source
case width
case height
case data
}

public let source: String
public let width: Int
public let height: Int
public var data: Data?
}

// MARK: - Featured Article - Properties

public var displayTitle: String
public let description: String?
public let extract: String
public let languageCode: String
public let languageDirection: String
public let contentURL: ContentURL
public var thumbnailImageSource: ImageSource?
public var originalImageSource: ImageSource?
}

// MARK: - Properties

public var featuredArticle: FeaturedArticleContent?
public var featuredArticle: WidgetFeaturedArticle?
public var topRead: WidgetTopRead?
public var onThisDay: [WidgetOnThisDayElement]?
public var pictureOfTheDay: WidgetPictureOfTheDay?

// MARK: - Properties - Network Fetch Metadata

Expand All @@ -71,7 +28,7 @@ public struct WidgetFeaturedContent: Codable {
// MARK: - Public

public static func previewContent() -> WidgetFeaturedContent? {
if let previewContentFilePath = Bundle.main.path(forResource: "Featured Article Widget Preview Content", ofType: "json"), let jsonData = try? String(contentsOfFile: previewContentFilePath).data(using: .utf8) {
if let previewContentFilePath = Bundle.main.path(forResource: "Widget Featured Content Preview", ofType: "json"), let jsonData = try? String(contentsOfFile: previewContentFilePath).data(using: .utf8) {
return try? JSONDecoder().decode(WidgetFeaturedContent.self, from: jsonData)
}

Expand Down
23 changes: 23 additions & 0 deletions WMF Framework/Widget/Models/WidgetImageSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

public struct WidgetImageSource: Codable {

// MARK: - Nested Types

enum CodingKeys: String, CodingKey {
case source
case width
case height
case data
}

// MARK: - Properties

public let source: String
public let width: Int
public let height: Int

// Data is populated via local network request, not via API featured content API request
public var data: Data?

}
54 changes: 54 additions & 0 deletions WMF Framework/Widget/Models/WidgetOnThisDayElement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Foundation

public struct WidgetOnThisDayElement: Codable {

// MARK: - Nested Types

public struct Page: Codable {

// MARK: - Nested Types

enum CodingKeys: String, CodingKey {
case title
case displayTitle = "displaytitle"
case normalizedTitle = "normalizedtitle"
case description
case language = "lang"
case languageDirection = "dir"
case extract
case extractHTML = "extract_html"
case contentURL = "content_urls"
case thumbnailImageSource = "thumbnail"
case originalImageSource = "originalimage"
}

// MARK: - Properties

let title: String
let displayTitle: String
let normalizedTitle: String
let description: String?
let language: String
let languageDirection: String
let extract: String
let extractHTML: String
let contentURL: WidgetContentURL
let thumbnailImageSource: WidgetImageSource?
let originalImageSource: WidgetImageSource?

// MARK: - Computed Properties

public var isRTL: Bool {
return languageDirection.caseInsensitiveCompare("rtl") == .orderedSame
}

}

// MARK: - Properties

let text: String
let year: Int
let pages: [Page]

}

38 changes: 38 additions & 0 deletions WMF Framework/Widget/Models/WidgetPictureOfTheDay.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

public struct WidgetPictureOfTheDay: Codable {

// MARK: - Nested Types

enum CodingKeys: String, CodingKey {
case description
case license
case thumbnailImageSource = "thumbnail"
case originalImageSource = "image"
}

public struct License: Codable {
let type: String
let url: String
}

public struct Description: Codable {
enum CodingKeys: String, CodingKey {
case text
case html
case language = "lang"
}

let text: String
let html: String
let language: String
}

// MARK: - Properties

let description: Description
let license: License
let thumbnailImageSource: WidgetImageSource? // thumbnail
let originalImageSource: WidgetImageSource? // image

}
10 changes: 10 additions & 0 deletions WMF Framework/Widget/Models/WidgetTitles.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

public struct WidgetTitles: Codable {

let canonical: String
let normalized: String
let display: String

}

65 changes: 65 additions & 0 deletions WMF Framework/Widget/Models/WidgetTopRead.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Foundation

public struct WidgetTopRead: Codable {

// MARK: - Nested Types

enum CodingKeys: String, CodingKey {
case elements = "articles"
}

public struct Article: Codable {

// MARK: - Nested Types

public struct ViewHistoryDataPoint: Codable {
let views: Int
}

enum CodingKeys: String, CodingKey {
case views
case titles
case pageTitle = "title"
case displayTitle = "displaytitle"
case normalizedTitle = "normalizedtitle"
case timestamp
case viewHistory = "view_history"
case thumbnailImageSource = "thumbnail"
case originalImageSource = "originalimage"
case language = "lang"
case languageDirection = "dir"
case contentURL = "content_urls"
case extract
case extractHTML = "extract_html"
}

// MARK: - Properties

let views: Int
let titles: WidgetTitles
let pageTitle: String
let displayTitle: String
let normalizedTitle: String
let timestamp: String
let viewHistory: [ViewHistoryDataPoint] // ordered from oldest to newest
let thumbnailImageSource: WidgetImageSource?
let originalImageSource: WidgetImageSource?
let language: String
let languageDirection: String
let contentURL: WidgetContentURL
let extract: String
let extractHTML: String

// MARK: - Computed Properties

public var isRTL: Bool {
return languageDirection.caseInsensitiveCompare("rtl") == .orderedSame
}

}

// MARK: - Properties

public let elements: [Article]

}
2 changes: 1 addition & 1 deletion WMF Framework/Widget/WidgetContentFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class WidgetContentFetcher {
dataTask.resume()
}

public func fetchImageDataFrom(imageSource: WidgetFeaturedContent.FeaturedArticleContent.ImageSource, completion: @escaping (Result<Data, FetcherError>) -> Void) {
public func fetchImageDataFrom(imageSource: WidgetImageSource, completion: @escaping (Result<Data, FetcherError>) -> Void) {
guard let imageURL = URL(string: imageSource.source) else {
completion(.failure(.urlFailure))
return
Expand Down
Loading

0 comments on commit 4c47aca

Please sign in to comment.