TitleOptimizerView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. import SwiftUI
  2. struct TitleOptimizerView: View {
  3. @Environment(\.requirePremiumAccess) private var requirePremiumAccess
  4. @Bindable var viewModel: TitleOptimizerViewModel
  5. var body: some View {
  6. VStack(spacing: 0) {
  7. header
  8. tabBar
  9. ScrollView {
  10. PostPageBoundary {
  11. switch viewModel.selectedTab {
  12. case .optimize:
  13. optimizeContent
  14. case .compare:
  15. compareContent
  16. case .preview:
  17. previewContent
  18. }
  19. }
  20. .padding(.horizontal, 24)
  21. .padding(.top, 8)
  22. .padding(.bottom, 24)
  23. }
  24. }
  25. .background(AppTheme.background)
  26. }
  27. // MARK: - Header
  28. private var header: some View {
  29. HStack(spacing: 14) {
  30. ModernSidebarIconView(kind: .titleOptimizer, size: 40)
  31. VStack(alignment: .leading, spacing: 2) {
  32. Text("Title Optimizer")
  33. .font(.system(size: 18, weight: .semibold))
  34. .foregroundStyle(AppTheme.textPrimary)
  35. Text("Craft viral Reddit titles with AI")
  36. .font(.system(size: 11))
  37. .foregroundStyle(AppTheme.textSecondary)
  38. }
  39. Spacer()
  40. headerActions
  41. }
  42. .padding(.horizontal, 24)
  43. .padding(.vertical, 16)
  44. .overlay(alignment: .bottom) {
  45. Rectangle()
  46. .fill(AppTheme.border)
  47. .frame(height: 1)
  48. }
  49. }
  50. private var headerActions: some View {
  51. HStack(spacing: 8) {
  52. Button {
  53. viewModel.resetDraft()
  54. } label: {
  55. Label("Reset", systemImage: "arrow.counterclockwise")
  56. .font(.system(size: 11, weight: .medium))
  57. }
  58. .buttonStyle(TitleSecondaryButtonStyle())
  59. Button {
  60. viewModel.copyToClipboard()
  61. } label: {
  62. Label("Copy", systemImage: "doc.on.doc")
  63. .font(.system(size: 11, weight: .medium))
  64. }
  65. .buttonStyle(TitleSecondaryButtonStyle())
  66. .disabled(viewModel.activeTitle.trimmingCharacters(in: .whitespaces).isEmpty)
  67. Button {
  68. guard requirePremiumAccess() else { return }
  69. Task { await viewModel.optimizeTitles() }
  70. } label: {
  71. HStack(spacing: 6) {
  72. if viewModel.isOptimizing {
  73. ProgressView()
  74. .controlSize(.small)
  75. .tint(.white)
  76. } else {
  77. Image(systemName: "sparkles")
  78. .font(.system(size: 11, weight: .semibold))
  79. }
  80. Text(viewModel.isOptimizing ? "Optimizing…" : "Optimize")
  81. .font(.system(size: 11, weight: .semibold))
  82. }
  83. .foregroundStyle(.white)
  84. .padding(.horizontal, 14)
  85. .padding(.vertical, 8)
  86. .background(viewModel.canOptimize ? AppTheme.accentBlue : AppTheme.accentBlue.opacity(0.4))
  87. .clipShape(RoundedRectangle(cornerRadius: 8))
  88. }
  89. .buttonStyle(.plain)
  90. .disabled(!viewModel.canOptimize)
  91. }
  92. }
  93. // MARK: - Tab Bar
  94. private var tabBar: some View {
  95. HStack(spacing: 4) {
  96. ForEach(TitleOptimizerTab.allCases) { tab in
  97. Button {
  98. viewModel.selectedTab = tab
  99. } label: {
  100. Text(tab.title)
  101. .font(.system(size: 11, weight: .semibold))
  102. .foregroundStyle(viewModel.selectedTab == tab ? AppTheme.textPrimary : AppTheme.textSecondary)
  103. .padding(.horizontal, 14)
  104. .padding(.vertical, 8)
  105. .background(
  106. viewModel.selectedTab == tab
  107. ? AppTheme.cardBackground
  108. : Color.clear
  109. )
  110. .clipShape(RoundedRectangle(cornerRadius: 8))
  111. }
  112. .buttonStyle(.plain)
  113. }
  114. Spacer()
  115. }
  116. .padding(.horizontal, 24)
  117. .padding(.top, 12)
  118. .padding(.bottom, 4)
  119. }
  120. // MARK: - Optimize
  121. private var optimizeContent: some View {
  122. HStack(alignment: .top, spacing: 16) {
  123. configurationPanel
  124. editorPanel
  125. }
  126. }
  127. private var configurationPanel: some View {
  128. VStack(spacing: 12) {
  129. PostFormCard(title: "Target Subreddit", subtitle: "Tailor titles for the community") {
  130. PostFormField(
  131. label: "Subreddit",
  132. placeholder: "technology",
  133. text: $viewModel.draft.subreddit,
  134. prefix: "r/"
  135. )
  136. }
  137. PostFormCard(title: "AI Settings", subtitle: "Guide topic, tone, and style") {
  138. VStack(alignment: .leading, spacing: 12) {
  139. PostFormField(
  140. label: "Topic / Keywords",
  141. placeholder: "e.g. macOS productivity tips",
  142. text: $viewModel.draft.topic
  143. )
  144. VStack(alignment: .leading, spacing: 6) {
  145. Text("Tone")
  146. .font(.system(size: 10, weight: .medium))
  147. .foregroundStyle(AppTheme.textTertiary)
  148. TonePicker(selectedTone: $viewModel.draft.tone)
  149. }
  150. }
  151. }
  152. PostFormCard(title: "Title Goal", subtitle: "What kind of hook do you want?") {
  153. TitleGoalPicker(selectedGoal: $viewModel.draft.titleGoal)
  154. }
  155. PostFormCard(title: "Post Context", subtitle: "Optional — affects preview style") {
  156. VStack(alignment: .leading, spacing: 12) {
  157. PostTypePicker(selectedType: Binding(
  158. get: { viewModel.draft.postType },
  159. set: { viewModel.selectPostType($0) }
  160. ))
  161. PostFormField(
  162. label: "Flair",
  163. placeholder: "Discussion, Question, Meme…",
  164. text: $viewModel.draft.flair
  165. )
  166. }
  167. }
  168. PostFormCard(title: "Post Tags", subtitle: "Reddit content labels") {
  169. HStack(spacing: 6) {
  170. PostTagToggle(
  171. title: "NSFW",
  172. systemImage: "exclamationmark.triangle.fill",
  173. activeColor: Color(hex: 0xEF4444),
  174. isOn: $viewModel.draft.isNSFW
  175. )
  176. PostTagToggle(
  177. title: "Spoiler",
  178. systemImage: "eye.slash.fill",
  179. activeColor: AppTheme.textSecondary,
  180. isOn: $viewModel.draft.isSpoiler
  181. )
  182. PostTagToggle(
  183. title: "OC",
  184. systemImage: "star.fill",
  185. activeColor: AppTheme.accentGreen,
  186. isOn: $viewModel.draft.isOC
  187. )
  188. }
  189. }
  190. PostFormCard(title: "Suggestions", subtitle: "How many variants to generate") {
  191. TitleVariantCountPicker(selectedCount: $viewModel.draft.variantCount)
  192. }
  193. if let error = viewModel.errorMessage {
  194. PostGeneratorMessageBanner(message: error, isError: true)
  195. } else if let success = viewModel.successMessage {
  196. PostGeneratorMessageBanner(message: success, isError: false)
  197. }
  198. }
  199. .frame(width: 300)
  200. }
  201. private var editorPanel: some View {
  202. VStack(spacing: 12) {
  203. PostFormCard(title: "Original Title", subtitle: "The title you want to improve") {
  204. PostFormField(
  205. label: "Post Title",
  206. placeholder: "Enter your draft title here",
  207. text: $viewModel.draft.originalTitle
  208. )
  209. }
  210. characterCountFooter
  211. if let analysis = viewModel.analysis {
  212. TitleAnalysisCard(analysis: analysis)
  213. }
  214. suggestionsSection
  215. }
  216. .frame(maxWidth: .infinity)
  217. }
  218. @ViewBuilder
  219. private var suggestionsSection: some View {
  220. PostFormCard(
  221. title: "AI Suggestions",
  222. subtitle: viewModel.variants.isEmpty
  223. ? "Hit Optimize to generate title variants"
  224. : "\(viewModel.variants.count) optimized titles ranked by score"
  225. ) {
  226. if viewModel.variants.isEmpty {
  227. TitleOptimizerEmptyState(
  228. icon: "textformat.size",
  229. title: "No suggestions yet",
  230. subtitle: "Fill in your subreddit, topic, and title,\nthen tap Optimize to get AI-powered variants."
  231. )
  232. } else {
  233. VStack(spacing: 8) {
  234. ForEach(viewModel.variants) { variant in
  235. TitleVariantRow(
  236. variant: variant,
  237. isSelected: viewModel.selectedVariantID == variant.id,
  238. onSelect: { viewModel.selectVariant(variant) },
  239. onApply: { viewModel.applyVariant(variant) }
  240. )
  241. }
  242. }
  243. }
  244. }
  245. }
  246. private var characterCountFooter: some View {
  247. HStack {
  248. Text("\(viewModel.draft.originalTitle.count) / 300")
  249. .font(.system(size: 10))
  250. .foregroundStyle(
  251. viewModel.draft.originalTitle.count > 300 ? Color(hex: 0xF87171) : AppTheme.textTertiary
  252. )
  253. Spacer()
  254. Text("Reddit limit: 300 characters for title")
  255. .font(.system(size: 10))
  256. .foregroundStyle(AppTheme.textTertiary)
  257. }
  258. }
  259. // MARK: - Compare
  260. private var compareContent: some View {
  261. VStack(spacing: 16) {
  262. if viewModel.variants.isEmpty {
  263. TitleOptimizerEmptyState(
  264. icon: "arrow.left.arrow.right",
  265. title: "Nothing to compare yet",
  266. subtitle: "Generate title suggestions on the Optimize tab first."
  267. )
  268. } else {
  269. compareHeader
  270. TitleCompareCard(
  271. title: viewModel.draft.originalTitle,
  272. score: viewModel.analysis?.overallScore,
  273. label: "Original",
  274. isOriginal: true,
  275. isSelected: viewModel.selectedVariantID == nil
  276. )
  277. HStack {
  278. Rectangle()
  279. .fill(AppTheme.border)
  280. .frame(height: 1)
  281. Text("vs")
  282. .font(.system(size: 10, weight: .semibold))
  283. .foregroundStyle(AppTheme.textTertiary)
  284. .padding(.horizontal, 8)
  285. Rectangle()
  286. .fill(AppTheme.border)
  287. .frame(height: 1)
  288. }
  289. VStack(spacing: 8) {
  290. ForEach(viewModel.variants) { variant in
  291. TitleCompareCard(
  292. title: variant.title,
  293. score: variant.score,
  294. label: "Variant #\(variantRank(variant))",
  295. isOriginal: false,
  296. isSelected: viewModel.selectedVariantID == variant.id
  297. )
  298. .onTapGesture {
  299. viewModel.selectVariant(variant)
  300. }
  301. }
  302. }
  303. compareMetadata
  304. }
  305. }
  306. .frame(maxWidth: 640)
  307. .frame(maxWidth: .infinity)
  308. }
  309. private var compareHeader: some View {
  310. HStack {
  311. VStack(alignment: .leading, spacing: 2) {
  312. Text("Side-by-Side Comparison")
  313. .font(.system(size: 12, weight: .semibold))
  314. .foregroundStyle(AppTheme.textPrimary)
  315. Text("Tap a variant to select it as active")
  316. .font(.system(size: 10))
  317. .foregroundStyle(AppTheme.textSecondary)
  318. }
  319. Spacer()
  320. if let best = viewModel.bestVariant {
  321. HStack(spacing: 6) {
  322. Image(systemName: "trophy.fill")
  323. .font(.system(size: 10))
  324. .foregroundStyle(AppTheme.accentYellow)
  325. Text("Best: \(best.score)/100")
  326. .font(.system(size: 10, weight: .semibold))
  327. .foregroundStyle(AppTheme.textSecondary)
  328. }
  329. }
  330. }
  331. }
  332. private var compareMetadata: some View {
  333. VStack(alignment: .leading, spacing: 8) {
  334. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  335. metadataRow(label: "Goal", value: viewModel.draft.titleGoal.title)
  336. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  337. metadataRow(label: "Active", value: viewModel.activeTitle)
  338. }
  339. .padding(14)
  340. .frame(maxWidth: .infinity, alignment: .leading)
  341. .background(
  342. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  343. .fill(AppTheme.cardBackground)
  344. .overlay(
  345. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  346. .stroke(AppTheme.cardBorder, lineWidth: 1)
  347. )
  348. )
  349. }
  350. private func variantRank(_ variant: TitleVariant) -> Int {
  351. (viewModel.variants.firstIndex(where: { $0.id == variant.id }) ?? 0) + 1
  352. }
  353. // MARK: - Preview
  354. private var previewContent: some View {
  355. VStack(spacing: 16) {
  356. Text("Live Preview")
  357. .font(.system(size: 12, weight: .semibold))
  358. .foregroundStyle(AppTheme.textSecondary)
  359. .frame(maxWidth: .infinity, alignment: .leading)
  360. RedditPostPreviewCard(
  361. draft: viewModel.previewDraft,
  362. formattedSubreddit: viewModel.formattedSubreddit
  363. )
  364. PostFormCard(title: "Feed Preview", subtitle: "How your title looks in a crowded feed") {
  365. VStack(spacing: 6) {
  366. TitleFeedPreviewRow(
  367. title: "Other trending post in this subreddit right now",
  368. subreddit: viewModel.formattedSubreddit
  369. )
  370. TitleFeedPreviewRow(
  371. title: viewModel.activeTitle,
  372. subreddit: viewModel.formattedSubreddit,
  373. isHighlighted: true
  374. )
  375. TitleFeedPreviewRow(
  376. title: "Another popular discussion happening nearby",
  377. subreddit: viewModel.formattedSubreddit
  378. )
  379. }
  380. }
  381. previewMetadata
  382. }
  383. .frame(maxWidth: 560)
  384. .frame(maxWidth: .infinity)
  385. }
  386. private var previewMetadata: some View {
  387. VStack(alignment: .leading, spacing: 8) {
  388. metadataRow(label: "Active Title", value: viewModel.activeTitle)
  389. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  390. metadataRow(label: "Goal", value: viewModel.draft.titleGoal.title)
  391. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  392. metadataRow(label: "Type", value: viewModel.draft.postType.title)
  393. if let score = viewModel.analysis?.overallScore {
  394. metadataRow(label: "Score", value: "\(score)/100")
  395. }
  396. if viewModel.draft.isNSFW || viewModel.draft.isSpoiler || viewModel.draft.isOC {
  397. metadataRow(
  398. label: "Tags",
  399. value: [
  400. viewModel.draft.isNSFW ? "NSFW" : nil,
  401. viewModel.draft.isSpoiler ? "Spoiler" : nil,
  402. viewModel.draft.isOC ? "OC" : nil,
  403. ]
  404. .compactMap { $0 }
  405. .joined(separator: ", ")
  406. )
  407. }
  408. }
  409. .padding(14)
  410. .frame(maxWidth: .infinity, alignment: .leading)
  411. .background(
  412. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  413. .fill(AppTheme.cardBackground)
  414. .overlay(
  415. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  416. .stroke(AppTheme.cardBorder, lineWidth: 1)
  417. )
  418. )
  419. }
  420. private func metadataRow(label: String, value: String) -> some View {
  421. HStack(alignment: .top) {
  422. Text(label)
  423. .font(.system(size: 10, weight: .medium))
  424. .foregroundStyle(AppTheme.textTertiary)
  425. .frame(width: 80, alignment: .leading)
  426. Text(value)
  427. .font(.system(size: 10))
  428. .foregroundStyle(AppTheme.textSecondary)
  429. .fixedSize(horizontal: false, vertical: true)
  430. }
  431. }
  432. }
  433. private struct TitleSecondaryButtonStyle: ButtonStyle {
  434. func makeBody(configuration: Configuration) -> some View {
  435. configuration.label
  436. .foregroundStyle(AppTheme.textSecondary)
  437. .padding(.horizontal, 12)
  438. .padding(.vertical, 8)
  439. .background(AppTheme.cardBackground.opacity(configuration.isPressed ? 0.6 : 1))
  440. .clipShape(RoundedRectangle(cornerRadius: 8))
  441. .overlay(
  442. RoundedRectangle(cornerRadius: 8)
  443. .stroke(AppTheme.cardBorder, lineWidth: 1)
  444. )
  445. }
  446. }
  447. #Preview {
  448. TitleOptimizerView(viewModel: TitleOptimizerViewModel())
  449. .frame(width: 880, height: 720)
  450. }