Skip to content

Commit

Permalink
下沉网络能力
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Apr 23, 2024
1 parent cb5e046 commit 23b3fe2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 49 deletions.
43 changes: 0 additions & 43 deletions SwiftPamphletApp/Core/FundationFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,49 +43,6 @@ func howLongFromNow(timeStr: String) -> String {
return howLongAgo(date: date)
}

// MARK: - 网络


// 跳到浏览器中显示网址内容
func gotoWebBrowser(urlStr: String) {
if !urlStr.isEmpty {
let validUrlStr = validHTTPUrlStrFromUrlStr(urlStr: urlStr)
NSWorkspace.shared.open(URL(string: validUrlStr)!)
} else {
print("error: url is empty!")
}
}

// 检查地址是否有效
func validHTTPUrlStrFromUrlStr(urlStr: String) -> String {
let httpPrefix = "http://"
let httpsPrefix = "https://"
if (urlStr.hasPrefix(httpPrefix) || urlStr.hasPrefix(httpsPrefix)) {
return urlStr
}
return httpsPrefix + urlStr
}

// 网页的相对地址转绝对地址
func urlWithSchemeAndHost(url: URL, urlStr: String) -> String {
var schemeStr = ""
var hostStr = ""
if let scheme = url.scheme, let host = url.host {
schemeStr = scheme
hostStr = host
}

if urlStr.hasPrefix("http") {
return urlStr
} else {
var slash = ""
if urlStr.hasPrefix("/") == false {
slash = "/"
}
return "\(schemeStr)://\(hostStr)\(slash)\(urlStr)"
}
}


// MARK: - 基础
// decoder
Expand Down
7 changes: 4 additions & 3 deletions SwiftPamphletApp/InfoOrganizer/Info/EditInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Ink
import PhotosUI
import InfoOrganizer
import SMFile
import SMNetwork

struct EditInfoView: View {
@Environment(\.modelContext) var modelContext
Expand Down Expand Up @@ -383,7 +384,7 @@ struct EditInfoView: View {
Text("解析")
}
Button {
gotoWebBrowser(urlStr: info.url)
SMNetwork.gotoWebBrowser(urlStr: info.url)
} label: {
Image(systemName: "safari")
}
Expand Down Expand Up @@ -519,7 +520,7 @@ struct EditInfoView: View {
for elm in imgs {
if let elmUrl = try? elm.attr("src") {
if elmUrl.isEmpty == false {
imageUrls.append(urlWithSchemeAndHost(url: url, urlStr: elmUrl))
imageUrls.append(SMNetwork.urlWithSchemeAndHost(url: url, urlStr: elmUrl))
}
}
}
Expand All @@ -531,7 +532,7 @@ struct EditInfoView: View {
imgUrl = imageUrls.first
}
if let okImgUrl = imgUrl {
imageUrl = urlWithSchemeAndHost(url: url, urlStr: okImgUrl)
imageUrl = SMNetwork.urlWithSchemeAndHost(url: url, urlStr: okImgUrl)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book


import Foundation
import Observation
import Network
#if os(macOS)
import AppKit
#endif

public class SMNetwork {
// 跳到浏览器中显示网址内容
public static func gotoWebBrowser(urlStr: String) {
if !urlStr.isEmpty {
let validUrlStr = SMNetwork.validHTTPUrlStrFromUrlStr(urlStr: urlStr)
#if os(macOS)
NSWorkspace.shared.open(URL(string: validUrlStr)!)
#endif
} else {
print("error: url is empty!")
}
}

// 检查地址是否有效
public static func validHTTPUrlStrFromUrlStr(urlStr: String) -> String {
let httpPrefix = "http://"
let httpsPrefix = "https://"
if (urlStr.hasPrefix(httpPrefix) || urlStr.hasPrefix(httpsPrefix)) {
return urlStr
}
return httpsPrefix + urlStr
}

// 网页的相对地址转绝对地址
public static func urlWithSchemeAndHost(url: URL, urlStr: String) -> String {
var schemeStr = ""
var hostStr = ""
if let scheme = url.scheme, let host = url.host {
schemeStr = scheme
hostStr = host
}

if urlStr.hasPrefix("http") {
return urlStr
} else {
var slash = ""
if urlStr.hasPrefix("/") == false {
slash = "/"
}
return "\(schemeStr)://\(hostStr)\(slash)\(urlStr)"
}
}
}

@Observable
public final class NetworkMonitor {
Expand Down
5 changes: 3 additions & 2 deletions SwiftPamphletApp/ViewComponet/ViewComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI
import WebKit
import MarkdownUI
import SMFile
import SMNetwork

// MARK: - 大纲
struct SPOutlineListView<D, Content>: View where D: RandomAccessCollection, D.Element: Identifiable, Content: View {
Expand Down Expand Up @@ -298,9 +299,9 @@ struct ButtonGoGitHubWeb: View {
var body: some View {
Button {
if ignoreHost == true {
gotoWebBrowser(urlStr: SPC.githubHost + url)
SMNetwork.gotoWebBrowser(urlStr: SPC.githubHost + url)
} else {
gotoWebBrowser(urlStr: url)
SMNetwork.gotoWebBrowser(urlStr: url)
}
} label: {
if bold == true {
Expand Down

0 comments on commit 23b3fe2

Please sign in to comment.