diff --git a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Dialogs.fs b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Dialogs.fs index dd7f13a9..cfa871b8 100644 --- a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Dialogs.fs +++ b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Dialogs.fs @@ -1,34 +1,39 @@ namespace Examples.MusicPlayer - module Dialogs = - open System - open Avalonia.Controls + open Avalonia + open Avalonia.Platform.Storage + open Avalonia.Threading + open System.Collections.Generic - let getMusicFilesDialog (filters: FileDialogFilter seq option) = - let dialog = OpenFileDialog() + let showMusicFilesDialog(provider: IStorageProvider, filters: FilePickerFileType list option) = let filters = match filters with | Some filter -> filter | None -> - let filter = FileDialogFilter() - filter.Extensions <- - Collections.Generic.List - (seq { - "mp3" - "wav" }) - filter.Name <- "Music" - seq { filter } - - dialog.AllowMultiple <- true - dialog.Directory <- Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) - dialog.Title <- "Select Your Music Files" - dialog.Filters <- System.Collections.Generic.List(filters) - dialog - - let getFolderDialog = - let dialog = OpenFolderDialog() - dialog.Directory <- Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) - dialog.Title <- "Choose where to look up for music" - dialog + let patterns = [ "*.mp3"; "*.wav" ] + let filter = FilePickerFileType("Music", Patterns = patterns) + [ filter ] + + let options = FilePickerOpenOptions(AllowMultiple = true, Title = "Select Your Music Files", FileTypeFilter = filters) + + async { + let! musicFolder = provider.TryGetWellKnownFolderAsync Platform.Storage.WellKnownFolder.Music |> Async.AwaitTask + options.SuggestedStartLocation <- musicFolder + + return! + Dispatcher.UIThread.InvokeAsync> + (fun _ -> provider.OpenFilePickerAsync(options)) |> Async.AwaitTask + } + + let showMusicFolderDialog(provider: IStorageProvider) = + async { + let! musicFolder = provider.TryGetWellKnownFolderAsync Platform.Storage.WellKnownFolder.Music |> Async.AwaitTask + let options = FolderPickerOpenOptions(Title = "Choose where to look up for music", SuggestedStartLocation = musicFolder) + + return! + Dispatcher.UIThread.InvokeAsync> + (fun _ -> provider.OpenFolderPickerAsync(options)) |> Async.AwaitTask + + } diff --git a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Examples.Elmish.MusicPlayer.fsproj b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Examples.Elmish.MusicPlayer.fsproj index e3eeda95..48b35350 100644 --- a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Examples.Elmish.MusicPlayer.fsproj +++ b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Examples.Elmish.MusicPlayer.fsproj @@ -32,7 +32,7 @@ - - + + diff --git a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Shell.fs b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Shell.fs index a2d6d4be..41054bd2 100644 --- a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Shell.fs +++ b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Shell.fs @@ -7,11 +7,13 @@ module Shell = open Avalonia.Controls open Avalonia.Input open Avalonia.Layout + open Avalonia.Platform.Storage open Avalonia.FuncUI.Elmish open Avalonia.FuncUI.Hosts open Avalonia.FuncUI.DSL open LibVLCSharp.Shared open Examples.MusicPlayer + open System.Collections.Generic type State = { title: string @@ -24,8 +26,8 @@ module Shell = | SetTitle of string | OpenFiles | OpenFolder - | AfterSelectFolder of string - | AfterSelectFiles of string array + | AfterSelectFolder of IReadOnlyList + | AfterSelectFiles of IReadOnlyList (* Handle Media Player Events *) | Playing | Paused @@ -122,15 +124,13 @@ module Shell = window.Title <- title { state with title = title }, Cmd.none | OpenFiles -> - let dialog = Dialogs.getMusicFilesDialog None - let showDialog window = dialog.ShowAsync(window) |> Async.AwaitTask - state, Cmd.OfAsync.perform showDialog window AfterSelectFiles + let showDialog storageProvider = Dialogs.showMusicFilesDialog (storageProvider, None) + state, Cmd.OfAsync.perform showDialog window.StorageProvider AfterSelectFiles | OpenFolder -> - let dialog = Dialogs.getFolderDialog - let showDialog window = dialog.ShowAsync(window) |> Async.AwaitTask - state, Cmd.OfAsync.perform showDialog window AfterSelectFolder - | AfterSelectFolder path -> - let songs = Songs.populateFromDirectory path |> Array.toList + let showDialog storageProvider = Dialogs.showMusicFolderDialog storageProvider + state, Cmd.OfAsync.perform showDialog window.StorageProvider AfterSelectFolder + | AfterSelectFolder paths -> + let songs = Songs.populateFromFolders paths |> Array.toList state, Cmd.map PlaylistMsg (Cmd.ofMsg (Playlist.Msg.AddFiles songs)) | AfterSelectFiles paths -> let songs = Songs.populateSongs paths |> Array.toList @@ -185,8 +185,10 @@ module Shell = type ShellWindow() as this = inherit HostWindow() do + let iconPath = System.IO.Path.Combine("Assets", "Icons", "icon.ico") + base.Title <- "Music Player in F# :)" - base.Icon <- WindowIcon("Assets\Icons\icon.ico") + base.Icon <- WindowIcon(iconPath) base.Width <- 800.0 base.Height <- 600.0 base.MinWidth <- 526.0 diff --git a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Songs.fs b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Songs.fs index e6c88d65..62a19607 100644 --- a/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Songs.fs +++ b/src/Examples/Elmish Examples/Examples.Elmish.MusicPlayer/Songs.fs @@ -1,20 +1,19 @@ namespace Examples.MusicPlayer - module Songs = open System open System.IO open Types + open Avalonia.Platform.Storage - let populateSongs (paths: string array): Types.SongRecord array = + let populateSongs (paths: IStorageFile seq): Types.SongRecord array = paths - |> Array.Parallel.map FileInfo - |> Array.Parallel.map (fun info -> info.Name, info.FullName) - |> Array.Parallel.map (fun (name, path) -> + |> Seq.map (fun storageFile -> { id = Guid.NewGuid() - name = name - path = path + name = storageFile.Name + path = storageFile.Path.LocalPath createdAt = DateTime.Now }) + |> Array.ofSeq let populateFromDirectory (path: string): Types.SongRecord array = match String.IsNullOrEmpty path with @@ -29,3 +28,9 @@ module Songs = name = name path = path createdAt = DateTime.Now }) + + let populateFromFolders(folders: IStorageFolder seq) = + folders + |> Seq.map _.Path.LocalPath + |> Seq.map populateFromDirectory + |> Array.concat \ No newline at end of file