Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

113 lines
3.8 KiB

  1. import Foundation
  2. import WriteFreely
  3. enum PostStatus {
  4. case local
  5. case edited
  6. case published
  7. }
  8. class Post: Identifiable, ObservableObject, Hashable {
  9. @Published var wfPost: WFPost
  10. @Published var status: PostStatus
  11. @Published var collection: PostCollection?
  12. @Published var hasNewerRemoteCopy: Bool = false
  13. let id = UUID()
  14. init(
  15. title: String = "Title",
  16. body: String = "Write your post here...",
  17. createdDate: Date = Date(),
  18. status: PostStatus = .draft,
  19. collection: PostCollection? = nil
  20. ) {
  21. self.wfPost = WFPost(body: body, title: title, createdDate: createdDate)
  22. self.status = status
  23. self.collection = collection
  24. }
  25. convenience init(wfPost: WFPost, in collection: PostCollection? = nil) {
  26. self.init(
  27. title: wfPost.title ?? "",
  28. body: wfPost.body,
  29. createdDate: wfPost.createdDate ?? Date(),
  30. status: .published,
  31. collection: collection
  32. )
  33. self.wfPost = wfPost
  34. }
  35. }
  36. extension Post {
  37. static func == (lhs: Post, rhs: Post) -> Bool {
  38. return lhs.id == rhs.id
  39. }
  40. func hash(into hasher: inout Hasher) {
  41. hasher.combine(id)
  42. }
  43. }
  44. #if DEBUG
  45. let userCollection1 = PostCollection(title: "Collection 1")
  46. let userCollection2 = PostCollection(title: "Collection 2")
  47. let userCollection3 = PostCollection(title: "Collection 3")
  48. let testPost = Post(
  49. title: "Test Post Title",
  50. body: """
  51. Here's some cool sample body text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ultrices \
  52. posuere dignissim. Vestibulum a libero tempor, lacinia nulla vitae, congue purus. Nunc ac nulla quam. Duis \
  53. tincidunt eros augue, et volutpat tortor pulvinar ut. Nullam sit amet maximus urna. Phasellus non dignissim lacus.\
  54. Nulla ac posuere ex. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec \
  55. non molestie mauris. Suspendisse potenti. Vivamus at erat turpis.
  56. Pellentesque porttitor gravida tincidunt. Sed vitae eros non metus aliquam hendrerit. Aliquam sed risus suscipit \
  57. turpis dictum dictum. Duis lacus lectus, dictum vel felis in, rhoncus fringilla felis. Nunc id dolor nisl. Aliquam \
  58. euismod purus elit. Nullam egestas neque leo, sed aliquet ligula ultrices nec.
  59. """,
  60. createdDate: Date()
  61. )
  62. let testPostData = [
  63. Post(
  64. title: "My First Post",
  65. body: "Look at me, creating a first post! That's cool.",
  66. createdDate: Date(timeIntervalSince1970: 1595429452),
  67. status: .published,
  68. collection: userCollection1
  69. ),
  70. Post(
  71. title: "Post 2: The Quickening",
  72. body: "See, here's the rule about Highlander jokes: _there can be only one_.",
  73. createdDate: Date(timeIntervalSince1970: 1595514125),
  74. status: .edited,
  75. collection: userCollection1
  76. ),
  77. Post(
  78. title: "The Post Revolutions",
  79. body: "I can never keep the Matrix movie order straight. Why not just call them part 2 and part 3?",
  80. createdDate: Date(timeIntervalSince1970: 1595600006)
  81. ),
  82. Post(
  83. title: "Episode IV: A New Post",
  84. body: "How many movies does this person watch? How many movie-title jokes will they make?",
  85. createdDate: Date(timeIntervalSince1970: 1596219877),
  86. status: .published,
  87. collection: userCollection2
  88. ),
  89. Post(
  90. title: "Fast (Post) Five",
  91. body: "Look, it was either a Fast and the Furious reference, or a Resident Evil reference."
  92. ),
  93. Post(
  94. title: "Post: The Final Chapter",
  95. body: "And there you have it, a Resident Evil movie reference.",
  96. createdDate: Date(timeIntervalSince1970: 1596043684),
  97. status: .edited,
  98. collection: userCollection3
  99. )
  100. ]
  101. #endif