TitleOptimizerView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 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. onApply: { viewModel.applyVariant(variant) }
  279. )
  280. }
  281. }
  282. }
  283. }
  284. }
  285. private var characterCountFooter: some View {
  286. HStack(spacing: 12) {
  287. Text("\(viewModel.draft.originalTitle.count) / 300")
  288. .font(.system(size: 10, weight: .medium, design: .monospaced))
  289. .foregroundStyle(
  290. viewModel.draft.originalTitle.count > 300 ? Color(hex: 0xF87171) : AppTheme.textTertiary
  291. )
  292. Spacer()
  293. Text("Reddit limit: 300 characters for title")
  294. .font(.system(size: 10))
  295. .foregroundStyle(AppTheme.textTertiary)
  296. }
  297. .padding(.horizontal, 12)
  298. .padding(.vertical, 9)
  299. .background(
  300. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  301. .fill(AppTheme.cardBackground)
  302. .overlay(
  303. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  304. .stroke(AppTheme.cardBorder, lineWidth: 1)
  305. )
  306. )
  307. }
  308. // MARK: - Compare
  309. private var compareContent: some View {
  310. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  311. compareHeader
  312. if viewModel.variants.isEmpty {
  313. TitleOptimizerEmptyState(
  314. icon: "arrow.left.arrow.right",
  315. title: "Nothing to compare yet",
  316. subtitle: "Generate title suggestions on the Optimize tab first."
  317. )
  318. .frame(maxWidth: .infinity)
  319. } else {
  320. compareBody
  321. }
  322. }
  323. }
  324. private var compareBody: some View {
  325. VStack(spacing: Layout.sectionSpacing) {
  326. TitleCompareCard(
  327. title: viewModel.draft.originalTitle,
  328. score: viewModel.analysis?.overallScore,
  329. label: "Original",
  330. isOriginal: true,
  331. isSelected: viewModel.selectedVariantID == nil
  332. )
  333. HStack {
  334. Rectangle()
  335. .fill(AppTheme.border)
  336. .frame(height: 1)
  337. Text("vs")
  338. .font(.system(size: 10, weight: .semibold))
  339. .foregroundStyle(AppTheme.textTertiary)
  340. .padding(.horizontal, 8)
  341. Rectangle()
  342. .fill(AppTheme.border)
  343. .frame(height: 1)
  344. }
  345. VStack(spacing: 8) {
  346. ForEach(viewModel.variants) { variant in
  347. TitleCompareCard(
  348. title: variant.title,
  349. score: variant.score,
  350. label: "Variant #\(variantRank(variant))",
  351. isOriginal: false,
  352. isSelected: viewModel.selectedVariantID == variant.id
  353. )
  354. .onTapGesture {
  355. viewModel.applyVariant(variant)
  356. }
  357. }
  358. }
  359. compareMetadata
  360. }
  361. .frame(maxWidth: 640)
  362. .frame(maxWidth: .infinity)
  363. }
  364. private var compareHeader: some View {
  365. HStack(alignment: .top) {
  366. sectionHeader(title: "Side-by-Side Comparison", icon: "arrow.left.arrow.right")
  367. Spacer()
  368. if let best = viewModel.bestVariant {
  369. HStack(spacing: 6) {
  370. Image(systemName: "trophy.fill")
  371. .font(.system(size: 10))
  372. .foregroundStyle(AppTheme.accentYellow)
  373. Text("Best: \(best.score)/100")
  374. .font(.system(size: 10, weight: .semibold))
  375. .foregroundStyle(AppTheme.textSecondary)
  376. }
  377. .padding(.top, 1)
  378. }
  379. }
  380. }
  381. private var compareMetadata: some View {
  382. VStack(alignment: .leading, spacing: 0) {
  383. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  384. metadataDivider
  385. metadataRow(label: "Goal", value: viewModel.draft.titleGoal.title)
  386. metadataDivider
  387. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  388. metadataDivider
  389. metadataRow(label: "Engine", value: viewModel.usesLiveAI ? "AI" : "Local templates")
  390. metadataDivider
  391. metadataRow(label: "Active", value: viewModel.activeTitle)
  392. }
  393. .padding(14)
  394. .frame(maxWidth: .infinity, alignment: .leading)
  395. .background(
  396. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  397. .fill(AppTheme.cardBackground)
  398. .overlay(
  399. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  400. .stroke(AppTheme.cardBorder, lineWidth: 1)
  401. )
  402. )
  403. }
  404. private func variantRank(_ variant: TitleVariant) -> Int {
  405. (viewModel.variants.firstIndex(where: { $0.id == variant.id }) ?? 0) + 1
  406. }
  407. // MARK: - Preview
  408. private var previewContent: some View {
  409. VStack(alignment: .leading, spacing: Layout.columnSpacing) {
  410. sectionHeader(title: "Live Preview", icon: "eye")
  411. HStack {
  412. Spacer(minLength: 0)
  413. VStack(spacing: Layout.sectionSpacing) {
  414. RedditPostPreviewCard(
  415. draft: viewModel.previewDraft,
  416. formattedSubreddit: viewModel.formattedSubreddit
  417. )
  418. PostFormCard(title: "Feed Preview", subtitle: "How your title looks in a crowded feed") {
  419. VStack(spacing: 6) {
  420. TitleFeedPreviewRow(
  421. title: "Other trending post in this subreddit right now",
  422. subreddit: viewModel.formattedSubreddit
  423. )
  424. TitleFeedPreviewRow(
  425. title: viewModel.activeTitle,
  426. subreddit: viewModel.formattedSubreddit,
  427. isHighlighted: true
  428. )
  429. TitleFeedPreviewRow(
  430. title: "Another popular discussion happening nearby",
  431. subreddit: viewModel.formattedSubreddit
  432. )
  433. }
  434. }
  435. previewMetadata
  436. }
  437. .frame(maxWidth: 560)
  438. Spacer(minLength: 0)
  439. }
  440. }
  441. }
  442. private var previewMetadata: some View {
  443. VStack(alignment: .leading, spacing: 0) {
  444. metadataRow(label: "Active Title", value: viewModel.activeTitle)
  445. metadataDivider
  446. metadataRow(label: "Subreddit", value: viewModel.formattedSubreddit)
  447. metadataDivider
  448. metadataRow(label: "Goal", value: viewModel.draft.titleGoal.title)
  449. metadataDivider
  450. metadataRow(label: "Tone", value: viewModel.draft.tone.title)
  451. metadataDivider
  452. metadataRow(label: "Type", value: viewModel.draft.postType.title)
  453. metadataDivider
  454. metadataRow(label: "Engine", value: viewModel.usesLiveAI ? "AI" : "Local templates")
  455. if let score = viewModel.analysis?.overallScore {
  456. metadataDivider
  457. metadataRow(label: "Score", value: "\(score)/100")
  458. }
  459. if viewModel.draft.isNSFW || viewModel.draft.isSpoiler || viewModel.draft.isOC {
  460. metadataDivider
  461. metadataRow(
  462. label: "Tags",
  463. value: [
  464. viewModel.draft.isNSFW ? "NSFW" : nil,
  465. viewModel.draft.isSpoiler ? "Spoiler" : nil,
  466. viewModel.draft.isOC ? "OC" : nil,
  467. ]
  468. .compactMap { $0 }
  469. .joined(separator: ", ")
  470. )
  471. }
  472. }
  473. .padding(14)
  474. .frame(maxWidth: .infinity, alignment: .leading)
  475. .background(
  476. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  477. .fill(AppTheme.cardBackground)
  478. .overlay(
  479. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  480. .stroke(AppTheme.cardBorder, lineWidth: 1)
  481. )
  482. )
  483. }
  484. private var metadataDivider: some View {
  485. Rectangle()
  486. .fill(AppTheme.border)
  487. .frame(height: 1)
  488. .padding(.vertical, 6)
  489. }
  490. private func metadataRow(label: String, value: String) -> some View {
  491. HStack(alignment: .top, spacing: 12) {
  492. Text(label)
  493. .font(.system(size: 10, weight: .medium))
  494. .foregroundStyle(AppTheme.textTertiary)
  495. .frame(width: 72, alignment: .leading)
  496. Text(value)
  497. .font(.system(size: 10))
  498. .foregroundStyle(AppTheme.textSecondary)
  499. .fixedSize(horizontal: false, vertical: true)
  500. }
  501. }
  502. }
  503. private struct TitleSecondaryButtonStyle: ButtonStyle {
  504. func makeBody(configuration: Configuration) -> some View {
  505. AppSecondaryButtonStyle().makeBody(configuration: configuration)
  506. }
  507. }
  508. #Preview {
  509. TitleOptimizerView(viewModel: TitleOptimizerViewModel())
  510. .frame(width: 880, height: 720)
  511. }