2019-12-20 19:09:20 -08:00
//
// W i d g e t S t o r y . s w i f t
// W i d g e t E x t e n s i o n
//
// C r e a t e d b y D a v i d S i n c l a i r o n 2 0 1 9 - 1 1 - 2 9 .
2021-08-11 21:57:49 -07:00
// C o p y r i g h t © 2 0 2 1 N e w s B l u r . A l l r i g h t s r e s e r v e d .
2019-12-20 19:09:20 -08:00
//
2021-08-11 21:57:49 -07:00
import SwiftUI
2019-12-20 19:09:20 -08:00
// / A s t o r y t o d i s p l a y i n t h e w i d g e t .
struct Story : Codable , Identifiable {
// / T h e v e r s i o n n u m b e r .
let version = 1
// / T h e s t o r y h a s h .
let id : String
// / T h e f e e d I D .
let feed : String
// / T h e d a t e a n d / o r t i m e a s a s t r i n g .
let date : String
// / T h e a u t h o r o f t h e s t o r y .
let author : String
// / T h e t i t l e o f t h e s t o r y .
let title : String
// / T h e c o n t e n t o f t h e s t o r y .
let content : String
2022-02-02 15:22:57 -08:00
// / T h e t h u m b n a i l i m a g e d a t a , o r ` n i l ` i f n o n e .
let imageData : Data ?
// / T h e t h u m b n a i l i m a g e , o r ` n i l ` i f n o n e .
let image : UIImage ?
2019-12-20 19:09:20 -08:00
// / K e y s f o r t h e d i c t i o n a r y r e p r e s e n t a t i o n .
struct DictionaryKeys {
static let id = " story_hash "
static let feed = " story_feed_id "
static let date = " short_parsed_date "
static let author = " story_authors "
static let title = " story_title "
static let content = " story_content "
2022-02-02 15:22:57 -08:00
static let imageData = " select_thumbnail_data "
2019-12-20 19:09:20 -08:00
}
// / I n i t i a l i z e r f r o m a d i c t i o n a r y .
// /
// / - P a r a m e t e r d i c t i o n a r y : D i c t i o n a r y f r o m t h e s e r v e r .
init ( from dictionary : [ String : Any ] ) {
id = dictionary [ DictionaryKeys . id ] as ? String ? ? " "
feed = dictionary [ DictionaryKeys . feed ] as ? String ? ? " \( dictionary [ DictionaryKeys . feed ] as ? Int ? ? 0 ) "
date = dictionary [ DictionaryKeys . date ] as ? String ? ? " "
author = dictionary [ DictionaryKeys . author ] as ? String ? ? " "
title = dictionary [ DictionaryKeys . title ] as ? String ? ? " "
content = dictionary [ DictionaryKeys . content ] as ? String ? ? " "
2022-02-02 15:22:57 -08:00
if let base64 = dictionary [ DictionaryKeys . imageData ] as ? String {
imageData = Data ( base64Encoded : base64 )
} else {
imageData = nil
}
if let data = imageData {
image = UIImage ( data : data )
2019-12-20 19:09:20 -08:00
} else {
2022-02-02 15:22:57 -08:00
image = nil
2019-12-20 19:09:20 -08:00
}
}
2021-08-11 21:57:49 -07:00
// / I n i t i a l i z e r f o r a s a m p l e .
// /
// / - P a r a m e t e r t i t l e : T h e t i t l e o f t h e s a m p l e .
// / - P a r a m e t e r f e e d : T h e f e e d i d e n t i f i e r .
init ( sample title : String , feed : String ) {
id = UUID ( ) . uuidString
self . feed = feed
date = " 2021-08-09 "
author = " Sample "
self . title = title
content = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec ornare dolor. Vivamus porta mi nec libero convallis tempus. Cras semper, ante et pretium vulputate, risus urna venenatis magna, vitae fringilla ipsum ante ut augue. Cras euismod, eros convallis scelerisque congue, massa sem elementum sem, ut condimentum est tortor id mauris. "
2022-02-02 15:22:57 -08:00
imageData = nil
image = UIImage ( systemName : " globe.americas.fill " )
2021-08-11 21:57:49 -07:00
}
2019-12-20 19:09:20 -08:00
// / K e y s f o r t h e c o d a b l e r e p r e s e n t a t i o n .
enum CodingKeys : String , CodingKey {
case version = " version "
case id = " id "
case feed = " feed "
case date = " date "
case author = " author "
case title = " title "
case content = " content "
2022-02-02 15:22:57 -08:00
case imageData = " imageData "
2019-12-20 19:09:20 -08:00
}
// / I n i t i a l i z e r t o l o a d f r o m t h e J S O N d a t a .
// /
// / - P a r a m e t e r d e c o d e r : T h e d e c o d e r f r o m w h i c h t o r e a d d a t a .
// / - T h r o w s : A n e r r o r i f t h e d a t a i s i n v a l i d .
init ( from decoder : Decoder ) throws {
let container = try decoder . container ( keyedBy : CodingKeys . self )
id = try container . decode ( String . self , forKey : . id )
feed = try container . decode ( String . self , forKey : . feed )
date = try container . decode ( String . self , forKey : . date )
author = try container . decode ( String . self , forKey : . author )
title = try container . decode ( String . self , forKey : . title )
content = try container . decode ( String . self , forKey : . content )
2022-02-02 15:22:57 -08:00
imageData = try container . decodeIfPresent ( Data . self , forKey : . imageData )
if let data = imageData {
image = UIImage ( data : data )
} else {
image = nil
}
2019-12-20 19:09:20 -08:00
}
// / E n c o d e s t h e s t o r y i n t o t h e g i v e n e n c o d e r .
// /
// / - P a r a m e t e r e n c o d e r : T h e e n c o d e r t o w h i c h t o w r i t e d a t a .
// / - T h r o w s : A n e r r o r i f t h e d a t a i s i n v a l i d .
func encode ( to encoder : Encoder ) throws {
var container = encoder . container ( keyedBy : CodingKeys . self )
try container . encode ( version , forKey : . version )
try container . encode ( id , forKey : . id )
try container . encode ( feed , forKey : . feed )
try container . encode ( date , forKey : . date )
try container . encode ( author , forKey : . author )
try container . encode ( title , forKey : . title )
try container . encode ( content , forKey : . content )
2022-02-02 15:22:57 -08:00
try container . encodeIfPresent ( imageData , forKey : . imageData )
2019-12-20 19:09:20 -08:00
}
}
extension Story : Equatable {
static func = = ( lhs : Story , rhs : Story ) -> Bool {
return lhs . id = = rhs . id
}
}
extension Story : CustomStringConvertible {
var description : String {
return " Story \( title ) by \( author ) ( \( id ) ) "
}
}