TitleOptimizerView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. VStack(spacing: 0) {
  47. HStack(spacing: 14) {
  48. ModernSidebarIconView(kind: .titleOptimizer, size: 40)
  49. VStack(alignment: .leading, spacing: 2) {
  50. Text("Title Optimizer")
  51. .font(.system(size: 18, weight: .semibold))
  52. .foregroundStyle(AppTheme.textPrimary)
  53. Text(viewModel.usesLiveAI ? "Craft viral Reddit titles with AI" : "Craft viral Reddit titles with AI templates")
  54. .font(.system(size: 11))
  55. .foregroundStyle(AppTheme.textSecondary)
  56. }
  57. Spacer()
  58. headerActions
  59. }
  60. .padding(.horizontal, Layout.horizontalPadding)
  61. .padding(.vertical, 16)
  62. FullWidthDivider()
  63. }
  64. }
  65. @ViewBuilder
  66. private var messageBanners: some View {
  67. if let error = viewModel.errorMessage {
  68. PostGeneratorMessageBanner(message: error, isError: true)
  69. .padding(.horizontal, Layout.horizontalPadding)
  70. .padding(.top, 8)
  71. } else if let success = viewModel.successMessage {
  72. PostGeneratorMessageBanner(message: success, isError: false)
  73. .padding(.horizontal, Layout.horizontalPadding)
  74. .padding(.top, 8)
  75. }
  76. }
  77. private var headerActions: some View {
  78. HStack(spacing: 8) {
  79. Button {
  80. viewModel.resetDraft()
  81. } label: {
  82. Label("Reset", systemImage: "arrow.counterclockwise")
  83. .font(.system(size: 11, weight: .medium))
  84. }
  85. .buttonStyle(AppSecondaryButtonStyle())
  86. Button {
  87. viewModel.copyToClipboard()
  88. } label: {
  89. Label("Copy", systemImage: "doc.on.doc")
  90. .font(.system(size: 11, weight: .medium))
  91. }
  92. .buttonStyle(AppSecondaryButtonStyle())
  93. .disabled(viewModel.activeTitle.trimmingCharacters(in: .whitespaces).isEmpty)
  94. Button {
  95. guard !viewModel.usesLiveAI || requirePremiumAccess() else { return }
  96. Task { await viewModel.optimizeTitles() }
  97. } label: {
  98. HStack(spacing: 6) {
  99. if viewModel.isOptimizing {
  100. ProgressView()
  101. .controlSize(.small)
  102. .tint(.white)
  103. } else {
  104. Image(systemName: "sparkles")
  105. .font(.system(size: 11, weight: .semibold))
  106. }
  107. Text(viewModel.isOptimizing ? "Optimizing…" : "Optimize")
  108. .font(.system(size: 11, weight: .semibold))
  109. }
  110. .foregroundStyle(.white)
  111. .padding(.horizontal, 14)
  112. .padding(.vertical, 8)
  113. .background(viewModel.canOptimize ? AppTheme.accentBlue : AppTheme.accentBlue.opacity(0.4))
  114. .clipShape(RoundedRectangle(cornerRadius: 8))
  115. }
  116. .buttonStyle(AppPrimaryButtonStyle())
  117. .disabled(!viewModel.canOptimize)
  118. }
  119. }
  120. // MARK: - Tab Bar
  121. private var tabBar: some View {
  122. ToolSegmentedTabBar(
  123. selection: $viewModel.selectedTab,
  124. accentColor: AppTheme.accentBlue,
  125. width: 300
  126. )
  127. }
  128. // MARK: - Optimize
  129. private var optimizeContent: some View {
  130. optimizeColumns
  131. }
  132. private var optimizeColumns: some View {
  133. HStack(alignment: .top, spacing: Layout.columnSpacing) {
  134. optimizeColumn(title: "Configuration", icon: "slider.horizontal.3") {
  135. configurationPanel
  136. }
  137. .frame(width: Layout.columnWidth)
  138. .layoutPriority(1)
  139. Rectangle()
  140. .fill(AppTheme.border)
  141. .frame(width: 1)
  142. .padding(.vertical, 2)
  143. optimizeColumn(title: "Title & Suggestions", icon: "textformat") {
  144. editorPanel
  145. }
  146. .frame(minWidth: 0, maxWidth: .infinity)
  147. .layoutPriority(0)
  148. }
  149. }
  150. private func optimizeColumn<Content: View>(
  151. title: String,
  152. icon: String,
  153. @ViewBuilder content: () -> Content
  154. ) -> some View {
  155. VStack(alignment: .leading, spacing: Layout.sectionSpacing) {
  156. sectionHeader(title: title, icon: icon)
  157. content()
  158. }
  159. }
  160. private func sectionHeader(title: String, icon: String) -> some View {
  161. HStack(spacing: 6) {
  162. Image(systemName: icon)
  163. .font(.system(size: 10, weight: .semibold))
  164. .foregroundStyle(AppTheme.accentBlue)
  165. Text(title)
  166. .font(.system(size: 11, weight: .semibold))
  167. .foregroundStyle(AppTheme.textSecondary)
  168. }
  169. .textCase(.uppercase)
  170. .tracking(0.4)
  171. }
  172. private var configurationPanel: some View {
  173. VStack(spacing: Layout.sectionSpacing) {
  174. PostFormCard(title: "Target Subreddit", subtitle: "Tailor titles for the community") {
  175. PostFormField(
  176. label: "Subreddit",
  177. placeholder: "technology",
  178. text: $viewModel.draft.subreddit,
  179. prefix: "r/"
  180. )
  181. }
  182. PostFormCard(title: "AI Settings", subtitle: "Guide topic, tone, and style") {
  183. VStack(alignment: .leading, spacing: 12) {
  184. PostFormField(
  185. label: "Topic / Keywords",
  186. placeholder: "e.g. macOS productivity tips",
  187. text: $viewModel.draft.topic
  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. )
  249. }
  250. characterCountFooter
  251. if let analysis = viewModel.analysis {
  252. TitleAnalysisCard(analysis: analysis)
  253. }
  254. suggestionsSection
  255. .id("title-suggestions")
  256. }
  257. }
  258. @ViewBuilder
  259. private var suggestionsSection: some View {
  260. PostFormCard(
  261. title: "AI Suggestions",
  262. subtitle: viewModel.variants.isEmpty
  263. ? "Hit Optimize to generate title variants"
  264. : "\(viewModel.variants.count) optimized titles ranked by score"
  265. ) {
  266. if viewModel.variants.isEmpty {
  267. TitleOptimizerEmptyState(
  268. icon: "textformat.size",
  269. title: "No suggestions yet",
  270. subtitle: "Fill in your subreddit, topic, and title,\nthen tap Optimize to get AI-powered variants."
  271. )
  272. } else {
  273. VStack(spacing: 8) {
  274. ForEach(viewModel.variants) { variant in
  275. TitleVariantRow(
  276. variant: variant,
  277. isSelected: viewModel.selectedVariantID == variant.id,
  278. onSelect: { viewModel.selectVariant(variant) },
  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.selectVariant(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. }