Skip to content
Open
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
410 changes: 410 additions & 0 deletions Example/SwiftUI/ExampleSwiftUI.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Example/SwiftUI/ExampleSwiftUI/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// AppDelegate.swift
// ExampleSwiftUI
//
// Created by Frank Schmitt on 5/11/23.
//

import UIKit
import ApptentiveKit

class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

Apptentive.shared.register(with: .init(key: "<#Your Apptentive App Key#>", signature: "<#Your Apptentive App Signature#>"))

return 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,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "P1040311.jpg",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
28 changes: 28 additions & 0 deletions Example/SwiftUI/ExampleSwiftUI/ExampleSwiftUIApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// ExampleSwiftUIApp.swift
// ExampleSwiftUI
//
// Created by Frank Schmitt on 5/3/23.
//

import SwiftUI
import ApptentiveKit

@main
struct ExampleSwiftUIApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

@State private var photoManager = PhotoManager.shared

var body: some Scene {
WindowGroup {
GalleryView()
.environment(photoManager)
.tint(.red)
}
}

init() {
PhotoManager.shared.load()
}
}
55 changes: 55 additions & 0 deletions Example/SwiftUI/ExampleSwiftUI/GalleryView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// ContentView.swift
// ExampleSwiftUI
//
// Created by Frank Schmitt on 5/3/23.
//

import SwiftUI
import ApptentiveKit

struct GalleryView: View {
@Environment(PhotoManager.self) private var photoManager
@State private var selectedTabIndex = 0

var body: some View {
TabView(selection: $selectedTabIndex) {
NavigationStack() {
PhotoListView(photoList: photoManager.allItems)
.navigationTitle("Photos")
}
.tabItem {
Label("Photos", systemImage: "photo")
}
.tag(1)

NavigationStack() {
PhotoListView(photoList: photoManager.favoriteItems)
.navigationTitle("Favorites")
}
.tabItem {
Label("Favorites", systemImage: "heart")
}
.tag(2)
}.onChange(of: selectedTabIndex) { _, newValue in
if newValue == 2 {
Apptentive.shared.engage(event: "favorite_photos")
} else {
Apptentive.shared.engage(event: "all_photos")
}
}
}
}

#Preview {
let photoManager = {
let result = PhotoManager()

result.load()

return result
}()

GalleryView()
.environment(photoManager)
}
51 changes: 51 additions & 0 deletions Example/SwiftUI/ExampleSwiftUI/PhotoCard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// PhotoCard.swift
// ExampleSwiftUI
//
// Created by Frank Schmitt on 5/3/23.
//

import SwiftUI
import ApptentiveKit

struct PhotoCard: View {
@Binding var isFavorite: Bool
let image: UIImage

var body: some View {
ZStack(alignment: .bottomTrailing) {
Image(uiImage: self.image)
.resizable()
.aspectRatio(contentMode: .fill)
.containerRelativeFrame(
.horizontal
) { length, axis in
if axis == .vertical {
return length
} else {
return length / 1.1
}
}
.clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous))

Button(action: {
self.isFavorite.toggle()
Apptentive.shared.engage(event: "toggle_favorite")
}) {
if self.isFavorite {
Image(systemName: "heart.fill")
} else {
Image(systemName: "heart")
}
}
.padding(8)
.background(.thinMaterial, in: Circle())
.padding()
}
.aspectRatio(1, contentMode: .fit)
}
}

#Preview {
PhotoCard(isFavorite: .constant(false), image: UIImage(named: "Placeholder")!)
}
50 changes: 50 additions & 0 deletions Example/SwiftUI/ExampleSwiftUI/PhotoListView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// PhotoListView.swift
// ExampleSwiftUI
//
// Created by Frank Schmitt on 5/3/23.
//

import SwiftUI

struct PhotoListView: View {
@Environment(PhotoManager.self) private var photoManager
var photoList: [PhotoManager.Photo]

var body: some View {
if photoList.isEmpty {
ContentUnavailableView("No Photos", systemImage: "photo.on.rectangle.angled")
} else {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible())], spacing: 16) {
ForEach(self.photoList) { photo in
PhotoCard(
isFavorite:
Binding(
get: { photo.isFavorite },
set: { photoManager.setFavorite($0, for: photo.tag) }
),
image: photo.image
)

}
}
.animation(.default, value: photoList)
}
}
}
}

#Preview("No Photos") {
PhotoListView(photoList: [])
.environment(PhotoManager())
}

#Preview("With Photos") { @MainActor in
let photoManager = PhotoManager()
photoManager.load()

return PhotoListView(photoList: photoManager.allItems)
.environment(photoManager)
}

57 changes: 57 additions & 0 deletions Example/SwiftUI/ExampleSwiftUI/PhotoManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// PhotoManager.swift
// Example
//
// Created by Frank Schmitt on 6/24/21.
//

import UIKit
import Observation

@Observable
@MainActor
class PhotoManager {
struct Photo: Hashable, Identifiable {
var id: Int { tag }
let image: UIImage
let tag: Int
var isFavorite: Bool
}

@ObservationIgnored private let imageURLs: [URL]
@ObservationIgnored private var images = [UIImage]()

private static let userDefaultsKey = "FavoritePhotos"

static let shared = PhotoManager()

var allItems = [Photo]() {
didSet {
self.favoriteItems = self.allItems.filter { $0.isFavorite }
UserDefaults.standard.set(self.favoriteItems.map { $0.tag }, forKey: Self.userDefaultsKey)
}
}

var favoriteItems = [Photo]()

init() {
self.imageURLs = Bundle.main.urls(forResourcesWithExtension: "jpg", subdirectory: "Photos") ?? []
}

func load() {
self.images = self.imageURLs.compactMap { url in
UIImage(contentsOfFile: url.path)
}

let defaultFavorites = Set(UserDefaults.standard.array(forKey: Self.userDefaultsKey) as? [Int] ?? [])

self.allItems = self.images.enumerated().map { (index, image) in
return Photo(image: image, tag: index, isFavorite: defaultFavorites.contains(index))
}
}

func setFavorite(_ isFavorite: Bool, for tag: Int) {
self.allItems[tag].isFavorite = isFavorite
}
}

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,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
21 changes: 21 additions & 0 deletions Example/SwiftUI/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Apptentive, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions Example/SwiftUI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ExampleSwiftUI
Example of Integrating ApptentiveKit into a SwiftUI App
Loading