1+ // --------------------------------------------------------------------------------------------------------------------
2+ // <copyright file="HeaderedTextBlock.xaml.cs" company="MADE Apps">
3+ // Copyright (c) MADE Apps.
4+ // </copyright>
5+ // <summary>
6+ // Defines a UI element representing read-only text with a header component.
7+ // </summary>
8+ // --------------------------------------------------------------------------------------------------------------------
9+
10+ namespace MADE . App . Views
11+ {
12+ using MADE . App . Views . Extensions ;
13+ using MADE . App . Views . Layout ;
14+
15+ using Xamarin . Forms ;
16+ using Xamarin . Forms . Xaml ;
17+
18+ /// <summary>
19+ /// Defines a UI element representing read-only text with a header component.
20+ /// </summary>
21+ [ XamlCompilation ( XamlCompilationOptions . Compile ) ]
22+ public partial class HeaderedTextBlock : IHeaderedTextBlock
23+ {
24+ /// <summary>
25+ /// Defines the bindable property for the <see cref="Header"/> value.
26+ /// </summary>
27+ public static readonly BindableProperty HeaderProperty = BindableProperty . Create (
28+ nameof ( Header ) ,
29+ typeof ( string ) ,
30+ typeof ( HeaderedTextBlock ) ,
31+ null ,
32+ propertyChanged : ( bindable , value , newValue ) =>
33+ {
34+ var control = ( HeaderedTextBlock ) bindable ;
35+ control . HeaderContent . Text = ( string ) newValue ;
36+ control . UpdateVisibility ( ) ;
37+ } ) ;
38+
39+ /// <summary>
40+ /// Defines the bindable property for the <see cref="HeaderStyle"/> value.
41+ /// </summary>
42+ public static readonly BindableProperty HeaderStyleProperty = BindableProperty . Create (
43+ nameof ( HeaderStyle ) ,
44+ typeof ( Style ) ,
45+ typeof ( HeaderedTextBlock ) ,
46+ default ( Style ) ,
47+ propertyChanged : ( bindable , value , newValue ) =>
48+ {
49+ var control = ( HeaderedTextBlock ) bindable ;
50+ control . HeaderContent . Style = ( Style ) newValue ;
51+ } ) ;
52+
53+ /// <summary>
54+ /// Defines the bindable property for the <see cref="Orientation"/> value.
55+ /// </summary>
56+ public static readonly BindableProperty OrientationProperty = BindableProperty . Create (
57+ nameof ( Orientation ) ,
58+ typeof ( Orientation ) ,
59+ typeof ( HeaderedTextBlock ) ,
60+ Orientation . Vertical ,
61+ propertyChanged : ( bindable , value , newValue ) =>
62+ {
63+ var control = ( HeaderedTextBlock ) bindable ;
64+ control . UpdateOrientation ( ) ;
65+ } ) ;
66+
67+ /// <summary>
68+ /// Defines the bindable property for the <see cref="Text"/> value.
69+ /// </summary>
70+ public static readonly BindableProperty TextProperty = BindableProperty . Create (
71+ nameof ( Text ) ,
72+ typeof ( string ) ,
73+ typeof ( HeaderedTextBlock ) ,
74+ null ,
75+ propertyChanged : ( bindable , value , newValue ) =>
76+ {
77+ var control = ( HeaderedTextBlock ) bindable ;
78+ control . TextContent . Text = ( string ) newValue ;
79+ control . UpdateVisibility ( ) ;
80+ } ) ;
81+
82+ /// <summary>
83+ /// Defines the bindable property for the <see cref="TextStyle"/> value.
84+ /// </summary>
85+ public static readonly BindableProperty TextStyleProperty = BindableProperty . Create (
86+ nameof ( TextStyle ) ,
87+ typeof ( Style ) ,
88+ typeof ( HeaderedTextBlock ) ,
89+ default ( Style ) ,
90+ propertyChanged : ( bindable , value , newValue ) =>
91+ {
92+ var control = ( HeaderedTextBlock ) bindable ;
93+ control . TextContent . Style = ( Style ) newValue ;
94+ } ) ;
95+
96+ /// <summary>
97+ /// Defines the bindable property for the <see cref="HideIfNullOrWhiteSpace"/> value.
98+ /// </summary>
99+ public static readonly BindableProperty HideIfNullOrWhiteSpaceProperty = BindableProperty . Create (
100+ nameof ( HideIfNullOrWhiteSpace ) ,
101+ typeof ( bool ) ,
102+ typeof ( HeaderedTextBlock ) ,
103+ false ,
104+ propertyChanged : ( bindable , value , newValue ) =>
105+ {
106+ var control = ( HeaderedTextBlock ) bindable ;
107+ control . UpdateVisibility ( ) ;
108+ } ) ;
109+
110+ /// <summary>
111+ /// Initializes a new instance of the <see cref="HeaderedTextBlock"/> class.
112+ /// </summary>
113+ public HeaderedTextBlock ( )
114+ {
115+ this . InitializeComponent ( ) ;
116+ }
117+
118+ /// <summary>
119+ /// Gets or sets the string associated with the header.
120+ /// </summary>
121+ public string Header
122+ {
123+ get => ( string ) this . GetValue ( HeaderProperty ) ;
124+ set => this . SetValue ( HeaderProperty , value ) ;
125+ }
126+
127+ /// <summary>
128+ /// Gets or sets the style associated with the header content.
129+ /// </summary>
130+ public Style HeaderStyle
131+ {
132+ get => ( Style ) this . GetValue ( HeaderStyleProperty ) ;
133+ set => this . SetValue ( HeaderStyleProperty , value ) ;
134+ }
135+
136+ /// <summary>
137+ /// Gets or sets the string associated with the text.
138+ /// </summary>
139+ public string Text
140+ {
141+ get => ( string ) this . GetValue ( TextProperty ) ;
142+ set => this . SetValue ( TextProperty , value ) ;
143+ }
144+
145+ /// <summary>
146+ /// Gets or sets the style associated with the text content.
147+ /// </summary>
148+ public Style TextStyle
149+ {
150+ get => ( Style ) this . GetValue ( TextStyleProperty ) ;
151+ set => this . SetValue ( TextStyleProperty , value ) ;
152+ }
153+
154+ /// <summary>
155+ /// Gets or sets the orientation the header and text should layout as.
156+ /// </summary>
157+ public Orientation Orientation
158+ {
159+ get => ( Orientation ) this . GetValue ( OrientationProperty ) ;
160+ set => this . SetValue ( OrientationProperty , value ) ;
161+ }
162+
163+ /// <summary>
164+ /// Gets or sets a value indicating whether to hide the control if the <see cref="IHeaderedTextBlock.Text"/> is null or whitespace.
165+ /// </summary>
166+ public bool HideIfNullOrWhiteSpace
167+ {
168+ get => ( bool ) this . GetValue ( HideIfNullOrWhiteSpaceProperty ) ;
169+ set => this . SetValue ( HideIfNullOrWhiteSpaceProperty , value ) ;
170+ }
171+
172+ /// <summary>
173+ /// Updates the layout for the control based on the current <see cref="IHeaderedTextBlock.Orientation"/> value.
174+ /// </summary>
175+ public void UpdateOrientation ( )
176+ {
177+ this . HeaderTextBlockContainer . SetOrientation ( this . Orientation ) ;
178+ }
179+
180+ /// <summary>
181+ /// Updates the visibility of the control based on the values of the <see cref="IHeaderedTextBlock.Header"/> and <see cref="IHeaderedTextBlock.Text"/> properties.
182+ /// </summary>
183+ public void UpdateVisibility ( )
184+ {
185+ if ( ! this . HideIfNullOrWhiteSpace || ! string . IsNullOrWhiteSpace ( this . Text ) )
186+ {
187+ this . IsVisible = true ;
188+ this . HeaderContent ? . SetVisible ( ! string . IsNullOrWhiteSpace ( this . Header ) ) ;
189+ this . TextContent ? . SetVisible ( ! string . IsNullOrWhiteSpace ( this . Text ) ) ;
190+ }
191+ else
192+ {
193+ this . IsVisible = false ;
194+ this . HeaderContent ? . SetVisible ( false ) ;
195+ this . TextContent ? . SetVisible ( false ) ;
196+ }
197+ }
198+ }
199+ }
0 commit comments