Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Add ContentPopupManager
Browse files Browse the repository at this point in the history
  • Loading branch information
sung-su committed Apr 28, 2020
1 parent 008c361 commit 69da61d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 112 deletions.
1 change: 0 additions & 1 deletion src/Tizen.Wearable.CircularUI.Forms.Renderer/CircularUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static void Init()
{
if (IsInitialized) return;
IsInitialized = true;
ContentPopup.RendererFunc = () => new ContentPopupRenderer();
}

public static void Init(string apiKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,25 @@

using System;
using System.ComponentModel;
using Tizen.Wearable.CircularUI.Forms;
using Tizen.Wearable.CircularUI.Forms.Renderer;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Tizen;
using XForms = Xamarin.Forms.Forms;

[assembly: ExportRenderer(typeof(ContentPopup), typeof(ContentPopupRenderer))]
[assembly: Dependency(typeof(Tizen.Wearable.CircularUI.Forms.Renderer.ContentPopupRenderer))]

namespace Tizen.Wearable.CircularUI.Forms.Renderer
{
public class ContentPopupRenderer : IContentPopupRenderer
{
ElmSharp.Popup _popup;
ContentPopup _element;

public void SetElement(Element element)
{
if (element.Parent == null)
element.Parent = Application.Current;
element.PropertyChanged += OnElementPropertyChanged;
_element = element as ContentPopup;

UpdateContent();
UpdateIsShow();
}
TaskCompletionSource<bool> _tcs;

public ContentPopupRenderer()
{
_popup = new ElmSharp.Popup(XForms.NativeParent);
_popup.Style = "circle";

_popup.BackButtonPressed += OnBackButtonPressed;
_popup.Dismissed += OnDismissed;
}
Expand All @@ -56,32 +44,37 @@ public ContentPopupRenderer()
Dispose(false);
}

public void Dispose()
public void SetElement(ContentPopup element)
{
Dispose(true);
GC.SuppressFinalize(this);
if (element.Parent == null)
element.Parent = Application.Current;
element.PropertyChanged += OnElementPropertyChanged;
_element = element;

UpdateContent();
}

public void Dismiss()
public void Dispose()
{
_popup?.Hide();
Dispose(true);
GC.SuppressFinalize(this);
}

public void Show()
public Task Open()
{
_popup?.Show();
_popup.Show();
_element.IsOpen = true;
_tcs = new TaskCompletionSource<bool>();
return _tcs.Task;
}

protected void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == ContentPopup.ContentProperty.PropertyName)
{
UpdateContent();
}
if (e.PropertyName == ContentPopup.IsShowProperty.PropertyName)
{
UpdateIsShow();
}

if (e.PropertyName == ContentPopup.IsOpenProperty.PropertyName)
UpdateIsOpen();
}

protected virtual void Dispose(bool disposing)
Expand All @@ -105,13 +98,14 @@ protected virtual void Dispose(bool disposing)

void OnBackButtonPressed(object sender, EventArgs e)
{
_element.SendBackButtonPressed();
if (!_element.SendBackButtonPressed())
_popup?.Hide();
}

void OnDismissed(object sender, EventArgs e)
{
_element.SendDismissed();
Dispose();
_tcs?.SetResult(true);
}

void UpdateContent()
Expand All @@ -121,7 +115,7 @@ void UpdateContent()
var renderer = Platform.GetOrCreateRenderer(_element.Content);
(renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();
var native = renderer.NativeView;
native.MinimumHeight = 360;
native.MinimumHeight = XForms.NativeParent.Geometry.Height;
_popup.SetContent(native, false);
}
else
Expand All @@ -130,12 +124,10 @@ void UpdateContent()
}
}

void UpdateIsShow()
void UpdateIsOpen()
{
if (_element.IsShow)
Show();
else
Dismiss();
if (!_element.IsOpen)
_popup?.Hide();
}
}
}
68 changes: 13 additions & 55 deletions src/Tizen.Wearable.CircularUI.Forms/ContentPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ namespace Tizen.Wearable.CircularUI.Forms
/// The ContentPopup is a Popup, which allows you to customize the View to be displayed.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public class ContentPopup : Element, IDisposable
public class ContentPopup : Element
{
IContentPopupRenderer _renderer;

/// <summary>
/// For internal use.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static Func<IContentPopupRenderer> RendererFunc { get; set; } = null;

/// <summary>
/// BindableProperty. Identifies the Content bindable property.
/// </summary>
Expand All @@ -44,13 +36,7 @@ public class ContentPopup : Element, IDisposable
/// BindableProperty. Identifies the IsShow bindable property.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public static readonly BindableProperty IsShowProperty = BindableProperty.Create(nameof(IsShow), typeof(bool), typeof(ContentPopup), false, propertyChanged:(b, o, n) => ((ContentPopup)b).UpdateRenderer());

/// <summary>
/// Occurs when the device's back button is pressed.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public event EventHandler BackButtonPressed;
public static readonly BindableProperty IsOpenProperty = BindableProperty.Create(nameof(IsOpen), typeof(bool), typeof(ContentPopup), false);

/// <summary>
/// Occurs when the popup is dismissed.
Expand All @@ -69,22 +55,13 @@ public View Content
}

/// <summary>
/// Gets or sets the popup is shown.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public bool IsShow
{
get { return (bool)GetValue(IsShowProperty); }
set { SetValue(IsShowProperty, value); }
}

/// <summary>
/// Shows the popup.
/// Gets or sets the popup is opened.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public void Show()
public bool IsOpen
{
IsShow = true;
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}

/// <summary>
Expand All @@ -93,7 +70,7 @@ public void Show()
/// <since_tizen> 4 </since_tizen>
public void Dismiss()
{
IsShow = false;
IsOpen = false;
}

/// <summary>
Expand All @@ -109,27 +86,18 @@ public void SendDismissed()
/// For internal use.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendBackButtonPressed()
public bool SendBackButtonPressed()
{
BackButtonPressed?.Invoke(this, EventArgs.Empty);
return OnBackButtonPressed();
}

/// <summary>
/// Dispose the popup.
/// To change the default behavior of the BackButton.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public void Dispose()
{
Dispose(true);
}

protected virtual void Dispose(bool disposing)
/// <returns>Default is false</returns>
public virtual bool OnBackButtonPressed()
{
if (disposing && _renderer != null)
{
_renderer.Dispose();
_renderer = null;
}
return false;
}

protected override void OnBindingContextChanged()
Expand All @@ -144,15 +112,5 @@ void UpdateContent()
if (Content != null)
OnChildAdded(Content);
}

void UpdateRenderer()
{
if (_renderer == null)
{
_renderer = RendererFunc();
_renderer.SetElement(this);
}
}

}
}
45 changes: 45 additions & 0 deletions src/Tizen.Wearable.CircularUI.Forms/ContentPopupManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Threading.Tasks;
using Xamarin.Forms;

namespace Tizen.Wearable.CircularUI.Forms
{
public static class ContentPopupManager
{
public static async Task ShowPopup(this INavigation navigation, ContentPopup popup)
{
await ShowPopup(popup);
}

public static async Task ShowPopup(ContentPopup popup)
{
if (popup == null)
await Task.FromResult(false);

using (var renderer = DependencyService.Get<IContentPopupRenderer>(DependencyFetchTarget.NewInstance))
{
if (renderer == null)
await Task.FromResult(false);

renderer.SetElement(popup);

await renderer.Open();
}
}
}
}
10 changes: 8 additions & 2 deletions src/Tizen.Wearable.CircularUI.Forms/IContentPopupRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

using System;
using Xamarin.Forms;
using System.Threading.Tasks;

namespace Tizen.Wearable.CircularUI.Forms
{
Expand All @@ -29,6 +29,12 @@ public interface IContentPopupRenderer : IDisposable
/// Sets the Element associated with this renderer.
/// </summary>
/// <param name="element">New element.</param>
void SetElement(Element element);
void SetElement(ContentPopup element);

/// <summary>
/// Open a popup.
/// </summary>
/// <returns>Returns a Task with the dismiss result of the popup.</returns>
Task Open();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Button
AutomationId="dismisstest1"
x:Name="button1"
Clicked="OnContentPopupDismissBackKeyClicked"
Clicked="OnContentPopupTest1Clicked"
FontSize="Small"
HeightRequest="50"
HorizontalOptions="Center"
Expand All @@ -30,7 +30,7 @@
<Button
AutomationId="dismisstest2"
x:Name="button2"
Clicked="OnContentPopupDismissButtonClicked"
Clicked="OnContentPopupTest2Clicked"
FontSize="Small"
HeightRequest="50"
HorizontalOptions="Center"
Expand Down
Loading

0 comments on commit 69da61d

Please sign in to comment.