From 6c9a0ab6b2e3855e95685719d6acb7e7398bd03b Mon Sep 17 00:00:00 2001 From: Zeljko Predjeskovic Date: Sun, 9 Oct 2022 17:43:09 +0200 Subject: [PATCH] Share pdf file to this app and render it Android/IOS --- .github/workflows/build.yml | 25 ++++++ .github/workflows/test.yml | 22 +++++ iCL.DocumentConverter.sln | 2 +- src/DocumentPicker.Droid/MainActivity.cs | 81 ++++++++++++++++++- .../Properties/AssemblyInfo.cs | 2 +- src/DocumentPicker.IOS/AppDelegate.cs | 24 +++++- src/DocumentPicker.IOS/Info.plist | 15 ++++ .../DocumentConverter.Plugin.csproj | 5 +- .../Shared/FileSharing/SharedStream.cs | 22 +++++ src/DocumentPicker.Sample/App.xaml.cs | 2 + .../DocumentPicker.Samples.csproj | 7 ++ .../DocumentPickerPage.xaml | 6 +- .../DocumentPickerPage.xaml.cs | 41 +++++----- .../NotifyVisibility/IObserver.cs | 11 +++ .../NotifyVisibility/ISubject.cs | 13 +++ .../NotifyVisibility/SharedVisibles.cs | 29 +++++++ .../NotifyVisibility/Visibles.cs | 36 +++++++++ .../PartialViews/ShareButton.xaml | 10 +++ .../PartialViews/ShareButton.xaml.cs | 45 +++++++++++ .../PartialViews/ShareNotification.xaml | 10 +++ .../PartialViews/ShareNotification.xaml.cs | 41 ++++++++++ 21 files changed, 416 insertions(+), 33 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/test.yml create mode 100644 src/DocumentPicker.Plugin/Shared/FileSharing/SharedStream.cs create mode 100644 src/DocumentPicker.Sample/NotifyVisibility/IObserver.cs create mode 100644 src/DocumentPicker.Sample/NotifyVisibility/ISubject.cs create mode 100644 src/DocumentPicker.Sample/NotifyVisibility/SharedVisibles.cs create mode 100644 src/DocumentPicker.Sample/NotifyVisibility/Visibles.cs create mode 100644 src/DocumentPicker.Sample/PartialViews/ShareButton.xaml create mode 100644 src/DocumentPicker.Sample/PartialViews/ShareButton.xaml.cs create mode 100644 src/DocumentPicker.Sample/PartialViews/ShareNotification.xaml create mode 100644 src/DocumentPicker.Sample/PartialViews/ShareNotification.xaml.cs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..d2b5b5b --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,25 @@ +name: Build +on: + push: + branches: [ feature/github_workflow ] + pull_request: + branches: [ master ] + +jobs: + Build: + runs-on: [windows-latest] + + steps: + - uses: actions/checkout@v2 + + - name: Setup MSBuild + uses: microsoft/setup-msbuild@v1 + + - name: Setup NuGet + uses: NuGet/setup-nuget@v1.0.5 + + - name: Restore NuGet packages + run: nuget restore iCL.DocumentConverter.sln + + - name: Build the Solution + run: msbuild iCL.DocumentConverter.sln /p:Configuration=Release \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..337ad53 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,22 @@ +name: Test + +on: + push: + branches: [ feature/github_workflow ] + pull_request: + branches: [ master ] + +jobs: + Build: + runs-on: [windows-latest] + + steps: + - uses: actions/checkout@v2 + with: + dotnet-version: 6.0.x + + - name: Setup MSBuild + uses: microsoft/setup-msbuild@v1 + + - name: Execute unit tests + run: dotnet test \ No newline at end of file diff --git a/iCL.DocumentConverter.sln b/iCL.DocumentConverter.sln index 8731614..fa91ed8 100644 --- a/iCL.DocumentConverter.sln +++ b/iCL.DocumentConverter.sln @@ -19,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DocumentConverter.Tests", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentPicker.Droid", "src\DocumentPicker.Droid\DocumentPicker.Droid.csproj", "{F7830ABA-CFCF-481C-9AB7-D5973FFE9B85}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentConverter.Benchmark", "tests\DocumentConverter.Benchmark\DocumentConverter.Benchmark.csproj", "{0275CD62-6BB8-4DBF-91B1-EB54CB18A184}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DocumentConverter.Benchmark", "tests\DocumentConverter.Benchmark\DocumentConverter.Benchmark.csproj", "{0275CD62-6BB8-4DBF-91B1-EB54CB18A184}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/DocumentPicker.Droid/MainActivity.cs b/src/DocumentPicker.Droid/MainActivity.cs index 968332e..0b21d03 100644 --- a/src/DocumentPicker.Droid/MainActivity.cs +++ b/src/DocumentPicker.Droid/MainActivity.cs @@ -1,30 +1,109 @@ -using Android.App; +using System.IO; +using Android.App; +using Android.Content; using Android.Content.PM; +using Android.Net; using Android.OS; +using Android.Provider; using Android.Runtime; +using Android.Views; +using Android.Widget; +using DocumentConverter.Plugin.Shared; +using DocumentConverter.Plugin.Shared.FileSharing; using DocumentConverter.Plugin.Shared.StreamProvider; using DocumentPicker.Samples; +using DocumentPicker.Samples.NotifyVisibility; using Svg; namespace DocumentPicker.Droid { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] + [IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = @"application/pdf")] public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); + LoadApp(bundle); + + + if (Intent.ActionSend.Equals(Intent?.Action) && + Intent.Type != null && + "application/pdf".Equals(Intent.Type)) + { + // This is just an example of the data stored in the extras + var uriFromExtras = Intent.GetParcelableExtra(Intent.ExtraStream) as Android.Net.Uri; + var subject = Intent.GetStringExtra(Intent.ExtraSubject); + + // Get the info from ClipData + var pdf = Intent.ClipData.GetItemAt(0); + + // Open a stream from the URI + var pdfStream = ContentResolver.OpenInputStream(pdf.Uri); + + SharedStream.Instance = pdfStream; + SharedVisibles.ShowShareViews(); + + //Save off the path and description here + //Remove dialog and navigate back to app or browser that shared + //the link + } + } + + private void LoadApp(Bundle bundle) + { FFImageLoading.Forms.Platform.CachedImageRenderer.Init(true); Xamarin.Essentials.Platform.Init(this, bundle); Xamarin.Forms.Forms.Init(this, bundle); SvgPlatform.Init(); LoadApplication(new App()); } + + //private Stream GetFileStream(Uri uri) + //{ + + // // Get the info from ClipData + // var pdf = Intent.ClipData.GetItemAt(0); + + // // Open a stream from the URI + // var pdfStream = ContentResolver.OpenInputStream(pdf.Uri); + + + // return pdfStream; + //} public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } + + //private void handleSendUrl() + //{ + // var view = new LinearLayout(this) { Orientation = Orientation.Vertical }; + // var url = Intent.GetStringExtra(Intent.ExtraText); + + // var urlTextView = new TextView(this) { Gravity = GravityFlags.Center }; + // urlTextView.Text = url; + + // view.AddView(urlTextView); + // var description = new EditText(this) { Gravity = GravityFlags.Top }; + // view.AddView(description); + + // new AlertDialog.Builder(this) + // .SetTitle("Save a URL Link") + // .SetMessage("Type a description for your link") + // .SetView(view) + // .SetPositiveButton("Add", (dialog, whichButton) => + // { + // var desc = description.Text; + // //Save off the url and description here + // //Remove dialog and navigate back to app or browser that shared + // //the link + // FinishAndRemoveTask(); + // FinishAffinity(); + // }) + // .Show(); + //} } } \ No newline at end of file diff --git a/src/DocumentPicker.Droid/Properties/AssemblyInfo.cs b/src/DocumentPicker.Droid/Properties/AssemblyInfo.cs index a152854..968b47a 100644 --- a/src/DocumentPicker.Droid/Properties/AssemblyInfo.cs +++ b/src/DocumentPicker.Droid/Properties/AssemblyInfo.cs @@ -9,7 +9,7 @@ [assembly: AssemblyTitle("DocumentPicker.Droid")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] +[assembly: AssemblyCompany("Optiq")] [assembly: AssemblyProduct("DocumentPicker.Droid")] [assembly: AssemblyCopyright("Copyright © 2018")] [assembly: AssemblyTrademark("")] diff --git a/src/DocumentPicker.IOS/AppDelegate.cs b/src/DocumentPicker.IOS/AppDelegate.cs index 5221b07..604e105 100644 --- a/src/DocumentPicker.IOS/AppDelegate.cs +++ b/src/DocumentPicker.IOS/AppDelegate.cs @@ -1,8 +1,12 @@ -using DocumentConverter.Plugin.Shared.StreamProvider; +using DocumentConverter.Plugin.Shared.FileSharing; +using DocumentConverter.Plugin.Shared.StreamProvider; using DocumentPicker.Samples; +using DocumentPicker.Samples.NotifyVisibility; using Foundation; using Svg; using UIKit; +using Xamarin.Essentials; +using Xamarin.Forms; namespace DocumentPicker.IOS { @@ -20,15 +24,29 @@ public override UIWindow Window } public override bool FinishedLaunching(UIApplication app, NSDictionary options) + { + LoadApp(); + return base.FinishedLaunching(app, options); + } + + public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) + { + SharedStream.Instance = System.IO.File.Open(url.Path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); + + SharedVisibles.ShowShareViews(); + + return true; + } + + private void LoadApp() { FFImageLoading.Forms.Platform.CachedImageRenderer.Init(); Xamarin.Forms.Forms.Init(); SvgPlatform.Init(); LoadApplication(new App()); - - return base.FinishedLaunching(app, options); } + public override void OnResignActivation(UIApplication application) { // Invoked when the application is about to move from active to inactive state. diff --git a/src/DocumentPicker.IOS/Info.plist b/src/DocumentPicker.IOS/Info.plist index 57ba747..e621e2b 100644 --- a/src/DocumentPicker.IOS/Info.plist +++ b/src/DocumentPicker.IOS/Info.plist @@ -10,6 +10,21 @@ 1.0 CFBundleVersion 1.0 + CFBundleDocumentTypes + + + CFBundleTypeName + PDF + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + LSItemContentTypes + + com.adobe.pdf + + + LSRequiresIPhoneOS MinimumOSVersion diff --git a/src/DocumentPicker.Plugin/DocumentConverter.Plugin.csproj b/src/DocumentPicker.Plugin/DocumentConverter.Plugin.csproj index bd0f1f8..37ef8f8 100644 --- a/src/DocumentPicker.Plugin/DocumentConverter.Plugin.csproj +++ b/src/DocumentPicker.Plugin/DocumentConverter.Plugin.csproj @@ -1,4 +1,4 @@ - + netstandard2.0;MonoAndroid90;Xamarin.iOS10;uap10.0.17763 @@ -17,11 +17,8 @@ - - - diff --git a/src/DocumentPicker.Plugin/Shared/FileSharing/SharedStream.cs b/src/DocumentPicker.Plugin/Shared/FileSharing/SharedStream.cs new file mode 100644 index 0000000..19a68bb --- /dev/null +++ b/src/DocumentPicker.Plugin/Shared/FileSharing/SharedStream.cs @@ -0,0 +1,22 @@ +using System; +using System.IO; +using DocumentConverter.Plugin.Shared.StreamProvider; + +namespace DocumentConverter.Plugin.Shared.FileSharing +{ + public static class SharedStream + { + private static Stream _stream; + + public static Stream Instance + { + get => _stream; + set => _stream = value; + } + + public static void DisposeStream() + { + Instance.Dispose(); + } + } +} \ No newline at end of file diff --git a/src/DocumentPicker.Sample/App.xaml.cs b/src/DocumentPicker.Sample/App.xaml.cs index 77fa4fa..bb05526 100644 --- a/src/DocumentPicker.Sample/App.xaml.cs +++ b/src/DocumentPicker.Sample/App.xaml.cs @@ -1,4 +1,5 @@ using DocumentConverter.Plugin.Shared.StreamProvider; +using DocumentPicker.Samples.NotifyVisibility; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -13,6 +14,7 @@ public App() CustomStreamProviderInitializer.Init(); MainPage = new NavigationPage(new DocumentPickerPage()); + SharedVisibles.Init(); } protected override void OnStart() diff --git a/src/DocumentPicker.Sample/DocumentPicker.Samples.csproj b/src/DocumentPicker.Sample/DocumentPicker.Samples.csproj index db7a39c..286023a 100644 --- a/src/DocumentPicker.Sample/DocumentPicker.Samples.csproj +++ b/src/DocumentPicker.Sample/DocumentPicker.Samples.csproj @@ -2,6 +2,7 @@ netstandard2.0 + latest @@ -19,6 +20,12 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + + + MSBuild:UpdateDesignTimeXaml + diff --git a/src/DocumentPicker.Sample/DocumentPickerPage.xaml b/src/DocumentPicker.Sample/DocumentPickerPage.xaml index a76de90..5404459 100644 --- a/src/DocumentPicker.Sample/DocumentPickerPage.xaml +++ b/src/DocumentPicker.Sample/DocumentPickerPage.xaml @@ -1,16 +1,18 @@  + + - -