Skip to content

Commit b9c4d4c

Browse files
committed
Add file picker
1 parent 2dd227a commit b9c4d4c

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

src/Vocup.Android/App.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ public App(MainActivity mainActivity)
1010
{
1111
this.mainActivity = mainActivity;
1212
}
13+
14+
protected override void BrowseFile()
15+
{
16+
mainActivity?.ShowFilePicker();
17+
}
1318
}

src/Vocup.Android/MainActivity.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Android.Content;
33
using Android.Content.PM;
44
using Android.OS;
5+
using Android.Runtime;
56
using Android.Util;
67
using Avalonia;
78
using Avalonia.Android;
@@ -25,6 +26,7 @@ namespace Vocup.Android;
2526
public class MainActivity : AvaloniaMainActivity<App>
2627
{
2728
private const string Tag = "Vocup.Android.MainActivity";
29+
private const int FilePickerRequestCode = 3147;
2830

2931
protected override AppBuilder CreateAppBuilder()
3032
{
@@ -50,6 +52,38 @@ protected override void OnNewIntent(Intent? intent)
5052
HandleIntent(intent);
5153
}
5254

55+
public void ShowFilePicker()
56+
{
57+
var intent = new Intent(Intent.ActionGetContent);
58+
intent.SetType("application/octet-stream");
59+
intent.AddCategory(Intent.CategoryOpenable);
60+
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
61+
StartActivityForResult(Intent.CreateChooser(intent, "Select a file"), FilePickerRequestCode);
62+
}
63+
64+
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent? data)
65+
{
66+
base.OnActivityResult(requestCode, resultCode, data);
67+
68+
if (requestCode == FilePickerRequestCode && resultCode == Result.Ok && data?.Data != null)
69+
{
70+
using System.IO.Stream? stream = ContentResolver?.OpenInputStream(data.Data);
71+
72+
if (stream == null)
73+
{
74+
Log.Error(Tag, "Stream is null in OnActivityResult");
75+
return;
76+
}
77+
if (Avalonia.Application.Current is not App app)
78+
{
79+
Log.Error(Tag, "App is null in OnActivityResult");
80+
return;
81+
}
82+
Log.Debug(Tag, $"File size is {stream.Length} bytes");
83+
app.OpenFile(stream);
84+
}
85+
}
86+
5387
private void HandleIntent(Intent? intent)
5488
{
5589
if (intent?.Action == Intent.ActionView && intent.Data != null)

src/Vocup/App.axaml.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Avalonia;
22
using Avalonia.Controls.ApplicationLifetimes;
33
using Avalonia.Markup.Xaml;
4+
using ReactiveUI;
45
using System.IO;
56
using Vocup.ViewModels;
67
using Vocup.Views;
@@ -26,21 +27,31 @@ public void OpenFile(Stream input)
2627

2728
public override void OnFrameworkInitializationCompleted()
2829
{
30+
mainViewModel = new MainViewModel
31+
{
32+
OpenFileCommand = ReactiveCommand.Create(BrowseFile)
33+
};
34+
2935
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
3036
{
3137
desktop.MainWindow = new MainWindow
3238
{
33-
DataContext = mainViewModel = new MainViewModel()
39+
DataContext = mainViewModel
3440
};
3541
}
3642
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
3743
{
3844
singleViewPlatform.MainView = new MainView
3945
{
40-
DataContext = mainViewModel = new MainViewModel()
46+
DataContext = mainViewModel
4147
};
4248
}
4349

4450
base.OnFrameworkInitializationCompleted();
4551
}
52+
53+
protected virtual void BrowseFile()
54+
{
55+
56+
}
4657
}

src/Vocup/ViewModels/MainViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ReactiveUI;
2+
using System.Windows.Input;
23

34
namespace Vocup.ViewModels;
45

@@ -12,4 +13,6 @@ public long FileLength
1213
}
1314

1415
public AboutViewModel About { get; } = new();
16+
17+
public required ICommand OpenFileCommand { get; init; }
1518
}

src/Vocup/Views/MainView.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<StackPanel>
1717
<TextBlock Text="{Binding FileLength}" />
18+
<Button Command="{Binding OpenFileCommand}" Content="Open File" />
1819
<views:AboutView DataContext="{Binding About}" />
1920
</StackPanel>
2021

0 commit comments

Comments
 (0)