Skip to content

Commit

Permalink
Merge pull request #9 from muak/survey/8
Browse files Browse the repository at this point in the history
Nested dialogs #8
  • Loading branch information
muak authored Jun 4, 2020
2 parents df0fb37 + 6c0996a commit d0808d6
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## files generated by popular Visual Studio add-ons.

.vs/
mono_crash.*

# User-specific files
*.suo
Expand Down
16 changes: 0 additions & 16 deletions AiForms.Dialogs.Android/DialogImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ namespace AiForms.Dialogs
[Android.Runtime.Preserve(AllMembers = true)]
public class DialogImplementation : IDialog
{
public static readonly string ExtraDialogTag = "ExtraDialog";
internal ExtraPlatformDialog ExtraDialog;
FragmentManager FragmentManager => Dialogs.FragmentManager;

public DialogImplementation()
{
ExtraDialog = new ExtraPlatformDialog();
}

public IReusableDialog Create<TView>(object viewModel = null) where TView : DialogView
Expand All @@ -30,8 +25,6 @@ public IReusableDialog Create(DialogView view, object viewModel = null)

public async Task<bool> ShowAsync<TView>(object viewModel = null) where TView : DialogView
{
if (IsRunning()) return false;

using (var dlg = Create<TView>(viewModel))
{
return await dlg.ShowAsync();
Expand All @@ -40,19 +33,10 @@ public async Task<bool> ShowAsync<TView>(object viewModel = null) where TView :

public async Task<bool> ShowAsync(DialogView view, object viewModel = null)
{
if (IsRunning()) return false;

using (var dlg = Create(view, viewModel))
{
return await dlg.ShowAsync();
}
}

bool IsRunning()
{
var dialog = Dialogs.FragmentManager.FindFragmentByTag<ExtraPlatformDialog>(DialogImplementation.ExtraDialogTag);
return dialog != null;
}

}
}
5 changes: 5 additions & 0 deletions AiForms.Dialogs.Android/ExtraPlatformDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class ExtraPlatformDialog : Android.App.DialogFragment
DialogView _dialogView;
ViewGroup _contentView;

public ExtraPlatformDialog() { }

// System Required!
public ExtraPlatformDialog(IntPtr handle, JniHandleOwnership transfer) :base(handle,transfer) { }

public override Android.App.Dialog OnCreateDialog(Bundle savedInstanceState)
{
base.OnCreateDialog(savedInstanceState);
Expand Down
25 changes: 14 additions & 11 deletions AiForms.Dialogs.Android/ReusableDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AiForms.Dialogs.Abstractions;
using Android.App;
Expand All @@ -16,18 +17,18 @@ namespace AiForms.Dialogs
[Android.Runtime.Preserve(AllMembers = true)]
public class ReusableDialog:Java.Lang.Object, IReusableDialog, View.IOnKeyListener
{
DialogImplementation _extraDialog;
ExtraPlatformDialog _platformDialog => _extraDialog.ExtraDialog;
ExtraPlatformDialog _platformDialog;
FragmentManager FragmentManager => Dialogs.FragmentManager;
DialogView _dlgView;
IVisualElementRenderer _renderer;
ViewGroup _contentView;
Action OnceInitializeAction;
Guid _guid;

public ReusableDialog(DialogView view)
{
_dlgView = view;
_extraDialog = Dialog.Instance as DialogImplementation;
_dlgView = view;
_guid = Guid.NewGuid();

// Because the process can't be executed until application completely loads,
// set the action here to execute later on.
Expand Down Expand Up @@ -125,7 +126,7 @@ void Initialize()

public async Task<bool> ShowAsync()
{
var dialog = FragmentManager.FindFragmentByTag<ExtraPlatformDialog>(DialogImplementation.ExtraDialogTag);
var dialog = FragmentManager.FindFragmentByTag<ExtraPlatformDialog>(_guid.ToString());
if (dialog != null)
{
return false;
Expand All @@ -148,7 +149,7 @@ async void complete(object sender, EventArgs e)
_dlgView.RunDismissalAnimation();
await Dismiss();
tcs.SetResult(true);
};
}

_dlgView.DialogNotifierInternal.Canceled += cancel;
_dlgView.DialogNotifierInternal.Completed += complete;
Expand All @@ -157,8 +158,9 @@ async void complete(object sender, EventArgs e)
var payload = new ExtraDialogPayload(_dlgView,_contentView);
var bundle = new Bundle();
bundle.PutSerializable("extraDialogPayload", payload);
_platformDialog = new ExtraPlatformDialog();
_platformDialog.Arguments = bundle;
_platformDialog.Show(FragmentManager, DialogImplementation.ExtraDialogTag);
_platformDialog.Show(FragmentManager, _guid.ToString());

try
{
Expand All @@ -171,6 +173,7 @@ async void complete(object sender, EventArgs e)
_dlgView.TearDown();
payload.Dispose();
bundle.Dispose();

}
}

Expand All @@ -190,16 +193,14 @@ protected override void Dispose(bool disposing)
if (!_renderer.View.IsDisposed())
{
_renderer.View.Dispose();
}
}

_contentView.Dispose();
_contentView = null;

_renderer.Dispose();
_renderer = null;

_extraDialog = null;

OnceInitializeAction = null;
}
base.Dispose(disposing);
Expand Down Expand Up @@ -256,9 +257,11 @@ void handler(object sender, Animation.AnimationEndEventArgs e)
await tcs.Task;
anim.AnimationEnd -= handler;

var dialog = FragmentManager.FindFragmentByTag<ExtraPlatformDialog>(DialogImplementation.ExtraDialogTag);
var dialog = FragmentManager.FindFragmentByTag<ExtraPlatformDialog>(_guid.ToString());
dialog.Dismiss();
_contentView.RemoveFromParent();
_platformDialog.Dispose();
_platformDialog = null;

await Task.Delay(250); // wait for a bit time until the dialog is completely released.
}
Expand Down
2 changes: 1 addition & 1 deletion Sample/Sample/ViewModels/DialogTestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ string GetHAlignString()

string GetVAlignString()
{
switch (HorizontalLayoutAlignment)
switch (VerticalLayoutAlignment)
{
case LayoutAlignment.Start:
return "Top";
Expand Down
26 changes: 26 additions & 0 deletions Sample/Sample/ViewModels/SurveyPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using AiForms.Dialogs.Abstractions;
using Prism.Mvvm;
using Reactive.Bindings;
using Sample.Views;
using Sample.Views.Dialogs;

namespace Sample.ViewModels
{
public class SurveyPageViewModel:BindableBase
{
public ReactiveCommand ShowDialogCommand { get; } = new ReactiveCommand();
public SurveyPageViewModel()
{
var dialog = AiForms.Dialogs.Dialog.Instance;
ShowDialogCommand.Subscribe(async _ =>
{
var ret = await dialog.ShowAsync<TestDialog>();
if (ret)
{
//await dialog.ShowAsync<DialogTestView>();
}
});
}
}
}
10 changes: 10 additions & 0 deletions Sample/Sample/Views/Dialogs/TestDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<extra:DialogView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:extra="clr-namespace:AiForms.Dialogs.Abstractions;assembly=AiForms.Dialogs.Abstractions"
x:Class="Sample.Views.Dialogs.TestDialog">

<Button Text="OK" Clicked="Button_Clicked" />

</extra:DialogView>
33 changes: 33 additions & 0 deletions Sample/Sample/Views/Dialogs/TestDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AiForms.Dialogs.Abstractions;
using Xamarin.Forms;

namespace Sample.Views.Dialogs
{
public partial class TestDialog : DialogView
{
public TestDialog()
{
InitializeComponent();
}

void Button_Clicked(System.Object sender, System.EventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
//(this.Parent as Page).DisplayAlert("", "Hoge","OK");
//(this.Parent as Page).DisplayAlert("", "Fuga", "OK");
AiForms.Dialogs.Dialog.Instance.ShowAsync<DialogTestView>();
});
//DialogNotifier.Complete();

}

public override void Destroy()
{

}
}
}
1 change: 1 addition & 0 deletions Sample/Sample/Views/IndexPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<Button Text="AutoTest" Command="{Binding GoToTest}" CommandParameter="AutoTest" />
<Button Text="ManualTest" Command="{Binding GoToTest}" CommandParameter="ManualTest" />
<Button Text="MainPage" Command="{Binding GoToTest}" CommandParameter="MainPage" />
<Button Text="SurveyPage" Command="{Binding GoToTest}" CommandParameter="SurveyPage" />
</StackLayout>
</ContentPage>
7 changes: 7 additions & 0 deletions Sample/Sample/Views/SurveyPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sample.Views.SurveyPage">
<Button Text="Show" Command="{Binding ShowDialogCommand}" />
</ContentPage>
15 changes: 15 additions & 0 deletions Sample/Sample/Views/SurveyPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Sample.Views
{
public partial class SurveyPage : ContentPage
{
public SurveyPage()
{
InitializeComponent();
}
}
}
4 changes: 3 additions & 1 deletion nuget/AzurePipelines.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
</description>
<summary></summary>
<releaseNotes>
First Release.
## Changes

* [Dialog][Android] Nested dialogs are now supported. #8
</releaseNotes>
<tags>Xamarin.Forms Xamarin dialog toast loading progress popup xaml netstandard ios android</tags>
<language>en-US</language>
Expand Down

0 comments on commit d0808d6

Please sign in to comment.