|
| 1 | +// |
| 2 | +// URLRouter.swift |
| 3 | +// V2er |
| 4 | +// |
| 5 | +// Created by RichView on 2025/1/19. |
| 6 | +// Copyright © 2025 lessmore.io. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import SwiftUI |
| 11 | + |
| 12 | +/// URL Router for handling V2EX internal and external links |
| 13 | +/// Similar to Android's UrlInterceptor.java |
| 14 | +class URLRouter { |
| 15 | + |
| 16 | + // MARK: - URL Patterns |
| 17 | + |
| 18 | + private static let v2exHost = "www.v2ex.com" |
| 19 | + private static let v2exAltHost = "v2ex.com" |
| 20 | + |
| 21 | + /// Result of URL interception |
| 22 | + enum InterceptResult { |
| 23 | + case topic(id: String) // /t/123456 |
| 24 | + case node(name: String) // /go/swift |
| 25 | + case member(username: String) // /member/username |
| 26 | + case external(url: URL) // External URL |
| 27 | + case webview(url: URL) // Internal URL to open in webview |
| 28 | + case invalid // Invalid URL |
| 29 | + } |
| 30 | + |
| 31 | + // MARK: - URL Parsing |
| 32 | + |
| 33 | + /// Parse and classify URL |
| 34 | + /// - Parameter urlString: URL string to parse |
| 35 | + /// - Returns: InterceptResult indicating how to handle the URL |
| 36 | + static func parse(_ urlString: String) -> InterceptResult { |
| 37 | + guard !urlString.isEmpty else { |
| 38 | + return .invalid |
| 39 | + } |
| 40 | + |
| 41 | + var fullURL = urlString |
| 42 | + |
| 43 | + // Handle relative paths |
| 44 | + if urlString.hasPrefix("/") { |
| 45 | + fullURL = "https://\(v2exHost)\(urlString)" |
| 46 | + } else if !urlString.hasPrefix("http://") && !urlString.hasPrefix("https://") { |
| 47 | + fullURL = "https://\(v2exHost)/\(urlString)" |
| 48 | + } |
| 49 | + |
| 50 | + guard let url = URL(string: fullURL), |
| 51 | + let host = url.host else { |
| 52 | + return .invalid |
| 53 | + } |
| 54 | + |
| 55 | + // Check if it's V2EX domain |
| 56 | + let isV2EX = host.contains(v2exHost) || host.contains(v2exAltHost) |
| 57 | + |
| 58 | + if !isV2EX { |
| 59 | + // External URL - open in Safari or custom tabs |
| 60 | + return .external(url: url) |
| 61 | + } |
| 62 | + |
| 63 | + // Parse V2EX internal URLs |
| 64 | + let path = url.path |
| 65 | + |
| 66 | + // Topic: /t/123456 or /t/123456#reply123 |
| 67 | + if path.contains("/t/") { |
| 68 | + if let topicId = extractTopicId(from: path) { |
| 69 | + return .topic(id: topicId) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Node: /go/swift |
| 74 | + if path.contains("/go/") { |
| 75 | + if let nodeName = extractNodeName(from: path) { |
| 76 | + return .node(name: nodeName) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Member: /member/username |
| 81 | + if path.contains("/member/") { |
| 82 | + if let username = extractUsername(from: path) { |
| 83 | + return .member(username: username) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Other V2EX URLs - open in webview |
| 88 | + return .webview(url: url) |
| 89 | + } |
| 90 | + |
| 91 | + // MARK: - Extraction Helpers |
| 92 | + |
| 93 | + /// Extract topic ID from path like /t/123456 or /t/123456#reply123 |
| 94 | + private static func extractTopicId(from path: String) -> String? { |
| 95 | + let pattern = "/t/(\\d+)" |
| 96 | + guard let regex = try? NSRegularExpression(pattern: pattern), |
| 97 | + let match = regex.firstMatch(in: path, range: NSRange(path.startIndex..., in: path)), |
| 98 | + let range = Range(match.range(at: 1), in: path) else { |
| 99 | + return nil |
| 100 | + } |
| 101 | + return String(path[range]) |
| 102 | + } |
| 103 | + |
| 104 | + /// Extract node name from path like /go/swift |
| 105 | + private static func extractNodeName(from path: String) -> String? { |
| 106 | + let components = path.components(separatedBy: "/") |
| 107 | + guard let goIndex = components.firstIndex(of: "go"), |
| 108 | + goIndex + 1 < components.count else { |
| 109 | + return nil |
| 110 | + } |
| 111 | + return components[goIndex + 1] |
| 112 | + } |
| 113 | + |
| 114 | + /// Extract username from path like /member/username |
| 115 | + private static func extractUsername(from path: String) -> String? { |
| 116 | + let components = path.components(separatedBy: "/") |
| 117 | + guard let memberIndex = components.firstIndex(of: "member"), |
| 118 | + memberIndex + 1 < components.count else { |
| 119 | + return nil |
| 120 | + } |
| 121 | + return components[memberIndex + 1] |
| 122 | + } |
| 123 | + |
| 124 | + // MARK: - Navigation Helpers |
| 125 | + |
| 126 | + /// Get NavigationDestination from URL |
| 127 | + /// - Parameter urlString: URL string |
| 128 | + /// - Returns: Optional NavigationDestination |
| 129 | + static func destination(from urlString: String) -> NavigationDestination? { |
| 130 | + switch parse(urlString) { |
| 131 | + case .topic(let id): |
| 132 | + return .feedDetail(id: id) |
| 133 | + case .member(let username): |
| 134 | + return .userDetail(username: username) |
| 135 | + case .node(let name): |
| 136 | + return .tagDetail(name: name) |
| 137 | + default: |
| 138 | + return nil |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// MARK: - Navigation Destination |
| 144 | + |
| 145 | +/// Navigation destinations in the app |
| 146 | +enum NavigationDestination: Hashable { |
| 147 | + case feedDetail(id: String) |
| 148 | + case userDetail(username: String) |
| 149 | + case tagDetail(name: String) |
| 150 | +} |
| 151 | + |
| 152 | +// MARK: - UIApplication Extension |
| 153 | + |
| 154 | +extension UIApplication { |
| 155 | + /// Open URL with smart routing |
| 156 | + /// - Parameters: |
| 157 | + /// - url: URL to open |
| 158 | + /// - completion: Optional completion handler |
| 159 | + @MainActor |
| 160 | + func openURL(_ url: URL, completion: ((Bool) -> Void)? = nil) { |
| 161 | + let urlString = url.absoluteString |
| 162 | + let result = URLRouter.parse(urlString) |
| 163 | + |
| 164 | + switch result { |
| 165 | + case .external(let externalUrl): |
| 166 | + // Open external URLs in Safari |
| 167 | + open(externalUrl, options: [:], completionHandler: completion) |
| 168 | + |
| 169 | + case .webview(let webviewUrl): |
| 170 | + // For now, open in Safari |
| 171 | + // TODO: Implement in-app webview |
| 172 | + open(webviewUrl, options: [:], completionHandler: completion) |
| 173 | + |
| 174 | + default: |
| 175 | + // For topic, node, member URLs - should be handled by navigation |
| 176 | + // Fall back to Safari if not handled |
| 177 | + open(url, options: [:], completionHandler: completion) |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// MARK: - URL Testing Helpers |
| 183 | + |
| 184 | +#if DEBUG |
| 185 | +extension URLRouter { |
| 186 | + /// Test URL parsing (for debugging) |
| 187 | + static func test() { |
| 188 | + let testCases = [ |
| 189 | + "https://www.v2ex.com/t/123456", |
| 190 | + "https://v2ex.com/t/123456#reply123", |
| 191 | + "/t/123456", |
| 192 | + "https://www.v2ex.com/go/swift", |
| 193 | + "/go/swift", |
| 194 | + "https://www.v2ex.com/member/livid", |
| 195 | + "/member/livid", |
| 196 | + "https://www.google.com", |
| 197 | + "https://www.v2ex.com/about" |
| 198 | + ] |
| 199 | + |
| 200 | + for testCase in testCases { |
| 201 | + let result = parse(testCase) |
| 202 | + print("URL: \(testCase) -> \(result)") |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | +#endif |
0 commit comments