暫無描述

DashboardView.swift 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. final class DashboardView: NSView {
  7. private let contentStack = NSStackView()
  8. private let documentContainer = NSView()
  9. private let sidebar = NSStackView()
  10. private let mainColumn = NSStackView()
  11. private let greetingLabel = NSTextField(labelWithString: "")
  12. private let subtitleLabel = NSTextField(labelWithString: "Find your perfect job with the power of AI.")
  13. private let heroCard = NSView()
  14. private let statGrid = NSGridView(views: [[]])
  15. private let recommendationsStack = NSStackView()
  16. private let insightsStack = NSStackView()
  17. private let scrollView = NSScrollView()
  18. override init(frame frameRect: NSRect) {
  19. super.init(frame: frameRect)
  20. setupLayout()
  21. }
  22. required init?(coder: NSCoder) {
  23. super.init(coder: coder)
  24. setupLayout()
  25. }
  26. override func layout() {
  27. super.layout()
  28. updateDocumentLayout()
  29. }
  30. func render(_ data: DashboardData) {
  31. greetingLabel.stringValue = "Welcome back, \(data.greetingName)! 👋"
  32. configureSidebar(data.sidebarItems)
  33. configureStats(data.stats)
  34. configureRecommendations(data.recommendations)
  35. configureInsights(data.insights)
  36. updateDocumentLayout()
  37. }
  38. private func setupLayout() {
  39. wantsLayer = true
  40. layer?.backgroundColor = NSColor(calibratedRed: 0.03, green: 0.06, blue: 0.14, alpha: 1).cgColor
  41. scrollView.translatesAutoresizingMaskIntoConstraints = false
  42. scrollView.hasVerticalScroller = true
  43. scrollView.drawsBackground = false
  44. addSubview(scrollView)
  45. contentStack.orientation = .horizontal
  46. contentStack.spacing = 20
  47. contentStack.translatesAutoresizingMaskIntoConstraints = false
  48. contentStack.edgeInsets = NSEdgeInsets(top: 20, left: 20, bottom: 24, right: 20)
  49. documentContainer.translatesAutoresizingMaskIntoConstraints = true
  50. documentContainer.autoresizingMask = [.width]
  51. documentContainer.frame = NSRect(x: 0, y: 0, width: 1040, height: 900)
  52. documentContainer.addSubview(contentStack)
  53. scrollView.documentView = documentContainer
  54. sidebar.orientation = .vertical
  55. sidebar.spacing = 10
  56. sidebar.alignment = .leading
  57. sidebar.wantsLayer = true
  58. sidebar.layer?.backgroundColor = NSColor(calibratedRed: 0.06, green: 0.09, blue: 0.2, alpha: 1).cgColor
  59. sidebar.layer?.cornerRadius = 16
  60. sidebar.edgeInsets = NSEdgeInsets(top: 18, left: 14, bottom: 18, right: 14)
  61. sidebar.translatesAutoresizingMaskIntoConstraints = false
  62. mainColumn.orientation = .vertical
  63. mainColumn.spacing = 14
  64. mainColumn.alignment = .leading
  65. mainColumn.translatesAutoresizingMaskIntoConstraints = false
  66. greetingLabel.font = .systemFont(ofSize: 30, weight: .bold)
  67. greetingLabel.textColor = .white
  68. subtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  69. subtitleLabel.textColor = NSColor.white.withAlphaComponent(0.75)
  70. heroCard.wantsLayer = true
  71. heroCard.layer?.backgroundColor = NSColor(calibratedRed: 0.08, green: 0.11, blue: 0.28, alpha: 1).cgColor
  72. heroCard.layer?.cornerRadius = 18
  73. heroCard.translatesAutoresizingMaskIntoConstraints = false
  74. let hero = buildHeroContent()
  75. heroCard.addSubview(hero)
  76. statGrid.translatesAutoresizingMaskIntoConstraints = false
  77. statGrid.rowSpacing = 10
  78. statGrid.columnSpacing = 10
  79. let lowerSection = NSStackView()
  80. lowerSection.orientation = .horizontal
  81. lowerSection.spacing = 12
  82. lowerSection.alignment = .top
  83. lowerSection.translatesAutoresizingMaskIntoConstraints = false
  84. let recommendationsBox = sectionBox(title: "Recommended for You", content: recommendationsStack)
  85. let insightsBox = sectionBox(title: "AI Insights", content: insightsStack)
  86. recommendationsBox.translatesAutoresizingMaskIntoConstraints = false
  87. insightsBox.translatesAutoresizingMaskIntoConstraints = false
  88. lowerSection.addArrangedSubview(recommendationsBox)
  89. lowerSection.addArrangedSubview(insightsBox)
  90. mainColumn.addArrangedSubview(greetingLabel)
  91. mainColumn.addArrangedSubview(subtitleLabel)
  92. mainColumn.addArrangedSubview(heroCard)
  93. mainColumn.addArrangedSubview(statGrid)
  94. mainColumn.addArrangedSubview(lowerSection)
  95. contentStack.addArrangedSubview(sidebar)
  96. contentStack.addArrangedSubview(mainColumn)
  97. NSLayoutConstraint.activate([
  98. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
  99. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
  100. scrollView.topAnchor.constraint(equalTo: topAnchor),
  101. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
  102. contentStack.leadingAnchor.constraint(equalTo: documentContainer.leadingAnchor),
  103. contentStack.trailingAnchor.constraint(equalTo: documentContainer.trailingAnchor),
  104. contentStack.topAnchor.constraint(equalTo: documentContainer.topAnchor),
  105. contentStack.bottomAnchor.constraint(equalTo: documentContainer.bottomAnchor),
  106. contentStack.widthAnchor.constraint(equalTo: documentContainer.widthAnchor),
  107. sidebar.widthAnchor.constraint(equalToConstant: 225),
  108. mainColumn.widthAnchor.constraint(greaterThanOrEqualToConstant: 760),
  109. heroCard.widthAnchor.constraint(equalTo: mainColumn.widthAnchor),
  110. heroCard.heightAnchor.constraint(equalToConstant: 140),
  111. hero.leadingAnchor.constraint(equalTo: heroCard.leadingAnchor, constant: 22),
  112. hero.trailingAnchor.constraint(equalTo: heroCard.trailingAnchor, constant: -22),
  113. hero.topAnchor.constraint(equalTo: heroCard.topAnchor, constant: 18),
  114. hero.bottomAnchor.constraint(equalTo: heroCard.bottomAnchor, constant: -18),
  115. recommendationsBox.widthAnchor.constraint(equalToConstant: 510),
  116. insightsBox.widthAnchor.constraint(equalToConstant: 238)
  117. ])
  118. }
  119. private func buildHeroContent() -> NSView {
  120. let container = NSStackView()
  121. container.orientation = .horizontal
  122. container.translatesAutoresizingMaskIntoConstraints = false
  123. container.distribution = .fillEqually
  124. let left = NSStackView()
  125. left.orientation = .vertical
  126. left.spacing = 8
  127. left.alignment = .leading
  128. let title = NSTextField(labelWithString: "AI Job Search Assistant")
  129. title.font = .systemFont(ofSize: 24, weight: .semibold)
  130. title.textColor = .white
  131. let body = NSTextField(labelWithString: "Let AI find the best jobs for you on Indeed based on your preferences.")
  132. body.font = .systemFont(ofSize: 12, weight: .regular)
  133. body.textColor = NSColor.white.withAlphaComponent(0.72)
  134. let action = NSButton(title: "Find Jobs with AI", target: nil, action: nil)
  135. action.bezelStyle = .rounded
  136. action.wantsLayer = true
  137. action.layer?.backgroundColor = NSColor(calibratedRed: 0.27, green: 0.42, blue: 1.0, alpha: 1).cgColor
  138. action.layer?.cornerRadius = 8
  139. action.contentTintColor = .white
  140. action.font = .systemFont(ofSize: 13, weight: .semibold)
  141. left.addArrangedSubview(title)
  142. left.addArrangedSubview(body)
  143. left.addArrangedSubview(action)
  144. let right = NSStackView()
  145. right.orientation = .vertical
  146. right.alignment = .trailing
  147. right.addArrangedSubview(tagBubble("Quick"))
  148. right.addArrangedSubview(tagBubble("Smart"))
  149. right.addArrangedSubview(tagBubble("Personalized"))
  150. container.addArrangedSubview(left)
  151. container.addArrangedSubview(right)
  152. return container
  153. }
  154. private func configureSidebar(_ items: [SidebarItem]) {
  155. sidebar.arrangedSubviews.forEach {
  156. sidebar.removeArrangedSubview($0)
  157. $0.removeFromSuperview()
  158. }
  159. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  160. brand.font = .systemFont(ofSize: 18, weight: .bold)
  161. brand.textColor = .white
  162. sidebar.addArrangedSubview(brand)
  163. let spacer = NSView()
  164. spacer.translatesAutoresizingMaskIntoConstraints = false
  165. spacer.heightAnchor.constraint(equalToConstant: 8).isActive = true
  166. sidebar.addArrangedSubview(spacer)
  167. items.enumerated().forEach { index, item in
  168. let row = NSStackView()
  169. row.orientation = .horizontal
  170. row.spacing = 8
  171. row.alignment = .centerY
  172. row.wantsLayer = true
  173. row.layer?.cornerRadius = 8
  174. row.edgeInsets = NSEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
  175. if index == 0 {
  176. row.layer?.backgroundColor = NSColor(calibratedRed: 0.22, green: 0.31, blue: 0.85, alpha: 0.4).cgColor
  177. }
  178. let icon = NSImageView()
  179. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  180. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  181. icon.contentTintColor = .white
  182. let text = NSTextField(labelWithString: item.title)
  183. text.font = .systemFont(ofSize: 14, weight: .medium)
  184. text.textColor = .white
  185. row.addArrangedSubview(icon)
  186. row.addArrangedSubview(text)
  187. if let badge = item.badge {
  188. let badgeField = NSTextField(labelWithString: badge)
  189. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  190. badgeField.textColor = .white
  191. badgeField.wantsLayer = true
  192. badgeField.layer?.backgroundColor = NSColor.systemPurple.cgColor
  193. badgeField.layer?.cornerRadius = 8
  194. badgeField.alignment = .center
  195. badgeField.maximumNumberOfLines = 1
  196. badgeField.lineBreakMode = .byClipping
  197. badgeField.translatesAutoresizingMaskIntoConstraints = false
  198. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  199. row.addArrangedSubview(NSView())
  200. row.addArrangedSubview(badgeField)
  201. }
  202. sidebar.addArrangedSubview(row)
  203. }
  204. }
  205. private func configureStats(_ stats: [StatCard]) {
  206. statGrid.removeRow(at: 0)
  207. let cards = stats.map { stat -> NSView in
  208. let card = NSStackView()
  209. card.orientation = .vertical
  210. card.spacing = 6
  211. card.edgeInsets = NSEdgeInsets(top: 14, left: 14, bottom: 14, right: 14)
  212. card.wantsLayer = true
  213. card.layer?.backgroundColor = NSColor(calibratedRed: 0.06, green: 0.11, blue: 0.24, alpha: 1).cgColor
  214. card.layer?.cornerRadius = 14
  215. let value = NSTextField(labelWithString: stat.value)
  216. value.font = .systemFont(ofSize: 30, weight: .bold)
  217. value.textColor = .white
  218. let title = NSTextField(labelWithString: stat.title)
  219. title.font = .systemFont(ofSize: 13, weight: .medium)
  220. title.textColor = NSColor.white.withAlphaComponent(0.74)
  221. let trend = NSTextField(labelWithString: stat.trend)
  222. trend.font = .systemFont(ofSize: 12, weight: .semibold)
  223. trend.textColor = NSColor.systemGreen
  224. card.addArrangedSubview(value)
  225. card.addArrangedSubview(title)
  226. card.addArrangedSubview(trend)
  227. card.translatesAutoresizingMaskIntoConstraints = false
  228. card.widthAnchor.constraint(equalToConstant: 185).isActive = true
  229. card.heightAnchor.constraint(equalToConstant: 120).isActive = true
  230. return card
  231. }
  232. statGrid.addRow(with: cards)
  233. }
  234. private func configureRecommendations(_ recommendations: [JobRecommendation]) {
  235. recommendationsStack.orientation = .vertical
  236. recommendationsStack.spacing = 10
  237. recommendationsStack.alignment = .leading
  238. recommendationsStack.arrangedSubviews.forEach {
  239. recommendationsStack.removeArrangedSubview($0)
  240. $0.removeFromSuperview()
  241. }
  242. recommendations.forEach { recommendation in
  243. let row = NSStackView()
  244. row.orientation = .horizontal
  245. row.spacing = 12
  246. row.alignment = .centerY
  247. let icon = NSView()
  248. icon.wantsLayer = true
  249. icon.layer?.cornerRadius = 10
  250. icon.layer?.backgroundColor = NSColor(calibratedRed: 0.19, green: 0.34, blue: 0.9, alpha: 0.9).cgColor
  251. icon.translatesAutoresizingMaskIntoConstraints = false
  252. icon.widthAnchor.constraint(equalToConstant: 38).isActive = true
  253. icon.heightAnchor.constraint(equalToConstant: 38).isActive = true
  254. let textColumn = NSStackView()
  255. textColumn.orientation = .vertical
  256. textColumn.spacing = 2
  257. textColumn.alignment = .leading
  258. let title = NSTextField(labelWithString: recommendation.title)
  259. title.font = .systemFont(ofSize: 15, weight: .semibold)
  260. title.textColor = .white
  261. let subtitle = NSTextField(labelWithString: "\(recommendation.company) • \(recommendation.location)")
  262. subtitle.font = .systemFont(ofSize: 12, weight: .regular)
  263. subtitle.textColor = NSColor.white.withAlphaComponent(0.7)
  264. textColumn.addArrangedSubview(title)
  265. textColumn.addArrangedSubview(subtitle)
  266. let meta = NSStackView()
  267. meta.orientation = .vertical
  268. meta.alignment = .trailing
  269. let match = NSTextField(labelWithString: recommendation.matchRate)
  270. match.font = .systemFont(ofSize: 12, weight: .semibold)
  271. match.textColor = NSColor.systemGreen
  272. let posted = NSTextField(labelWithString: recommendation.postedAgo)
  273. posted.font = .systemFont(ofSize: 11, weight: .regular)
  274. posted.textColor = NSColor.white.withAlphaComponent(0.6)
  275. meta.addArrangedSubview(match)
  276. meta.addArrangedSubview(posted)
  277. row.addArrangedSubview(icon)
  278. row.addArrangedSubview(textColumn)
  279. row.addArrangedSubview(NSView())
  280. row.addArrangedSubview(meta)
  281. recommendationsStack.addArrangedSubview(row)
  282. }
  283. }
  284. private func configureInsights(_ insights: [InsightItem]) {
  285. insightsStack.orientation = .vertical
  286. insightsStack.spacing = 12
  287. insightsStack.alignment = .leading
  288. insightsStack.arrangedSubviews.forEach {
  289. insightsStack.removeArrangedSubview($0)
  290. $0.removeFromSuperview()
  291. }
  292. insights.forEach { insight in
  293. let card = NSStackView()
  294. card.orientation = .vertical
  295. card.spacing = 4
  296. card.edgeInsets = NSEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
  297. card.wantsLayer = true
  298. card.layer?.backgroundColor = NSColor(calibratedRed: 0.07, green: 0.12, blue: 0.22, alpha: 1).cgColor
  299. card.layer?.cornerRadius = 10
  300. let title = NSTextField(labelWithString: insight.title)
  301. title.font = .systemFont(ofSize: 14, weight: .semibold)
  302. title.textColor = .white
  303. let body = NSTextField(labelWithString: insight.description)
  304. body.font = .systemFont(ofSize: 12, weight: .regular)
  305. body.textColor = NSColor.white.withAlphaComponent(0.7)
  306. body.maximumNumberOfLines = 2
  307. body.lineBreakMode = .byWordWrapping
  308. card.addArrangedSubview(title)
  309. card.addArrangedSubview(body)
  310. insightsStack.addArrangedSubview(card)
  311. }
  312. }
  313. private func sectionBox(title: String, content: NSStackView) -> NSView {
  314. let box = NSStackView()
  315. box.orientation = .vertical
  316. box.spacing = 12
  317. box.alignment = .leading
  318. box.edgeInsets = NSEdgeInsets(top: 14, left: 14, bottom: 14, right: 14)
  319. box.wantsLayer = true
  320. box.layer?.backgroundColor = NSColor(calibratedRed: 0.05, green: 0.09, blue: 0.19, alpha: 1).cgColor
  321. box.layer?.cornerRadius = 16
  322. let titleLabel = NSTextField(labelWithString: title)
  323. titleLabel.font = .systemFont(ofSize: 18, weight: .semibold)
  324. titleLabel.textColor = .white
  325. box.addArrangedSubview(titleLabel)
  326. box.addArrangedSubview(content)
  327. return box
  328. }
  329. private func tagBubble(_ text: String) -> NSView {
  330. let label = NSTextField(labelWithString: text)
  331. label.font = .systemFont(ofSize: 11, weight: .medium)
  332. label.textColor = NSColor(calibratedRed: 0.75, green: 0.79, blue: 1, alpha: 1)
  333. label.wantsLayer = true
  334. label.layer?.backgroundColor = NSColor(calibratedRed: 0.2, green: 0.24, blue: 0.45, alpha: 0.5).cgColor
  335. label.layer?.cornerRadius = 7
  336. label.alignment = .center
  337. label.translatesAutoresizingMaskIntoConstraints = false
  338. label.widthAnchor.constraint(equalToConstant: 90).isActive = true
  339. return label
  340. }
  341. private func updateDocumentLayout() {
  342. documentContainer.layoutSubtreeIfNeeded()
  343. let fittingHeight = max(contentStack.fittingSize.height, bounds.height)
  344. documentContainer.frame = NSRect(x: 0, y: 0, width: bounds.width, height: fittingHeight)
  345. }
  346. }