Skip to content

Add Extensions Example #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions Examples/apps/SegmentExtensionsExample/ArticleWidget/Article.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Article.swift
// SegmentExtensionsExample
//
// Created by Alan Charles on 8/15/21.
//

import Foundation
import Segment

struct Article {
let title: String
let source: String
let imageName: String
}

extension Article {

private static let availableLibDocs = [
Article(title:"Analytics for iOS ", source: "https://segment.com/docs/connections/sources/catalog/libraries/mobile/ios/#analytics-for-ios", imageName: "Segment_logo"),
Article(title:"Analytics for Android", source: "https://segment.com/docs/connections/sources/catalog/libraries/mobile/android/", imageName: "Segment_logo"),
Article(title:"Analytics for React Native", source: "https://segment.com/docs/connections/sources/catalog/libraries/mobile/react-native/", imageName: "Segment_logo"),
Article(title:"Analytics.js", source: "https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/", imageName: "Segment_logo"),
Article(title:"Analytics for Node ", source: "https://segment.com/docs/connections/sources/catalog/libraries/server/node/", imageName: "Segment_logo"),
]

private static let availableDestDocs = [
Article(title:"Appsflyer Destination", source: "https://segment.com/docs/connections/destinations/catalog/appsflyer/", imageName: "Segment_logo"),
Article(title:"Firebase Destination", source: "https://segment.com/docs/connections/destinations/catalog/firebase/", imageName: "Segment_logo"),
Article(title:"Facebook App Events Destination", source: "https://segment.com/docs/connections/destinations/catalog/facebook-app-events/", imageName: "Segment_logo"),
Article(title:"Mixpanel Destination", source: "https://segment.com/docs/connections/destinations/catalog/mixpanel/", imageName: "Segment_logo"),
Article(title:"Amplitude Destination", source: "https://segment.com/docs/connections/destinations/catalog/Amplitude/", imageName: "Segment_logo"),

]

static var libDocs: [Article] {
return Array(availableLibDocs.shuffled().prefix(2))
}

static var destDocs: [Article] {
return Array(availableDestDocs.shuffled().prefix(2))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ArticleItemView.swift
// SegmentExtensionsExample
//
// Created by Alan Charles on 8/15/21.
//

import SwiftUI
import Segment

struct ArticleItemView: View {
var article : Article

var body: some View {
HStack {
Image(article.imageName)
.resizable()
.frame(width: 50, height: 50, alignment: .center)
.cornerRadius(8)
VStack(alignment: .leading) {
Text(article.title)
.lineLimit(/*@START_MENU_TOKEN@*/2/*@END_MENU_TOKEN@*/)
.font(.system(size: 14, weight: .semibold, design: .default))
Text(article.source)
.lineLimit(1)
.font(.caption2)
.foregroundColor(.gray)
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ArticleSectionView.swift
// SegmentExtensionsExample
//
// Created by Alan Charles on 8/15/21.
//

import SwiftUI
import Segment

struct ArticleSectionView: View {

var sectionTitle: String
var articles: [Article]

var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(sectionTitle)
.font(.subheadline)
.fontWeight(.heavy)
.foregroundColor(Color.blue)
ArticleItemView(article: articles[0])
ArticleItemView(article: articles[1])
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// ArticleWidget.swift
// ArticleWidget
//
// Created by Alan Charles on 8/15/21.
//

import Segment
import WidgetKit
import SwiftUI

struct Provider: TimelineProvider {

func placeholder(in context: TimelineProvider.Context) -> ArticleEntry {
ArticleEntry(date: Date(),
libArticles: Article.libDocs,
destArticles: Article.destDocs)
}

func getSnapshot(in context: TimelineProvider.Context, completion: @escaping (ArticleEntry) -> ()) {
let entry = ArticleEntry(date: Date(),
libArticles: Article.libDocs,
destArticles: Article.destDocs)
completion(entry)

Analytics.main.track(name:"Widget Snapshot")
}

func getTimeline(in context: TimelineProvider.Context, completion: @escaping (WidgetKit.Timeline<Entry>) -> ()) {
var entries: [ArticleEntry] = []

// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = ArticleEntry(date: entryDate,
libArticles: Article.libDocs,
destArticles: Article.destDocs)
entries.append(entry)
}

let timeline = WidgetKit.Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}

}

struct ArticleEntry: TimelineEntry {
let date: Date
let libArticles: [Article]
let destArticles: [Article]
}

struct ArticleWidgetEntryView : View {
var entry: ArticleEntry

@Environment(\.widgetFamily) var widgetFamily

var body: some View {
VStack(alignment: .leading, spacing: 8) {
ArticleSectionView(sectionTitle: "Library Docs", articles: entry.libArticles)
if widgetFamily == .systemLarge {
ArticleSectionView(sectionTitle: "Destinations", articles: entry.destArticles)
}
}
.padding(10)
}
}

@main
struct ArticleWidget: Widget {
let kind: String = "ArticleWidget"

var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
ArticleWidgetEntryView(entry: entry)
}
.supportedFamilies([.systemMedium, .systemLarge])
.configurationDisplayName("Segment Documentation")
.description("Documentation at your fingertips.")
}
}

struct ArticleWidget_Previews: PreviewProvider {
static var previews: some View {
Group {
ArticleWidgetEntryView(entry: ArticleEntry(date: Date(),
libArticles: Article.libDocs,
destArticles: Article.destDocs))
.previewContext(WidgetPreviewContext(family: .systemSmall))
ArticleWidgetEntryView(entry: ArticleEntry(date: Date(),
libArticles: Article.libDocs,
destArticles: Article.destDocs))
.previewContext(WidgetPreviewContext(family: .systemSmall))

}
}
}

extension Analytics {
static var main = Analytics(configuration:
Configuration(writeKey: "ABCD")
.flushAt(3)
.trackApplicationLifecycleEvents(true))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "Segment_logo.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading