-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView_with_ImportView.swift
More file actions
773 lines (669 loc) · 27.1 KB
/
ContentView_with_ImportView.swift
File metadata and controls
773 lines (669 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
import SwiftUI
import UniformTypeIdentifiers
struct ContentView: View {
@StateObject private var fileImporter = FileImporter()
@State private var selectedTab = 0
var body: some View {
TabView(selection: $selectedTab) {
ImportView(fileImporter: fileImporter)
.tabItem {
Label("Import", systemImage: "square.and.arrow.down")
}
.tag(0)
AnalysisView(fileImporter: fileImporter)
.tabItem {
Label("Analysis", systemImage: "brain")
}
.tag(1)
.disabled(fileImporter.messages.isEmpty)
InsightsView(fileImporter: fileImporter)
.tabItem {
Label("Insights", systemImage: "chart.line.uptrend.xyaxis")
}
.tag(2)
.disabled(fileImporter.messages.isEmpty)
}
.frame(minWidth: 800, minHeight: 600)
.onChange(of: fileImporter.messages.count) { oldCount, newCount in
// Auto-switch to analysis tab when messages are imported
if oldCount == 0 && newCount > 0 {
withAnimation {
selectedTab = 1
}
}
}
}
}
// MARK: - Analysis View (Placeholder)
struct AnalysisView: View {
@ObservedObject var fileImporter: FileImporter
@State private var analysisProgress: Double = 0
@State private var isAnalyzing = false
var body: some View {
VStack(spacing: 32) {
// Header
VStack(spacing: 8) {
Text("Message Analysis")
.font(.largeTitle)
.fontWeight(.bold)
Text("\(fileImporter.messages.count) messages ready for analysis")
.font(.subheadline)
.foregroundColor(.secondary)
}
.padding(.top, 40)
if !isAnalyzing {
// Pre-analysis view
VStack(spacing: 24) {
Image(systemName: "brain")
.font(.system(size: 64))
.foregroundColor(.accentColor)
.symbolEffect(.pulse)
Text("Ready to analyze your conversations")
.font(.title3)
Text("Analysis will include sentiment detection, topic extraction, and conversation patterns")
.font(.subheadline)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
.frame(maxWidth: 400)
Button(action: startAnalysis) {
Label("Start Analysis", systemImage: "play.fill")
.font(.system(size: 16, weight: .medium))
.foregroundColor(.white)
.padding(.horizontal, 24)
.padding(.vertical, 12)
.background(Color.accentColor)
.cornerRadius(10)
}
// Quick stats
QuickStatsView(fileImporter: fileImporter)
.padding(.top)
}
} else {
// Analysis in progress
VStack(spacing: 24) {
ProgressView(value: analysisProgress) {
Text("Analyzing conversations...")
.font(.headline)
}
.progressViewStyle(.linear)
.frame(maxWidth: 400)
Text("Using Apple Foundation Models for on-device processing")
.font(.caption)
.foregroundColor(.secondary)
}
}
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(NSColor.windowBackgroundColor))
}
private func startAnalysis() {
isAnalyzing = true
// Simulate analysis progress
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
analysisProgress += 0.02
if analysisProgress >= 1.0 {
timer.invalidate()
// Analysis complete
}
}
}
}
// MARK: - Insights View
struct InsightsView: View {
@ObservedObject var fileImporter: FileImporter
var body: some View {
ScrollView {
VStack(spacing: 32) {
// Header
VStack(spacing: 8) {
Text("Conversation Insights")
.font(.largeTitle)
.fontWeight(.bold)
Text("Statistical overview of your messages")
.font(.subheadline)
.foregroundColor(.secondary)
}
.padding(.top, 40)
// Stats Grid
LazyVGrid(columns: [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
], spacing: 20) {
StatCard(
title: "Total Messages",
value: "\(fileImporter.totalMessages)",
icon: "message.fill",
color: .blue
)
StatCard(
title: "Sent by You",
value: "\(fileImporter.sentMessages.count)",
icon: "arrow.up.message",
color: .green
)
StatCard(
title: "Received",
value: "\(fileImporter.receivedMessages.count)",
icon: "arrow.down.message",
color: .orange
)
StatCard(
title: "Participants",
value: "\(fileImporter.uniqueParticipants.count)",
icon: "person.2",
color: .purple
)
let timespan = fileImporter.conversationTimespan
if let start = timespan.start, let end = timespan.end {
let days = Calendar.current.dateComponents([.day], from: start, to: end).day ?? 0
StatCard(
title: "Days Covered",
value: "\(days)",
icon: "calendar",
color: .pink
)
let avgPerDay = fileImporter.totalMessages / max(days, 1)
StatCard(
title: "Avg/Day",
value: "\(avgPerDay)",
icon: "chart.bar",
color: .teal
)
}
}
.padding(.horizontal, 40)
// Participant breakdown
if !fileImporter.uniqueParticipants.isEmpty {
GroupBox {
VStack(alignment: .leading, spacing: 16) {
Label("Participants", systemImage: "person.3")
.font(.headline)
ForEach(Array(fileImporter.messagesByParticipant.keys.sorted()), id: \.self) { participant in
if let messages = fileImporter.messagesByParticipant[participant] {
HStack {
Image(systemName: participant == "Me" ? "person.fill" : "person")
.foregroundColor(participant == "Me" ? .accentColor : .secondary)
Text(participant)
.fontWeight(participant == "Me" ? .medium : .regular)
Spacer()
Text("\(messages.count)")
.foregroundColor(.secondary)
// Simple bar visualization
GeometryReader { geometry in
Rectangle()
.fill(participant == "Me" ? Color.accentColor : Color.secondary)
.opacity(0.3)
.frame(width: geometry.size.width * (Double(messages.count) / Double(fileImporter.totalMessages)))
}
.frame(width: 100, height: 20)
.background(Color.secondary.opacity(0.1))
.cornerRadius(4)
}
}
}
}
.padding()
}
.frame(maxWidth: 600)
}
Spacer(minLength: 40)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(NSColor.windowBackgroundColor))
}
}
// MARK: - Supporting Views
struct QuickStatsView: View {
@ObservedObject var fileImporter: FileImporter
var body: some View {
HStack(spacing: 32) {
QuickStat(label: "Messages", value: "\(fileImporter.totalMessages)")
QuickStat(label: "Participants", value: "\(fileImporter.uniqueParticipants.count)")
let timespan = fileImporter.conversationTimespan
if let start = timespan.start {
let formatter = DateFormatter()
formatter.dateStyle = .medium
QuickStat(label: "Since", value: formatter.string(from: start))
}
}
.padding()
.background(Color.secondary.opacity(0.1))
.cornerRadius(10)
}
}
struct QuickStat: View {
let label: String
let value: String
var body: some View {
VStack(spacing: 4) {
Text(value)
.font(.title3)
.fontWeight(.semibold)
Text(label)
.font(.caption)
.foregroundColor(.secondary)
}
}
}
struct StatCard: View {
let title: String
let value: String
let icon: String
let color: Color
var body: some View {
VStack(spacing: 12) {
Image(systemName: icon)
.font(.title2)
.foregroundColor(color)
Text(value)
.font(.title)
.fontWeight(.bold)
Text(title)
.font(.caption)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 12)
.fill(color.opacity(0.1))
)
.overlay(
RoundedRectangle(cornerRadius: 12)
.strokeBorder(color.opacity(0.2), lineWidth: 1)
)
}
}
// MARK: - IMPORT VIEW CODE (Temporary - move to separate file later)
// MARK: - Main Import View
struct ImportView: View {
@ObservedObject var fileImporter: FileImporter
@State private var currentStep: ImportStep = .export
@State private var hasExported = false
@State private var isDragging = false
enum ImportStep {
case export
case waitingForFile
case importing
case complete
}
var body: some View {
VStack(spacing: 0) {
// Clean header
VStack(spacing: 8) {
Text("Import Your Messages")
.font(.largeTitle)
.fontWeight(.bold)
Text("A simple two-step process to analyze your conversations")
.font(.subheadline)
.foregroundColor(.secondary)
}
.padding(.top, 40)
.padding(.bottom, 32)
// Main content area
ZStack {
// Step 1: Export Instructions
if currentStep == .export {
ExportStepView(
onExportStarted: {
withAnimation(.easeInOut(duration: 0.3)) {
hasExported = true
currentStep = .waitingForFile
}
}
)
.transition(.asymmetric(
insertion: .move(edge: .leading).combined(with: .opacity),
removal: .move(edge: .leading).combined(with: .opacity)
))
}
// Step 2: Drop Zone
if currentStep == .waitingForFile {
FileDropView(
fileImporter: fileImporter,
isDragging: $isDragging,
onFileImported: {
withAnimation(.easeInOut(duration: 0.3)) {
currentStep = .complete
}
}
)
.transition(.asymmetric(
insertion: .move(edge: .trailing).combined(with: .opacity),
removal: .move(edge: .leading).combined(with: .opacity)
))
}
// Step 3: Success
if currentStep == .complete {
ImportSuccessView(
messageCount: fileImporter.messages.count,
onAnalyze: {
// Navigate to analysis tab
},
onImportAnother: {
fileImporter.clearMessages()
withAnimation {
currentStep = .export
hasExported = false
}
}
)
.transition(.asymmetric(
insertion: .scale.combined(with: .opacity),
removal: .opacity
))
}
}
.frame(maxWidth: 600, maxHeight: 400)
.animation(.easeInOut(duration: 0.3), value: currentStep)
// Progress indicator
ProgressIndicatorView(
currentStep: currentStep,
hasExported: hasExported
)
.padding(.top, 40)
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(NSColor.windowBackgroundColor))
}
}
// MARK: - Export Step
struct ExportStepView: View {
let onExportStarted: () -> Void
@State private var commandCopied = false
private let exportCommand = """
sqlite3 -header -csv ~/Library/Messages/chat.db "SELECT datetime(message.date/1000000000 + strftime('%s', '2001-01-01'), 'unixepoch', 'localtime') as timestamp, message.text, CASE WHEN message.is_from_me = 1 THEN 'Me' ELSE COALESCE(handle.id, 'Unknown') END as sender, CASE WHEN message.is_from_me = 1 THEN 'true' ELSE 'false' END as isFromMe, 'direct' as chatIdentifier FROM message LEFT JOIN handle ON message.handle_id = handle.ROWID WHERE message.text IS NOT NULL AND message.text != '' ORDER BY message.date LIMIT 50000;" > ~/Downloads/messages.csv
"""
var body: some View {
VStack(spacing: 24) {
// Icon
Image(systemName: "terminal")
.font(.system(size: 48))
.foregroundColor(.accentColor)
.symbolEffect(.pulse)
// Title
Text("Step 1: Export Your Messages")
.font(.title2)
.fontWeight(.semibold)
// Instructions
VStack(alignment: .leading, spacing: 16) {
InstructionRow(number: "1", text: "Open Terminal", subtext: "Press ⌘+Space and type 'Terminal'")
InstructionRow(number: "2", text: "Copy the export command", subtext: "Click the button below")
InstructionRow(number: "3", text: "Paste and run", subtext: "Press ⌘+V then Enter in Terminal")
InstructionRow(number: "4", text: "Wait for completion", subtext: "Usually takes 5-30 seconds")
}
.frame(maxWidth: 400)
// Copy button
Button(action: copyCommand) {
HStack {
Image(systemName: commandCopied ? "checkmark.circle.fill" : "doc.on.clipboard")
Text(commandCopied ? "Command Copied!" : "Copy Export Command")
}
.font(.system(size: 16, weight: .medium))
.foregroundColor(.white)
.padding(.horizontal, 24)
.padding(.vertical, 12)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(commandCopied ? Color.green : Color.accentColor)
)
}
.scaleEffect(commandCopied ? 1.05 : 1.0)
.animation(.spring(response: 0.3, dampingFraction: 0.6), value: commandCopied)
// Continue button
Button(action: onExportStarted) {
Text("I've run the command →")
.font(.subheadline)
.foregroundColor(.accentColor)
}
.buttonStyle(.plain)
.padding(.top, 8)
}
}
private func copyCommand() {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(exportCommand, forType: .string)
withAnimation {
commandCopied = true
}
// Reset after delay
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
withAnimation {
commandCopied = false
}
}
// Open Terminal
NSWorkspace.shared.launchApplication("Terminal")
}
}
// MARK: - File Drop View
struct FileDropView: View {
@ObservedObject var fileImporter: FileImporter
@Binding var isDragging: Bool
let onFileImported: () -> Void
var body: some View {
VStack(spacing: 24) {
// Icon
Image(systemName: isDragging ? "doc.badge.plus" : "arrow.down.doc")
.font(.system(size: 48))
.foregroundColor(isDragging ? .accentColor : .secondary)
.scaleEffect(isDragging ? 1.2 : 1.0)
.animation(.spring(response: 0.3), value: isDragging)
// Title
Text("Step 2: Import Your Data")
.font(.title2)
.fontWeight(.semibold)
// Drop zone
RoundedRectangle(cornerRadius: 16)
.strokeBorder(
isDragging ? Color.accentColor : Color.secondary.opacity(0.3),
style: StrokeStyle(lineWidth: 2, dash: isDragging ? [] : [8])
)
.background(
RoundedRectangle(cornerRadius: 16)
.fill(isDragging ? Color.accentColor.opacity(0.1) : Color.clear)
)
.frame(width: 400, height: 150)
.overlay(
VStack(spacing: 8) {
Text(isDragging ? "Drop your file here" : "Drag messages.csv here")
.font(.headline)
.foregroundColor(isDragging ? .accentColor : .secondary)
if !isDragging {
Text("or")
.font(.caption)
.foregroundColor(.secondary)
Button("Browse for file") {
selectFile()
}
.buttonStyle(.link)
}
}
)
.onDrop(of: [.fileURL], isTargeted: $isDragging) { providers in
handleDrop(providers: providers)
}
.animation(.easeInOut(duration: 0.2), value: isDragging)
// Status
if case .loading = fileImporter.importStatus {
HStack {
ProgressView()
.scaleEffect(0.8)
Text("Importing messages...")
.font(.subheadline)
.foregroundColor(.secondary)
}
} else if case .error(let message) = fileImporter.importStatus {
Label(message, systemImage: "exclamationmark.triangle")
.font(.caption)
.foregroundColor(.red)
}
// Help text
Text("The file should be in your Downloads folder")
.font(.caption)
.foregroundColor(.secondary)
}
.onChange(of: fileImporter.importStatus) { oldStatus, newStatus in
if case .success = newStatus {
onFileImported()
}
}
}
private func selectFile() {
let panel = NSOpenPanel()
panel.title = "Select messages.csv"
panel.directoryURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first
panel.allowedContentTypes = [.commaSeparatedText]
panel.allowsMultipleSelection = false
if panel.runModal() == .OK, let url = panel.url {
importFile(at: url)
}
}
private func handleDrop(providers: [NSItemProvider]) -> Bool {
guard let provider = providers.first else { return false }
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, error in
guard let data = item as? Data,
let url = URL(dataRepresentation: data, relativeTo: nil) else { return }
DispatchQueue.main.async {
importFile(at: url)
}
}
return true
}
private func importFile(at url: URL) {
fileImporter.importMessages(from: url)
}
}
// MARK: - Success View
struct ImportSuccessView: View {
let messageCount: Int
let onAnalyze: () -> Void
let onImportAnother: () -> Void
var body: some View {
VStack(spacing: 24) {
// Success icon
Image(systemName: "checkmark.circle.fill")
.font(.system(size: 64))
.foregroundColor(.green)
.symbolEffect(.bounce)
// Title
Text("Import Complete!")
.font(.title)
.fontWeight(.bold)
// Stats
Text("\(messageCount) messages imported successfully")
.font(.headline)
.foregroundColor(.secondary)
// Action buttons
HStack(spacing: 16) {
Button("Import Another") {
onImportAnother()
}
.buttonStyle(.bordered)
Button("Analyze Messages") {
onAnalyze()
}
.buttonStyle(.borderedProminent)
.controlSize(.large)
}
}
}
}
// MARK: - Supporting Views
struct InstructionRow: View {
let number: String
let text: String
let subtext: String
var body: some View {
HStack(alignment: .top, spacing: 12) {
ZStack {
Circle()
.fill(Color.accentColor.opacity(0.1))
.frame(width: 28, height: 28)
Text(number)
.font(.system(size: 14, weight: .semibold))
.foregroundColor(.accentColor)
}
VStack(alignment: .leading, spacing: 2) {
Text(text)
.font(.system(size: 15, weight: .medium))
Text(subtext)
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
}
}
}
struct ProgressIndicatorView: View {
let currentStep: ImportView.ImportStep
let hasExported: Bool
var body: some View {
HStack(spacing: 32) {
ProgressDot(
isActive: currentStep == .export,
isCompleted: hasExported,
label: "Export"
)
ProgressConnector(isActive: hasExported)
ProgressDot(
isActive: currentStep == .waitingForFile || currentStep == .importing,
isCompleted: currentStep == .complete,
label: "Import"
)
ProgressConnector(isActive: currentStep == .complete)
ProgressDot(
isActive: currentStep == .complete,
isCompleted: false,
label: "Analyze"
)
}
}
}
struct ProgressDot: View {
let isActive: Bool
let isCompleted: Bool
let label: String
var body: some View {
VStack(spacing: 8) {
ZStack {
Circle()
.fill(dotColor)
.frame(width: 24, height: 24)
if isCompleted {
Image(systemName: "checkmark")
.font(.system(size: 12, weight: .bold))
.foregroundColor(.white)
}
}
Text(label)
.font(.caption)
.foregroundColor(isActive || isCompleted ? .primary : .secondary)
}
}
var dotColor: Color {
if isCompleted {
return .green
} else if isActive {
return .accentColor
} else {
return Color.secondary.opacity(0.3)
}
}
}
struct ProgressConnector: View {
let isActive: Bool
var body: some View {
Rectangle()
.fill(isActive ? Color.green : Color.secondary.opacity(0.3))
.frame(width: 40, height: 2)
}
}
#Preview {
ContentView()
}