Skip to content

Commit

Permalink
feat: add download option
Browse files Browse the repository at this point in the history
Signed-off-by: Jianbai Ye <[email protected]>
  • Loading branch information
gusye1234 committed Nov 3, 2022
1 parent 26a89e9 commit 3967143
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
build
.DS_Store
.vscode/
Youtube Downloader.xcodeproj/project.xcworkspace/xcshareddata/
Youtube Downloader.xcodeproj/xcuserdata/Mathias.xcuserdatad/xcdebugger/
Youtube Downloader.xcodeproj/project.xcworkspace/xcuserdata/
Youtube Downloader.xcodeproj/xcuserdata/gus.xcuserdatad/
32 changes: 27 additions & 5 deletions Youtube Downloader/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="19529" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19529"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="20037"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -707,7 +707,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</customView>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="hvN-kN-ctS">
<rect key="frame" x="112" y="29" width="132" height="34"/>
<rect key="frame" x="112" y="19" width="132" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Download" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="dxw-6p-HaB">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -729,12 +729,21 @@ DQ
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" textCompletion="NO" translatesAutoresizingMaskIntoConstraints="NO" id="DiZ-A9-SKk" userLabel="Download Path">
<rect key="frame" x="20" y="57" width="314" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" state="on" borderStyle="bezel" title="~/Downloads" drawsBackground="YES" id="pmN-ih-ISc">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<progressIndicator hidden="YES" wantsLayer="YES" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" maxValue="100" bezeled="NO" indeterminate="YES" style="spinning" translatesAutoresizingMaskIntoConstraints="NO" id="o2I-Vf-I2E">
<rect key="frame" x="198" y="31" width="32" height="32"/>
<rect key="frame" x="198" y="16" width="32" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</progressIndicator>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="jqp-I9-CCn">
<rect key="frame" x="244" y="29" width="73" height="34"/>
<rect key="frame" x="244" y="19" width="73" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Quit" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="HP5-IK-cHN">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -744,10 +753,23 @@ DQ
<action selector="QuitWithSender:" target="XfG-lQ-9wD" id="L89-8n-T6Q"/>
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="LAd-AT-FA6">
<rect key="frame" x="331" y="50" width="83" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="browser" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="JF6-Wv-sbI">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<connections>
<action selector="browserPathClicked:" target="XfG-lQ-9wD" id="mhE-fa-Vtt"/>
</connections>
</buttonCell>
</button>
</subviews>
</view>
<connections>
<outlet property="browserPath" destination="LAd-AT-FA6" id="xOW-gD-YeS"/>
<outlet property="downloadButton" destination="hvN-kN-ctS" id="aXr-jB-OVD"/>
<outlet property="downloadPath" destination="DiZ-A9-SKk" id="LoS-p2-310"/>
<outlet property="message" destination="DMU-29-6Yz" id="o8D-V8-L6J"/>
<outlet property="preview" destination="geM-iV-lon" id="juc-B9-Y9e"/>
<outlet property="progressbar" destination="re9-0Z-l3o" id="EEh-A1-CLz"/>
Expand Down
45 changes: 43 additions & 2 deletions Youtube Downloader/DownloadViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Cocoa
import Alamofire
import SwiftUI


func errorDialog(question: String, text: String) -> Bool {
Expand All @@ -25,29 +26,67 @@ class DownloadViewController: NSViewController {


@IBOutlet weak var videoUrl: NSTextField!
@IBOutlet weak var downloadPath: NSTextField!
var downloadUrl: String = ""

@IBOutlet weak var spinner: NSProgressIndicator!
@IBOutlet weak var progressbar: NSProgressIndicator!
@IBOutlet weak var downloadButton: NSButton!
@IBOutlet weak var quitButton: NSButton!
@IBOutlet weak var browserPath: NSButton!
@IBOutlet weak var message: NSTextField!



@IBOutlet weak var preview: PreviewView!

@IBAction func browserPathClicked(_ sender: Any){
let dialog = NSOpenPanel();

dialog.title = "Choose a directory";
dialog.showsResizeIndicator = true;
dialog.showsHiddenFiles = false;
dialog.allowsMultipleSelection = false;
dialog.canChooseFiles = false;
dialog.canChooseDirectories = true;

if (dialog.runModal() == NSApplication.ModalResponse.OK) {
let result = dialog.url // Pathname of the file

if (result != nil) {
self.downloadPath.stringValue = result!.path
}
} else {
return
}

}



@IBAction func downloadButtonClicked(_ sender: Any) {

downloadButton.isHidden = true
quitButton.isHidden = true
browserPath.isHidden = true
spinner.isHidden = false
spinner.startAnimation(self)

let url = videoUrl.stringValue

// handle paths like ~/Downloads to absolute paths
let download2path = downloadPath.stringValue.replacingOccurrences(of: "~", with: FileManager.default.homeDirectoryForCurrentUser.path)
var documentsURL = URL(fileURLWithPath: download2path, isDirectory: true)
// check out if the path is valid
if !FileManager.default.fileExists(atPath: documentsURL.path){
let alert = NSAlert()
alert.messageText = "Oops, something went wrong"
alert.informativeText = documentsURL.path + " is not a directory"
alert.alertStyle = NSAlert.Style.warning
alert.runModal()
self.reset()
return
}

DispatchQueue.global(qos: .background).async {
// This is run on the background queue
var videoInfo: VideoInfo = VideoInfo()
Expand Down Expand Up @@ -84,12 +123,12 @@ class DownloadViewController: NSViewController {
self.progressbar.isHidden = false
self.preview.isHidden = false
self.videoUrl.isHidden = true
self.downloadPath.isHidden = true


let destination: DownloadRequest.Destination = { _, _ in
let suffix = Date().toString(dateFormat: "dd-MM-YY")
let pathComponent = "\(title) (\(suffix)).mp4"
var documentsURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent(pathComponent)
return (documentsURL, [.removePreviousFile, .createIntermediateDirectories])
}
Expand Down Expand Up @@ -121,8 +160,10 @@ class DownloadViewController: NSViewController {
self.message.isHidden = true
self.downloadButton.isHidden = false
self.videoUrl.stringValue = ""
self.downloadPath.stringValue = "~/Downloads"
self.preview.isHidden = true
self.videoUrl.isHidden = false
self.browserPath.isHidden = false
self.spinner.isHidden = true
self.spinner.stopAnimation(self)
self.quitButton.isHidden = false
Expand Down

0 comments on commit 3967143

Please sign in to comment.