feat: add live listener count support
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
F12B73452C55505E0064A230 /* NeuButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F12B73442C55505E0064A230 /* NeuButton.swift */; };
|
||||
F12B73472C55AC6B0064A230 /* PlaybackManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F12B73462C55AC6B0064A230 /* PlaybackManager.swift */; };
|
||||
F12B73492C55C0C40064A230 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = F12B73482C55C0C40064A230 /* .gitignore */; };
|
||||
F12B734B2C56AEB00064A230 /* LiveStatusManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F12B734A2C56AEB00064A230 /* LiveStatusManager.swift */; };
|
||||
F12B734D2C5702EC0064A230 /* LiveListenerCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F12B734C2C5702EC0064A230 /* LiveListenerCounter.swift */; };
|
||||
F13839702C51BABD00B4814F /* InfinifiIOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = F138396F2C51BABD00B4814F /* InfinifiIOSApp.swift */; };
|
||||
F13839722C51BABD00B4814F /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F13839712C51BABD00B4814F /* ContentView.swift */; };
|
||||
F13839742C51BABE00B4814F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F13839732C51BABE00B4814F /* Assets.xcassets */; };
|
||||
@@ -20,6 +22,8 @@
|
||||
F12B73442C55505E0064A230 /* NeuButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NeuButton.swift; sourceTree = "<group>"; };
|
||||
F12B73462C55AC6B0064A230 /* PlaybackManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaybackManager.swift; sourceTree = "<group>"; };
|
||||
F12B73482C55C0C40064A230 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = SOURCE_ROOT; };
|
||||
F12B734A2C56AEB00064A230 /* LiveStatusManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveStatusManager.swift; sourceTree = "<group>"; };
|
||||
F12B734C2C5702EC0064A230 /* LiveListenerCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveListenerCounter.swift; sourceTree = "<group>"; };
|
||||
F138396C2C51BABD00B4814F /* InfinifiIOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = InfinifiIOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
F138396F2C51BABD00B4814F /* InfinifiIOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfinifiIOSApp.swift; sourceTree = "<group>"; };
|
||||
F13839712C51BABD00B4814F /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
||||
@@ -64,6 +68,8 @@
|
||||
F12B73442C55505E0064A230 /* NeuButton.swift */,
|
||||
F12B73462C55AC6B0064A230 /* PlaybackManager.swift */,
|
||||
F12B73482C55C0C40064A230 /* .gitignore */,
|
||||
F12B734A2C56AEB00064A230 /* LiveStatusManager.swift */,
|
||||
F12B734C2C5702EC0064A230 /* LiveListenerCounter.swift */,
|
||||
);
|
||||
path = InfinifiIOS;
|
||||
sourceTree = "<group>";
|
||||
@@ -147,7 +153,9 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
F12B734B2C56AEB00064A230 /* LiveStatusManager.swift in Sources */,
|
||||
F12B73452C55505E0064A230 /* NeuButton.swift in Sources */,
|
||||
F12B734D2C5702EC0064A230 /* LiveListenerCounter.swift in Sources */,
|
||||
F13839722C51BABD00B4814F /* ContentView.swift in Sources */,
|
||||
F12B73472C55AC6B0064A230 /* PlaybackManager.swift in Sources */,
|
||||
F13839702C51BABD00B4814F /* InfinifiIOSApp.swift in Sources */,
|
||||
|
@@ -2,7 +2,7 @@ import AVKit
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@ObservedObject private var playbackManager = PlaybackManager()
|
||||
@StateObject private var playbackManager = PlaybackManager()
|
||||
|
||||
private func toggleAudioPlayback() {
|
||||
switch playbackManager.playbackState {
|
||||
@@ -42,6 +42,10 @@ struct ContentView: View {
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
LiveListenerCounter(
|
||||
playbackManager: playbackManager
|
||||
)
|
||||
}
|
||||
.frame(
|
||||
maxWidth: .infinity,
|
||||
|
25
InfinifiIOS/LiveListenerCounter.swift
Normal file
25
InfinifiIOS/LiveListenerCounter.swift
Normal file
@@ -0,0 +1,25 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct LiveListenerCounter: View {
|
||||
@StateObject private var liveStatusManager: LiveStatusManager
|
||||
|
||||
init(playbackManager: PlaybackManager) {
|
||||
_liveStatusManager = StateObject(wrappedValue: LiveStatusManager(playbackManager: playbackManager))
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
let text = if liveStatusManager.listenerCount >= 0 && liveStatusManager.listenerCount <= 1 {
|
||||
"\(liveStatusManager.listenerCount) person tuned in"
|
||||
} else if liveStatusManager.listenerCount > 1 {
|
||||
"\(liveStatusManager.listenerCount) ppl tuned in"
|
||||
} else {
|
||||
"connecting"
|
||||
}
|
||||
|
||||
Text(text)
|
||||
.font(.system(.body, design: .monospaced))
|
||||
.padding()
|
||||
.opacity(0.8)
|
||||
}
|
||||
}
|
75
InfinifiIOS/LiveStatusManager.swift
Normal file
75
InfinifiIOS/LiveStatusManager.swift
Normal file
@@ -0,0 +1,75 @@
|
||||
import Combine
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
class LiveStatusManager: ObservableObject {
|
||||
@Published var listenerCount: Int = -1
|
||||
|
||||
@ObservedObject private var playbackManager: PlaybackManager
|
||||
private var ws: URLSessionWebSocketTask?
|
||||
private var playbackStateObserver: AnyCancellable?
|
||||
|
||||
init(playbackManager: PlaybackManager) {
|
||||
self.playbackManager = playbackManager
|
||||
playbackStateObserver = playbackManager.$playbackState.sink { playbackState in
|
||||
self.onPlaybackStateChanged(playbackState)
|
||||
}
|
||||
connectToWebSocket()
|
||||
}
|
||||
|
||||
func connectToWebSocket() {
|
||||
guard let url = URL(string: "wss://infinifi.cafe/ws") else {
|
||||
return
|
||||
}
|
||||
|
||||
let req = URLRequest(url: url)
|
||||
let ws = URLSession.shared.webSocketTask(with: req)
|
||||
ws.resume()
|
||||
|
||||
self.ws = ws
|
||||
receiveWebSocketMessage()
|
||||
}
|
||||
|
||||
private func receiveWebSocketMessage() {
|
||||
ws?.receive { result in
|
||||
switch result {
|
||||
#if DEBUG
|
||||
case .failure(let err):
|
||||
print("failed to received \(err)")
|
||||
#endif
|
||||
case .success(.string(let msg)):
|
||||
self.handleWebSocketMessage(msg)
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
self.receiveWebSocketMessage()
|
||||
}
|
||||
}
|
||||
|
||||
private func handleWebSocketMessage(_ msg: String) {
|
||||
guard let count = Int(msg) else {
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.listenerCount = count
|
||||
}
|
||||
}
|
||||
|
||||
private func onPlaybackStateChanged(_ playbackState: PlaybackState) {
|
||||
guard let ws = ws else {
|
||||
return
|
||||
}
|
||||
|
||||
switch playbackState {
|
||||
case .paused:
|
||||
ws.send(.string("paused")) { _ in }
|
||||
case .playing:
|
||||
ws.send(.string("playing")) { _ in }
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user