Update description of FloatingButton
[platform/core/csapi/xamarin-forms-extension.git] / Tizen.Xamarin.Forms.Extension / FloatingButton.cs
1 using System;
2 using Xamarin.Forms;
3
4 namespace Tizen.Xamarin.Forms.Extension
5 {
6     /// <summary>
7     /// The FloatingButton class.
8     /// This is only supported for mobile profiles.
9     /// </summary>
10     /// <example>
11     /// <code>
12     /// FloatingButton floatingButton = new FloatingButton
13     /// {
14     ///     MovablePosition = FloatingButtonMovablePosition.All,
15     /// };
16     ///
17     /// FloatingButtonItem item = new FloatingButtonItem();
18     /// item.Icon = new FileImageSource { File = "Icon.png" };
19     /// item.Clicked += (s,e) => { ... };
20     /// floatingButton.FirstButton = item;
21     ///
22     /// this.Appearing += (s,e) =>
23     /// {
24     ///     If (!floatingButton.IsShown)
25     ///         floatingButton.Show();
26     /// }
27     /// this.Disappearing += (s,e) =>
28     /// {
29     ///     If (floatingButton.IsShown)
30     ///         floatingButton.Hide();
31     /// }
32     /// </code>
33     /// </example>
34     public class FloatingButton : BindableObject, IFloatingButton
35     {
36         public static readonly BindableProperty MovablePositionProperty = BindableProperty.Create(nameof(MovablePosition), typeof(FloatingButtonMovablePosition), typeof(FloatingButton), FloatingButtonMovablePosition.All);
37
38         public static readonly BindableProperty IsShownProperty = BindableProperty.Create(nameof(IsShown), typeof(bool), typeof(FloatingButton), true);
39
40         public static readonly BindableProperty FirstButtonProperty = BindableProperty.Create(nameof(FirstButton), typeof(FloatingButtonItem), typeof(FloatingButton), null);
41
42         public static readonly BindableProperty SecondButtonProperty = BindableProperty.Create(nameof(SecondButton), typeof(FloatingButtonItem), typeof(FloatingButton), null);
43
44         IFloatingButton _floatingButton = null;
45
46         /// <summary>
47         /// Gets or sets the movability state of the FloatingButton. This is a bindable property.
48         /// </summary>
49         public FloatingButtonMovablePosition MovablePosition
50         {
51             get { return (FloatingButtonMovablePosition)GetValue(MovablePositionProperty); }
52             set { SetValue(MovablePositionProperty, value); }
53         }
54
55         /// <summary>
56         /// Gets the horizontal position of the FloatingButton. This is a bindable property.
57         /// </summary>
58         public FloatingButtonPosition Position
59         {
60             get { return _floatingButton.Position; }
61         }
62
63         /// <summary>
64         /// Gets the visible state of the FloatingButton.
65         /// </summary>
66         public bool IsShown
67         {
68             get { return (bool)GetValue(IsShownProperty); }
69             internal set { SetValue(IsShownProperty, value); }
70         }
71
72         /// <summary>
73         /// Gets and sets the internal button of the FloatingButton.
74         /// </summary>
75         public FloatingButtonItem FirstButton
76         {
77             get { return (FloatingButtonItem)GetValue(FirstButtonProperty); }
78             set { SetValue(FirstButtonProperty, value); }
79         }
80
81         /// <summary>
82         /// Gets and sets the internal button of the FloatingButton.
83         /// </summary>
84         public FloatingButtonItem SecondButton
85         {
86             get { return (FloatingButtonItem)GetValue(SecondButtonProperty); }
87             set { SetValue(SecondButtonProperty, value); }
88         }
89
90         /// <summary>
91         /// Sets the horizontal position of the FloatingButton.
92         /// </summary>
93         /// <param name="position"> The destination position of the FloatingButton. </param>
94         /// <param name="animated"> Set the animation when moving the position. </param>
95         public void SetPosition(FloatingButtonPosition position, bool animated)
96         {
97             if (MovablePosition != FloatingButtonMovablePosition.None)
98             {
99                 _floatingButton.SetPosition(position, animated);
100             }
101         }
102
103         /// <summary>
104         /// A method to hide the FloatingButton.
105         /// </summary>
106         public void Hide()
107         {
108             IsShown = false;
109             _floatingButton.Hide();
110         }
111
112         /// <summary>
113         /// A method to show the FloatingButton.
114         /// </summary>
115         public void Show()
116         {
117             IsShown = true;
118             _floatingButton.Show();
119         }
120
121         public FloatingButton()
122         {
123             if (Device.Idiom != TargetIdiom.Phone)
124                 throw new Exception("FloatingButton is only support mobile profiles.");
125
126             _floatingButton = DependencyService.Get<IFloatingButton>(DependencyFetchTarget.GlobalInstance);
127
128             if (_floatingButton == null)
129                 throw new Exception("Object reference not set to an instance of a FloatingButton.");
130
131             SetBinding(MovablePositionProperty, new Binding(nameof(MovablePosition), mode: BindingMode.TwoWay, source: _floatingButton));
132             SetBinding(FirstButtonProperty, new Binding(nameof(FirstButton), mode: BindingMode.TwoWay, source: _floatingButton));
133             SetBinding(SecondButtonProperty, new Binding(nameof(SecondButton), mode: BindingMode.TwoWay, source: _floatingButton));
134         }
135     }
136 }