TitleOptimizerView.swift 18 KB

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