33// Distributed under the MIT software license, see the accompanying
44// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
55
6+ using Avalonia . Platform . Storage ;
67using Newtonsoft . Json ;
78using System ;
89using System . Collections . Generic ;
910using System . IO ;
11+ using System . Linq ;
12+ using System . Threading . Tasks ;
1013using WatchOnlyBitcoinWallet . Models ;
1114
1215namespace WatchOnlyBitcoinWallet . Services
1316{
1417 public interface IFileManager
1518 {
19+ public Task < string [ ] > OpenFilePickerAsync ( ) ;
20+ public IStorageProvider ? StorageProvider { get ; set ; }
21+
1622 SettingsModel ReadSettingsFile ( ) ;
1723 List < BitcoinAddress > ReadWalletFile ( ) ;
1824 void WriteSettings ( SettingsModel settings ) ;
@@ -38,6 +44,48 @@ public FileManager()
3844
3945 private readonly string mainDir , walletPath , settingsPath ;
4046
47+ public IWindowManager ? WinMan { get ; set ; }
48+ public IStorageProvider ? StorageProvider { get ; set ; }
49+
50+
51+ public async Task < string [ ] > OpenFilePickerAsync ( )
52+ {
53+ if ( WinMan is null )
54+ {
55+ return [ "WindowManager instance is not set (this is a bug)." ] ;
56+ }
57+ if ( StorageProvider is null )
58+ {
59+ await WinMan . ShowMessageBox ( MessageBoxType . Ok , "StorageProvider is not set (this is a bug)." ) ;
60+ return Array . Empty < string > ( ) ;
61+ }
62+
63+ FilePickerFileType fileType = new ( "txt" )
64+ {
65+ Patterns = [ "*.txt" ]
66+ } ;
67+
68+ FilePickerOpenOptions options = new ( )
69+ {
70+ AllowMultiple = false ,
71+ FileTypeFilter = [ fileType ] ,
72+ Title = "Text files (.txt)"
73+ } ;
74+
75+ try
76+ {
77+ IReadOnlyList < IStorageFile > dir = await StorageProvider . OpenFilePickerAsync ( options ) ;
78+ if ( dir != null && dir . Count > 0 )
79+ {
80+ return File . ReadAllLines ( dir . ElementAt ( 0 ) . Path . LocalPath ) ;
81+ }
82+ }
83+ catch ( Exception ex )
84+ {
85+ await WinMan . ShowMessageBox ( MessageBoxType . Ok , ex . Message ) ;
86+ }
87+ return Array . Empty < string > ( ) ;
88+ }
4189
4290 public static T ? ReadFile < T > ( string filePath )
4391 {
0 commit comments