Skip to content
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

Feature/SwiftUI View Modifiers #307

Merged
merged 9 commits into from
May 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Alamofire
import Foundation
import PovioKitCore
import PovioKitUtilities

public final class AlamofireConsoleLogger: EventMonitor {
public let queue = DispatchQueue(label: "com.alamofire.console.networklogger")
Expand Down
49 changes: 49 additions & 0 deletions Sources/UI/SwiftUI/View Modifiers/MeasureSizeModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// MeasureSizeModifier.swift
// PovioKit
//
// Created by Borut Tomazin on 10/02/2024.
// Copyright © 2024 Povio Inc. All rights reserved.
//

import SwiftUI

@available(iOS 15.0, *)
public struct MeasureSizeModifier: ViewModifier {
public typealias SizeHandler = (CGSize) -> Void
let onSize: SizeHandler
let isInitialOnly: Bool

public func body(content: Content) -> some View {
content
.background(GeometryReader { geo in
if isInitialOnly {
Color.clear
.onFirstAppear {
onSize(geo.size)
}
} else {
Color.clear
.onAppear {
onSize(geo.size)
}
.onChange(of: geo.size) { size in
onSize(size)
}
}
})
}
}

@available(iOS 15, *)
public extension View {
/// Measure view size everytime it changes
func measureSize(_ handler: @escaping MeasureSizeModifier.SizeHandler) -> some View {
modifier(MeasureSizeModifier(onSize: handler, isInitialOnly: false))
}

/// Measure view size only for the first time
func measureInitialSize(_ handler: @escaping MeasureSizeModifier.SizeHandler) -> some View {
modifier(MeasureSizeModifier(onSize: handler, isInitialOnly: true))
}
}
31 changes: 31 additions & 0 deletions Sources/UI/SwiftUI/View Modifiers/OnFirstAppearModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// OnFirstAppearModifier.swift
// PovioKit
//
// Created by Borut Tomazin on 10/02/2024.
// Copyright © 2024 Povio Inc. All rights reserved.
//

import SwiftUI

public struct OnFirstAppearModifier: ViewModifier {
public typealias VoidHandler = () -> Swift.Void
@State private var didLoad = false
let action: VoidHandler?

public func body(content: Content) -> some View {
content.onAppear {
guard !didLoad else { return }
didLoad = true
action?()
}
}
}

public extension View {
/// Modifier action is executed only once per view lifecycle.
/// It differs from `onAppear` modifier which is executed everytime wiew appears on the screen.
func onFirstAppear(perform action: OnFirstAppearModifier.VoidHandler? = nil) -> some View {
modifier(OnFirstAppearModifier(action: action))
}
}
76 changes: 76 additions & 0 deletions Sources/UI/SwiftUI/View Modifiers/PhotoPickerModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// PhotoPickerModifier.swift
// PovioKit
//
// Created by Borut Tomazin on 10/02/2024.
// Copyright © 2024 Povio Inc. All rights reserved.
//

import SwiftUI

@available(iOS 15.0, *)
public struct PhotoPickerModifier: ViewModifier {
public typealias VoidHandler = () -> Swift.Void
public typealias ImageHandler = (UIImage) -> Swift.Void

@Binding var present: Bool
var canRemove: Bool = true
var configuration: Configuration = .init()
var removeHandler: VoidHandler?
let imageHandler: ImageHandler

@State private var showImageCapture: Bool = false
@State private var showPhotoLibrary: Bool = false

public func body(content: Content) -> some View {
content
.confirmationDialog("", isPresented: $present) {
Button(configuration.takePhoto) {
showImageCapture.toggle()
}
Button(configuration.chooseFromLibrary) {
showPhotoLibrary.toggle()
}
if canRemove {
Button(configuration.removePhoto, role: .destructive) {
removeHandler?()
}
}
}
.sheet(isPresented: $showPhotoLibrary) {
PhotoPickerView(sourceType: .photoLibrary) { image in
imageHandler(image)
}
.ignoresSafeArea()
}
.fullScreenCover(isPresented: $showImageCapture) {
PhotoPickerView(sourceType: .camera) { image in
imageHandler(image)
}
.ignoresSafeArea()
}
}

struct Configuration {
var takePhoto: String = "Take a Photo"
var chooseFromLibrary: String = "Choose from Library"
var removePhoto: String = "Remove Photo"
}
}

@available(iOS 15.0, *)
public extension View {
func photoPicker(
present: Binding<Bool>,
canRemove: Bool = true,
removeHandler: PhotoPickerModifier.VoidHandler?,
imageHandler: @escaping PhotoPickerModifier.ImageHandler
) -> some View {
modifier(PhotoPickerModifier(
present: present,
canRemove: canRemove,
removeHandler: removeHandler,
imageHandler: imageHandler
))
}
}
33 changes: 33 additions & 0 deletions Sources/UI/SwiftUI/View Modifiers/SquaredModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// SquaredModifier.swift
// PovioKit
//
// Created by Borut Tomazin on 10/02/2024.
// Copyright © 2024 Povio Inc. All rights reserved.
//

import SwiftUI

public struct SquaredModifier: ViewModifier {
var cornerRadius: CGFloat = 0
var aspectRatio: CGFloat = 1

public func body(content: Content) -> some View {
content
.frame(
minWidth: 0,
maxWidth: .infinity,
minHeight: 0,
maxHeight: .infinity
)
.aspectRatio(aspectRatio, contentMode: .fit)
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
}
}

public extension View {
/// Makes view squared
func squared(cornerRadius: CGFloat = 0, aspectRatio: CGFloat = 1) -> some View {
modifier(SquaredModifier(cornerRadius: cornerRadius, aspectRatio: aspectRatio))
}
}
31 changes: 31 additions & 0 deletions Sources/UI/SwiftUI/View Modifiers/TextFieldLimitModifer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// TextFieldLimitModifer.swift
// PovioKit
//
// Created by Dejan Skledar on 29/12/2023.
// Copyright © 2024 Povio Inc. All rights reserved.
//

import SwiftUI

@available(iOS 15.0, *)
public struct TextFieldLimitModifer: ViewModifier {
@Binding var text: String
let limit: Int

public func body(content: Content) -> some View {
content
.onChange(of: text) { newValue in
guard newValue.count > limit else { return }
text = String(newValue.prefix(limit))
}
}
}

@available(iOS 15.0, *)
public extension View {
/// Limit input length
func limitInput(text: Binding<String>, limit: Int) -> some View {
modifier(TextFieldLimitModifer(text: text, limit: limit))
}
}
54 changes: 54 additions & 0 deletions Sources/UI/SwiftUI/Views/PhotoPickerView/PhotoPickerView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// PhotoPickerView.swift
// PovioKit
//
// Created by Borut Tomazin on 10/02/2024.
// Copyright © 2024 Povio Inc. All rights reserved.
//

import SwiftUI
import UIKit

/// This view should be used in conjuction with `PhotoPicker` view modifier.
struct PhotoPickerView: UIViewControllerRepresentable {
let sourceType: UIImagePickerController.SourceType
let onComplete: (UIImage) -> Void

func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = sourceType
imagePicker.delegate = context.coordinator
return imagePicker
}

func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { /* --- */ }

func makeCoordinator() -> Coordinator {
Coordinator(onComplete: onComplete)
}
}

// MARK: - Coordinator
extension PhotoPickerView {
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let onComplete: (UIImage) -> Void

init(onComplete: @escaping (UIImage) -> Void) {
self.onComplete = onComplete
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let image = info[.originalImage] as? UIImage else {
picker.dismiss(animated: true)
return
}
picker.dismiss(animated: true) {
self.onComplete(image)
}
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
}
}
Loading