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
411 changes: 411 additions & 0 deletions chrisharper22/AirDrop/AirDrop.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
10 changes: 10 additions & 0 deletions chrisharper22/AirDrop/AirDrop/AirDrop.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
17 changes: 17 additions & 0 deletions chrisharper22/AirDrop/AirDrop/AirDropApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AirDropApp.swift
// AirDrop
//
// Created by Chris Harper on 7/30/22.
//

import SwiftUI

@main
struct AirDropApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
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,63 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions chrisharper22/AirDrop/AirDrop/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
69 changes: 69 additions & 0 deletions chrisharper22/AirDrop/AirDrop/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// ContentView.swift
// AirDrop
//
// Created by Chris Harper on 7/30/22.
//

import SwiftUI

struct ContentView: View {
@ObservedObject var viewModel = AirDropAvatarViewModel()

var body: some View {
VStack(spacing: 20) {
Spacer()
AirDropAvatar(model: viewModel)
.frame(width: 225, height: 225)
textStack
Spacer()
actionButton
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.background {
Color(uiColor: .secondarySystemGroupedBackground)
.edgesIgnoringSafeArea(.all)
}
}

var textStack: some View {
VStack {
Text("Colton Westfield") // some random name just because
Group {
if viewModel.state != .waiting {
Text(viewModel.stateText)
.opacity(viewModel.stateOpacity)
} else {
Text(viewModel.stateText)
.opacity(viewModel.stateOpacity)
.animation(viewModel.state == .waiting ? .easeInOut(duration: 0.75) : .none, value: viewModel.stateOpacity)
}
}
.onReceive(viewModel.timer) { _ in
viewModel.updateStateOpacity()
}
.foregroundColor(viewModel.stateColor)
}
.font(.title2)
}

var actionButton: some View {
Button(role: viewModel.buttonRole) {
viewModel.buttonAction()
} label: {
Text(viewModel.buttonText)
.frame(width: 250)
}
.controlSize(.large)
.buttonStyle(.borderedProminent)
.disabled(viewModel.running)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.preferredColorScheme(.dark)
}
}
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,16 @@
//
// Task+sleep.swift
// AirDrop
//
// Created by Chris Harper on 7/30/22.
//

import Foundation

public extension Task where Success == Never, Failure == Never {
///Suspends the current task for at least the given duration in seconds.
static func sleep(seconds duration: Double) async throws {
let duration = UInt64(duration * 1_000_000_000)
try await Task.sleep(nanoseconds: duration)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//
// AirDropAvatarViewModel.swift
// AirDrop
//
// Created by Chris Harper on 7/30/22.
//

import Combine
import SwiftUI

/// The view model for a `AirDropAvatar`,
@MainActor class AirDropAvatarViewModel: ObservableObject {
/// The current state of AirDrop, as an ``AirDropState``.
@Published var state: AirDropState = .idle
/// The progress of AirDrop.
@Published var progress: Double = 0.0
/// The opacity of the AirDrop progress indicator.
@Published var progressOpacity: Double = 0.0
/// The opacity of the AirDrop state text.
@Published var stateOpacity: Double = 1.0
/// The timer responsible for the flashing of the AirDrop state text when waiting.
@Published var timer = Timer.publish(every: 0.75, on: .main, in: .common).autoconnect()

/// The possible states of AirDrop.
enum AirDropState {
/// An idle state.
case idle
/// A waiting state.
case waiting
/// A sending state in which data is being transferred.
case sending
/// A completed state where data transfer is complete.
case complete
}

/// The text of an ``AirDropState``.
var stateText: String {
switch state {
case .idle:
return " "
case .waiting:
return "Waiting..."
case .sending:
return "Sending..."
case .complete:
return "Sent"
}
}

/// A `Bool` indicating whether or not AirDrop is running.
var running: Bool {
switch state {
case .idle, .complete:
return false
case .waiting, .sending:
return true
}
}

/// The text of the button as a result of the current ``AirDropState``.
var buttonText: String {
switch state {
case .idle:
return "Start"
case .waiting, .sending:
return "Running"
case .complete:
return "Reset"
}
}

/// Set the state opacity according to the current A``AirDropState``.
func updateStateOpacity() {
switch state {
case .idle:
stateOpacity = 0
case .waiting:
stateOpacity == 0 ? (stateOpacity = 1.0) : (stateOpacity = 0.0)
default:
stateOpacity = 1.0
}
}

/// The action of the button as a result of the current ``AirDropState``.
func buttonAction() {
switch state {
case .idle:
Task { await prepareAirDrop() }
case .complete:
resetAirDrop()
default:
fatalError("AvatarViewModel.buttonAction(): really, this shouldn't have happened.")
}
}

/// The role of the button as a result of the current ``AirDropState``.
var buttonRole: ButtonRole? {
switch state {
case .idle, .waiting, .sending:
return .none
case .complete:
return .destructive
}
}

/// The color of an ``AirDropState``.
var stateColor: Color {
switch state {
case .idle:
return .primary
case .waiting, .sending:
return .secondary
case .complete:
return .accentColor
}
}

/// Resets properties to their defaults.
private func resetAirDrop() {
state = .idle
progressOpacity = 0.0
progress = 0.0
stateOpacity = 1.0
}

/// The first stage of the AirDrop animation.
private func prepareAirDrop() async {
state = .waiting
progressOpacity = 1.0
await commenceAirDrop()
}

/// The second stage of the AirDrop animation.
private func commenceAirDrop() async {
try? await Task.sleep(seconds: 5)
state = .sending
progress = 1.0
await finishAirDrop()
}

/// The third stage of the AirDrop animation.
private func finishAirDrop() async {
try? await Task.sleep(seconds: 3.25)
state = .complete
try? await Task.sleep(seconds: 1.25)
progressOpacity = 0.0
}
}
Loading