Skip to content

Commit ebdd751

Browse files
authored
add extensions example (#40)
1 parent dc0e797 commit ebdd751

File tree

34 files changed

+2029
-0
lines changed

34 files changed

+2029
-0
lines changed

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Article.swift
3+
// SegmentExtensionsExample
4+
//
5+
// Created by Alan Charles on 8/15/21.
6+
//
7+
8+
import Foundation
9+
import Segment
10+
11+
struct Article {
12+
let title: String
13+
let source: String
14+
let imageName: String
15+
}
16+
17+
extension Article {
18+
19+
private static let availableLibDocs = [
20+
Article(title:"Analytics for iOS ", source: "https://segment.com/docs/connections/sources/catalog/libraries/mobile/ios/#analytics-for-ios", imageName: "Segment_logo"),
21+
Article(title:"Analytics for Android", source: "https://segment.com/docs/connections/sources/catalog/libraries/mobile/android/", imageName: "Segment_logo"),
22+
Article(title:"Analytics for React Native", source: "https://segment.com/docs/connections/sources/catalog/libraries/mobile/react-native/", imageName: "Segment_logo"),
23+
Article(title:"Analytics.js", source: "https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/", imageName: "Segment_logo"),
24+
Article(title:"Analytics for Node ", source: "https://segment.com/docs/connections/sources/catalog/libraries/server/node/", imageName: "Segment_logo"),
25+
]
26+
27+
private static let availableDestDocs = [
28+
Article(title:"Appsflyer Destination", source: "https://segment.com/docs/connections/destinations/catalog/appsflyer/", imageName: "Segment_logo"),
29+
Article(title:"Firebase Destination", source: "https://segment.com/docs/connections/destinations/catalog/firebase/", imageName: "Segment_logo"),
30+
Article(title:"Facebook App Events Destination", source: "https://segment.com/docs/connections/destinations/catalog/facebook-app-events/", imageName: "Segment_logo"),
31+
Article(title:"Mixpanel Destination", source: "https://segment.com/docs/connections/destinations/catalog/mixpanel/", imageName: "Segment_logo"),
32+
Article(title:"Amplitude Destination", source: "https://segment.com/docs/connections/destinations/catalog/Amplitude/", imageName: "Segment_logo"),
33+
34+
]
35+
36+
static var libDocs: [Article] {
37+
return Array(availableLibDocs.shuffled().prefix(2))
38+
}
39+
40+
static var destDocs: [Article] {
41+
return Array(availableDestDocs.shuffled().prefix(2))
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// ArticleItemView.swift
3+
// SegmentExtensionsExample
4+
//
5+
// Created by Alan Charles on 8/15/21.
6+
//
7+
8+
import SwiftUI
9+
import Segment
10+
11+
struct ArticleItemView: View {
12+
var article : Article
13+
14+
var body: some View {
15+
HStack {
16+
Image(article.imageName)
17+
.resizable()
18+
.frame(width: 50, height: 50, alignment: .center)
19+
.cornerRadius(8)
20+
VStack(alignment: .leading) {
21+
Text(article.title)
22+
.lineLimit(/*@START_MENU_TOKEN@*/2/*@END_MENU_TOKEN@*/)
23+
.font(.system(size: 14, weight: .semibold, design: .default))
24+
Text(article.source)
25+
.lineLimit(1)
26+
.font(.caption2)
27+
.foregroundColor(.gray)
28+
}
29+
}
30+
}
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// ArticleSectionView.swift
3+
// SegmentExtensionsExample
4+
//
5+
// Created by Alan Charles on 8/15/21.
6+
//
7+
8+
import SwiftUI
9+
import Segment
10+
11+
struct ArticleSectionView: View {
12+
13+
var sectionTitle: String
14+
var articles: [Article]
15+
16+
var body: some View {
17+
VStack(alignment: .leading, spacing: 8) {
18+
Text(sectionTitle)
19+
.font(.subheadline)
20+
.fontWeight(.heavy)
21+
.foregroundColor(Color.blue)
22+
ArticleItemView(article: articles[0])
23+
ArticleItemView(article: articles[1])
24+
}
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// ArticleWidget.swift
3+
// ArticleWidget
4+
//
5+
// Created by Alan Charles on 8/15/21.
6+
//
7+
8+
import Segment
9+
import WidgetKit
10+
import SwiftUI
11+
12+
struct Provider: TimelineProvider {
13+
14+
func placeholder(in context: TimelineProvider.Context) -> ArticleEntry {
15+
ArticleEntry(date: Date(),
16+
libArticles: Article.libDocs,
17+
destArticles: Article.destDocs)
18+
}
19+
20+
func getSnapshot(in context: TimelineProvider.Context, completion: @escaping (ArticleEntry) -> ()) {
21+
let entry = ArticleEntry(date: Date(),
22+
libArticles: Article.libDocs,
23+
destArticles: Article.destDocs)
24+
completion(entry)
25+
26+
Analytics.main.track(name:"Widget Snapshot")
27+
}
28+
29+
func getTimeline(in context: TimelineProvider.Context, completion: @escaping (WidgetKit.Timeline<Entry>) -> ()) {
30+
var entries: [ArticleEntry] = []
31+
32+
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
33+
let currentDate = Date()
34+
for hourOffset in 0 ..< 5 {
35+
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
36+
let entry = ArticleEntry(date: entryDate,
37+
libArticles: Article.libDocs,
38+
destArticles: Article.destDocs)
39+
entries.append(entry)
40+
}
41+
42+
let timeline = WidgetKit.Timeline(entries: entries, policy: .atEnd)
43+
completion(timeline)
44+
}
45+
46+
}
47+
48+
struct ArticleEntry: TimelineEntry {
49+
let date: Date
50+
let libArticles: [Article]
51+
let destArticles: [Article]
52+
}
53+
54+
struct ArticleWidgetEntryView : View {
55+
var entry: ArticleEntry
56+
57+
@Environment(\.widgetFamily) var widgetFamily
58+
59+
var body: some View {
60+
VStack(alignment: .leading, spacing: 8) {
61+
ArticleSectionView(sectionTitle: "Library Docs", articles: entry.libArticles)
62+
if widgetFamily == .systemLarge {
63+
ArticleSectionView(sectionTitle: "Destinations", articles: entry.destArticles)
64+
}
65+
}
66+
.padding(10)
67+
}
68+
}
69+
70+
@main
71+
struct ArticleWidget: Widget {
72+
let kind: String = "ArticleWidget"
73+
74+
var body: some WidgetConfiguration {
75+
StaticConfiguration(kind: kind, provider: Provider()) { entry in
76+
ArticleWidgetEntryView(entry: entry)
77+
}
78+
.supportedFamilies([.systemMedium, .systemLarge])
79+
.configurationDisplayName("Segment Documentation")
80+
.description("Documentation at your fingertips.")
81+
}
82+
}
83+
84+
struct ArticleWidget_Previews: PreviewProvider {
85+
static var previews: some View {
86+
Group {
87+
ArticleWidgetEntryView(entry: ArticleEntry(date: Date(),
88+
libArticles: Article.libDocs,
89+
destArticles: Article.destDocs))
90+
.previewContext(WidgetPreviewContext(family: .systemSmall))
91+
ArticleWidgetEntryView(entry: ArticleEntry(date: Date(),
92+
libArticles: Article.libDocs,
93+
destArticles: Article.destDocs))
94+
.previewContext(WidgetPreviewContext(family: .systemSmall))
95+
96+
}
97+
}
98+
}
99+
100+
extension Analytics {
101+
static var main = Analytics(configuration:
102+
Configuration(writeKey: "ABCD")
103+
.flushAt(3)
104+
.trackApplicationLifecycleEvents(true))
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"scale" : "2x",
6+
"size" : "20x20"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"scale" : "3x",
11+
"size" : "20x20"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"scale" : "2x",
16+
"size" : "29x29"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"scale" : "3x",
21+
"size" : "29x29"
22+
},
23+
{
24+
"idiom" : "iphone",
25+
"scale" : "2x",
26+
"size" : "40x40"
27+
},
28+
{
29+
"idiom" : "iphone",
30+
"scale" : "3x",
31+
"size" : "40x40"
32+
},
33+
{
34+
"idiom" : "iphone",
35+
"scale" : "2x",
36+
"size" : "60x60"
37+
},
38+
{
39+
"idiom" : "iphone",
40+
"scale" : "3x",
41+
"size" : "60x60"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"scale" : "1x",
46+
"size" : "20x20"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"scale" : "2x",
51+
"size" : "20x20"
52+
},
53+
{
54+
"idiom" : "ipad",
55+
"scale" : "1x",
56+
"size" : "29x29"
57+
},
58+
{
59+
"idiom" : "ipad",
60+
"scale" : "2x",
61+
"size" : "29x29"
62+
},
63+
{
64+
"idiom" : "ipad",
65+
"scale" : "1x",
66+
"size" : "40x40"
67+
},
68+
{
69+
"idiom" : "ipad",
70+
"scale" : "2x",
71+
"size" : "40x40"
72+
},
73+
{
74+
"idiom" : "ipad",
75+
"scale" : "1x",
76+
"size" : "76x76"
77+
},
78+
{
79+
"idiom" : "ipad",
80+
"scale" : "2x",
81+
"size" : "76x76"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"scale" : "2x",
86+
"size" : "83.5x83.5"
87+
},
88+
{
89+
"idiom" : "ios-marketing",
90+
"scale" : "1x",
91+
"size" : "1024x1024"
92+
}
93+
],
94+
"info" : {
95+
"author" : "xcode",
96+
"version" : 1
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "Segment_logo.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}

0 commit comments

Comments
 (0)