Skip to content

Commit

Permalink
Add SwiftUI Tutorials (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jul 10, 2022
1 parent 0980f93 commit 1b8744b
Show file tree
Hide file tree
Showing 36 changed files with 495 additions and 11 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import SwiftUI

struct ContentView: View {

@ObservedObject
var reachabilityManager: ReachabilityManager

var body: some View {
Text("Hello, world!")
.padding()
}

private var symbolName: String {
switch reachabilityManager.reachability {
case .wifi:
return "wifi.circle.fill"
case .ethernet:
return "cable.connector"
case .unknown:
return "questionmark.circle.fill"
case .cellular:
return "antenna.radiowaves.left.and.right.circle.fill"
case .disconnected:
return "xmark.circle.fill"
}
}

private var backgroundColor: Color {
switch reachabilityManager.reachability {
case .disconnected:
return .red
case .unknown:
return .yellow
case .wifi, .ethernet, .cellular:
return .green
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(reachabilityManager: .init())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import SwiftUI

struct ContentView: View {

@ObservedObject
var reachabilityManager: ReachabilityManager

var body: some View {
Text("Hello, world!")
.padding()
}

private var symbolName: String {
switch reachabilityManager.reachability {
case .wifi:
return "wifi.circle.fill"
case .ethernet:
return "cable.connector"
case .unknown:
return "questionmark.circle.fill"
case .cellular:
return "antenna.radiowaves.left.and.right.circle.fill"
case .disconnected:
return "xmark.circle.fill"
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(reachabilityManager: .init())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import SwiftUI

struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
final class ReachabilityManager {

enum Status {
case ethernet
case wifi
case cellular
case unknown
case disconnected
}

var reachability: Status = .disconnected

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import SwiftUI

struct ContentView: View {

@ObservedObject
var reachabilityManager: ReachabilityManager

var body: some View {
Image(systemName: symbolName)
.resizable()
.frame(width: 60, height: 60)
}

private var symbolName: String {
switch reachabilityManager.reachability {
case .wifi:
return "wifi.circle.fill"
case .ethernet:
return "cable.connector"
case .unknown:
return "questionmark.circle.fill"
case .cellular:
return "antenna.radiowaves.left.and.right.circle.fill"
case .disconnected:
return "xmark.circle.fill"
}
}

private var backgroundColor: Color {
switch reachabilityManager.reachability {
case .disconnected:
return .red
case .unknown:
return .yellow
case .wifi, .ethernet, .cellular:
return .green
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(reachabilityManager: .init())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Combine
import Network
import NetworkReachability

final class ReachabilityManager: ObservableObject {

init() {
setUp()
}

enum Status {
case ethernet
case wifi
case cellular
case unknown
case disconnected
}

@Published
var reachability: Status = .disconnected

private var monitor: NetworkMonitor!

private func setUp() {
monitor = .init() { [weak self] _, networkPath in
guard let self = self else { return }
if networkPath.usesInterfaceType(.wiredEthernet) {
self.reachability = .ethernet
} else if networkPath.usesInterfaceType(.wifi) {
self.reachability = .wifi
} else if networkPath.usesInterfaceType(.cellular) {
self.reachability = .cellular
} else if networkPath.status == .satisfied {
self.reachability = .unknown
} else {
self.reachability = .disconnected
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Combine

final class ReachabilityManager: ObservableObject {

enum Status {
case ethernet
case wifi
case cellular
case unknown
case disconnected
}

@Published
var reachability: Status = .disconnected

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SwiftUI

@main
struct ReachabilityApp: App {
var body: some Scene {
WindowGroup {
ContentView(reachabilityManager: ReachabilityManager())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SwiftUI

@main
struct ReachabilityApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import SwiftUI

struct ContentView: View {

@ObservedObject
var reachabilityManager: ReachabilityManager

var body: some View {
ZStack {
Image(systemName: symbolName)
.resizable()
.frame(width: 60, height: 60)
}
.frame(maxWidth: .infinity,
maxHeight: .infinity)
.background(backgroundColor)
}

private var symbolName: String {
switch reachabilityManager.reachability {
case .wifi:
return "wifi.circle.fill"
case .ethernet:
return "cable.connector"
case .unknown:
return "questionmark.circle.fill"
case .cellular:
return "antenna.radiowaves.left.and.right.circle.fill"
case .disconnected:
return "xmark.circle.fill"
}
}

private var backgroundColor: Color {
switch reachabilityManager.reachability {
case .disconnected:
return .red
case .unknown:
return .yellow
case .wifi, .ethernet, .cellular:
return .green
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(reachabilityManager: .init())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import SwiftUI

struct ContentView: View {

@ObservedObject
var reachabilityManager: ReachabilityManager

var body: some View {
Image(systemName: symbolName)
}

private var symbolName: String {
switch reachabilityManager.reachability {
case .wifi:
return "wifi.circle.fill"
case .ethernet:
return "cable.connector"
case .unknown:
return "questionmark.circle.fill"
case .cellular:
return "antenna.radiowaves.left.and.right.circle.fill"
case .disconnected:
return "xmark.circle.fill"
}
}

private var backgroundColor: Color {
switch reachabilityManager.reachability {
case .disconnected:
return .red
case .unknown:
return .yellow
case .wifi, .ethernet, .cellular:
return .green
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(reachabilityManager: .init())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
Step-by-step tutorials to learn NetworkReachability using Xcode 13
@Image(source: "Intro.png", alt: "A MacBook Pro Running Xcode")
}
@Chapter(name: "Build a Sample App") {
Create a sample iOS application using NetworkReachability
@Chapter(name: "UIKit Sample App") {
Create a sample iOS application with NetworkReachability using UIKit

@Image(source: "SampleIcon.png", alt: "Xcode Icon")
@Image(source: "SampleIcon.png", alt: "Sample App Icon")
@TutorialReference(tutorial: "doc:SampleProjectSetup-UIKit")
@TutorialReference(tutorial: "doc:SampleProjectUI-UIKit")
@TutorialReference(tutorial: "doc:SampleProjectIntegration-UIKit")
}
@Chapter(name: "SwiftUI Sample App") {
Create a sample iOS application with NetworkReachability using SwiftUI

@Image(source: "SampleIcon2.png", alt: "Sample App Icon")
@TutorialReference(tutorial: "doc:SampleProjectSetup-SwiftUI")
@TutorialReference(tutorial: "doc:SampleProjectIntegration-SwiftUI")
@TutorialReference(tutorial: "doc:SampleProjectUI-SwiftUI")
}
@Resources {
Explore more resources for learning about NetworkReachability.
@Downloads(destination: "https://github.com/vsanthanam/NetworkReachability/releases") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@Tutorial(time: 5, projectFiles: "ProjectIntegrate-SwiftUI.zip") {
@XcodeRequirement(title: "Xcode 13", destination: "https://developer.apple.com/download/")
@Intro(title: "Implementing Reachability") {
This tutorial guides you through the process of consuming reachability data in your application.
}

@Section(title: "Integrate Reachability") {
@ContentAndMedia {
Create an `ObservableObject` for use with SwiftUI using NetworkReachability and callback closures.
@Image(source: "SampleIcon2.png", alt: "Sample App Icon")
}

@Steps {
@Step {
Choose File > New > File, select iOS as the platform, select the “Swift File” template, and click Next. Name the new file "ReachabilityManager.swift" and click Create.

@Image(source: "XcodeNewFile", alt: "New File Chooser")
}

@Step {
Create a new class, and create an enumeration representing the various reachability options within your new class.
@Code(name: "ReachabilityManager.swift", file: "CreateStatus.swift")
}

@Step {
Make your class an `ObservableObject` and annotate the instance variable with the `@Published` property wrapper.
@Code(name: "ReachabilityManager.swift", file: "MakeObservableObject.swift")
}

@Step {
Create a `NetworkMonitor` class and use the callback API to recieve updates from the monitor.
@Code(name: "ReachabilityManager.swift", file: "ImplementManager.swift")
}
}
}
}
Loading

0 comments on commit 1b8744b

Please sign in to comment.