« get me outta code hell

rename handlePlaylistSource & let accept grouplike - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-07-09 14:40:25 -0300
committerFlorrie <towerofnix@gmail.com>2020-07-09 16:18:32 -0300
commitbb6a25151e44c524cd463df52a7bdb2a4e0eda67 (patch)
tree249b472198d054c8dc44f211c915700e08215851
parent04e457e9e5e1739ce8fb26e6f0085fafff0c8db1 (diff)
rename handlePlaylistSource & let accept grouplike
It's called loadPlaylistOrSource now, and can take a grouplike (which it
will process with processSmartPlaylist as usual) instead of a URL to
pass to a crawler.

This is so that all functionality for loading a playlist can be
collected in and accessed through one interface, so that modifications
to the way playlists are loaded will be reflected across everywhere that
loads a playlist.
-rw-r--r--client.js3
-rwxr-xr-xindex.js2
-rw-r--r--ui.js34
3 files changed, 21 insertions, 18 deletions
diff --git a/client.js b/client.js
index 33cc62a..645fe42 100644
--- a/client.js
+++ b/client.js
@@ -70,8 +70,7 @@ const setupClient = async ({backend, writable, interfacer, appConfig}) => {
       ' or pass mtui your own playlist.json file!)'),
     source: ['crawl-local', os.homedir() + '/Music']
   }
-  grouplike = await processSmartPlaylist(grouplike)
-  appElement.tabber.currentElement.loadGrouplike(grouplike)
+  await appElement.loadPlaylistOrSource(grouplike, true)
 
   root.select(appElement)
 
diff --git a/index.js b/index.js
index c40bbb9..4b7654d 100755
--- a/index.js
+++ b/index.js
@@ -125,7 +125,7 @@ async function main() {
 
   const loadPlaylists = async () => {
     for (const source of playlistSources) {
-      await appElement.handlePlaylistSource(source, true)
+      await appElement.loadPlaylistOrSource(source, true)
     }
   }
 
diff --git a/ui.js b/ui.js
index 04c65c0..41ab4cc 100644
--- a/ui.js
+++ b/ui.js
@@ -295,8 +295,8 @@ class AppElement extends FocusElement {
     this.openPlaylistDialog = new OpenPlaylistDialog()
     this.setupDialog(this.openPlaylistDialog)
 
-    this.openPlaylistDialog.on('source selected', source => this.handlePlaylistSource(source))
-    this.openPlaylistDialog.on('source selected (new tab)', source => this.handlePlaylistSource(source, true))
+    this.openPlaylistDialog.on('source selected', source => this.loadPlaylistOrSource(source))
+    this.openPlaylistDialog.on('source selected (new tab)', source => this.loadPlaylistOrSource(source, true))
 
     this.alertDialog = new AlertDialog()
     this.setupDialog(this.alertDialog)
@@ -944,7 +944,7 @@ class AppElement extends FocusElement {
 
   openSpecialOrThroughSystem(item) {
     if (item.url.endsWith('.json')) {
-      return this.handlePlaylistSource(item.url, true)
+      return this.loadPlaylistOrSource(item.url, true)
       /*
     } else if (item.url.endsWith('.txt')) {
       if (this.textInfoPane.visible) {
@@ -1094,7 +1094,7 @@ class AppElement extends FocusElement {
     })
   }
 
-  async handlePlaylistSource(source, newTab = false) {
+  async loadPlaylistOrSource(sourceOrPlaylist, newTab = false) {
     if (this.openPlaylistDialog.visible) {
       this.openPlaylistDialog.close()
     }
@@ -1102,18 +1102,22 @@ class AppElement extends FocusElement {
     this.alertDialog.showMessage('Opening playlist...', false)
 
     let grouplike
-    try {
-      grouplike = await this.openPlaylist(source)
-    } catch (error) {
-      if (error === 'unknown argument') {
-        this.alertDialog.showMessage('Could not figure out how to load a playlist from: ' + source)
-      } else if (typeof error === 'string') {
-        this.alertDialog.showMessage(error)
-      } else {
-        throw error
-      }
+    if (typeof sourceOrPlaylist === 'object' && isGroup(sourceOrPlaylist) || sourceOrPlaylist.source) {
+      grouplike = sourceOrPlaylist
+    } else {
+      try {
+        grouplike = await this.openPlaylist(sourceOrPlaylist)
+      } catch (error) {
+        if (error === 'unknown argument') {
+          this.alertDialog.showMessage('Could not figure out how to load a playlist from: ' + sourceOrPlaylist)
+        } else if (typeof error === 'string') {
+          this.alertDialog.showMessage(error)
+        } else {
+          throw error
+        }
 
-      return
+        return
+      }
     }
 
     this.alertDialog.close()