OpenTok / assign5 / PlayerView.swift
PlayerView.swift
Raw
//
//  PlayerView.swift
//  assign5
//
//  Created by Jason Kim on 5/12/22.
//

import SwiftUI
import AVKit
import Firebase
import FirebaseDatabase

struct PlayerView: View {
    @State var urls: [String] = []
    @State var desc: [String] = []
    @State var likes: [Int] = []
    @State var ids: [String] = []
    @State private var count = 0
    @State private var curr = 0
    @State private var isPresented = false
    
    func presslike() {
        if ids.count != 0 {
            likes[count] = likes[count] + 1
            Database.database().reference().child("urls").child(ids[count]).updateChildValues(["likes": likes[count], "name": desc[count], "url":urls[count]])
        }
    }
    
    func seen() {
        Database.database().reference().child("seen").child("jason").updateChildValues([(ids.count == 0 ? "placeholder" : ids[count]): 1])
    }
    
    func refresh() {
        count = 0
        ids = []
        urls = []
        desc = []
        Database.database().reference(withPath: "urls").observeSingleEvent(of: .value, with: { snapshot in
            if !snapshot.exists() { return }
            for child in snapshot.children{
                let temp = child as! DataSnapshot
                let id = temp.key
                let name = (temp.value as! NSDictionary)["name"] as! String
                let url = (temp.value as! NSDictionary)["url"] as! String
                let like = (temp.value as! NSDictionary)["likes"] as! Int
              //  likes.append(like)
                likes.insert(like, at: 0)
              //  desc.append(name)
                desc.insert(name, at: 0)
              //  urls.append(url)
                urls.insert(url, at: 0)
              //  ids.append(id)
                ids.insert(id, at: 0)
                seen()
            }
        })
    }

    var body: some View {
        VStack{
            Text(desc.count == 0 ? "Placeholder" : desc[count]).padding().font(.largeTitle)
            VideoPlayer(player: AVPlayer(url: URL(string: urls.count == 0 ? "https://jacobsm.com/geedryve/mpegs/Bill%20Gates%20Win%2098%20crash.mp4" : urls[count])!))
            HStack{
                Button("\({Image(systemName: "heart")}())"){
                    presslike()
                    isPresented = true
                }.font(.largeTitle)
                    .alert(isPresented: $isPresented){
                        Alert(title: Text("Liked!"))
                    }
                Text(":  \(likes.count == 0 ? "0" : String(likes[count]))").padding().font(.largeTitle)
            }
            Button("Update", action: refresh).frame(width: 100, height: 20, alignment: .center)
                .padding()
                .background(Color.green)
                .foregroundColor(Color.white)
                .cornerRadius(15)
            HStack{
                Button("Prev", action: {
                    if count == 0 {
                        count = urls.count - 1
                    } else {
                        count = count - 1
                    }
                    seen()
                }).font(.title2)
                    .foregroundColor(.white)
                    .frame(width: 100, height: 20)
                    .padding(15)
                    .background(Color.blue)
                    .cornerRadius(15)
                Button("Next", action: {
                    if count == (urls.count - 1) {
                        count = 0
                    } else {
                        count = count + 1
                    }
                    seen()
                }).font(.title2)
                    .foregroundColor(.white)
                    .frame(width: 100, height: 20)
                    .padding(15)
                    .background(Color.blue)
                    .cornerRadius(15)
            }
        }.gesture(DragGesture(minimumDistance: 50, coordinateSpace: .local).onEnded({
                position in
                if position.location.x - position.startLocation.x > 50 {
                    if count == 0{
                        count = urls.count - 1
                    } else {
                        count = count - 1
                    }
                    seen()
                } else if position.startLocation.x - position.location.x > 50{
                    if count == (urls.count - 1) {
                        count = 0
                    } else {
                        count = count + 1
                    }
                    seen()
                }
            }))
        .onAppear(perform: refresh)
    }
}

struct PlayerView_Previews: PreviewProvider {
    static var previews: some View {
        PlayerView()
    }
}