TitleOptimizerView.swift 21 KB

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