Skip to content

Commit 9e4f6c3

Browse files
sinppets for code
1 parent 15f2ea1 commit 9e4f6c3

File tree

1 file changed

+361
-0
lines changed

1 file changed

+361
-0
lines changed
Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
//
2+
// CodeSnippetsView.swift
3+
// GitDiffExample
4+
//
5+
// Created by Tornike Gomareli on 18.06.25.
6+
//
7+
8+
import SwiftUI
9+
import gitdiff
10+
11+
struct CodeSnippetsView: View {
12+
@State private var selectedCategory = "Basic"
13+
@State private var copiedSnippet: String?
14+
15+
let categories = ["Basic", "Themes", "Configuration", "Advanced"]
16+
17+
var snippets: [CodeSnippet] {
18+
CodeSnippet.allSnippets.filter { $0.category == selectedCategory }
19+
}
20+
21+
var body: some View {
22+
NavigationView {
23+
VStack(spacing: 0) {
24+
Picker("Category", selection: $selectedCategory) {
25+
ForEach(categories, id: \.self) { category in
26+
Text(category).tag(category)
27+
}
28+
}
29+
.pickerStyle(.segmented)
30+
.padding()
31+
32+
ScrollView {
33+
VStack(spacing: 16) {
34+
ForEach(snippets) { snippet in
35+
SnippetCard(
36+
snippet: snippet,
37+
onCopy: {
38+
copyToClipboard(snippet.code)
39+
withAnimation {
40+
copiedSnippet = snippet.id.uuidString
41+
}
42+
43+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
44+
withAnimation {
45+
copiedSnippet = nil
46+
}
47+
}
48+
},
49+
isCopied: copiedSnippet == snippet.id.uuidString
50+
)
51+
}
52+
}
53+
.padding()
54+
}
55+
}
56+
.navigationTitle("Code Examples")
57+
}
58+
}
59+
60+
private func copyToClipboard(_ text: String) {
61+
UIPasteboard.general.string = text
62+
}
63+
}
64+
65+
struct CodeSnippet: Identifiable {
66+
let id = UUID()
67+
let title: String
68+
let description: String
69+
let category: String
70+
let code: String
71+
let preview: AnyView?
72+
73+
init(title: String, description: String, category: String, code: String, preview: AnyView? = nil) {
74+
self.title = title
75+
self.description = description
76+
self.category = category
77+
self.code = code
78+
self.preview = preview
79+
}
80+
81+
static let allSnippets = [
82+
CodeSnippet(
83+
title: "Basic Usage",
84+
description: "The simplest way to display a diff",
85+
category: "Basic",
86+
code: """
87+
import SwiftUI
88+
import gitdiff
89+
90+
struct ContentView: View {
91+
let diffText = "your git diff output"
92+
93+
var body: some View {
94+
DiffRenderer(diffText: diffText)
95+
}
96+
}
97+
""",
98+
preview: AnyView(
99+
DiffRenderer(diffText: SampleDiffs.miniDiff)
100+
.frame(height: 150)
101+
)
102+
),
103+
104+
CodeSnippet(
105+
title: "With Theme",
106+
description: "Apply a built-in theme",
107+
category: "Basic",
108+
code: """
109+
DiffRenderer(diffText: diffText)
110+
.diffTheme(.vsCodeDark)
111+
""",
112+
preview: AnyView(
113+
DiffRenderer(diffText: SampleDiffs.miniDiff)
114+
.diffTheme(.vsCodeDark)
115+
.frame(height: 150)
116+
)
117+
),
118+
119+
CodeSnippet(
120+
title: "All Built-in Themes",
121+
description: "Available themes you can use",
122+
category: "Themes",
123+
code: """
124+
// GitHub (default)
125+
DiffRenderer(diffText: diffText)
126+
.diffTheme(.github)
127+
128+
// GitLab
129+
DiffRenderer(diffText: diffText)
130+
.diffTheme(.gitlab)
131+
132+
// VS Code Light
133+
DiffRenderer(diffText: diffText)
134+
.diffTheme(.vsCodeLight)
135+
136+
// VS Code Dark
137+
DiffRenderer(diffText: diffText)
138+
.diffTheme(.vsCodeDark)
139+
140+
// Xcode Light
141+
DiffRenderer(diffText: diffText)
142+
.diffTheme(.xcodeLight)
143+
144+
// Xcode Dark
145+
DiffRenderer(diffText: diffText)
146+
.diffTheme(.xcodeDark)
147+
"""
148+
),
149+
150+
CodeSnippet(
151+
title: "Custom Theme",
152+
description: "Create your own theme",
153+
category: "Themes",
154+
code: """
155+
let customTheme = DiffTheme(
156+
addedBackground: Color.green.opacity(0.2),
157+
addedText: Color.green,
158+
removedBackground: Color.red.opacity(0.2),
159+
removedText: Color.red,
160+
contextBackground: Color.gray.opacity(0.1),
161+
contextText: Color.primary,
162+
lineNumberBackground: Color.gray.opacity(0.1),
163+
lineNumberText: Color.secondary,
164+
headerBackground: Color.blue.opacity(0.1),
165+
headerText: Color.blue,
166+
fileHeaderBackground: Color.gray.opacity(0.1),
167+
fileHeaderText: Color.primary
168+
)
169+
170+
DiffRenderer(diffText: diffText)
171+
.diffTheme(customTheme)
172+
"""
173+
),
174+
175+
CodeSnippet(
176+
title: "View Modifiers",
177+
description: "Customize using view modifiers",
178+
category: "Configuration",
179+
code: """
180+
DiffRenderer(diffText: diffText)
181+
.diffTheme(.github)
182+
.diffLineNumbers(true)
183+
.diffFont(size: 14, weight: .medium)
184+
.diffLineSpacing(.comfortable)
185+
.diffWordWrap(true)
186+
"""
187+
),
188+
189+
CodeSnippet(
190+
title: "Configuration Object",
191+
description: "Use a configuration object for reusability",
192+
category: "Configuration",
193+
code: """
194+
let config = DiffConfiguration(
195+
theme: .vsCodeDark,
196+
showLineNumbers: true,
197+
fontSize: 14,
198+
fontWeight: .medium,
199+
lineSpacing: .comfortable,
200+
wordWrap: true
201+
)
202+
203+
DiffRenderer(diffText: diffText)
204+
.diffConfiguration(config)
205+
"""
206+
),
207+
208+
CodeSnippet(
209+
title: "Preset Configurations",
210+
description: "Use built-in configuration presets",
211+
category: "Configuration",
212+
code: """
213+
// Default configuration
214+
DiffRenderer(diffText: diffText)
215+
.diffConfiguration(.default)
216+
217+
// Optimized for code review
218+
DiffRenderer(diffText: diffText)
219+
.diffConfiguration(.codeReview)
220+
221+
// Optimized for mobile devices
222+
DiffRenderer(diffText: diffText)
223+
.diffConfiguration(.mobile)
224+
225+
// Perfect for presentations
226+
DiffRenderer(diffText: diffText)
227+
.diffConfiguration(.presentation)
228+
229+
// Dark mode configuration
230+
DiffRenderer(diffText: diffText)
231+
.diffConfiguration(.darkMode)
232+
"""
233+
),
234+
235+
CodeSnippet(
236+
title: "Configuration Builder",
237+
description: "Build configurations using fluent API",
238+
category: "Advanced",
239+
code: """
240+
let config = DiffConfiguration.default
241+
.with(theme: .gitlab)
242+
.withLineNumbers(false)
243+
.withFont(size: 16, weight: .semibold)
244+
.withLineSpacing(.spacious)
245+
246+
DiffRenderer(diffText: diffText)
247+
.diffConfiguration(config)
248+
"""
249+
),
250+
251+
CodeSnippet(
252+
title: "Environment Values",
253+
description: "Pass configuration through environment",
254+
category: "Advanced",
255+
code: """
256+
struct MyApp: View {
257+
var body: some View {
258+
ContentView()
259+
.environment(\\.diffConfiguration, .codeReview)
260+
}
261+
}
262+
263+
struct ContentView: View {
264+
// Configuration is automatically inherited
265+
var body: some View {
266+
VStack {
267+
DiffRenderer(diffText: diff1)
268+
DiffRenderer(diffText: diff2)
269+
}
270+
}
271+
}
272+
"""
273+
),
274+
275+
CodeSnippet(
276+
title: "Dynamic Theme Switching",
277+
description: "Switch themes at runtime",
278+
category: "Advanced",
279+
code: """
280+
struct ThemeSwitcher: View {
281+
@State private var selectedTheme: DiffTheme = .github
282+
283+
var body: some View {
284+
VStack {
285+
Picker("Theme", selection: $selectedTheme) {
286+
Text("GitHub").tag(DiffTheme.github)
287+
Text("VS Code Dark").tag(DiffTheme.vsCodeDark)
288+
}
289+
.pickerStyle(.segmented)
290+
291+
DiffRenderer(diffText: diffText)
292+
.diffTheme(selectedTheme)
293+
.animation(.easeInOut, value: selectedTheme)
294+
}
295+
}
296+
}
297+
"""
298+
)
299+
]
300+
}
301+
302+
struct SnippetCard: View {
303+
let snippet: CodeSnippet
304+
let onCopy: () -> Void
305+
let isCopied: Bool
306+
307+
var body: some View {
308+
VStack(alignment: .leading, spacing: 12) {
309+
HStack {
310+
VStack(alignment: .leading, spacing: 4) {
311+
Text(snippet.title)
312+
.font(.headline)
313+
Text(snippet.description)
314+
.font(.caption)
315+
.foregroundColor(.secondary)
316+
}
317+
318+
Spacer()
319+
320+
Button(action: onCopy) {
321+
Image(systemName: isCopied ? "checkmark.circle.fill" : "doc.on.doc")
322+
.foregroundColor(isCopied ? .green : .blue)
323+
.animation(.easeInOut, value: isCopied)
324+
}
325+
}
326+
327+
ScrollView(.horizontal, showsIndicators: false) {
328+
Text(snippet.code)
329+
.font(.system(.caption, design: .monospaced))
330+
.padding()
331+
.frame(maxWidth: .infinity, alignment: .leading)
332+
.background(Color(.secondarySystemBackground))
333+
.cornerRadius(8)
334+
}
335+
336+
if let preview = snippet.preview {
337+
VStack(alignment: .leading, spacing: 8) {
338+
Text("Preview")
339+
.font(.caption.bold())
340+
.foregroundColor(.secondary)
341+
342+
preview
343+
.background(Color.white)
344+
.cornerRadius(8)
345+
.overlay(
346+
RoundedRectangle(cornerRadius: 8)
347+
.stroke(Color.gray.opacity(0.2), lineWidth: 1)
348+
)
349+
}
350+
}
351+
}
352+
.padding()
353+
.background(Color(.systemBackground))
354+
.cornerRadius(12)
355+
.shadow(color: .black.opacity(0.05), radius: 5)
356+
}
357+
}
358+
359+
#Preview {
360+
CodeSnippetsView()
361+
}

0 commit comments

Comments
 (0)