TitleOptimizerView.swift 21 KB

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