<?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 Mo Sattar on Medium]]></title>
        <description><![CDATA[Stories by Mo Sattar on Medium]]></description>
        <link>https://medium.com/@mo-sattar?source=rss-410b16dc6f1e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tE-sW7eU4qiGZXpQSZrTqA.jpeg</url>
            <title>Stories by Mo Sattar on Medium</title>
            <link>https://medium.com/@mo-sattar?source=rss-410b16dc6f1e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:05:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mo-sattar/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[Run AI Models Locally: A Step-by-Step Guide to Deepseek, Ollama, and Seamless IDE Integration]]></title>
            <link>https://mo-sattar.medium.com/run-ai-models-locally-a-step-by-step-guide-to-deepseek-ollama-and-seamless-ide-integration-01d7ffa9f2f3?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/01d7ffa9f2f3</guid>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[code-generation]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Tue, 18 Feb 2025 18:30:52 GMT</pubDate>
            <atom:updated>2025-04-23T14:49:50.139Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*-zK-8NcZJ2RCbE0Q.png" /></figure><p>It’s been a while since I wrote my last Medium article, but now I have come out with an interesting topic: AI.</p><p>In this article, we will see how to use any model on your local machine, run it, and even add it to an IDE, for example, your Android Studio/IntelliJ</p><p>Running AI models on your local machine has never been easier, thanks to tools like <strong>Ollama</strong>, <strong>WebUI</strong>, and seamless integration with popular IDEs like <strong>IntelliJ</strong>. Whether you’re a developer, data scientist, or AI enthusiast, this guide will walk you through the steps to set up, run, and interact with AI models on your machine using these tools.</p><p><strong>Benefits of AI running locally on your machine:</strong></p><p>Imagine having the power of cutting-edge AI, like Deepseek or image generators, running directly on your computer, with no internet connection, no subscription fees, and no data privacy worries. While cloud-based AI services dominate headlines, running AI models locally unlocks unparalleled benefits: full control over your data, offline accessibility, and the freedom to customize models for your unique needs. Whether you’re a developer, a business handling sensitive information, or a curious tinkerer, this guide will show you why ditching the cloud and embracing local AI is not just possible — it’s the future.</p><h3>Prerequisites</h3><p>Before we begin, ensure you have the following:</p><ol><li><strong>Python Installed</strong>: Python 3.7 or later. Download it from <a href="https://www.python.org/">python.org</a>.</li><li><strong>Package Manager</strong>: pip3 for installing Python libraries.</li></ol><h3>Step 1: Set Up Ollama</h3><p><strong>Ollama</strong> is a powerful tool that simplifies running and managing AI models locally. It supports a variety of models, including LLMs (Large Language Models) like LLaMA, GPT, and more.</p><h3>Install <strong>Ollama</strong></h3><ul><li>Visit the official <a href="https://github.com/ollama/ollama">Ollama GitHub repository</a> or website for installation instructions.</li><li>Follow the steps for your operating system (Windows, macOS, or Linux).</li></ul><p>2. <strong>Run Ollama</strong>:</p><ul><li>After installation, start Ollama by running the following command in your terminal:</li></ul><pre>ollama serve</pre><ul><li>This will start the Ollama server, allowing you to interact with it via the command line or API.</li></ul><p>3. <strong>Pull a Model</strong>:</p><ul><li>Ollama provides pre-configured models. For example, to use the <a href="https://ollama.com/library/deepseek-r1">deepseek-r1</a> model.</li><li>You can <a href="https://ollama.com/search">explore</a> Ollama Models <a href="https://medium.com/justeattakeaway-tech/run-ai-models-locally-a-step-by-step-guide-to-deepseek-ollama-and-seamless-ide-integration-904811e64ad9#c7ce">(1)</a> and choose what’s best for you and your machine, also you can have multiple models at the same time.</li><li>Run the following command:</li></ul><pre>ollama pull deepseek-r1:7b</pre><ul><li>This will download DeepSeek model and make it available for use.</li></ul><p>4. <strong>Run the Model</strong>:</p><ul><li>Start interacting with the model using:</li></ul><pre>ollama run deepseek-r1:7b</pre><ul><li>You can now input prompts and receive responses directly in your terminal.</li></ul><h3>Step 2: Set Up WebUI for a User-Friendly Interface</h3><p>While Ollama provides a command-line interface, you prefer a more intuitive <strong>UI</strong> to interact with your AI models. Tools like <a href="http://open-webui/"><strong>Open-WebUI</strong></a> can help you create a web-based interface.</p><p>How to Install 🚀</p><p><strong>Installation via Python pip 🐍</strong></p><p>Open WebUI can be installed using pip, the Python package installer. Before proceeding, ensure you’re using Python 3.11 to avoid compatibility issues.</p><p>1. <strong>Install Open WebUI</strong>:<br> Open your terminal and run the following command to install Open WebUI:</p><pre>pip3 install open-webui</pre><p>2. <strong>Running Open WebUI</strong>:<br> After installation, you can start Open WebUI by executing:</p><pre>open-webui serve</pre><p>This will start the Open WebUI server, which you can access at [http://localhost:8080](http://localhost:8080)</p><p>Or if you want to use it with <strong>Docker</strong> so you don’t need to write the last command each time to open your system, and do you want to have it in a different environment</p><p><strong>1- Install Docker on your machine from </strong><a href="https://docs.docker.com/desktop/setup/install/mac-install/"><strong>here</strong></a><strong>.</strong></p><p><strong>2-</strong><a href="https://github.com/open-webui/open-webui?tab=readme-ov-file#installation-with-default-configuration"><strong> Connect it to your Ollama</strong></a> <a href="https://docs.docker.com/desktop/setup/install/mac-install/"><strong>here</strong></a></p><pre>docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main</pre><blockquote><em>PS2: I had a problem after installing python and had to run </em><a href="https://stackoverflow.com/a/71923962/3553843"><em>this solution</em></a></blockquote><pre>export PATH=&quot;$PATH:/Applications/Docker.app/Contents/Resources/bin/&quot;</pre><ul><li>Then it will be shown in your Docker Desktop like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*ChqHMbTZZ807-c5g.png" /></figure><ul><li>First time it will require email and password, just a security measure so no one on your network can access it without credentials.</li><li>Then, if you click on the port you will see the following:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*lz0tIMSIDLe1xyZy.png" /></figure><ul><li>Create your new admin and Tadaaa. You have it.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*cva6N3MdY1fwh_uR.png" /><figcaption>Open-webui</figcaption></figure><ul><li>If you have multiple models installed, you can select which model you want to use in the top left corner.</li><li>If you want to add it to your Dock for easy access:</li><li>on Google-Chrome</li><li>Settings -&gt; cast, save and share -&gt; Install Page as an app</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/0*QXCOp5vxzry8hCMu.png" /><figcaption>Mac dock</figcaption></figure><h3>Step 3: Integrate with IntelliJ IDEA/Android Studio</h3><p>IntelliJ IDEA is a powerful IDE for developers, and you can integrate AI models directly into your workflow using plugins .</p><blockquote><em>PS: you can search plugin for your favorite IDE and follow their installation</em></blockquote><h3>Install the Proxy AI Plugin:</h3><blockquote><em>Proxy AI is an AI-powered code assistant designed to help you with various programming activities. It’s a powerful alternative to GitHub Copilot, AI Assistant, Codiumate, and other JetBrains plugins.</em></blockquote><ol><li><strong>Open IntelliJ/Android Studio IDE</strong>:</li><li>Go to File &gt; Settings &gt; Plugins.</li><li>Search for the <strong>CodeGPT</strong> plugin and install it if not already installed. <br>Or download it from <a href="https://plugins.jetbrains.com/plugin/21056-codegpt">JetBrains Marketplace</a>.</li><li>Then it will be shown in 2 places in the right and the bottom toolbar</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/0*8xxiKX0QAkti7Yx8.png" /></figure><p>5. Our last step is to connect the Proxy AI plugin with our model. We will click on the button in the bottom toolbar, and then:</p><ul><li>Open Settings. from the right bottom corner after clicking on the Proxy AI icon.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/0*fGfI-Z5wLHwfvagz.png" /></figure><ul><li>You will see the following: click on the last one Ollama (local)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*cetfgS2QNhsKVHmp.png" /></figure><ul><li>Then choose your model ( if you follow the steps, you see deepseek-r1:7b )</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*FYDJJ2ExlRt2mFfo.png" /></figure><p>🚀 Now Happy coding !!🚀</p><h3>Step 4: Fine-Tuning and Customization</h3><p>Once you have the basic setup running, you can explore advanced features:</p><ol><li><strong>Fine-Tune Models</strong>:</li></ol><ul><li>Use your dataset to fine-tune pre-trained models for specific tasks.</li></ul><ol><li><strong>Experiment with Different Models</strong>:</li></ol><ul><li>Ollama supports multiple models. Try experimenting with GPT, Mistral, or other LLMs.</li></ul><h3>Conclusion</h3><p>By combining <strong>Ollama</strong>, <strong>WebUI</strong>, and <strong>IntelliJ IDEA</strong>, you can create a powerful local AI development environment. Whether you’re building chatbots, experimenting with language models, or integrating AI into your applications, this setup provides the flexibility and ease of use you need.</p><p>With these tools, running AI models on your machine is no longer a daunting task. Start exploring, experimenting, and building today!</p><p>Tell us which model and plugin work best for you ?</p><p>All content provided in this article is for informational purposes only. The chosen LLM is for illustrative purposes only, and we do not engage in any legal matters.</p><h3>(1) Models Explanation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*N0q9ivgFJnKSytbb.png" /><figcaption>deepseek-r1 Ollama</figcaption></figure><p><strong>AI models come in different sizes, like software versions tailored for specific needs:</strong></p><ul><li><strong>Small models</strong> (e.g., 1.5B parameters, 1.1GB) are lightweight and fast, ideal for basic tasks like simple chatbots or text summaries. They work on most computers but lack depth.</li><li><strong>Mid-sized models</strong> (e.g., 7 B-14 B parameters, 4–9GB) balance speed and capability. They handle complex questions, translations, or coding help, requiring a decent computer with enough RAM.</li><li><strong>Large models</strong> (e.g., 32 B-70 B parameters, 20–43GB) are “powerhouse” models for advanced tasks like creative writing, detailed analysis, or mimicking human conversation. These need powerful hardware (like a gaming PC or workstation) to run smoothly.</li></ul><p><strong>Key idea</strong>: The number of <strong>parameters</strong> (e.g., 1.5B = 1.5 billion) reflects how much “knowledge” the model has. <strong>More parameters </strong>= <strong>smarter</strong> but <strong>harder</strong> to run. Choose based on your task and the computer’s strength!</p><p><a href="https://medium.com/justeattakeaway-tech/run-ai-models-locally-a-step-by-step-guide-to-deepseek-ollama-and-seamless-ide-integration-904811e64ad9#6d03">[↑]Back To Top</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=01d7ffa9f2f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Architecture explained Part 1 (UI-Layer)]]></title>
            <link>https://mo-sattar.medium.com/android-architecture-explained-part-1-ui-layer-78378ca717f6?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/78378ca717f6</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[android-architecture]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Thu, 08 Sep 2022 13:56:45 GMT</pubDate>
            <atom:updated>2022-09-09T08:13:30.224Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m3_0fanGF_OIPhtbOHvV3Q.png" /></figure><p>if this is the first article you landed on, there exists an intro to explain the <a href="https://medium.com/@m-abdelsattar/how-to-write-a-well-structured-app-using-the-new-android-architecture-from-google-b73ef6ba41cc">big picture</a> please read it first and continue this article.</p><p>To recall our app it will be <a href="https://github.com/Abdelsattar/Notes"><strong>Notes app</strong></a><strong> </strong>will contain the following:</p><p><strong>1- List of notes in the app</strong></p><p>2- Note details screen</p><p>3- Add a note screen.</p><p>Now is the dirty work we need to open <strong>Android Studio</strong> -&gt; <strong>File</strong> -&gt; N<strong>ew</strong> -&gt; <strong>New Project</strong>.</p><p>If you have the latest Android studio ( I’m using Electric El) then you can start with <strong>Empty Compose Activity.</strong></p><p>This will create the following:</p><ul><li>MainActivity.kt</li><li>ui/theme/Color.kt</li><li>ui/theme/Theme.kt</li><li>ui/theme/Type.kt</li></ul><p>We will start with the First screen and focus on it right now, which will contain The list of notes.</p><p>With Jetpack Compose it’s pretty easy with List so we will add a title and a list as follows in MainActivity</p><p><strong>1- List of notes (static data in the UI for now)</strong></p><p>this will be our start screen so in our MainActivity we will have the following</p><pre><strong>class </strong>MainActivity : ComponentActivity() {<br>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        <em>setContent </em><strong>{<br>            </strong>NotesTheme<strong>{<br>                </strong><em>NotesScreen</em>()<br>            <strong>}<br>        }<br>    </strong>}<br>}</pre><p>and then we will create a new Kotlin file and Name it NotesScreen</p><p>so for our basic screen, we will create the fun for the screen as the following:</p><pre>@Composable<br><strong>fun </strong>NotesScreen() {}</pre><p>we will build our screen now so first of all we will add a <a href="https://developer.android.com/reference/kotlin/androidx/compose/material/package-summary#scaffold">Scaffold</a> as this layout provides slots for the most common top-level Material components, such as TopAppBar, BottomAppBar, FloatingActionButton, and Drawer</p><pre>@Composable<br><strong>fun </strong>NotesScreen() {<br>    Scaffold(<br>        topBar = <strong>{ </strong>AppBar() <strong>}</strong>, <br>        content = <strong>{ </strong>ScreenContent(<strong>it</strong>) <strong>} // we must pass the paddingValues in use it in the child<br>    </strong>)<br>}</pre><p>so our app bar will be very simple that has the title as our app name and we change the background to white</p><pre>@Composable<br><strong>fun </strong>AppBar(){<br>    SmallTopAppBar(<br>        title = <strong>{ </strong>Text(text = stringResource(id = R.string.<em>app_name</em>)) <strong>}</strong>,<br>        colors = TopAppBarDefaults.smallTopAppBarColors(<br>            containerColor = <em>White<br>        </em>)<br>    )<br>}</pre><p>and then we will need to build `ScreenContent()` but first we need to know that it will contain a list of notes so we need to build our note item first it will have a simple title and date as follows:</p><pre>@Composable<br><strong>fun </strong>NoteItem(note: Note) {<br>    Column(<br>        modifier = Modifier.<em>padding</em>(8.dp)<br>    ) <strong>{<br>        </strong>Text(<br>            modifier = Modifier<br>                .<em>fillMaxWidth</em>()<br>                .<em>padding</em>(4.dp),<br>            text = note.<strong>title</strong>,<br>            style = MaterialTheme.<strong>typography</strong>.<strong>titleMedium</strong>,<br>        )<br>        <em>Text</em>(<br>            modifier = Modifier<br>                .<em>fillMaxWidth</em>()<br>                .<em>padding</em>(horizontal = 4.dp),<br>            text = note.<strong>date</strong>,<br>            style = MaterialTheme.<strong>typography</strong>.<strong>titleSmall<br>        </strong>)<br>    <strong>}<br></strong>}</pre><p>we created a model class to help us a little with the item :</p><pre><strong>data class </strong>Note(<strong>val title</strong>:String, <strong>val date</strong>:String)</pre><p>Then we will create our ScreenContent() and build a list with notes items in it</p><pre>@Composable<br><strong>fun </strong>ScreenContent(paddingValues: PaddingValues) {</pre><pre>   // create static notes in the ui that will be replaced later  <br><strong>   val </strong>notes: List&lt;Note&gt; = <em>listOf</em>(<br>        Note(<strong>&quot;Title1&quot;</strong>, <strong>&quot;12:23&quot;</strong>),<br>        Note(<strong>&quot;Title2&quot;</strong>, <strong>&quot;16:12&quot;</strong>),<br>        Note(<strong>&quot;Title3&quot;</strong>, <strong>&quot;21:32&quot;</strong>)<br>    )</pre><pre>   //Build a card that will contain the list of the notes <br>   Card(<br>        modifier = Modifier<br>            .<em>padding</em>(paddingValues)<br>            .<em>padding</em>(16.dp)<br>    ) <strong>{</strong></pre><pre>       // create our list and gave it a background<br><strong>       </strong>LazyColumn (<br>            Modifier.<em>background</em>(<em>lightGray200</em>)<br>        )<strong>{<br>            </strong>items(<br>                items = notes, // passing the notes list<br>                itemContent = <strong>{<br>                    </strong>NoteItem(<strong>it</strong>) //building the note items basd on the list<br>                <strong>}</strong>)<br>        <strong>}<br>    }<br></strong>}</pre><p>if you run the app right now you will see the following screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yxHBTnFE0qraCs1DenpX9w.png" /><figcaption>Screen with 3 notes</figcaption></figure><p>then we need some improvements to our code and include a viewModel that will hold the UI state in this part I’ll use Hilt for DI but if you don’t know it you can learn from <a href="https://developer.android.com/training/dependency-injection/hilt-android">here</a>.</p><p>to use hilt with Compose we need to add one special lib for it as followed:</p><pre>implementation <strong>&#39;androidx.hilt:hilt-navigation-compose:1.0.0&#39;</strong></pre><p>and then we will create our ViewModel as follows:</p><pre>// anotating it as hilt knows it&#39;s a view model and it&#39;s required<br>// to have an @Ijecct constructor<br>@HiltViewModel<br><strong>class </strong>NotesViewModel @Inject <strong>constructor</strong>() : ViewModel() {}</pre><p>we will create our Ui state inside our view model and will hold all data for this out notes list as follows:</p><p>we need to create our Ui state class that will hold the data inside and make it very simple now:</p><pre>// we have an empty list so this will be our defult value <br><strong>data class </strong>NotesUiState(<strong>val notes</strong>: List&lt;Note&gt; = <em>emptyList</em>())</pre><p>and inside our view model, we will create the State flow of our UI state</p><pre>// we need to pass our init state to the state flow <br><strong>val uiState </strong>= <em>MutableStateFlow</em>(NotesUiState())</pre><p>then we will assume later on we need to update our UI state and will do it as follows:</p><pre><strong>init </strong>{<br>    <strong>uiState</strong>.<em>update </em><strong>{<br>        it</strong>.copy(<br>            notes = <em>listOf</em>(<br>                Note(<strong>&quot;Title1&quot;</strong>, <strong>&quot;12:23&quot;</strong>),<br>                Note(<strong>&quot;Title2&quot;</strong>, <strong>&quot;16:12&quot;</strong>),<br>                Note(<strong>&quot;Title3&quot;</strong>, <strong>&quot;21:32&quot;</strong>)<br>            )<br>        )<br>    <strong>}<br></strong>}</pre><p>and then our complete view model will look like this:</p><pre>@HiltViewModel<br><strong>class </strong>NotesViewModel @Inject <strong>constructor</strong>() : ViewModel() {<br><br>    <strong>val uiState </strong>= <em>MutableStateFlow</em>(NotesUiState())<br><br>    <strong>init </strong>{<br>        <strong>uiState</strong>.<em>update </em><strong>{<br>            it</strong>.copy(<br>                notes = <em>listOf</em>(<br>                    Note(<strong>&quot;Title1&quot;</strong>, <strong>&quot;12:23&quot;</strong>),<br>                    Note(<strong>&quot;Title2&quot;</strong>, <strong>&quot;16:12&quot;</strong>),<br>                    Note(<strong>&quot;Title3&quot;</strong>, <strong>&quot;21:32&quot;</strong>)<br>                )<br>            )<br>        <strong>}<br>    </strong>}</pre><pre>// you can here remove the data from the init block and get it from your server or whenever you want to get your data<br>}</pre><p>after changing the data to come from the view model we need to have some modifications to the view so we can make our data observe the change of the uiState.</p><p>we will use the <a href="https://medium.com/androiddevelopers/consuming-flows-safely-in-jetpack-compose-cde014d0d5a3">recommended way</a> of observing the data in Compose.</p><p>and we need to add our lifecycle lib</p><pre>implementation &quot;androidx.lifecycle:lifecycle-runtime-compose:2.6.0-alpha01&quot;</pre><p>then back to our NoteScreen() we will first need to inject our view model and create our observer on the screen and bind the data to its view</p><pre><strong>// </strong>inject our view model ot the constructor</pre><pre>@Composable<br><strong>fun </strong>NotesScreen(viewModel: NotesViewModel = hiltViewModel()) {</pre><pre>  //get our ui state with awareness of the life cycle<br>   <strong>val </strong>state <strong>by </strong>viewModel.<strong>uiState</strong>.<em>collectAsStateWithLifecycle</em>()<br>   Scaffold(<br>     topBar = <strong>{ </strong>AppBar() <strong>}</strong>,</pre><pre>     //we will pass our list of notes to screenContent<br>     content = <strong>{ </strong><em>ScreenContent</em>(<strong>it</strong>, state.<strong>notes</strong>) <strong>}<br>   </strong>)<br>}<br><br>// we will remove the created list in screen content and use our list that comes from the view model<br>@Composable<br><strong>fun </strong>ScreenContent(paddingValues: PaddingValues, notes: List&lt;Note&gt;) {<br>    Card(<br>        modifier = Modifier<br>            .<em>padding</em>(paddingValues)<br>            .<em>padding</em>(16.<em>dp</em>)<br>    ) <strong>{<br>        </strong>LazyColumn (<br>            Modifier.<em>background</em>(<em>lightGray200</em>)<br>        )<strong>{<br>            </strong><em>items</em>(<br>                items = notes,<br>                itemContent = <strong>{<br>                    </strong>NoteItem(<strong>it</strong>)<br>                <strong>}</strong>)<br>        <strong>}<br>    }<br></strong>}</pre><p>now we achieved and built the whole UI part with its own ViewModel and you can run it now you will see the same UI but depending on the data in the view model</p><p>you can see the changes in this article here in this <a href="https://github.com/Abdelsattar/Notes/commit/e73b52de16edac9f4045310ac42980ef17ffb961">GitHub project commit</a>.</p><p><a href="https://github.com/Abdelsattar/Notes">https://github.com/Abdelsattar/Notes</a></p><p>if you have any questions let me know in the comments.</p><p>don’t forget to clap, and follow me for more articles, and you can find me on GitHub.</p><p><a href="https://github.com/Abdelsattar">https://github.com/Abdelsattar</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78378ca717f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Single Activity App with Bottom Navigation built by Jetpack Compose]]></title>
            <link>https://mo-sattar.medium.com/single-activity-app-with-bottom-navigation-built-by-jetpack-compose-c7befba558c6?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7befba558c6</guid>
            <category><![CDATA[jetpack-navigation]]></category>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Tue, 02 Aug 2022 16:09:11 GMT</pubDate>
            <atom:updated>2022-08-02T16:09:11.760Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ZHAa32_wNRMh5Jf1ssI03w.png" /><figcaption>source <a href="https://material.io/">https://material.io/</a></figcaption></figure><p>It’s been a while now since Jetpack Compose became more stable and has more features and google pushing toward using <strong>jetpack compose</strong> in the app. I’m really excited to migrate to Jetpack compose and I was trying to understand how it will work to have a single activity app with Compose as our app is already written in one activity all with fragments so I tried to know the big picture before starting migrates everything.</p><p>I managed to understand how <strong>navigation compose</strong> is working with a single activity so I wanted to share it with a tiny demo to explain it and here we go let’s build our small demo to understand it.</p><p>So we will create an app that has a <strong>bottom navigation bar</strong> with 3 screens and once you clicked on any of them you will go to this screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*h6a7sLesgvIbBOU1nPr-SA.gif" /><figcaption>Output application</figcaption></figure><p>First of all, we need to create a <strong>new projec</strong>t with a compose activity as this will create for us</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*M8TVdGGIMvsIQOsEv2d8gQ.png" /></figure><p><a href="https://github.com/Abdelsattar/BottomNavigationCompose/commit/b8e17a5efa42dae01e2b13b8794137e5dbcc9d19">0- add </a><a href="https://github.com/Abdelsattar/BottomNavigationCompose/commit/b8e17a5efa42dae01e2b13b8794137e5dbcc9d19">navigation-compose</a> the last version now is “2.5.1” and that’s what we will use.</p><pre>implementation &#39;androidx.navigation:navigation-compose:2.5.1&#39;</pre><p>and now we need to create our dummy screens for now let’s make a new package under ui and call it screens and then let’s create our screens</p><p><strong>1- </strong><a href="https://github.com/Abdelsattar/BottomNavigationCompose/commit/e1099a6062c60668fe1e7334ef1cc129ff90b1ac"><strong>Add our Compose Screens</strong></a></p><p>we will create simple screens, we just add a box that has a background and inside this box, we are adding the title of the screen</p><p>Home Screen:</p><pre>@Composable<br>fun HomeScreen() {<br>    Box(<br>        modifier = Modifier<br>            .<em>fillMaxSize</em>()<br>            .<em>background</em>(Color.Magenta),<br>        contentAlignment = Alignment.Center<br>    ) <strong>{<br>        </strong>Text(<br>            text = &quot;HOME&quot;,<br>            fontSize = MaterialTheme.typography.h3.fontSize,<br>            fontWeight = FontWeight.Bold,<br>            color = Color.White<br>        )<br>    <strong>}<br></strong>}</pre><p>then for Profile Screen:</p><pre>@Composable<br>fun ProfileScreen() {<br>    Box(<br>        modifier = Modifier<br>            .<em>fillMaxSize</em>()<br>            .<em>background</em>(Color.Blue),<br>        contentAlignment = Alignment.Center<br>    ) <strong>{<br>        </strong>Text(<br>            text = &quot;PROFILE&quot;,<br>            fontSize = MaterialTheme.typography.h3.fontSize,<br>            fontWeight = FontWeight.Bold,<br>            color = Color.White<br>        )<br>    <strong>}<br></strong>}</pre><p>and then NotificationsScreen</p><pre><a href="http://twitter.com/Composable">@Composable</a><br>fun NotificationsScreen() {<br>    Box(<br>        modifier = Modifier<br>            .fillMaxSize()<br>            .background(Color.Red),<br>        contentAlignment = Alignment.Center<br>    ) {<br>        Text(<br>            text = &quot;NOTIFICATIONS&quot;,<br>            fontSize = MaterialTheme.typography.h3.fontSize,<br>            fontWeight = FontWeight.Bold,<br>            color = Color.White<br>        )<br>    }<br>}</pre><p>and then we will make our MainScreen that will hold and navigate between these screens</p><p>we will add it to an empty screen right now and will back to it later</p><pre>@Composable<br>fun MainScreen() {<br>   <strong>//</strong> we will add all logic later in the article <strong><br></strong>}</pre><p>and now we have all screens we have let’s add the main screen to MainActivity</p><p>so our MainActivity will look like this</p><pre>class MainActivity : ComponentActivity() {<br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        <em>setContent </em><strong>{<br>            </strong>MyBottomNavigationTheme <strong>{<br>                </strong>MainScreen()<br>            <strong>}<br>        }<br>    </strong>}<br>}</pre><p>now we have the skeleton of the app let’s handle the logic inside the main screen and add our bottom navigation.</p><p><strong>2-</strong><a href="https://github.com/Abdelsattar/BottomNavigationCompose/commit/b00f6fd45bd0d4e371f098a3dd385ba59f64d1ba"><strong>Add Bottom Navigation Graph</strong></a></p><p>we need to create a sealed class that holds all our screens for navigation which will contain route , title and icon</p><pre>sealed class BottomBarScreen(<br>    val route: String,<br>    val title: String,<br>    val icon: ImageVector<br>) {<br>    object Home : BottomBarScreen(<br>        route = &quot;home&quot;,<br>        title = &quot;Home&quot;,<br>        icon = Icons.Default.<em>Home<br>    </em>)<br><br>    object Notifications : BottomBarScreen(<br>        route = &quot;notifications&quot;,<br>        title = &quot;Notifications&quot;,<br>        icon = Icons.Default.<em>Notifications<br>    </em>)<br><br>    object Profile : BottomBarScreen(<br>        route = &quot;profile&quot;,<br>        title = &quot;Profile&quot;,<br>        icon = Icons.Default.<em>Person<br>    </em>)<br>}</pre><p>and then let’s create our bottomNavGraph by Compose as follow</p><pre>@Composable<br>fun BottomNavGraph(navController: NavHostController) {</pre><pre>//Creat our main navhost <br>    NavHost( <br>        //pass navController<br>        navController = navController,</pre><pre>        //we are declaring our default destination as home <br>        startDestination = BottomBarScreen.Home.route<br>    ) <strong>{<br>        </strong><em>composable</em>(route = BottomBarScreen.Home.route) <strong>{<br>            </strong>HomeScreen()<br>        <strong>}<br>        </strong><em>composable</em>(route = BottomBarScreen.Notifications.route) <strong>{<br>            </strong><em>NotificationsScreen</em>()<br>        <strong>}<br>        </strong><em>composable</em>(route = BottomBarScreen.Profile.route) <strong>{<br>            </strong>ProfileScreen()<br>        <strong>}<br>    }<br></strong>}</pre><p><strong>3- </strong><a href="https://github.com/Abdelsattar/BottomNavigationCompose/commit/b7e544654d475b5feb9c545b1aefcea4cc428de4"><strong>Add bottom Navigation</strong></a></p><p>and then let’s back to our screen to fill the gaps and it will look like this:</p><pre>@Composable<br>fun MainScreen() {<br>    <em>//get our navController for the app <br>    </em>val navController = rememberNavController()<br>    Scaffold(<br>        bottomBar = <strong>{ <br>            </strong><em>//create our bottom bar with navigation <br>            </em>BottomBar(navController = navController) <br>        <strong>}<br>    </strong>) <strong>{<br>        </strong><em>//create our nav graph we implemented with all screen in bottom bar<br>        </em>BottomNavGraph(navController = navController)<br>    <strong>}<br></strong>}</pre><p>and you will see BottomBar is red so let’s create it.</p><pre>@Composable<br>fun BottomBar(navController: NavHostController) {<br>    <em>//Create our screens to pass it to our bottom navigation.<br>    </em>val screens = <em>listOf</em>(<br>        BottomBarScreen.Home,<br>        BottomBarScreen.Profile,<br>        BottomBarScreen.Notifications,<br>    )<br>    <em>//observe whenever our back stack changed<br>    </em>val navBackStackEntry by navController.<em>currentBackStackEntryAsState</em>()<br>    val currentDestination = navBackStackEntry?.destination<br><br>    <em>//Create our composable BottomNavigation already predefined function comes with mateial lib<br>    BottomNavigation </em><strong>{<br>        </strong>screens.<em>forEach </em><strong>{ </strong>screen <strong>-&gt;<br>            </strong>AddItem(<br>                screen = screen,<br>                currentDestination = currentDestination,<br>                navController = navController<br>            )<br>        <strong>}<br>    }<br></strong>}</pre><p>and inside our <em>BottomNavigation we have to add an item for each screen let’s see what it looks like.</em></p><pre>@Composable<br>fun RowScope.AddItem(<br>    screen: BottomBarScreen,<br>    currentDestination: NavDestination?,<br>    navController: NavHostController<br>) {<br>    <em>// for each screen we are calling this to add Navigation item to BottomNavigation<br>    </em>BottomNavigationItem(<br>        label = <strong>{<br>            </strong>Text(text = screen.title)<br>        <strong>}</strong>,<br>        icon = <strong>{<br>            </strong>Icon(<br>                imageVector = screen.icon,<br>                contentDescription = &quot;Navigation Icon&quot;<br>            )<br>        <strong>}</strong>,<br>        <em>// checking if this item is the  current Destination to mark it selected<br>        </em>selected = currentDestination?.<em>hierarchy</em>?.<em>any </em><strong>{<br>            it</strong>.route == screen.route<br>        <strong>} </strong>== true,<br>        <em>//make unselected item as faded<br>        </em>unselectedContentColor = <em>LocalContentColor</em>.current.copy(alpha = ContentAlpha.disabled),<br>        onClick = <strong>{<br>            </strong><em>//make the navController navgiate to our screen as we are using BottomNavGraph<br>            </em>navController.navigate(screen.route) <strong>{<br>                </strong><em>// we are popup current destination so when click back button it will always go to home<br>                </em>popUpTo(navController.graph.<em>findStartDestination</em>().id)<br>                launchSingleTop = true<br>            <strong>}<br>        }<br>    </strong>)<br>}</pre><p>and that’s it. build and run the app so you can see you did amazing 🤩🤩</p><p>follow me for more articles and if you have any comments 📃 please leave them in the comments.</p><p>and here is the demo link you can explore and give it a star:</p><p><a href="https://github.com/Abdelsattar/BottomNavigationCompose">https://github.com/Abdelsattar/BottomNavigationCompose</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7befba558c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to write a well-Structured app using the new Android Architecture from Google]]></title>
            <link>https://mo-sattar.medium.com/how-to-write-a-well-structured-app-using-the-new-android-architecture-from-google-b73ef6ba41cc?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b73ef6ba41cc</guid>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-architecture]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Sat, 30 Jul 2022 20:37:05 GMT</pubDate>
            <atom:updated>2022-09-08T15:50:04.476Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G8Hun0fDtRnd3bCKUdhZjg.png" /></figure><p>Google recently published its latest update for Android Architecture to simplify it and make it more clear and add the Domain layer as optional in the architecture which is similar to the clean architecture in my opinion.</p><p>In <strong>Figure 1. </strong>we have 3 big layers (UI layer &lt;&gt; Data Layer) and another (<strong>optional</strong>) layer is the domain layer.</p><p>So how this article will go?</p><ul><li>We will describe each layer in a different article starting with <strong>1- UI layer</strong> and <strong>2- data layer</strong> to explain the<strong> basic layers</strong> and then we will add the <strong>optional layer </strong>(Domain)<strong> </strong>in the last article for more separation.</li></ul><p>Starting with the</p><p><strong>1- UI layer:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2dRWVyhWbxv2llL2p4e5zA.png" /><figcaption><strong>Figure 2.</strong> The UI layer’s role in app architecture.</figcaption></figure><blockquote>The role of the UI is to display the application data on the screen and also to serve as the primary point of user interaction.</blockquote><p>So basically what is inside this layer there will be our UI elements which in this case will be our view (activity, fragment, or Compose) later on in the demo will be our Compose, and the state holders will be our view model which will contain a UiState variable, ( will explain with an example later).</p><p><strong>2-Data Layer</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-mePGUY3fzzXvlAKiaBnNQ.png" /><figcaption><strong>Figure 3. </strong>The Data layer’s role in app architecture.</figcaption></figure><blockquote>The data layer is made of <em>repositories</em> that each can contain zero to many <em>data sources</em></blockquote><p>The data layer will be responsible for getting/updating any data related to the app and it will decide also if we are gonna save it locally or remote and also how we manage our single source of truth and how we deal with it ( will explain with an example later).</p><p><strong>3- Domain layer (Optional)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A1cscgHAh_PbhQ7N0Ew3Sw.png" /><figcaption><strong>Figure 4. </strong>The Domain layer’s role in app architecture.</figcaption></figure><blockquote>The domain layer is responsible for encapsulating complex business logic, or simple business logic that is reused by multiple ViewModels</blockquote><p>Cause it’s not needed in all apps and it should be decided by the developer so it’s optional as it’s based on the app scalability if you think it will grow so much and there exists some business logic that might be used in multiple places best place to put it is in the domain layer.</p><p><strong>Big Picture Flow</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JS8RekjN-YEsw2tl0P0r9A.png" /><figcaption><strong>Figure 5. </strong>Flow from UI to the domain to the data layer</figcaption></figure><p>to have the big picture in your mind this flow is supposed to be in your head while designing the app I made the arrows 2 ways as we will use flow in the app we will make it, and it’s kind of listening to the data all the time and once something changes it will notify back the flow.</p><p>Now we know how it will go let’s get our hands dirty and start working layer by layer with a simple app idea and apply all of this to the app and see how it goes.</p><p>While learning this new architecture I had some problems when I’m building the whole flow I didn’t know where to put these things and when you are working on a complex business app it becomes more challenging to migrate to the new approach while delivering new features as this it’s not simple as the android developers show with tiny snippet we will try to put all in action and see how it will go.</p><p><strong>Android Architecture in practice:</strong></p><p>To understand everything with a use case step by step we will create together a <strong>Notes App </strong>and divide it into 3 more articles to go through each layer by code and build an app from scratch that follows this architecture to make our app scalable and testable.</p><p><strong>Notes app </strong>will contain 3 screen</p><ul><li>List of notes in the app</li><li>Note details screen</li><li>Add note screen.</li></ul><p>Tech will be used in this app:</p><ul><li>Kotlin</li><li>MVVM</li><li>Jetpack (Compose, Navigation)</li><li>Koin (Dependence Injection).</li><li>Room</li><li>Kotlin</li><li>Flow</li></ul><p>next Article:<a href="https://mo-sattar.medium.com/android-architecture-explained-part-1-ui-layer-78378ca717f6"> Android Architecture explained Part 1 (UI-Layer)</a>.</p><p>We all still learning together if you have any questions leave them in the comments or if you find something that I understand is wrong or I missed it in the article let me know.</p><p>follow me for more articles and don’t forget to clap if you benefit from it.</p><p><a href="https://github.com/Abdelsattar">Abdelsattar - Overview</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b73ef6ba41cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to request permission in Jetpack Compose]]></title>
            <link>https://mo-sattar.medium.com/how-to-request-permission-in-jetpack-compose-ec37c7890a81?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec37c7890a81</guid>
            <category><![CDATA[permission]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[andorid]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Mon, 11 Jul 2022 19:46:06 GMT</pubDate>
            <atom:updated>2022-07-12T08:26:31.841Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*uQsE2ae2-Dk3kO9rSG72CA.png" /><figcaption><a href="https://clevertap.com/blog/understanding-android-permissions/">https://clevertap.com/blog/understanding-android-permissions/</a></figcaption></figure><p>we have some areas in the app that need runtime permission before accessing it like Location, Camera… etc.</p><p>so here will have a simple request for permission in Compose so we can use it easily and fast.</p><p>we will take a Camera Permission is an example in this article.</p><p>so we need to declare it to our manifest first.</p><p>&lt;uses-permission andorid:name=&quot;android.permission.CAMERA&quot;/&gt;</p><p>and with the help of</p><pre>implementation &quot;com.google.accompanist:accompanist-permissions:$accompanist_version&quot;</pre><p>currently the accompanist_version=&quot;0.24.13-rc&quot; .</p><p>the flow we should follow is the <a href="https://developer.android.com/training/permissions/requesting#workflow_for_requesting_permissions">google flow</a>.</p><pre>@OptIn(ExperimentalPermissionsApi::class)<br>@Composable<br>fun RequestPermission() {<br>    val isShowPermission = <em>mutableStateOf</em>(false)<br>    var textToShow = &quot;Camera permission Needed&quot;<br><br>    Column <strong>{<br>        </strong>if (isShowPermission.value) {<br>            val cameraPermissionState =<br>                rememberPermissionState(Manifest.permission.<em>CAMERA</em>)<br>            if (cameraPermissionState.status.<em>isGranted</em>) {<br>                isShowPermission.value = false<br>                textToShow = &quot;Camera permission is Granted&quot;<br>            } else {<br><br>                val messageToShow =<br>                    if (cameraPermissionState.status.<em>shouldShowRationale</em>) {<br>                        &quot;The camera is important for this app. Please grant the permission.&quot;<br>                    } else {<br>                        &quot;Camera not available please enable in settings&quot;<br>                    }<br><br>                AlertDialog(<br>                    onDismissRequest = <strong>{<br>                        </strong>isShowPermission.value = false<br>                    <strong>}</strong>,<br>                    title = <strong>{<br>                        </strong>Text(text = &quot;Permission request&quot;)<br>                    <strong>}</strong>,<br>                    text = <strong>{<br>                        </strong>Text(messageToShow)<br>                    <strong>}</strong>,<br>                    confirmButton = <strong>{<br>                        </strong>Button(onClick = <strong>{ </strong>cameraPermissionState.launchPermissionRequest() <strong>}</strong>) <strong>{<br>                            </strong><em>Text</em>(&quot;Request permission&quot;)<br>                        <strong>}<br>                    }<br>                </strong>)<br>            }<br>        }<br><br>        Button(onClick = <strong>{<br>            </strong>isShowPermission.value = true<br>        <strong>}</strong>) <strong>{<br>            </strong>Text(&quot;Request Camera permission&quot;)<br>        <strong>}<br>        </strong>Text(textToShow)<br>    <strong>}<br></strong>}</pre><p>Pretty easy !! :D</p><p>I will add later on how to Request multiple permissions at once.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec37c7890a81" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to work with WebSockets and unit-test them by OkHttp and Mockito]]></title>
            <link>https://mo-sattar.medium.com/how-to-work-with-websockets-and-unit-test-them-by-okhttp-and-mockito-2f179967e7d?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f179967e7d</guid>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[mockito]]></category>
            <category><![CDATA[okhttp]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Mon, 10 May 2021 23:12:41 GMT</pubDate>
            <atom:updated>2021-05-26T09:49:44.800Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*MMl8CTtP-rY3DN32uYXCOA.png" /></figure><p>When I’m looking for a new thing to work with, or some lib to work with or some technology, I always find how to use it, but rarely can I find it with its tests, and I’ve decided that whenever I work with something and can test it in a simple way, I’ll try to write an article on how to do that in simple steps.</p><p>This article is basically about working with WebSockets to connect and get updates from them and add a unit test for that function.</p><p>what do we need in this article?</p><p>OkHttp to connect to WebSockets and Rx-Java to observing messages from sockets</p><pre>implementation &quot;com.squareup.okhttp3:okhttp:$okhttp_version&quot; <br>implementation &quot;io.reactivex.rxjava3:rxjava:$rx_java_version&quot;</pre><p>and at this time of writing article the version is okhttp_version = &#39;4.9.0&#39;- and rx_java_version = &#39;3.0.0&#39;</p><p>and for testing, we will use Mockito</p><pre>testImplementation &quot;org.mockito.kotlin:mockito-kotlin:$mockito_kotlin_version&quot;<br>testImplementation &quot;org.mockito:mockito-inline:$mockito_inline_version&quot;and the current version is</pre><pre>mockito_kotlin_version = &#39;3.1.0&#39;<br>mockito_inline_version = &#39;3.9.0&#39;</pre><h3><strong>1- </strong>Establish<strong> our </strong>connection</h3><p>Let’s start by initiating our WebSocket</p><pre>fun connectToWebSockets() {</pre><pre>  val okHttpClient = OkHttpClient.Builder()<br>      .readTimeout(3, TimeUnit.<em>SECONDS</em>)<br>      .build()</pre><pre> val webSocketUrl= BuildConfig.YOUR_<em>Websocket_Endpoint</em></pre><pre> val webSocketReques = tRequest.Builder()<br>      .url(https://medium.com/webSocketUrl)<br>      .build()</pre><pre>    okHttpClient.newWebSocket(webSocketRequest, WebSocketRideCallBack())<br>}</pre><p>and here is WebSocketCallBack and here you can override the functions of WebSocketListener like onOpen, onMessage, onClosing …etc</p><p>but we have a very simple one here to add onMessage, and in it we use our class RxBus you can use any Observer pattern.</p><pre>class WebSocketCallBack : WebSocketListener() {<br><br>    override fun onMessage(webSocket: WebSocket, text: String) {<br>        RxBus.publish(RxEvent.Message(text))<br>    }<br>}</pre><p>here is our Rx-Bus</p><pre>object RxBus : BaseRxBus {<br><br>    private val publisher = PublishSubject.create&lt;Any&gt;()<br><br>    override fun publish(event: Any) {<br>        publisher.onNext(event)<br>    }<br><br>    fun &lt;T&gt; listen(eventType: Class&lt;T&gt;): Observable&lt;T&gt; =   publisher.ofType(eventType)<br>}</pre><p>And now let’s add our observer: (Anywhere I put it in my ViewModel)</p><p>(here we use Rx-Java)</p><pre>val eventLiveData= MutableLiveData&lt;String&gt;()</pre><pre>fun startWebSocket(){</pre><pre>   connectToWebSockets()</pre><pre>   val publisher = RxBus.listen(RxEvent.Message::class.<em>java</em>)<br>      .map <strong>{ it</strong>.message.<em>toString</em>() <strong>}<br>      </strong>.subscribe(<strong>{ </strong>data <strong>-&gt;<br>         </strong>eventLiveData.postValue(data)<br>      <strong>}</strong>, <strong>{ </strong>e <strong>-&gt;<br>          </strong><em>println</em>(&quot;publisher err ${e.<em>localizedMessage</em>}&quot;)<br>          //todo handle your error here<br>     <strong>}</strong>)</pre><pre>  return evenLiveData<br>}</pre><p>now the connection to the webSocket is established and when the server is connected and starts receiving messages you will get them here in subscribe.</p><p>Now it’s Time for testing</p><h3><strong>Testing</strong></h3><p>and for mocking WebSocket we will mock with simple function OnMessage() and in this function we will send the message as string to the function and it will be returned in our callback .</p><pre>@Mock(answer = Answers.RETURNS_DEEP_STUBS)   <br>lateinit var mockWebSocket: WebSocket    </pre><pre>@Mock(answer = Answers.RETURNS_DEEP_STUBS) <br>private lateinit var mockClient: OkHttpClient</pre><pre>private fun mockWebSocket(message: String) {<br>    `when`(<br>        mockClient.newWebSocket(<em>any</em>(), <em>any</em>())<br>    ).thenAnswer <strong>{ </strong>newWebSocket: InvocationOnMock <strong>-&gt;<br>        </strong>val wsl: WebSocketRideCallBack =<br>            newWebSocket.getArgument(1, WebSocketCallBack::class.<em>java</em>)<br><br>        wsl.onMessage(mockWebSocket, message)<br>        mockWebSocket<br>    <strong>}<br></strong>}</pre><p>For our test function itself, we will do the following</p><pre>@Test<br>fun `should update stops when get ride closed event changed from webSocket `() {<br>    //given <br>    val expectedValue = &quot;this is a message from the socket&quot;</pre><pre>    mockWebSocket(expectedValue)<br>    val expectedValue = expectedValue<br><br>    ///when<br>    val updates = viewModel.startWebSocket()</pre><pre>    //then<br>    val actualValue = updates.<em>value<br>    </em>assertEquals(expectedValue, actualValue)<br><br>}</pre><p>Woo! We are done building and testing our WebSocket :D</p><p>I hope it’s clear and if there is any confusion feel free to add your comment.</p><p>You can find me on Github here: <a href="https://github.com/Abdelsattar">https://github.com/Abdelsattar</a> !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f179967e7d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How can succeed as a freelancer for the Android platform?]]></title>
            <link>https://mo-sattar.medium.com/how-can-i-succeed-as-a-freelancer-for-the-android-platform-b56d6fb1ae3b?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b56d6fb1ae3b</guid>
            <category><![CDATA[freelancing]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Thu, 11 May 2017 08:44:41 GMT</pubDate>
            <atom:updated>2018-12-23T20:31:34.861Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*vfmOOMJz4ibWSK0G7Odwfg.jpeg" /></figure><p>Success means that you must work smart to get it, so I will tell you the keys you can work hard on it so you can keep going in your freelance work</p><p><strong>1-Never say no for any client in the beginning</strong></p><p>if you don’t have time or qualification of the job required never tell the client no, till you know all that he wants he might mean another thing or if you don’t have the time right now he might just gather information about budget and time and will back to you within month if he will develop it ( most of them do this)</p><p><strong>2- always show your work on your media</strong></p><p>post your latest apps on facebook, twitter, screenshots on Instagram, Linked-in and update your website as you don’t the next client comes from as you also update your profile on freelancer or Upwork</p><p><strong>3- answer questions about your work to your friends</strong></p><p>make<strong> </strong>your friends know what are you working as they might have also friends want to develop android app so you put yourself is the first one they think about when anyone wants to develop android app ( and that really gave you more opportunities ) it is like drive your clients to you without any effort</p><p><strong>4- make your work professional by document</strong></p><p>when you deal with client make your work professional by making a document that contains all the features he wants and the timeline and also the price so the document doesn’t go or forgotten that will be the one you and he will return if you differentiate about anything</p><p><strong>5- always advise the client what should be and do what he wants</strong></p><p>all the time you will make what the client said so you want to make something else or better convince the client so that will let him know that he controls everything and you will not do anything out of his control so you are doing what you want but in his way.</p><p><strong>6- good relationship is always your retention rate</strong></p><p>if you deal with a client in a good manner and keep your relationship with him fine and did what he want that will lead him to long-term client and also he will recommend you for other clients</p><p><strong>7- your work quality</strong></p><p>don’t make the client back to you with a lot of issues you will need to improve make everything quality and check on everything in your mind and test case before deliver</p><p>(always there exist some test cases forgotten :D that’s fine but don’t make a lot )</p><p><strong>8- offer new features free</strong></p><p>if you see that there will be a good feature to be added to the app and will not take a lot of money and wasn’t in the document you wrote to offer him to make it free as it was the first work we have or you are a good client to work with they will be happy for that</p><p><strong>9- always keep the conversation open</strong></p><p>give him your email to tell him that he always more than welcome to talk to you about the app and anything in android and if you need support for anything you can call me</p><p>that’s some key to follow with your current client and future one but keep this always will lead you to more work that “the word of mouth” rule will keep you in their mind for future work</p><h4>I work as an android freelancer in my free time If you want to build app visit my website</h4><p><a href="https://abdelsattar.github.io/">Sattar Website</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b56d6fb1ae3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to choose lib to import in your app]]></title>
            <link>https://mo-sattar.medium.com/how-to-choose-lib-to-import-in-your-app-a6e54ec2314f?source=rss-410b16dc6f1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6e54ec2314f</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[developer-tools]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Mo Sattar]]></dc:creator>
            <pubDate>Sun, 07 May 2017 09:12:35 GMT</pubDate>
            <atom:updated>2017-05-07T09:12:35.358Z</atom:updated>
            <content:encoded><![CDATA[<p>Libs are the most time saver in any kind of development (but if you choose it right).</p><p>we know that there exist a massive number of libs in github that increasing everyday and made almost the same thing but they are different in how it implemented and also how many things the lib do</p><p>there exist some key factors that you must put into your consideration before the made the choice to use any lib into your app and we are here to discuss them</p><p><strong>1- documentation</strong></p><p>first of all you want to know what this lib do and how to do this so code itself is good but you want to save time not spending time to understand how you will use the lib.</p><p><strong>2- Number of stars</strong></p><p>number of stars lib get is based on how many users liked it and really helpful to them the more the best.</p><p><strong>3- Issues</strong></p><p>don’t ever forget to check issues tab as this is very important to know the current issues in the lib and also to see the number of opened vs closed issue and are the contributors respond to these issues frequently or people just open the issue and nothing happen.</p><p><strong>4- last update</strong></p><p>when it was the last time they update this lib the close the best as you know development in any framework is changed a lot in the past 2 years and if we take about android you will see that it change a lot in different things so if the the lib didn’t handle the new things you will need to handle it yourself.</p><p><strong>5- size &amp; methods count</strong></p><p>size of lib will affect also the size of your program so see the size and methods count also so you can avoid 64K methods also.</p><p>these factors are the main factors you must put it into your mind before choose anything to work with also you must see the functions that every lib do that you will need to use 2 lib for 2 different thing or you will just use the lib that will do the 2 things</p><p>also search about the pros and cons of the libs that do the same thing you will really know which lib will use as it is different into many things and many situation, so the first and last choice will back to you</p><h4>So<strong> Choose Wisely</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zZ_-f5n45vnjrQt2G3FIyg.png" /><figcaption>Github (google)</figcaption></figure><h3><a href="https://goo.gl/kUeGvF">you can see my github repo about new articles in android</a></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6e54ec2314f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>