<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Artem Denisov on Medium]]></title>
        <description><![CDATA[Stories by Artem Denisov on Medium]]></description>
        <link>https://medium.com/@tema.denisoff?source=rss-ad0737e96be5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UjAR2A6-gf2kKHwe9OX1mA.jpeg</url>
            <title>Stories by Artem Denisov on Medium</title>
            <link>https://medium.com/@tema.denisoff?source=rss-ad0737e96be5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 11:16:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tema.denisoff/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Customizing iTerm2: Crafting My Ideal Terminal Experience]]></title>
            <link>https://medium.com/@tema.denisoff/customizing-iterm2-crafting-my-ideal-terminal-experience-46a4c53af6ec?source=rss-ad0737e96be5------2</link>
            <guid isPermaLink="false">https://medium.com/p/46a4c53af6ec</guid>
            <category><![CDATA[mac-os-x]]></category>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[oh-my-zsh]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[shell-script]]></category>
            <dc:creator><![CDATA[Artem Denisov]]></dc:creator>
            <pubDate>Sat, 25 Jan 2025 18:57:00 GMT</pubDate>
            <atom:updated>2025-01-25T18:57:00.570Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xh_9NCsSWWkmaPh3jdEWvQ.jpeg" /></figure><p><a href="https://iterm2.com/"><strong>iTerm2</strong></a> is a versatile and highly customizable terminal emulator for macOS. In this guide, I’ll share how I’ve optimized it to suit my workflow. While I’m not a fan of interacting with the OS exclusively via the command line, there are tasks I consistently perform in the terminal, such as:</p><ul><li>Running scripts for work projects</li><li>Managing packages with Homebrew</li><li>Using CLI tools</li></ul><p>For me, the terminal is mainly a place to execute work-related scripts. Initially, I relied on macOS’s default terminal, but over time, I found myself juggling multiple terminal windows — each running just a couple of commands. That’s when I realized how much simpler it would be to manage everything through tabs in a single window.</p><p>Enter <a href="https://iterm2.com/">iTerm2</a> — the solution to my organizational woes. Not only does it allow multiple terminal sessions in one window, but it also offers extensive theme customization and supports powerful extensions. After spending an evening tweaking its settings, I ended up with a terminal that’s both visually pleasing and functionally efficient. Here’s how you can do the same.</p><h3>Getting Started with iTerm2</h3><h4>Installation Options</h4><p>There are two straightforward ways to install iTerm2:</p><ol><li><strong>Download</strong> the installer from the <a href="https://iterm2.com/">official website</a>.</li><li><strong>Use </strong><a href="https://brew.sh/"><strong>Homebrew</strong></a>, a popular package manager for macOS.</li></ol><p>The first method is quick and doesn’t require any additional tools — perfect if you want a simple setup. However, since I planned to customize iTerm2 further (and needed extra tools), I opted for the Homebrew method.</p><h4>Installing with Homebrew</h4><p>To install iTerm2 using Homebrew, follow these steps:</p><ul><li>Install Homebrew by running:</li></ul><pre>/bin/bash -c &quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)&quot;</pre><ul><li>Add Homebrew to your PATH:</li></ul><pre>echo &#39;eval &quot;$(/opt/homebrew/bin/brew shellenv)&quot;&#39; &gt;&gt; $HOME/.zprofile<br>eval &quot;$(/opt/homebrew/bin/brew shellenv)&quot;</pre><ul><li>Install iTerm2:</li></ul><pre>brew install iterm2</pre><p>Launch iTerm2, and you’ll notice it looks quite similar to macOS’s default terminal.</p><figure><img alt="macOS Terminal" src="https://cdn-images-1.medium.com/max/1024/1*9_WcHyIVwHNycFDjTnxgMA.png" /><figcaption>macOS Terminal</figcaption></figure><figure><img alt="iTerm2" src="https://cdn-images-1.medium.com/max/1024/1*ik653k2LvKdCo-IY_e35iA.png" /><figcaption>iTerm2</figcaption></figure><p>Now it’s time to elevate its functionality and appearance.</p><h3>Enhancing iTerm2 with Oh My Zsh and Powerlevel10k</h3><h4>Oh My Zsh</h4><p><a href="https://ohmyz.sh/"><strong>Oh My Zsh</strong></a> is a framework that simplifies Zsh configuration. While some claim it slows down terminal performance, I’ve experienced no significant impact. Its convenience makes it an invaluable addition.</p><p>To install Oh My Zsh:</p><pre>sh -c &quot;$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)&quot;</pre><h4>Powerlevel10k Theme</h4><p>Next, add the <a href="https://github.com/romkatv/powerlevel10k?ysclid=m6cj99472s252529856"><strong>powerlevel10k</strong></a> theme for an ultra-modern look and additional functionality:</p><pre>brew install powerlevel10k<br><br>echo &quot;source $(brew --prefix)/share/powerlevel10k/powerlevel10k.zsh-theme&quot; &gt;&gt; ~/.zshrc</pre><p>Restart iTerm2, and you’ll be greeted by the Powerlevel10k configuration wizard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YdbrQzoOUEbh9sxKL_PMZw.png" /><figcaption>powerlevel10k theme wizard</figcaption></figure><p>Set up the theme to your liking (you can always reconfigure it later). After completing the setup, your terminal should now have a clean, functional, and visually striking appearance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CmmiGJPEm5hq-5mp-zagpg.png" /><figcaption>iTerm2 with powerlevel10k theme</figcaption></figure><h3>Expanding Functionality with Plugins</h3><p>One of iTerm2’s strengths is its support for plugins. Two plugins I found particularly helpful are:</p><ul><li><strong>zsh-autosuggestions</strong>: Provides command suggestions as you type.</li><li><strong>zsh-syntax-highlighting</strong>: Highlights command syntax for easier readability.</li></ul><p>To install these plugins:</p><pre>git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions<br><br>git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting</pre><p>Edit your .zshrc file to activate the plugins by updating the plugins line:</p><pre>plugins=(git zsh-autosuggestions zsh-syntax-highlighting)</pre><p>Restart iTerm2 to apply the changes. With these plugins, your terminal becomes smarter and more intuitive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lYADVYwITPhpgerMMkLI0A.gif" /><figcaption>plugins in action</figcaption></figure><h3>Adding a Touch of Color</h3><p>To improve tab differentiation, I decided to add random background colors to each new tab. Since I couldn’t find an existing plugin for this feature, I created a simple script.</p><p>Add this code snippet to the end of your .zshrc file:</p><pre>PRELINE=&quot;\r\033[A&quot;<br><br>function color { <br>    echo -e &quot;\033]6;1;bg;red;brightness;$((1 + $RANDOM % 255))\a&quot;$PRELINE<br>    echo -e &quot;\033]6;1;bg;green;brightness;$((1 + $RANDOM % 255))\a&quot;$PRELINE<br>    echo -e &quot;\033]6;1;bg;blue;brightness;$((1 + $RANDOM % 255))\a&quot;$PRELINE  <br>}<br><br>color</pre><p>Now, every new tab will be assigned a random background color, making navigation even more intuitive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7u2yQA2h5XxnNJKjqqsc1w.gif" /><figcaption>colored tabs</figcaption></figure><h3>Final Thoughts</h3><p>With these enhancements, iTerm2 has transformed into my ideal terminal: efficient, feature-packed, and visually appealing. To save time in the future, I’ve automated the setup process with a script you can find <a href="https://github.com/temamarvel/iterm2setup">on my GitHub</a>.</p><p>Whether you’re a terminal power user or just looking for small improvements, iTerm2’s flexibility allows you to tailor it to your exact needs. Happy customizing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46a4c53af6ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functional MAUI control based on SwiftUI view]]></title>
            <link>https://medium.com/@tema.denisoff/functional-maui-control-based-on-swiftui-view-e23135d1f2bc?source=rss-ad0737e96be5------2</link>
            <guid isPermaLink="false">https://medium.com/p/e23135d1f2bc</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[maui]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Artem Denisov]]></dc:creator>
            <pubDate>Sun, 04 Dec 2022 18:26:30 GMT</pubDate>
            <atom:updated>2022-12-05T06:55:22.767Z</atom:updated>
            <content:encoded><![CDATA[<p>In the first article of this series, I looked at how to use SwiftUI components as iOS implementations for MAUI controls. To demonstrate this, I used the HelloWorldView component, which only displays the “HelloWorld” label on an orange background. Not a great feature, is it?</p><p>So, in this article, I am going to demonstrate a much more complete component and show not only how to display SwiftUI controls in MAUI applications but also how to bind properties from SwiftUI implementation to MAUI code to observe changes in these properties in both directions: from Swift code to C# and vice versa.</p><p>In the context of this article, I am going to create a counter component. In terms of C# frameworks, it is usually called SpinEdit. Such control provides a way to display numbers (and only numbers) in the input field and use a pair of arrow buttons to decrease or increase the value in the box by a given interval. The user can also edit the displayed value himself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*Mybfk38RbcvIum7VWGYDwA.gif" /><figcaption>The appearance of SpinEdit</figcaption></figure><p>Define the component specifications. To provide the necessary functionality, it should have the Value property, which stores the current value displayed by the component. Also, it should have the Interval property, which indicates how much value increases or decreases when a user clicks on the arrows. I also decided to add a couple of properties that manage the color of the border around the input box, showing when the editor is in focus and when it’s not.</p><p>The final specification of the MAUI control looks like this:</p><pre>class SpinEditControl {<br>    public decimal Value { get; set; }<br>    public decimal Interval { get; set; }<br>    public Brush BorderColor { get; set; }<br>    public Brush FocusedBorderColor { get; set; }<br>}</pre><p>I am not going to describe the creation and configuration of projects in detail as I did in the previous article. Here, I will focus only on the source code of the component implementation.</p><p>Same as the HelloWorld component from the previous post, SpinEdit will consist of four parts:</p><ul><li>SpinEditView — SwiftUI view</li><li>SpinEditWrapper — UIKit wrapper over SwiftUI element to interact with</li><li>SpinEditHandler — MAUI class which binds Swift component and .NET MAUI control</li><li>SpinEditControl — .NET MAUI control</li></ul><p>At first, I want to limit the functionality of the SpinEdit component to the Value property only. And on the example of only one property, I am going to show the whole process of creating the component.</p><p>Explore the development of all these parts in order.</p><h4>SpinEditView</h4><p>Let’s write code for SpinEditView.</p><pre>struct SpinEditView: View {<br>    @ObservedObject var wrapper: SpinEditWrapper<br>    <br>    var body: some View {<br>        VStack{<br>            Button {wrapper.value += 1} label: {<br>                Image(systemName: &quot;chevron.up&quot;)<br>                    .resizable()<br>                    .frame(width: 40, height: 20)<br>            }<br>            <br>            TextField(&quot;Number&quot;, value: $wrapper.value, format: .number)<br>                .multilineTextAlignment(.center)<br>                .background(){<br>                    RoundedRectangle(cornerRadius: 4)<br>                        .stroke(.gray, lineWidth: 2)<br>                }<br>            <br>            Button {wrapper.value -= 1} label: {<br>                Image(systemName: &quot;chevron.down&quot;)<br>                    .resizable()<br>                    .frame(width: 40, height: 20)<br>            }<br>        }<br>    }<br>}</pre><p>In the code above, you can see that SpinEditView significantly differs from HelloWorldView in the earlier article. In addition to the body section, it appears with the description of some wrapper. Earlier I also created a wrapper class (HelloWorldWrapper), but I didn’t use it inside HelloWorldView itself.</p><p>The wrapper class connects SwiftUI view and MAUI code because only NSObject descendant classes can be projected in C#. So the MAUI control communicates with Wrapper, listens, and reacts to changes in its properties, but at the same time, it interacts with the SwiftUI view, which listens to changes of the same properties and changes its state accordingly. So Wrapper is a kind of view model for both MAUI control and SwiftUI view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EHgo-5fitKW72-lOGWUXFA.png" /><figcaption>Diagram of the relationship between the SwiftUI view, Wrapper, and the MAUI Handler</figcaption></figure><p>Thus, SpinEditView does not store the value itself but uses the SpinEditWrapper.value property. When a user edits the value in TextField — SpinEditWrapper.value is changed, SpinEditHandler tracks these modifications and changes the value of properties of SpinEditControl. Suppose the user changes values programmatically from MAUI by modifying SpinEditControl.Value property, the new value is passed through the chain in the opposite direction: SpinEditControl -&gt; SpinEditHandler -&gt; SpinEditWrapper -&gt; SpinEditView, which redraws itself and displays the new value.</p><p>To make SpinEditView aware that the wrapper property can send notifications of changes to its properties, the property wrapper @ObservedObject must be added.</p><p>It is necessary to add property wrapper @ObservedObject so that SpinEditView knows that the wrapper can send notifications of changes to its properties.</p><p>SpinEditView is ready. Let’s create SpinEditWrapper for it.</p><h4>SpinEditWrapper</h4><p>Code for SpinEditWrapper.</p><pre>@objc public class SpinEditWrapper: NSObject, ObservableObject {<br>    var swiftUIView: SpinEditView?<br>    var hostingController: UIHostingController&lt;SpinEditView&gt;?<br>    <br>    @objc @Published public var value: Decimal = 0<br>    <br>    @objc public var uiView: UIView? { hostingController?.view }<br>    <br>    public override init() {<br>        super.init()<br>        <br>        swiftUIView = SpinEditView(wrapper: self)<br>        guard let rootView = swiftUIView else { return }<br>        hostingController = UIHostingController(rootView: rootView)<br>    }<br>}</pre><p>You can notice that the wrapper class code is different from the previous article. In SpinEditWapper, there is the value property because both MAUI control and SwiftUI view are going to work with it. In order for the SwiftUI framework to track changes in this property and pass them to SpinEditView, it must be marked with the property wrapper @Published. It is also important to note that to make SpinEditWrapper track its property changes, it must conform to the ObservableObject protocol.</p><p>Moreover, when instantiating the SpinEditView, I pass SpinEditWrapper as a constructor parameter, thus setting up a connection between the wrapper (view model) and the SwiftUI component (view).</p><p>Let’s build the framework and create the bindings library as described in the previous article. To simplify the build process, you can use a ready-made script from my GitHub <a href="https://github.com/temamarvel/SwiftUI_To_MAUI/tree/main/Scripts">repository</a> (instructions on how to use it are in the ReadMe file).</p><p>After successfully executing all the commands, we will have the SwiftUI framework and the bindings library, the ApiDefinitions.cs file should have this content:</p><pre>using System;<br>using Foundation;<br>using ObjCRuntime;<br>using UIKit;<br><br>namespace SwiftUI_MAUI_Framework<br>{<br> // @interface SpinEditWrapper : NSObject<br> [BaseType (typeof(NSObject), Name = &quot;_TtC22SwiftUI_MAUI_Framework15SpinEditWrapper&quot;)]<br> interface SpinEditWrapper<br> {<br>  // @property (nonatomic) NSDecimal value;<br>  [Export (&quot;value&quot;, ArgumentSemantic.Assign)]<br>  NSDecimal Value { get; set; }<br><br>  // @property (readonly, nonatomic, strong) UIView * _Nullable uiView;<br>  [NullAllowed, Export (&quot;uiView&quot;, ArgumentSemantic.Strong)]<br>  UIView UiView { get; }<br> }<br>}</pre><p>I’m done with Swift code, and now it’s time to do C#.</p><h4>SpinEditControl</h4><p>First, I will create an MAUI control with which the user will interact.</p><pre>namespace MAUI_Library;<br><br>public class SpinEditControl : View {<br>    public static readonly BindableProperty ValueProperty = BindableProperty.Create(nameof(Value), typeof(decimal), typeof(SpinEditControl), default(decimal));<br>    <br>    public decimal Value {<br>        get =&gt; (decimal)GetValue(ValueProperty);<br>        set =&gt; SetValue(ValueProperty, value);<br>    }<br>}</pre><p>In the implementation of SpinEditControl, I added just one binding property — Value. It stores the current value displayed in SpinEdit. The value from this property will be sent by SpinEditHandler to SpinEditWrapper and then to SpinEditView.</p><p>Let’s move on to the essential part of the .NET implementation, the SpinEditHandler.</p><h4>SpinEditHandler</h4><p>In the context of this article, I only focus on the relationship between SwiftUI and MAUI so I won’t describe in detail the Android implementation of the MAUI handler. I will only concentrate on the iOS part.</p><p>The SpinEditHandler code:</p><pre>using Foundation;<br>using Microsoft.Maui.Graphics.Platform;<br>using Microsoft.Maui.Handlers;<br>using SwiftUI_MAUI_Framework;<br>using UIKit;<br><br>namespace MAUI_Library;<br><br>public partial class SpinEditHandler : ViewHandler&lt;SpinEditControl, UIView&gt; {<br>    private SpinEditWrapper Wrapper { get; set; }<br><br>    public SpinEditHandler() : base(PropertyMapper) {<br>    }<br><br>    private static IPropertyMapper&lt;SpinEditControl, SpinEditHandler&gt; PropertyMapper =<br>        new PropertyMapper&lt;SpinEditControl, SpinEditHandler&gt;(ViewMapper) {<br>            [nameof(SpinEditControl.Value)] = MapValue,<br>        };<br><br>    private static void MapValue(SpinEditHandler handler, SpinEditControl spinEditControl) {<br>        if (handler == null || handler.Wrapper.Value == spinEditControl.Value)<br>            return;<br>        <br>        handler.Wrapper.Value= spinEditControl.Value;<br>    }<br><br>    protected override UIView CreatePlatformView() {<br>        Wrapper = new SpinEditWrapper();<br>        return Wrapper.UiView;<br>    }<br>}</pre><p>Since SpinEditControl has the Value property I want to synchronize with platform implementation, I need to add to the PropertyMapper record, which sets a handler that will be triggered when the Value property of SpinEditControl changes.</p><p>When the SpinEditControl.Value property changes and the SpinEditHandler tracks this change and calls the MapValue handler method, in which I pass the new value to the platform code. That way, the SwiftUI SpinEditView gets the updated value and redraws itself.</p><h4>Let’s test how it works</h4><p>Well, it looks like the task is done. All elements for SpinEditControl are created. I can build the MAUI component library, add the line of code with SpinEditControl to the .xaml file of the test application and enjoy the result.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;<br>&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;<br>             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;<br>             xmlns:mauiLibrary=&quot;clr-namespace:MAUI_Library;assembly=MAUI_Library&quot;<br>             x:Class=&quot;MAUI_App.MainPage&quot;&gt;<br><br>    &lt;ScrollView&gt;<br>        &lt;VerticalStackLayout<br>            Spacing=&quot;25&quot;<br>            Padding=&quot;30,0&quot;<br>            VerticalOptions=&quot;Center&quot;&gt;<br><br>            &lt;Image<br>                Source=&quot;dotnet_bot.png&quot;<br>                SemanticProperties.Description=&quot;Cute dot net bot waving hi to you!&quot;<br>                HeightRequest=&quot;200&quot;<br>                HorizontalOptions=&quot;Center&quot; /&gt;<br>            <br>            &lt;mauiLibrary:HelloWorldControl/&gt;<br>            <br>            &lt;mauiLibrary:SpinEditControl x:Name=&quot;spinEditControl&quot; <br>                                         Value=&quot;1&quot; /&gt;<br>            <br>        &lt;/VerticalStackLayout&gt;<br>    &lt;/ScrollView&gt;<br><br>&lt;/ContentPage&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*bckVJSUObtgZKqzJXbVtiw.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*deNghvoBYlGOGW0mvvRUJA.gif" /><figcaption>The working test app</figcaption></figure><p>As you can see, the SpinEdit value responds to arrow buttons, and the input box allows the user to edit the value.</p><p>Now I would check if the value of SpinEdit changes if I change it programmatically.</p><p>I have added an input box to the .xaml file of the application, associated with the Value property of the SpinEditControl.</p><pre>&lt;mauiLibrary:SpinEditControl x:Name=&quot;spinEditControl&quot; <br>                             Value=&quot;1&quot; /&gt;<br><br>&lt;Label Text=&quot;Value:&quot;/&gt;<br>&lt;Entry BindingContext=&quot;{x:Reference spinEditControl}&quot;<br>       Text=&quot;{Binding Value, Mode=TwoWay}&quot;/&gt;</pre><p>Let’s run the application and check how the Value changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*b7Ol4Kyz4hvRkSh1ft-twA.gif" /><figcaption>Test how the value changes programmatically</figcaption></figure><p>It seems that if the value in added Entry changes, the value in SpinEditControl also changes, which means that data are successfully transferred from the MAUI code to the SwiftUI view. But suppose the value is changed using buttons, i.e. by interacting with SpinEditView directly. In that case, the value in Entry is not changed which means that the Value property of SpinEditControl is not changed too. Hence no data is passed from the Swift part to the .NET part. All that means SpinEditHendler doesn’t know anything about property changes on platform view. Let’s fix this.</p><p>Make a delegate property for SpinEditWrapper that is called when the value property changes.</p><pre>@objc @Published public var value: Decimal = 0 {<br>        didSet {<br>            guard let handler = onValueChanged else { return }<br>            handler(value)<br>        }<br>    }<br>    <br>@objc public var onValueChanged : ((Decimal) -&gt; Void)?</pre><p>And add a handler method to the SpinEditHandler for this delegate.</p><pre>protected override UIView CreatePlatformView() {<br>    Wrapper = new SpinEditWrapper();<br>    Wrapper.OnValueChanged = OnValueChanged;<br>    return Wrapper.UiView;<br>}<br><br>private void OnValueChanged(NSDecimal value) =&gt; VirtualView.Value = (decimal)value;</pre><p>So, when SpinEditWrapper changes value, SpinEditHandler.OnValueChanged() method is called via a delegate, which updates the value of SpinEditControl.</p><p>Let’s assemble all the libraries, ensure there are no errors, and run the test application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*JQAjQFsxtHcAWxnIdsIgEg.gif" /><figcaption>It works as expected</figcaption></figure><p>Now it seems to work as intended and the value of the Value property is synchronized between Swift and C# code.</p><p>Similarly, I have added the Interval, BorderColor, and FocusedBorderColor properties.</p><p>Let’s have a look at how the added properties work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*hrS_1icn7jfAwhBpM4pHdg.gif" /><figcaption>Nice! All properties work</figcaption></figure><p>Great! All the properties that we planned work as expected. This means that the challenge is complete and I have a fully functional MAUI SpinEditControl iOS part of which is implemented in Swift language with the use of UIKit and SwiftUI.</p><p>The complete code from this article is on <a href="https://github.com/temamarvel/SwiftUI_To_MAUI">my GitHub</a>.</p><p>Articles in this series:</p><ul><li><a href="https://medium.com/@tema.denisoff/how-to-use-swiftui-components-in-maui-f43c54d2173c">How to use SwiftUI components in MAUI</a></li></ul><p>Thanks for reading, see you in future publications. Bye!</p><p>Environment specifications:</p><ul><li>Xcode 14.1 (14B47b)</li><li>Command Line Tools 14.1 (14B47b)</li><li>Visual Studio 2022 for Mac Preview 17.5 Preview (17.5 build 437)</li><li>.NET 7.0.100</li><li>.NET MAUI workloads iOS/Android 7.0.49/7.0.100</li><li>macOS Ventura 13.0.1 (22A400)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e23135d1f2bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use SwiftUI components in MAUI]]></title>
            <link>https://medium.com/@tema.denisoff/how-to-use-swiftui-components-in-maui-f43c54d2173c?source=rss-ad0737e96be5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f43c54d2173c</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[maui]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Artem Denisov]]></dc:creator>
            <pubDate>Sun, 04 Dec 2022 18:23:34 GMT</pubDate>
            <atom:updated>2022-12-06T05:42:00.044Z</atom:updated>
            <content:encoded><![CDATA[<p>The standard MAUI component library includes an extensive list of essential elements for creating a user interface. You can use them to develop feature-rich cross-platform applications that look great on iOS and Android devices. But no matter how diverse the standard library is, it is not exhaustive, and sometimes there is a need to create a unique interface element.</p><p>In MAUI, you should follow the steps below to create a custom control:</p><ol><li>Create the component by the target platform tools (for example, using UIKit or SwiftUI for iOS).</li><li>Create the Binding library, which will bind the platform implementation and the .NET code.</li><li>Create a .NET control, which will be a projection of the platform component for MAUI applications.</li></ol><p>In this article, I will show how to go through all these steps to use modern SwiftUI components as a platform implementation for MAUI controls.</p><h4>Create the SwiftUI component library</h4><p>In Xcode, create a new <em>Framework project</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*91IHRHitRIKGLYNJPqIyOA.png" /><figcaption>Choose Framework template</figcaption></figure><p>Next, let’s configure the framework for further work:</p><ol><li>Go to the project file, and in the <em>General</em> tab in the <em>Supported Destinations</em> section, keep only the iPhone.</li><li>In the <em>Minimum Deployments</em> section, set the minimum iOS version to 15.5.</li><li>Go to the <em>Build Settings</em> tab and type “Build libraries” in the search box. Set Build Libraries for Distribution to Yes. Also, set the Skip Install parameter to No.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kk0xkH3mCiufIW3BRaxESQ.png" /><figcaption>SwiftUI_MAUI_Framework configuration</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y5wuqm4pc7gQlAxv82d72Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2_kSbtB3M1vpfgt2ToTpFQ.png" /></figure><p>Now delete the default SwiftUI_MAUI_Framework.h header file. We won’t need it. So, the setup is complete, and we can finally add the SwiftUI component we will work with. To do this, add a new file and select the SwiftUI View template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTQwdKBZJaWRxzDBcsSMcQ.png" /><figcaption>Choose SwiftUI template</figcaption></figure><p>Xcode creates a SwiftUI component template. Since this will be our first component, of course, I named it HelloWorld. Let’s change it a little bit to give the component some personality. I added an orange background under the Label.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4TM2ZdfpyrZQLi6ZQlQJtg.png" /><figcaption>HelloWorld SwiftUI component</figcaption></figure><p>As you can see in the preview, our changes are applied, and everything works fine.</p><p>Unfortunately, at the moment, we can’t bind SwiftUI views directly to the .NET code: MAUI can work only with UIKit classes, so we have to make a UIKit wrapper over the SwiftUI element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-kD5vQ_7SAKo4JXNx_lTAQ.png" /><figcaption>Choose the Swift File template</figcaption></figure><p>Create a wrapper class. SwiftUI already has the UIHostingController class, which allows you to display a View on SwiftUI as a UIKit UIView.</p><pre>@objc public class HelloWorldWrapper: NSObject, ObservableObject {<br>    var swiftUIView: HelloWorldView?<br>    var hostingController: UIHostingController&lt;HelloWorldView&gt;?<br>    <br>    @objc public var uiView: UIView? { hostingController?.view }<br>    <br>    public override init() {<br>        super.init()<br><br>        swiftUIView = HelloWorldView()<br>        guard let rootView = swiftUIView else { return }<br>        hostingController = UIHostingController(rootView: rootView)<br>    }</pre><p>The HelloWorldWrapper class is going to be the bridge between the SwiftUI View and the .NET part of the code. The wrapper class must be a descendant of NSObject in order for .NET to be able to work with it (the ObservableOblect protocol in this example is optional). Since the Swift and C# code are linked through Objective-C, you must mark the elements you want accessible from C# code with the <a href="http://twitter.com/objc">@objc</a> attribute (this can be applied to classes, protocols, methods, or fields).</p><p>So, the SwiftUI and UIKit classes are written. Let’s build the project to make sure everything is done without errors. After successfully compiling the project, you need to generate the .xcframework file that the Binding library will interact with.</p><p>To create the xcframework, start Terminal, go to the directory where the project file SwiftUI_MAUI_Framework.xcodeproj is, and then run the following commands:</p><pre>xcodebuild archive \<br> -scheme SwiftUI_MAUI_Framework \<br> -archivePath ../XCFrameworks/SwiftMaui-ios.xcarchive \<br> -sdk iphoneos \<br> SKIP_INSTALL=NO</pre><pre>xcodebuild archive \<br> -scheme SwiftUI_MAUI_Framework \<br> -archivePath ../XCFrameworks/SwiftMaui-sim.xcarchive \<br> -sdk iphonesimulator \<br> SKIP_INSTALL=NO</pre><pre>xcodebuild -create-xcframework \<br> -framework ../XCFrameworks/SwiftMaui-sim.xcarchive/Products/Library/Frameworks/SwiftUI_MAUI_Framework.framework \<br> -framework ../XCFrameworks/SwiftMaui-ios.xcarchive/Products/Library/Frameworks/SwiftUI_MAUI_Framework.framework \<br> -output ../XCFrameworks/SwiftUI_MAUI_Framework.xcframework</pre><p>Once these commands are successfully executed, the XCFrameworks folder will be created, containing two archive files (SwiftMaui-sim.xcarchive for the simulator and SwiftMaui-ios.xcarchive for real devices) as well as the necessary SwiftUI_MAUI_Framework.xcframework directory. This is what you are going to use for the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K3IDYGQnYlObUfKh2FQ81A.png" /><figcaption>The xcframework is created</figcaption></figure><p>Now the Swift framework is ready, and you can move on to the second stage.</p><h4>Creation of the Binding Library</h4><p>Visual Studio has a ready-made template for creating a Binding library. Let’s use it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GGFNre9yQLISVK9H_UAgOA.png" /><figcaption>Choose the iOS Binding Library template</figcaption></figure><p>Set up the created library project. The project contains two files ApiDefinition.cs and StructsAndEnums.cs. Rename the file ApiDefinition.cs to ApiDefinitions.cs. It will be helpful in further automatic generation of this file.</p><p>Make sure that after rename, the BuildAction option is set as follows:</p><ul><li>For file ApiDefinitions.cs — ObjcBindingApiDefinition</li><li>For file StructsAndEnums.cs — ObjcBindingCoreSource</li></ul><p>And the project file .csproj looks as shown in the screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e35qc-MgLKXM_RG6YRD0kg.png" /><figcaption>.csproj file after initial setup</figcaption></figure><p>Now add the SwiftUI_MAUI_Framework.xcframework that we created in the first part to the Native References section. Then go to the parameters of the connected framework and set the following options:</p><ul><li>Frameworks: Foundation UIKit</li><li>Kind: Framework</li><li>Smart Link: True</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eIw7oPDllE7-fxZ_R2xJTg.png" /><figcaption>Native library (SwiftUI_MAUI_Framework) properties</figcaption></figure><p>The Bindings library needs descriptions of the Swift classes we are going to interact with. Use the Objective Sharpie utility for this (you can download it <a href="https://learn.microsoft.com/en-us/xamarin/cross-platform/macios/binding/objective-sharpie/get-started">here</a>). When the utility is installed, you have to go to the XCFrameworks directory in the Terminal and execute the command:</p><pre>sharpie bind \<br> -sdk iphoneos \<br> -output ./ \<br> -namespace SwiftUI_MAUI_Framework \<br> -framework ./SwiftUI_MAUI_Framework.xcframework/ios-arm64/SwiftUI_MAUI_Framework.framework</pre><p>Sharpie analyzes the .xcframework and creates an ApiDefinitions.cs file in the XCFrameworks directory with descriptions of public classes, protocols, methods, and fields that we have marked with the @objc attribute. Replace the ApiDefinitions.cs file in the Bindings library directory with this generated file. Now the content of the file looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Qpym6fkacd2uENVJnu8XA.png" /><figcaption>Content of the ApiDefinitions.cs file</figcaption></figure><p>Let’s assemble the project and ensure we haven’t made any mistakes. The Bindings library is ready, so let’s move on to the third part.</p><h4>Creation of a .NET component</h4><p>In Visual Studio, there is a template for creating a library of MAUI components. Let’s use it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*USWJC2IP9hPTuwJBVkWKgQ.png" /><figcaption>Choose the .NET MAUI Class Library template</figcaption></figure><p>I’m only considering the iOS platform in this article, so after creation, I edited the project file .csproj and left only iOS and Android platforms as targets.</p><p>Also, it is necessary to add a reference to the Bindings library that was created in the second part. As a result, my project file looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mCTP0LBfq-DtVcaSynBxFg.png" /><figcaption>MAUI_Library .csproj file after setup</figcaption></figure><p>To associate a platform component with an MAUI control, there is a ViewHandler class in MAUI that is used to bind platform code (Swift, in our case) and C# code.</p><p>I have created a new class, a descendant of ViewHandler. This class is declared as a partial class in more than one file: the platform-independent part, and file for each platform, to implement platform-specific relationships and platform-specific logic (e.g., iOS and Android).</p><p>I have created three files for my handler class:</p><ul><li>iOSHelloWorldHandler.cs — for iOS</li><li>AndroidHelloWorldHandler.cs — for Android</li><li>CommonHelloWorldHandler.cs — for platform-independent part</li></ul><p>And the file for the MAUI class HelloWorldControl.cs itself, also in the platform-independent part.</p><p>Let’s take a detailed look at the contents of these files.</p><p><strong>HelloWorldControl.cs</strong></p><pre>namespace MAUI_Library;<br><br>public class HelloWorldControl : View { }</pre><p>In my example, the HelloWorldControl class is a simple descendant of Microsoft.Maui.Controls.View. It is used to display the platform SwiftUI View only, so it does not contain any additional code.</p><p><strong>CommonHelloWorldHandler.cs</strong></p><pre>using Microsoft.Maui.Handlers;<br><br>namespace MAUI_Library;<br><br>public partial class HelloWorldHandler { }</pre><p>Also, since there is no platform-independent code in this example, the general HelloWorldHandler part does not contain any code, but it is architecturally important that it exists.</p><p><strong>AndroidHelloWorldHandler.cs</strong></p><pre>using Android.Widget;<br>using Microsoft.Maui.Handlers;<br>using View = Android.Views.View;<br><br>namespace MAUI_Library;<br><br>//Implement handler for Android platform here<br>public partial class HelloWorldHandler: ViewHandler&lt;HelloWorldControl, Android.Views.View&gt; {<br>    public HelloWorldHandler() : base(PropertyMapper) { }<br><br>    public static IPropertyMapper&lt;HelloWorldControl, HelloWorldHandler&gt; PropertyMapper =<br>        new PropertyMapper&lt;HelloWorldControl, HelloWorldHandler&gt;(ViewHandler.ViewMapper) { };<br>    <br>    protected override Android.Views.View CreatePlatformView() {<br>        //it is just a mock view<br>        //create something real for a correct implementation<br>        return new View(null);<br>    }<br>}</pre><p>The Android part of the HelloWorldHandler implementation contains minimal workable code to implement inheritance from ViewHandler. But since the Android platform is not the subject of this article, I will not describe it in detail.</p><p><strong>iOSHelloWorldHandler.cs</strong></p><pre>using Microsoft.Maui.Handlers;<br>using SwiftUI_MAUI_Framework;<br>using UIKit;<br><br>namespace MAUI_Library;<br><br>public partial class HelloWorldHandler : ViewHandler&lt;HelloWorldControl, UIView&gt; {<br>    public HelloWorldHandler() : base(PropertyMapper) { }<br><br>    public static IPropertyMapper&lt;HelloWorldControl, HelloWorldHandler&gt; PropertyMapper =<br>        new PropertyMapper&lt;HelloWorldControl, HelloWorldHandler&gt;(ViewHandler.ViewMapper) { };<br><br>    protected override UIView CreatePlatformView() {<br>        var wrapper = new HelloWorldWrapper();<br>        return wrapper.UiView;<br>    }<br>}</pre><p>The iOS implementation of HelloWorldHandler is also inherited from the generic type ViewHandler&lt;TVirtualView, TPlatformView&gt;, where TVirtualView is the MAUI class that will be the representation for the platform implementation, and TPlatformView is the type of platform UIKit object to bind to the MAUI. In this example:</p><ul><li>TVirtualView is the HelloWorldControl described above</li><li>TPlatformView is UIView because the UIHostingController from the first part of the article wraps SwiftUI View into UIView</li></ul><p>An inheritance from ViewHandler requires the implementation of three components:</p><ul><li>Constructor</li><li>PropertyMapper property, which maps the properties of the platform component and the MAUI control</li><li>The CreatePlatformView() method, which instantiates the platform component class</li></ul><p>In the current example, the platform object has no properties or methods, so the PropertyMapper is empty, as well as the constructor does not execute any specific code. In this case, only the CreatePlatformView method does all the work. As you can see, it creates an instance of the Swift class HelloWorldWrapper, which is a wrapper for the SwiftUI HelloWorldView. And because we can only operate with UIKit classes, we get an instance of UIView from the HelloWorldWrapper, which represents HelloWorldView.</p><p>At this point, the entire code of the MAUI library is written, let’s build the library, and it becomes usable in the final applications. How to do this is explained below.</p><h4>How to use the MAUI library in an MAUI application</h4><p>As for creating the MAUI library, let’s use the Visual Studio template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tWFo6iuGL3-HWUU1YqX73Q.png" /><figcaption>Choose the .NET MAUI App template</figcaption></figure><p>Just like when I created the library, I edited the .csproj file and removed the unnecessary platform in order to speed up the building of the application and reduce possible conflicts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FAf2EUqi2xRGnp4m3cdVsA.png" /><figcaption>Kept only necessary target platforms in the .csproj file</figcaption></figure><p>In order to use a component from the MAUI library, you need to connect both the library itself and the Bindings library. As a result, the project file contains, in my case, the following lines:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MKq5tOlKFds4xB3J6c6MKg.png" /><figcaption>The project file with the required references added</figcaption></figure><p>As soon as the references to the libraries are added, it is first necessary to assign the class handler that I’m going to use to the application. To do this, add the HelloWorldHandler in the MauiProgram.cs file.</p><pre>using MAUI_Library;<br>using Microsoft.Extensions.Logging;<br><br>namespace MAUI_App;<br><br>public static class MauiProgram {<br>    public static MauiApp CreateMauiApp() {<br>        var builder = MauiApp.CreateBuilder();<br>        builder<br>            .UseMauiApp&lt;App&gt;()<br>            .ConfigureFonts(fonts =&gt;<br>            {<br>                fonts.AddFont(&quot;OpenSans-Regular.ttf&quot;, &quot;OpenSansRegular&quot;);<br>                fonts.AddFont(&quot;OpenSans-Semibold.ttf&quot;, &quot;OpenSansSemibold&quot;);<br>            })<br>            //Add setup handler here<br>            .ConfigureMauiHandlers(handlers =&gt;<br>            {<br>                handlers.AddHandler(typeof(HelloWorldControl), typeof(HelloWorldHandler));<br>            });<br><br>#if DEBUG<br>        builder.Logging.AddDebug();<br>#endif<br><br>        return builder.Build();<br>    }<br>}</pre><p>Now the MAUI application knows about the HelloWorldControl class and that you need to use HelloWorldHandler to manipulate it.</p><p>Let’s add a link to the MAUI_Library and HelloWorldControl itself to the MainPage.xaml markup file to see it in the application.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;<br>&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;<br>             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;<br>             xmlns:mauiLibrary=&quot;clr-namespace:MAUI_Library;assembly=MAUI_Library&quot;<br>             x:Class=&quot;MAUI_App.MainPage&quot;&gt;<br><br>    &lt;ScrollView&gt;<br>        &lt;VerticalStackLayout<br>            Spacing=&quot;25&quot;<br>            Padding=&quot;30,0&quot;<br>            VerticalOptions=&quot;Center&quot;&gt;<br><br>            &lt;!-- Autogenerated code here --&gt;<br>            &lt;...&gt;<br>            <br>            &lt;mauiLibrary:HelloWorldControl/&gt;<br>            <br>        &lt;/VerticalStackLayout&gt;<br>    &lt;/ScrollView&gt;<br><br>&lt;/ContentPage&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23kmDnk2AgAhycGStyD_JA.png" /><figcaption>The final markup of the application</figcaption></figure><p>Let’s build and run an application to see the SwiftUI HelloWorldView in a run-time MAUI application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*VK2PQ7U3jwvTHvx-xV2xNA.png" /><figcaption>It works!</figcaption></figure><p>As you can see, the SwiftUI component I created is displayed on the application’s main screen.</p><p>Obviously, this article only covered the concept of using SwiftUI components as a platform implementation of MAUI controls. In the following articles, I am going to show how to create more functional components and how to bind properties and methods from Swift code to C#.</p><p>The complete code from this article is on <a href="https://github.com/temamarvel/SwiftUI_To_MAUI">my GitHub</a>.</p><p>Articles in this series:</p><ul><li><a href="https://medium.com/@tema.denisoff/functional-maui-control-based-on-swiftui-view-e23135d1f2bc">Functional MAUI control based on SwiftUI view</a></li></ul><p>Thanks for reading, see you in future publications. Bye!</p><p>Environment specifications:</p><ul><li>Xcode 14.1 (14B47b)</li><li>Command Line Tools 14.1 (14B47b)</li><li>Visual Studio 2022 for Mac Preview 17.5 Preview (17.5 build 437)</li><li>.NET 7.0.100</li><li>.NET MAUI workloads iOS/Android 7.0.49/7.0.100</li><li>macOS Ventura 13.0.1 (22A400)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f43c54d2173c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>