Skip to content

Commit

Permalink
Merge pull request #70 from Aayush9029/PR69v2
Browse files Browse the repository at this point in the history
Pr69v2
  • Loading branch information
Aayush9029 authored Dec 23, 2022
2 parents 7dd6d8d + e3317b7 commit 3aa8f4b
Show file tree
Hide file tree
Showing 37 changed files with 417 additions and 905 deletions.
125 changes: 61 additions & 64 deletions NativeYoutube.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
{
"identity" : "sdwebimageswiftui",
"kind" : "remoteSourceControl",
"location" : "https://github.com/SDWebImage/SDWebImageSwiftUI.git",
"location" : "https://github.com/SDWebImage/SDWebImageSwiftUI",
"state" : {
"branch" : "master",
"revision" : "ed288667c909c89127ab1b690113a3e397af3098"
"revision" : "ed288667c909c89127ab1b690113a3e397af3098",
"version" : "2.2.1"
}
},
{
Expand All @@ -28,12 +28,12 @@
}
},
{
"identity" : "youtubeplayerkit",
"identity" : "youtubekit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/SvenTiigi/YouTubePlayerKit.git",
"location" : "https://github.com/alexeichhorn/YouTubeKit",
"state" : {
"revision" : "1f78a2121f58b393bca54bc0a8da9e9b11cab198",
"version" : "1.1.9"
"revision" : "7dcbb57b39a58dd5c208c40665383de0934e1ce7",
"version" : "0.1.6"
}
}
],
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion NativeYoutube/API/PlaylistsRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import SwiftyJSON

struct PlaylistsRequest: Request {
struct PlaylistsRequest {
let apiKey: String
let playListID: String
let maxResults: String
Expand Down
16 changes: 0 additions & 16 deletions NativeYoutube/API/Request.swift

This file was deleted.

2 changes: 1 addition & 1 deletion NativeYoutube/API/SearchRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import SwiftyJSON

struct SearchRequest: Request {
struct SearchRequest {
let apiKey: String
let query: String
let maxResults: String
Expand Down
15 changes: 0 additions & 15 deletions NativeYoutube/Assets.xcassets/StatusBarIcon.imageset/Contents.json

This file was deleted.

Binary file not shown.
2 changes: 1 addition & 1 deletion NativeYoutube/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct ContentView: View {
}
BottomBarView(currentPage: $currentPage)
}
.frame(width: 380.0)
.frame(width: 360.0)
}
}

Expand Down
14 changes: 0 additions & 14 deletions NativeYoutube/Extensions/Collections+Extension.swift

This file was deleted.

29 changes: 0 additions & 29 deletions NativeYoutube/Extensions/String+Time.swift

This file was deleted.

14 changes: 0 additions & 14 deletions NativeYoutube/Extensions/URL+Extension.swift

This file was deleted.

19 changes: 13 additions & 6 deletions NativeYoutube/Extensions/View+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,43 @@ import Cocoa
import SwiftUI

extension View {
private func newWindowInternal(with title: String, isTransparent: Bool = false) -> NSWindow {
private func newWindowInternal(with title: String, isTransparent: Bool = false, appState: AppStateViewModel? = nil) -> NSWindow {
let window = KeyWindow(
contentRect: NSRect(x: 20, y: 20, width: 640, height: 360),
contentRect: NSRect(x: 20, y: 20, width: 480, height: 270),
styleMask: [.titled, .closable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false
)
window.appState = appState

window.makeKey()
window.isReleasedWhenClosed = false
window.title = title
window.makeKeyAndOrderFront(self)
window.level = .floating
window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .stationary]
// Failed attempt to make the window sticks at the right aspect ratio, keeping it in comment as it should work (see https://developer.apple.com/documentation/appkit/nswindow/1419507-aspectratio)
// window.aspectRatio = NSMakeSize(16.0, 9.0)
if isTransparent {
window.backgroundColor = .clear
window.isOpaque = false
window.styleMask = [.hudWindow, .closable, .resizable]
window.isMovableByWindowBackground = true
window.makeKeyAndOrderFront(self)
}
window.setIsVisible(true)
return window
}

func openNewWindow(with title: String = "New Window", isTransparent: Bool = false) {
let window = newWindowInternal(with: title, isTransparent: isTransparent)
func openNewWindow(with title: String = "New Window", isTransparent: Bool = false, appState: AppStateViewModel? = nil) {
let window = newWindowInternal(with: title, isTransparent: isTransparent, appState: appState)
window.contentView = NSHostingView(rootView: self)
NSApp.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(self)
}
}

extension View {
func playVideo(url: URL, appState: AppStateViewModel) {
PopupPlayerView(appStateViewModel: appState, videoURL: url)
.openNewWindow(isTransparent: true, appState: appState)
}
}
4 changes: 4 additions & 0 deletions NativeYoutube/Helpers/KeyWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import AppKit

class KeyWindow: NSWindow {
var appState: AppStateViewModel?
override var canBecomeKey: Bool {
return true
}
Expand All @@ -18,6 +19,9 @@ class KeyWindow: NSWindow {
extension KeyWindow {
override func keyDown(with event: NSEvent) {
if event.modifierFlags.intersection(.deviceIndependentFlagsMask) == .command && event.charactersIgnoringModifiers == "w" {
if self.appState != nil {
self.appState!.stopPlaying()
}
close()
return
} else {
Expand Down
24 changes: 5 additions & 19 deletions NativeYoutube/Models/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,9 @@ struct Constants {
static let defaultPlaylistID: String = "PLFgquLnL59alKyN8i_z5Ofm_h0KthT072"
}

enum StatusStates: String {
case starting = "Starting.."
case badOuath = "Incorrect Access Token"
case badClient = "Incorrect Client ID"
case badScopes = "BAD Scopes (Client ID / Access Token)"
case userValidating = "Validating User"
case userValidated = "User Validated"
case userLoading = "Loading User Data"
case userLoaded = "Got User Data"
case videoLoading = "Loading videos"
case videoLoaded = "View Has Been Loaded"
}

enum AppStorageStrings: String {
case apiKey = "Api Key"
case playListID = "Playlist ID for your goto playlist"

// iina plus works much better, this could potentially replace youtubeplayerkit
case useIINA = "Use IINA Plus"
enum AppStorageStrings {
static let apiKey = "Api Key"
static let playListID = "Playlist ID for your goto playlist"
static let videoClickBehaviour = "com.aayush.nativeyoutube.videoClickBehaviour"
static let useIINA = "com.aayush.nativeyoutube.useIINA"
}
18 changes: 18 additions & 0 deletions NativeYoutube/Models/Enums.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Enums.swift
// NativeYoutube
//
// Created by Adélaïde Sky on 23/12/2022.
//

import Foundation

// Didn't knew where to put it so i created this file lol

// All behaviours available on double clicking a video element.
enum VideoClickBehaviour: String, CaseIterable {
case nothing = "Do Nothing"
case playVideo = "Play Video"
case openOnYoutube = "Open on Youtube"
case playInIINA = "Play Using IINA"
}
12 changes: 0 additions & 12 deletions NativeYoutube/Models/VideoModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ struct VideoModel {
channelTitle: "OliviaRodrigoVEVO"
)

var cleanTitle: String {
var title = String(title.split(separator: "(")[0])

if title.split(separator: "-").count > 1 {
title = String(title.split(separator: "-")[1])
}
if title.split(separator: ":").count > 1 {
title = String(title.split(separator: ":")[1])
}
return title
}

enum VideoKind: String {
case playlist = "youtube#playlistItem"
case search = "youtube#searchResult"
Expand Down
12 changes: 1 addition & 11 deletions NativeYoutube/NativeYoutubeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@ import SwiftUI
@main
struct NativeYoutubeApp: App {
@StateObject private var appStateViewModel = AppStateViewModel()
@StateObject private var youtubePlayerViewModel = YoutubePlayerViewModel()
@StateObject private var searchViewModel = SearchViewModel()
var body: some Scene {
MenuBarExtra("Native Youtube", systemImage: "play.circle") {
MenuBarExtra("Native Youtube", systemImage: "play.rectangle.fill") {
ContentView()
.frame(width: 360, height: 512)
.environmentObject(appStateViewModel)
.environmentObject(youtubePlayerViewModel)
.environmentObject(searchViewModel)
}
.menuBarExtraStyle(WindowMenuBarExtraStyle())

Settings {
PreferencesView()
.padding(-12)
.frame(minWidth: 320, minHeight: 512)
.environmentObject(appStateViewModel)
}
.windowStyle(.hiddenTitleBar)
}
}
37 changes: 11 additions & 26 deletions NativeYoutube/ViewModels/AppStateViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,30 @@ import SwiftUI
class AppStateViewModel: ObservableObject {
// MARK: Global values

@AppStorage(AppStorageStrings.apiKey.rawValue) var apiKey = Constants.defaultAPIKey
@AppStorage(AppStorageStrings.playListID.rawValue) var playListID = Constants.defaultPlaylistID
@AppStorage(AppStorageStrings.useIINA.rawValue) var useIINA: Bool = false
@AppStorage(AppStorageStrings.apiKey) var apiKey = Constants.defaultAPIKey
@AppStorage(AppStorageStrings.playListID) var playListID = Constants.defaultPlaylistID
@AppStorage(AppStorageStrings.useIINA) var useIINA: Bool = false
@AppStorage(AppStorageStrings.videoClickBehaviour) var vidClickBehaviour: VideoClickBehaviour = .playVideo

// MARK: States

@Published var logs: [String] = .init()
@Published var isPlaying: Bool = false
@Published var currentlyPlaying: String = ""

func changePlayListID(for playlistURL: String) -> Bool {
// Using regex would be 100% better (this is a quick and dirty method)
var changed = false
let splitted = playlistURL.split(separator: "&")
for splitted in splitted {
if splitted.contains("list") {
let id = splitted.split(separator: "=")
if let idCount = id.last?.count {
if idCount > 6 {
playListID = String(id.last!)
changed = true
}
}
}
}
return changed
}

func addToLogs(for page: Pages, message: String) {
logs.append("Log at: \(Date()), from \(page.rawValue), message => \(message)")
}

func stopPlaying() {
currentlyPlaying = ""
isPlaying = false
Task {
let shellOutput = shell("killall IINA")
DispatchQueue.main.async {
self.logs.append(shellOutput)
if useIINA {
Task {
let shellOutput = shell("killall IINA")
DispatchQueue.main.async {
self.logs.append(shellOutput)
}
}
}
}
Expand All @@ -61,7 +46,7 @@ class AppStateViewModel: ObservableObject {
}

func playVideoIINA(url: URL, title: String) {
// togglePlaying(title)
togglePlaying(title)
Task {
DispatchQueue.main.async {
self.logs.append(self.shell("open -a iina '\(url)'"))
Expand Down
Loading

0 comments on commit 3aa8f4b

Please sign in to comment.