TitleOptimizerView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. isRequired: true
  179. )
  180. }
  181. PostFormCard(title: "AI Settings", subtitle: "Guide topic, tone, and style") {
  182. VStack(alignment: .leading, spacing: 12) {
  183. PostFormField(
  184. label: "Topic / Keywords",
  185. placeholder: "e.g. macOS productivity tips",
  186. text: $viewModel.draft.topic,
  187. isRequired: true
  188. )
  189. VStack(alignment: .leading, spacing: 6) {
  190. Text("Tone")
  191. .font(.system(size: 10, weight: .medium))
  192. .foregroundStyle(AppTheme.textTertiary)
  193. TonePicker(selectedTone: $viewModel.draft.tone)
  194. }
  195. }
  196. }
  197. PostFormCard(title: "Title Goal", subtitle: "What kind of hook do you want?") {
  198. TitleGoalPicker(selectedGoal: $viewModel.draft.titleGoal)
  199. }
  200. PostFormCard(title: "Preview Context", subtitle: "Optional — affects preview style") {
  201. VStack(alignment: .leading, spacing: 12) {
  202. PostTypePicker(selectedType: Binding(
  203. get: { viewModel.draft.postType },
  204. set: { viewModel.selectPostType($0) }
  205. ))
  206. HStack(spacing: 8) {
  207. PostTagToggle(
  208. title: "NSFW",
  209. systemImage: "exclamationmark.triangle.fill",
  210. activeColor: Color(hex: 0xEF4444),
  211. isOn: $viewModel.draft.isNSFW
  212. )
  213. .frame(maxWidth: .infinity)
  214. PostTagToggle(
  215. title: "Spoiler",
  216. systemImage: "eye.slash.fill",
  217. activeColor: AppTheme.textSecondary,
  218. isOn: $viewModel.draft.isSpoiler
  219. )
  220. .frame(maxWidth: .infinity)
  221. PostTagToggle(
  222. title: "OC",
  223. systemImage: "star.fill",
  224. activeColor: AppTheme.accentGreen,
  225. isOn: $viewModel.draft.isOC
  226. )
  227. .frame(maxWidth: .infinity)
  228. }
  229. PostFormField(
  230. label: "Flair",
  231. placeholder: "Discussion, Question, Meme…",
  232. text: $viewModel.draft.flair
  233. )
  234. }
  235. }
  236. PostFormCard(title: "Suggestions", subtitle: "How many variants to generate") {
  237. TitleVariantCountPicker(selectedCount: $viewModel.draft.variantCount)
  238. }
  239. }
  240. }
  241. private var editorPanel: some View {
  242. VStack(spacing: Layout.sectionSpacing) {
  243. PostFormCard(title: "Original Title", subtitle: "The title you want to improve") {
  244. PostFormField(
  245. label: "Post Title",
  246. placeholder: "Enter your draft title here",
  247. text: $viewModel.draft.originalTitle,
  248. isRequired: true
  249. )
  250. }
  251. characterCountFooter
  252. if let analysis = viewModel.analysis {
  253. TitleAnalysisCard(analysis: analysis)
  254. }
  255. suggestionsSection
  256. .id("title-suggestions")
  257. }
  258. }
  259. @ViewBuilder
  260. private var suggestionsSection: some View {
  261. PostFormCard(
  262. title: "AI Suggestions",
  263. subtitle: viewModel.variants.isEmpty
  264. ? "Hit Optimize to generate title variants"
  265. : "\(viewModel.variants.count) optimized titles ranked by score"
  266. ) {
  267. if viewModel.variants.isEmpty {
  268. TitleOptimizerEmptyState(
  269. icon: "textformat.size",
  270. title: "No suggestions yet",
  271. subtitle: "Fill in your subreddit, topic, and title,\nthen tap Optimize to get AI-powered variants."
  272. )
  273. } else {
  274. VStack(spacing: 8) {
  275. ForEach(viewModel.variants) { variant in
  276. TitleVariantRow(
  277. variant: variant,
  278. isSelected: viewModel.selectedVariantID == variant.id,
  279. onApply: { viewModel.applyVariant(variant) }
  280. )
  281. }
  282. }
  283. }
  284. }
  285. }
  286. private var characterCountFooter: some View {
  287. HStack(spacing: 12) {
  288. Text("\(viewModel.draft.originalTitle.count) / 300")
  289. .font(.system(size: 10, weight: .medium, design: .monospaced))
  290. .foregroundStyle(
  291. viewModel.draft.originalTitle.count > 300 ? Color(hex: 0xF87171) : AppTheme.textTertiary
  292. )
  293. Spacer()
  294. Text("Reddit limit: 300 characters for title")
  295. .font(.system(size: 10))
  296. .foregroundStyle(AppTheme.textTertiary)
  297. }
  298. .padding(.horizontal, 12)
  299. .padding(.vertical, 9)
  300. .background(
  301. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  302. .fill(AppTheme.cardBackground)
  303. .overlay(
  304. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  305. .stroke(AppTheme.cardBorder, lineWidth: 1)
  306. )
  307. )
  308. }
  309. // MARK: - Compare
  310. private var compareContent: some View {
  311. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  312. compareHeader
  313. if viewModel.variants.isEmpty {
  314. TitleOptimizerEmptyState(
  315. icon: "arrow.left.arrow.right",
  316. title: "Nothing to compare yet",
  317. subtitle: "Generate title suggestions on the Optimize tab first."
  318. )
  319. .frame(maxWidth: .infinity)
  320. } else {
  321. compareBody
  322. }
  323. }
  324. }
  325. private var compareBody: some View {
  326. VStack(spacing: Layout.sectionSpacing) {
  327. TitleCompareCard(
  328. title: viewModel.draft.originalTitle,
  329. score: viewModel.analysis?.overallScore,
  330. label: "Original",
  331. isOriginal: true,
  332. isSelected: viewModel.selectedVariantID == nil
  333. )
  334. HStack {
  335. Rectangle()
  336. .fill(AppTheme.border)
  337. .frame(height: 1)
  338. Text("vs")
  339. .font(.system(size: 10, weight: .semibold))
  340. .foregroundStyle(AppTheme.textTertiary)
  341. .padding(.horizontal, 8)
  342. Rectangle()
  343. .fill(AppTheme.border)
  344. .frame(height: 1)
  345. }
  346. VStack(spacing: 8) {
  347. ForEach(viewModel.variants) { variant in
  348. TitleCompareCard(
  349. title: variant.title,
  350. score: variant.score,
  351. label: "Variant #\(variantRank(variant))",
  352. isOriginal: false,
  353. isSelected: viewModel.selectedVariantID == variant.id
  354. )
  355. .onTapGesture {
  356. viewModel.applyVariant(variant)
  357. }
  358. }
  359. }
  360. compareMetadata
  361. }
  362. .frame(maxWidth: 640)
  363. .frame(maxWidth: .infinity)
  364. }
  365. private var compareHeader: some View {
  366. HStack(alignment: .top) {
  367. sectionHeader(title: "Side-by-Side Comparison", icon: "arrow.left.arrow.right")
  368. Spacer()
  369. if let best = viewModel.bestVariant {
  370. HStack(spacing: 6) {
  371. Image(systemName: "trophy.fill")
  372. .font(.system(size: 10))
  373. .foregroundStyle(AppTheme.accentYellow)
  374. Text("Best: \(best.score)/100")
  375. .font(.system(size: 10, weight: .semibold))
  376. .foregroundStyle(AppTheme.textSecondary)
  377. }
  378. .padding(.top, 1)
  379. }
  380. }
  381. }
  382. private var compareMetadata: some View {
  383. VStack(alignment: .leading, spacing: 0) {
  384. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  385. metadataDivider
  386. metadataRow(label: "Goal", value: viewModel.draft.titleGoal.title)
  387. metadataDivider
  388. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  389. metadataDivider
  390. metadataRow(label: "Engine", value: viewModel.usesLiveAI ? "AI" : "Local templates")
  391. metadataDivider
  392. metadataRow(label: "Active", value: viewModel.activeTitle)
  393. }
  394. .padding(14)
  395. .frame(maxWidth: .infinity, alignment: .leading)
  396. .background(
  397. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  398. .fill(AppTheme.cardBackground)
  399. .overlay(
  400. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  401. .stroke(AppTheme.cardBorder, lineWidth: 1)
  402. )
  403. )
  404. }
  405. private func variantRank(_ variant: TitleVariant) -> Int {
  406. (viewModel.variants.firstIndex(where: { $0.id == variant.id }) ?? 0) + 1
  407. }
  408. // MARK: - Preview
  409. private var previewContent: some View {
  410. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  411. sectionHeader(title: "Live Preview", icon: "eye")
  412. HStack {
  413. Spacer(minLength: 0)
  414. VStack(spacing: Layout.sectionSpacing) {
  415. RedditPostPreviewCard(
  416. draft: viewModel.previewDraft,
  417. formattedSubreddit: viewModel.formattedSubreddit
  418. )
  419. PostFormCard(title: "Feed Preview", subtitle: "How your title looks in a crowded feed") {
  420. VStack(spacing: 6) {
  421. TitleFeedPreviewRow(
  422. title: "Other trending post in this subreddit right now",
  423. subreddit: viewModel.formattedSubreddit
  424. )
  425. TitleFeedPreviewRow(
  426. title: viewModel.activeTitle,
  427. subreddit: viewModel.formattedSubreddit,
  428. isHighlighted: true
  429. )
  430. TitleFeedPreviewRow(
  431. title: "Another popular discussion happening nearby",
  432. subreddit: viewModel.formattedSubreddit
  433. )
  434. }
  435. }
  436. previewMetadata
  437. }
  438. .frame(maxWidth: 560)
  439. Spacer(minLength: 0)
  440. }
  441. }
  442. }
  443. private var previewMetadata: some View {
  444. VStack(alignment: .leading, spacing: 0) {
  445. metadataRow(label: "Active Title", value: viewModel.activeTitle)
  446. metadataDivider
  447. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  448. metadataDivider
  449. metadataRow(label: "Goal", value: viewModel.draft.titleGoal.title)
  450. metadataDivider
  451. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  452. metadataDivider
  453. metadataRow(label: "Type", value: viewModel.draft.postType.title)
  454. metadataDivider
  455. metadataRow(label: "Engine", value: viewModel.usesLiveAI ? "AI" : "Local templates")
  456. if let score = viewModel.analysis?.overallScore {
  457. metadataDivider
  458. metadataRow(label: "Score", value: "\(score)/100")
  459. }
  460. if viewModel.draft.isNSFW || viewModel.draft.isSpoiler || viewModel.draft.isOC {
  461. metadataDivider
  462. metadataRow(
  463. label: "Tags",
  464. value: [
  465. viewModel.draft.isNSFW ? "NSFW" : nil,
  466. viewModel.draft.isSpoiler ? "Spoiler" : nil,
  467. viewModel.draft.isOC ? "OC" : nil,
  468. ]
  469. .compactMap { $0 }
  470. .joined(separator: ", ")
  471. )
  472. }
  473. }
  474. .padding(14)
  475. .frame(maxWidth: .infinity, alignment: .leading)
  476. .background(
  477. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  478. .fill(AppTheme.cardBackground)
  479. .overlay(
  480. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  481. .stroke(AppTheme.cardBorder, lineWidth: 1)
  482. )
  483. )
  484. }
  485. private var metadataDivider: some View {
  486. Rectangle()
  487. .fill(AppTheme.border)
  488. .frame(height: 1)
  489. .padding(.vertical, 6)
  490. }
  491. private func metadataRow(label: String, value: String) -> some View {
  492. HStack(alignment: .top, spacing: 12) {
  493. Text(label)
  494. .font(.system(size: 10, weight: .medium))
  495. .foregroundStyle(AppTheme.textTertiary)
  496. .frame(width: 72, alignment: .leading)
  497. Text(value)
  498. .font(.system(size: 10))
  499. .foregroundStyle(AppTheme.textSecondary)
  500. .fixedSize(horizontal: false, vertical: true)
  501. }
  502. }
  503. }
  504. private struct TitleSecondaryButtonStyle: ButtonStyle {
  505. func makeBody(configuration: Configuration) -> some View {
  506. AppSecondaryButtonStyle().makeBody(configuration: configuration)
  507. }
  508. }
  509. #Preview {
  510. TitleOptimizerView(viewModel: TitleOptimizerViewModel())
  511. .frame(width: 880, height: 720)
  512. }