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
79 changes: 79 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/Focuser.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1630"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Focuser"
BuildableName = "Focuser"
BlueprintName = "Focuser"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FocuserTests"
BuildableName = "FocuserTests"
BlueprintName = "FocuserTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Focuser"
BuildableName = "Focuser"
BlueprintName = "Focuser"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

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

24 changes: 11 additions & 13 deletions Package.resolved

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

17 changes: 12 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
// swift-tools-version:5.3
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Focuser",
platforms: [
.iOS(.v13),
.iOS(.v14),
],
products: [
.library(
name: "Focuser",
targets: ["Focuser"]),
],
dependencies: [
.package(name: "Introspect", url: "https://github.com/siteline/SwiftUI-Introspect.git", from: "0.1.3")
.package(url: "https://github.com/siteline/swiftui-introspect", from: "1.4.0-beta.3")
],
targets: [
.target(
name: "Focuser",
dependencies: ["Introspect"]),
dependencies: [
.product(name: "SwiftUIIntrospect", package: "swiftui-introspect")
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "FocuserTests",
dependencies: ["Focuser"]),
dependencies: ["Focuser"]
),
]
)
15 changes: 15 additions & 0 deletions Sources/Focuser/ComplianceProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@ public protocol FocusStateCompliant: Hashable {
static var last: Self { get }
var next: Self? { get }
}

public extension FocusStateCompliant where Self: CaseIterable, AllCases: BidirectionalCollection {

static var last: Self {
return Self.allCases.last! //swiftlint:disable:this force_unwrapping
}

var next: Self? {
let all = Self.allCases
let index = all.firstIndex(of: self)! //swiftlint:disable:this force_unwrapping
let next = all.index(after: index)
return next == all.endIndex ? nil : all[next]
}

}
147 changes: 147 additions & 0 deletions Sources/Focuser/LifeCycleEventHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//
// LifeCycleEventHandler.swift
//
//
// Created by Tarek Sabry on 03/02/2023.
//

import UIKit
import SwiftUI

public typealias LifeCycleEventHandler = ((LifeCycleEvent) -> Void)

public enum LifeCycleEvent {
case viewWillAppear
case viewDidAppear
case viewWillDisappear
case viewDidDisappear
}

struct ViewControllerLifeCycleHandler: UIViewControllerRepresentable {

private let onLifeCycleEvent: LifeCycleEventHandler

init(onLifeCycleEvent: @escaping LifeCycleEventHandler) {
self.onLifeCycleEvent = onLifeCycleEvent
}

func makeUIViewController(context: Context) -> UIViewController {
LifeCycleViewController(onLifeCycleEvent: onLifeCycleEvent)
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}

private class LifeCycleViewController: UIViewController {
private let onLifeCycleEvent: ((LifeCycleEvent) -> Void)

init(onLifeCycleEvent: @escaping LifeCycleEventHandler) {
self.onLifeCycleEvent = onLifeCycleEvent
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
onLifeCycleEvent(.viewWillAppear)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
onLifeCycleEvent(.viewDidAppear)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
onLifeCycleEvent(.viewWillDisappear)
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
onLifeCycleEvent(.viewDidDisappear)
}
}
}

struct ViewDidLoadModifier: ViewModifier {

@State private var didLoad = false
private let action: (() -> Void)?

init(perform action: (() -> Void)? = nil) {
self.action = action
}

func body(content: Content) -> some View {
content.onAppear {
if didLoad == false {
didLoad = true
action?()
}
}
}

}


public extension View {

func onLifeCycleEvent(perform: @escaping LifeCycleEventHandler) -> some View {
background(ViewControllerLifeCycleHandler(onLifeCycleEvent: perform))
}

func onDidLoad(perform: @escaping (() -> Void)) -> some View {
modifier(ViewDidLoadModifier(perform: perform))
}

func onWillAppear(perform: @escaping (() -> Void)) -> some View {
onLifeCycleEvent { event in
switch event {
case .viewWillAppear:
perform()

default:
break
}
}
}

func onDidAppear(perform: @escaping (() -> Void)) -> some View {
onLifeCycleEvent { event in
switch event {
case .viewDidAppear:
perform()

default:
break
}
}
}

func onWillDisappear(perform: @escaping (() -> Void)) -> some View {
onLifeCycleEvent { event in
switch event {
case .viewWillDisappear:
perform()

default:
break
}
}
}

func onDidDisappear(perform: @escaping (() -> Void)) -> some View {
onLifeCycleEvent { event in
switch event {
case .viewDidDisappear:
perform()

default:
break
}
}
}

}
Loading