-
Notifications
You must be signed in to change notification settings - Fork 48
feat: Display online user count in pull-to-refresh view #56
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // | ||
| // OnlineStatsInfo.swift | ||
| // V2er | ||
| // | ||
| // Created by ghui on 2025/10/18. | ||
| // Copyright © 2025 lessmore.io. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
| import SwiftSoup | ||
|
|
||
| public struct OnlineStatsInfo: BaseModel, Codable { | ||
| var rawData: String? | ||
| var onlineCount: Int = 0 | ||
| var maxRecord: Int = 0 | ||
|
|
||
| init() {} | ||
|
|
||
| enum CodingKeys: String, CodingKey { | ||
| case onlineCount, maxRecord | ||
| } | ||
|
|
||
| init?(from html: Element?) { | ||
| guard let root = html else { | ||
| log("OnlineStatsInfo: root element is nil") | ||
| return nil | ||
| } | ||
|
|
||
| // Parse from footer HTML | ||
| // Structure: <strong>... 2576 人在线</strong> <span class="fade">最高记录 6679</span> | ||
|
|
||
| // Get all text content from the page | ||
| let pageText = root.value(.text) | ||
|
|
||
| // Extract online count using simple pattern matching | ||
| // Pattern: "数字 人在线" | ||
| let onlinePattern = "(\\d+)\\s*人在线" | ||
| if let regex = try? NSRegularExpression(pattern: onlinePattern) { | ||
| let nsText = pageText as NSString | ||
| let matches = regex.matches(in: pageText, range: NSRange(location: 0, length: nsText.length)) | ||
| if let match = matches.first, match.numberOfRanges > 1 { | ||
| let numberStr = nsText.substring(with: match.range(at: 1)) | ||
| onlineCount = Int(numberStr.replacingOccurrences(of: ",", with: "")) ?? 0 | ||
| log("OnlineStatsInfo: Found online count = \(onlineCount)") | ||
| } | ||
| } | ||
|
|
||
| // Extract max record | ||
| let maxPattern = "最高记录\\s+(\\d+)" | ||
| if let regex = try? NSRegularExpression(pattern: maxPattern) { | ||
| let nsText = pageText as NSString | ||
| let matches = regex.matches(in: pageText, range: NSRange(location: 0, length: nsText.length)) | ||
| if let match = matches.first, match.numberOfRanges > 1 { | ||
| let numberStr = nsText.substring(with: match.range(at: 1)) | ||
| maxRecord = Int(numberStr.replacingOccurrences(of: ",", with: "")) ?? 0 | ||
| log("OnlineStatsInfo: Found max record = \(maxRecord)") | ||
| } | ||
| } | ||
|
|
||
| // If we didn't find the data, return nil | ||
| if onlineCount == 0 { | ||
| log("OnlineStatsInfo: Parse failed, onlineCount = 0") | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| private func extractNumber(before keyword: String, from text: String) -> Int? { | ||
| let components = text.components(separatedBy: keyword) | ||
| guard let firstPart = components.first else { return nil } | ||
|
|
||
| // Extract the last number from the text before keyword | ||
| let pattern = "(\\d+)\\s*$" | ||
| guard let regex = try? NSRegularExpression(pattern: pattern) else { return nil } | ||
|
|
||
| let nsText = firstPart as NSString | ||
| let matches = regex.matches(in: firstPart, range: NSRange(location: 0, length: nsText.length)) | ||
|
|
||
| if let match = matches.last, match.numberOfRanges > 1 { | ||
| let numberStr = nsText.substring(with: match.range(at: 1)) | ||
| return Int(numberStr.replacingOccurrences(of: ",", with: "")) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| private func extractNumber(after keyword: String, from text: String) -> Int? { | ||
| let components = text.components(separatedBy: keyword) | ||
| guard components.count > 1 else { return nil } | ||
|
|
||
| let afterPart = components[1] | ||
|
|
||
| // Extract the first number from the text after keyword | ||
| let pattern = "(\\d+)" | ||
| guard let regex = try? NSRegularExpression(pattern: pattern) else { return nil } | ||
|
|
||
| let nsText = afterPart as NSString | ||
| let matches = regex.matches(in: afterPart, range: NSRange(location: 0, length: nsText.length)) | ||
|
|
||
| if let match = matches.first, match.numberOfRanges > 1 { | ||
| let numberStr = nsText.substring(with: match.range(at: 1)) | ||
| return Int(numberStr.replacingOccurrences(of: ",", with: "")) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func isValid() -> Bool { | ||
| return onlineCount > 0 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,8 +44,10 @@ struct FeedPage: BaseHomePageView { | |||||
| } | ||||||
| } | ||||||
| } | ||||||
| .updatable(autoRefresh: state.showProgressView, hasMoreData: state.hasMoreData, max(state.scrollToTop, scrollTop(tab: .feed))) { | ||||||
| .updatable(autoRefresh: state.showProgressView, hasMoreData: state.hasMoreData, max(state.scrollToTop, scrollTop(tab: .feed)), onlineStats: state.onlineStats) { | ||||||
| if AccountState.hasSignIn() { | ||||||
| // Fetch online stats in parallel with feed data | ||||||
| async let _ = run(action: FeedActions.FetchOnlineStats.Start()) | ||||||
|
||||||
| async let _ = run(action: FeedActions.FetchOnlineStats.Start()) | |
| Task { await run(action: FeedActions.FetchOnlineStats.Start()) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
extractNumber(before:)andextractNumber(after:)methods are defined but not used in the current implementation. These methods create unnecessary code complexity and should be removed since the parsing logic uses direct regex patterns instead.