You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WatchOSPlayer/MusicPlayer Watch App/PlaybackView.swift

143 lines
5.4 KiB

//
// PlaybackView.swift
// MusicPlayer Watch App
//
// Created by BillSun on 3/18/23.
//
import SwiftUI
2 years ago
import UIKit
class AppearTimer : ObservableObject {
@Published var appear = false
var timeout = 0
let lock : NSLock = NSLock()
func appear(time: Int = 5, _appear: Bool = false) {
self.lock.lock()
self.timeout = self.timeout + time
self.appear = timeout > 0
DispatchQueue.main.asyncAfter(deadline: .now().advanced(by: .seconds(time)),
execute: {
self.lock.lock()
self.timeout -= time
self.appear = self.timeout > 0
self.lock.unlock()
}
)
self.lock.unlock()
}
}
2 years ago
struct PlaybackView: View {
//var music : TrackInfo? = nil
2 years ago
var parent : ContentView? = nil
//@ObservedObject var timeout = Timeout(timeout: 5)
var title = ""
2 years ago
@State var playing = true
@State private var appearSelf = 3
2 years ago
@ObservedObject var appearTimer = AppearTimer()
@ObservedObject var trackInfo : TrackInfo = TrackInfo()
2 years ago
var body: some View {
if trackInfo.m != nil {
2 years ago
GeometryReader { geo in
ZStack {
if(trackInfo.art == nil) {
Image(systemName: "music.note")
2 years ago
.resizable()
.scaledToFit()
.foregroundColor(.white)
.frame(width: geo.size.width*0.84, height: geo.size.height*0.84)
.padding(.leading, geo.size.width*0.08)
.padding(.top, geo.size.height*0.08)
2 years ago
}
else {
trackInfo.art!.resizable().scaledToFill()
2 years ago
}
if (appearTimer.appear)
2 years ago
{
VStack {
HStack {
Button {
if ( parent!.player.timeControlStatus == .playing ) {
parent!.player.pause()
self.playing = false
} else {
parent!.player.audiovisualBackgroundPlaybackPolicy = .continuesIfPossible
parent!.player.play()
self.playing = true
}
} label: {
(
self.playing ?
Image(systemName: "stop") :
Image(systemName: "play")
)
.resizable()
.scaledToFit()
.frame(width: geo.size.width/5.5)
}.background(Color(red: 0,green: 0,blue: 0,opacity: 0.2))
.frame(width: geo.size.width/2.5)
.cornerRadius(90, antialiased: true)
.foregroundColor(.white)
.opacity(1)
.buttonStyle(.plain)
Button {
let curr = parent!.player.currentItem
parent!.player.advanceToNextItem()
curr!.seek(to: .zero)
parent!.player.play()
self.playing = true
} label : {
Image(systemName: "chevron.forward")
2 years ago
.resizable()
.scaledToFit()
.frame(width: geo.size.width/7, height: geo.size.height/7)
}.background(Color.clear)
.clipShape(Circle())
.foregroundColor(.white)
.frame(width: geo.size.width/4, height: geo.size.height/4)
.padding(0)
.opacity(1)
.buttonStyle(.plain)
}
}
2 years ago
}
}.onTapGesture {
appearTimer.appear()
}
}.navigationBarBackButtonHidden(false)
.toolbar(.visible, for: .navigationBar)
.onAppear() {
appearTimer.appear(time: 3, _appear: true)
2 years ago
}
}
2 years ago
}
2 years ago
init() { }
init(parent:ContentView, music: TrackInfo? = nil) {
2 years ago
if music != nil && music!.art != nil {
self.parent = parent
self.playing = parent.player.timeControlStatus == .playing
2 years ago
}
}
2 years ago
mutating func update (music: TrackInfo) {
self.trackInfo.from(other: music)
2 years ago
self.title = music.s
self.playing = self.parent!.player.timeControlStatus == .playing
2 years ago
}
}
struct PlaybackView_Previews: PreviewProvider {
static var previews: some View {
PlaybackView()
}
}