RedditSubmitPrefill.swift 747 B

12345678910111213141516171819202122232425
  1. import Foundation
  2. struct RedditSubmitPrefill: Equatable, Sendable {
  3. let postType: RedditPostType
  4. let body: String
  5. let pollOptions: [String]
  6. let pollDurationDays: Int
  7. static func from(draft: PostDraft) -> RedditSubmitPrefill? {
  8. guard draft.postType == .poll else { return nil }
  9. let options = draft.pollOptions
  10. .map { $0.text.trimmingCharacters(in: .whitespaces) }
  11. .filter { !$0.isEmpty }
  12. guard options.count >= 2 else { return nil }
  13. return RedditSubmitPrefill(
  14. postType: .poll,
  15. body: draft.body.trimmingCharacters(in: .whitespaces),
  16. pollOptions: options,
  17. pollDurationDays: draft.pollDuration.rawValue
  18. )
  19. }
  20. }