| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import Foundation
- protocol PostGenerationServiceProtocol: Sendable {
- func generatePost(from draft: PostDraft) async throws -> GeneratedPost
- }
- enum PostGenerationError: LocalizedError {
- case emptyTopic
- case emptySubreddit
- var errorDescription: String? {
- switch self {
- case .emptyTopic: "Enter a topic or keywords for AI generation."
- case .emptySubreddit: "Enter a subreddit to tailor the post."
- }
- }
- }
- struct MockPostGenerationService: PostGenerationServiceProtocol {
- func generatePost(from draft: PostDraft) async throws -> GeneratedPost {
- guard !draft.subreddit.trimmingCharacters(in: .whitespaces).isEmpty else {
- throw PostGenerationError.emptySubreddit
- }
- guard !draft.topic.trimmingCharacters(in: .whitespaces).isEmpty else {
- throw PostGenerationError.emptyTopic
- }
- try await Task.sleep(for: .milliseconds(900))
- let subreddit = draft.subreddit.replacingOccurrences(of: "r/", with: "")
- let topic = draft.topic.trimmingCharacters(in: .whitespaces)
- let tone = draft.tone.title.lowercased()
- let title = makeTitle(topic: topic, subreddit: subreddit, tone: draft.tone)
- let body = makeBody(
- topic: topic,
- subreddit: subreddit,
- tone: tone,
- postType: draft.postType
- )
- let flair = suggestFlair(for: draft.postType, subreddit: subreddit)
- return GeneratedPost(title: title, body: body, suggestedFlair: flair)
- }
- private func makeTitle(topic: String, subreddit: String, tone: PostTone) -> String {
- switch tone {
- case .humorous:
- "I tried \(topic) so you don't have to — r/\(subreddit) was not wrong"
- case .professional:
- "Analysis: \(topic) — findings from r/\(subreddit)"
- case .informative:
- "Everything you need to know about \(topic) [Guide]"
- case .controversial:
- "Unpopular opinion: \(topic) is overrated. Change my mind."
- case .storytelling:
- "How \(topic) completely changed my perspective"
- case .casual:
- "Anyone else obsessed with \(topic) lately?"
- }
- }
- private func makeBody(
- topic: String,
- subreddit: String,
- tone: String,
- postType: RedditPostType
- ) -> String {
- switch postType {
- case .text:
- """
- Hey r/\(subreddit),
- I've been diving deep into **\(topic)** and wanted to share what I've learned.
- **Key points:**
- - Started exploring this after seeing it trending here
- - The community's take has been incredibly helpful
- - Still have a few open questions (see below)
- **My experience:**
- This started as a casual experiment but turned into something I genuinely care about. Would love to hear how others in this subreddit approach \(topic).
- **Questions for the community:**
- 1. What's your go-to resource for \(topic)?
- 2. Any common mistakes beginners should avoid?
- 3. Who else here is working on something similar?
- Written in a \(tone) tone — happy to refine based on feedback!
- """
- case .image:
- "Caption: A visual breakdown of \(topic) — details in the comments. Let me know what you think!"
- case .link:
- "Found this great resource on \(topic). Thought r/\(subreddit) would find it useful. Summary in comments."
- case .video:
- "Just watched this breakdown of \(topic) and had to share. The section at 3:42 really nails it."
- case .poll:
- "Curious where r/\(subreddit) stands on \(topic). Vote below!"
- }
- }
- private func suggestFlair(for postType: RedditPostType, subreddit: String) -> String {
- switch postType {
- case .text: "Discussion"
- case .image: "Media"
- case .link: "News"
- case .video: "Video"
- case .poll: "Poll"
- }
- }
- }
|