Skip to content

Commit

Permalink
Updated for continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowsMEMZ committed Jul 23, 2024
1 parent 9a43584 commit 2c120c9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
45 changes: 45 additions & 0 deletions Sources/DarockKit/DarockKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Foundation
import Alamofire
import SwiftyJSON

private let void: Void = ()

public class DarockKit {
//MARK: Network
public class Network {
Expand Down Expand Up @@ -46,6 +48,43 @@ public class DarockKit {
}
}

open func requestJSON(_ convertible: URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil, interceptor: RequestInterceptor? = nil, requestModifier: Session.RequestModifier? = nil) async -> Result<JSON, Void> {
await withCheckedContinuation { continuation in
let convertible = RequestConvertible(url: convertible, method: method, parameters: parameters, encoding: encoding, headers: headers, requestModifier: requestModifier)
AF.request(convertible).responseData { response in
let data = response.data
if data != nil {
do {
let json = try JSON(data: data!)
continuation.resume(returning: .success(json))
} catch {
continuation.resume(returning: .failure(void))
}
} else {
continuation.resume(returning: .failure(void))
}
}
}
}
open func requestString(_ convertible: URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil, interceptor: RequestInterceptor? = nil, requestModifier: Session.RequestModifier? = nil) async -> Result<String, Void> {
await withCheckedContinuation { continuation in
let convertible = RequestConvertible(url: convertible, method: method, parameters: parameters, encoding: encoding, headers: headers, requestModifier: requestModifier)
AF.request(convertible).responseData { response in
let data = response.data
if data != nil {
let str = String(data: data!, encoding: .utf8)
if str != nil {
continuation.resume(returning: .success(str!))
} else {
continuation.resume(returning: .failure(void))
}
} else {
continuation.resume(returning: .failure(void))
}
}
}
}

private struct RequestConvertible: URLRequestConvertible {
let url: URLConvertible
let method: HTTPMethod
Expand Down Expand Up @@ -128,3 +167,9 @@ public protocol AlertIconAnimatable {
func animate()
}
#endif

@frozen
public enum Result<Success, Failure> {
case success(Success)
case failure(Failure)
}
37 changes: 32 additions & 5 deletions Sources/DarockKit/UIExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
import WatchKit
#endif

@available(*, deprecated, renamed: "View.centerAligned", message: "`CenterAlign` modifier was deprecated")
public struct CenterAlign: ViewModifier {
public func body(content: Content) -> some View {
HStack {
Expand All @@ -20,14 +21,31 @@ public struct CenterAlign: ViewModifier {
}
}
}
public extension View {
func centerAligned() -> some View {
HStack {
Spacer()
self
Spacer()
}
}
}

@available(*, deprecated, renamed: "View.noAutoInput", message: "`NoAutoInput` modifier was deprecated")
public struct NoAutoInput: ViewModifier {
public func body(content: Content) -> some View {
content
.autocorrectionDisabled()
.textInputAutocapitalization(.never)
}
}
public extension View {
func noAutoInput() -> some View {
self
.autocorrectionDisabled()
.textInputAutocapitalization(.never)
}
}

#if !os(visionOS) && !os(tvOS)
//MARK: Neumorphic
Expand Down Expand Up @@ -297,20 +315,31 @@ import WebKit

public struct WebView: UIViewRepresentable {
let url: URL

let builder: ((WKWebView) -> WKWebView)?

public init(url: URL) {
self.url = url
self.builder = nil
}
public init(url: URL, builder: @escaping (WKWebView) -> WKWebView) {
self.url = url
self.builder = builder
}

public func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
var webView = WKWebView()
if let builder {
webView = builder(webView)
}
webView.load(URLRequest(url: url))
return webView
}

public func updateUIView(_ uiView: WKWebView, context: Context) {}
public func updateUIView(_ uiView: WKWebView, context: Context) { }
}
#endif

#if os(iOS)
public struct TextSelectView: View {
var text: String

Expand Down Expand Up @@ -341,9 +370,7 @@ public struct CopyableView<V: View>: View {
.contextMenu {
Button(action: {
UIPasteboard.general.string = content
#if !os(visionOS)
AlertKitAPI.present(title: "已复制", subtitle: "简介内容已复制到剪贴板", icon: .done, style: .iOS17AppleMusic, haptic: .success)
#endif
}, label: {
Label("复制", systemImage: "doc.on.doc")
})
Expand Down

0 comments on commit 2c120c9

Please sign in to comment.