<?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 Kaustubh Patange on Medium]]></title>
        <description><![CDATA[Stories by Kaustubh Patange on Medium]]></description>
        <link>https://medium.com/@kaustubhpatange?source=rss-124f1e86b15------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oo1RHikYmHXS-KjzEFa7VQ.jpeg</url>
            <title>Stories by Kaustubh Patange on Medium</title>
            <link>https://medium.com/@kaustubhpatange?source=rss-124f1e86b15------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 07:51:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaustubhpatange/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[My Journey Building a Polymarket BTC Trading Engine]]></title>
            <link>https://kaustubhpatange.medium.com/my-journey-building-a-polymarket-btc-trading-engine-577436189a3b?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/577436189a3b</guid>
            <category><![CDATA[trading-bot]]></category>
            <category><![CDATA[crypto-trading]]></category>
            <category><![CDATA[trading-strategy]]></category>
            <category><![CDATA[polymarket]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Sun, 05 Apr 2026 17:32:44 GMT</pubDate>
            <atom:updated>2026-04-05T17:32:44.119Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have been following my previous articles, you already know that I developed an interest in the crypto market and started investing, not just in crypto but in other financial markets as well. My journey into trading on Polymarket is an interesting one, especially because I am someone who believes in long-term investing. My approach has always been to dollar-cost average or put a lump sum into an asset I believe in and let the returns compound over time as the price appreciates.</p><p><a href="https://kaustubhpatange.medium.com/importance-of-self-custody-in-crypto-321429c5e789">Importance of Self Custody in Crypto</a></p><p>I have read books like “A Random Walk Down Wall Street” by Burton G. Malkiel and “The Intelligent Investor” by Benjamin Graham, which taught me what investment truly means. The central theme of both books is that greed and emotional decision-making are your two biggest enemies as an investor. Through historical examples, they show how these forces can sabotage even the most well-reasoned investment decisions. They also present evidence that the long-term returns of professionally managed funds and index funds are roughly the same. In fact, some comparisons show that the monetary benefits of active trading and passive index investing converge over long periods. Trading can produce short-term gains, but when emotions and greed take over, a single mistake can wipe out three months of profit. The lesson is always the same: investing is a long-term game.</p><p>So why trading? Because humans are greedy. Everyone wants to make a quick dollar and nobody wants to wait. A good example is today’s BTC market. BTC has been moving between 65k and 70k USD, and whenever it crosses above 70k there is usually a sharp pullback that drags it back into that range. The common thought process is “buy low, sell high,” but when prices actually drop, most people get scared and hold back. Instead, they invest when prices are climbing, like when BTC pushes above 70k, and then a sudden pullback turns their investment into unrealized losses. The same pattern plays out in gold and the stock market. People rush to buy when prices rise, and when prices fall they blame the market. The reality is that this is how markets work. The wrong decisions of some become profits for others.</p><p>The same applies to trading. I have seen countless examples on YouTube of people treating day trading as pure gambling. If you have a genuine edge or some form of insider knowledge, that is one thing. But learning candlestick patterns and going long or short based purely on intuition is not a strategy that works over the long term. That is why you see so many videos titled “How I Lost X Amount of Money in Day Trading.” This is exactly the trap Graham warns you not to fall into.</p><h3>My Journey</h3><p>A few months ago, I stumbled upon prediction markets like Kalshi and Polymarket. At first I thought these were just gambling websites, but that changed completely after watching a video from the “Art of Problem” YouTube channel about how prediction markets work and how Polymarket operates <a href="https://youtu.be/ngX1nIvnMOM?si=yOhu1GSqt90rk9I8">https://youtu.be/ngX1nIvnMOM?si=yOhu1GSqt90rk9I8</a>.</p><p>A prediction market is most of the time a binary market where you bet on the outcome of a question. Take “Will Arsenal win the quarter-finals?” with two outcomes: “YES” and “NO.” If the price of YES is 0.70 and NO is 0.30, that implies a 70% probability of a YES outcome. If you also believe YES, you put money in and buy shares of that side. If you are correct, each share pays out 1.00 and you pocket the difference as profit (0.30 per share in this case). If you are wrong, your shares resolve to 0.00 and you lose your entire investment.</p><p>The interesting part is that these prices are driven entirely by collective opinion. When you buy shares of the YES side, you are betting against people on the NO side, believing you know something they do not. This is essentially a form of game theory: your decision influences other participants’ views, and over time the market converges toward the correct outcome. We have seen this play out with insider trading. A recent example involves the capture of Venezuela’s president: a whale placed a massive bet on the YES outcome that the US would take action against Venezuela and capture its president, three to four days before the event actually unfolded. When that bet appeared, others noticed and quickly repositioned to the YES side, driving the price of YES shares up.</p><p>For a long period of time, Polymarket became my primary source for news and information related to geopolitics and world events. I was still not trading on it, but I was observing patterns in how prediction market movements correlated with Gold, BTC, and the stock market. I was using those signals to buy Gold and hedge against broader market moves. It was an interesting exercise until I came across the BTC Up/Down 5-minute markets.</p><h3>BTC 5-Minute Markets</h3><p>On Polymarket, there are 5-minute prediction markets for BTC, ETH, SOL, and XRP. Each market has a target price called the “Price to Beat.” If you correctly predict that the price will be above or below the target at the end of the 5-minute window, you win. It sounds simple, but that simplicity is an illusion. BTC is a highly volatile asset, and a 5-minute window is far too short for traditional indicators like RSI, MACD, or EMA to reliably predict direction. The time window may seem small, but when share prices drop significantly and you cannot explain why, that is when you start thinking more carefully about how these markets actually move.</p><p>I became very interested in this because I follow BTC price movements daily and I thought I could find a pattern that would give me an edge. It started as a fun experiment and then turned into something serious. Part of what pulled me in was the technology behind Polymarket itself. It uses blockchain internally for settlement, performs off-chain order matching for speed, and leverages smart contracts to manage the entire market lifecycle.</p><p>I wanted to understand how Polymarket creates these market contracts, how the Conditional Token Framework works, what happens to your USDC when you buy shares, why shares are not instantly available to sell after a purchase, where the Price to Beat comes from, and who resolves whether the outcome is UP or DOWN. I wanted to explore how trading works on a blockchain and go deep into Polymarket’s internals, because if you are going to use the platform seriously, you need to understand what happens at every step of a trade.</p><h3>Polymarket - Under the Hood</h3><p>After spending time digging into Polymarket’s internals, here is how everything fits together. At the foundation, Polymarket runs on Polygon, an Ethereum Layer 2 network that keeps transaction costs negligible. When a binary market is created, the Gnosis Conditional Token Framework handles the core mechanics. For every dollar of USDC you lock into the contract, it mints two conditional tokens: one for each outcome, say UP and DOWN. These tokens follow the ERC-1155 standard, meaning a single smart contract manages the balances for all positions. A complete set of tokens (one UP and one DOWN) can always be redeemed for exactly 1.00 USDC, which is the fundamental constraint that keeps prices rational through arbitrage.</p><p>When it comes to resolving markets, Polymarket originally relied on UMA’s Optimistic Oracle for event-based outcomes. UMA works on an optimistic model: someone proposes an outcome, and it is accepted as truth unless someone else disputes it within a challenge window. If a dispute is raised, it escalates to a decentralized vote among UMA token holders to determine the correct result. This works well for subjective or event-based questions like election outcomes, but for something like a BTC price market that resolves every five minutes, you need something faster and more objective. That is where Chainlink comes in. For BTC 5-minute markets, Polymarket uses Chainlink price feeds, which aggregate BTC prices from multiple exchanges and deliver them on-chain. The resolution is automatic and trustless: the smart contract reads the Chainlink feed at the end of the window, compares it to the Price to Beat, and settles the market without any human involvement.</p><p>The trading layer is equally interesting. Polymarket does not use an AMM (Automatic Market Maker) like most DeFi platforms. Instead, it uses a hybrid Central Limit Order Book (CLOB). When you place an order, it goes to an off-chain matching engine that pairs compatible buy and sell orders. Both parties sign their orders using EIP-712 typed signatures, and the matched pair is then submitted to the CTF Exchange smart contract on Polygon for atomic on-chain settlement. This is where the order lifecycle becomes visible: after matching, the transaction is submitted to the blockchain (MATCHED), waits to be included in a block (MINED), and finally receives enough block confirmations (CONFIRMED). This is also the reason why shares are not instantly available to sell after a buy. Your purchase needs to go through this full on-chain settlement cycle before the tokens appear in your balance and can be used in a new order. The hybrid design gives you the speed of a centralized exchange for order matching while preserving the security guarantees of blockchain settlement, where every trade is verifiable and non-custodial.</p><h4>Reference</h4><ul><li><a href="https://github.com/KaustubhPatange/polymarket-trade-engine/blob/master/docs/LEARNING.md">https://github.com/KaustubhPatange/polymarket-trade-engine/blob/master/docs/LEARNING.md</a></li></ul><h3>Trade Engine</h3><p><a href="https://github.com/KaustubhPatange/polymarket-trade-engine">GitHub - KaustubhPatange/polymarket-trade-engine: Automated trading engine for Polymarket binary prediction markets (e.g. BTC Up/Down 5-minute)</a></p><p>I knew I needed to build an automated trading bot because there is no way a human should be involved in this process. Emotions kick in and lead to more mistakes than any algorithm would make. I wanted to build a system around the patterns I identified, a script that would trade on my behalf. If I lose, I lose purely because my algorithm did not work, not because of some emotional decision or third-party interference.</p><p>A good friend of mine told me that the order book is always the source of truth for trading in these kinds of markets. So my first trading engine, called “late-entry,” was event-driven. The idea was to display the order book in the terminal and keep it continuously updated by subscribing to Polymarket’s order book WebSocket, so updates were real-time with no lag. I also wanted to minimize API calls and rely on the WebSocket wherever possible to reduce latency when placing orders. One thing worth noting about Polymarket is that there is no concept of a market order. Everything is a limit order. When you try to create a market order through the SDK or buy through the UI, it actually places a limit order at the best ask for that side. I wanted to keep API calls to a minimum and lean on the WebSocket for as much of the trading flow as possible. The initial version of the script looked like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BPWktIcN6NDkgDf-_jLhAw.png" /></figure><p>The terminal view showed live order book coverage with real-time updates. I used to stare at it for hours trying to find patterns and observe how people reacted to price movements. I pulled BTC prices from multiple sources like Binance, Coinbase, and Chainlink itself to see how the divergence between them affected order book depth and positioning.</p><p>This continued for weeks as I built out the engine as an event-driven system. There was a component responsible for sending a signal on when to enter the market. I used indicators like RSI, ATR, and cross-source price divergence to detect whale dumps or pumps and trigger a buy signal. The engine would then place the order, and a separate component would monitor the position until the market slot ended. It placed the necessary stop losses, identified bell curve patterns, filtered out false signals, and only exited in the case of a stop loss being hit.</p><p>On the surface this approach looked like it worked, and it did about 95% of the time. But for that remaining 5%, I was losing a significant amount of money. That is when it clicked: the goal is not to always hold shares until the market resolves. As soon as you have an appropriate profit, you sell and exit. These markets are unpredictable at times, so even if a strategy works today, it may not work tomorrow.</p><p>My earlier engine had a few fundamental limitations. It was not capable of a buy-and-sell strategy. It was purely signal-based: one buy, hold to resolution, no intermediate sell. I also needed a simulation system that could test against the real market using a paper wallet. I could not afford to lose money while iterating on strategies, so I needed a simulation environment that mirrored real-world conditions: latency, partial fills, unexpected order cancellations, and the inability to sell immediately after a buy because the transaction needs on-chain confirmation. All of these had to be accounted for so that when I wrote a strategy and tested it, I could trust that what worked in simulation would work in production.</p><h3>Planning the new Engine with Claude</h3><p>I did not know how to get started or how to make the engine extensible. The idea was simple: you write a strategy and the engine trades for you. I used Claude Code (Opus 4.6) to plan the engine architecture, focusing on how to make it extensible and how simulation would work. I have to say, I was genuinely impressed with the results. I used the <a href="https://plannotator.ai/">Plannotator</a> plugin to plan with Claude, which provides a clean UI where you can read through the plan and give feedback to the agent. After several rounds of back and forth, we arrived at a final architecture that I was confident in.</p><p>The new engine is based on lifecycles. You always start in a future market slot, at least one ahead of the current one, which is why I named it “early-bird.” At no point can you start the engine in the current market. Every market is a lifecycle: it goes through start -&gt; run -&gt; end states. The engine’s job is to orchestrate these market lifecycles. Each market runs exactly one strategy when it is created. You pass a parameter specifying which strategy you want to run, and the market lifecycle creates it during the start state, executes it during the run state, monitors it, and waits for completion before transitioning to the end state. It then computes PnL and notifies the engine that it is done. The engine provides a set of APIs for building strategies. A strategy is simply a function that uses these APIs to place buy and sell orders. The full architecture diagram is shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j9QPSLBBqVVOGmBUWCibgA.png" /></figure><p>Once the planning was done, I asked Claude to create an initial skeleton based on this architecture, which it did quite well, including the simulation code. I also knew I needed a solid logging system so that when I win or lose, I have the necessary data to debug why a strategy failed. Claude helped with both the planning and implementation here as well. It suggested building a chart visualization tool that takes existing log files and generates an interactive chart for each market window. The output looks something like the image below. This makes it much easier to visually analyze what went wrong and find potential improvements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2ejEJtGbvnKB6ysmuI4iA.png" /></figure><h3>Strategy and Final Thoughts</h3><p>With the engine in place, I was ready to write my own strategy. I cannot share my specific findings because that would give away my edge, but I can tell you that no single strategy will win all the time. When you study the order book and indicators, you will find certain patterns that only appear during specific periods of the day and not always. Your edge is knowing when those windows are and only trade during that period. It is also not about winning every round. You will lose sometimes, but your strategy should have excellent exit signals that minimize how much you lose. The engine provides the necessary APIs and includes examples under the simulation and late-entry strategies (/engine/strategy/) that demonstrate how to exit without giving back significant profits.</p><p>One thing worth mentioning is that AI will not help you write a strategy directly. It is all about context. If you train a model on specific metrics like order book movements, gap behavior, and price divergence, you could potentially ask it to generate a strategy. That is an experiment I want to try, but it is something for another time.</p><p>The engine is open source and actively maintained. If you are interested, you can take a look at <a href="https://github.com/KaustubhPatange/polymarket-trade-engine">https://github.com/KaustubhPatange/polymarket-trade-engine</a>. The README links to documentation covering market terminology and a full guide on how to develop strategies using the engine. The repository is the public-facing version of the engine and regularly syncs from my private repo. The only difference is the strategies: the public version ships with test strategies, while my private repo contains the actual strategies I trade with. Here is a screenshot of what the engine outputs,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U23bEBuhYmSBwMEMfJJffA.png" /><figcaption>a sample simulation strategy output</figcaption></figure><p>If you are wondering whether this has been profitable, I have made a few hundred dollars from it. But continuing to develop strategies is a challenge. What worked three days ago, fails today, so you need to constantly look for new patterns and identify whatever edge you can find. I have a full-time job, and keeping this up every day is not sustainable, so I try to only trade during weekends when I have the time. I spent countless sleepless nights building the initial engine and finding patterns that actually worked for a while. I cannot say whether I will continue doing this long term given my job and the toll it takes, but I will not push myself the way I did before. My goal was never to become an active trader. It was to learn and explore a subject that genuinely interests me, and to write articles like this so others can learn from my experience.</p><p>So if you liked this post, feel free to connect with me on <a href="https://www.linkedin.com/in/kaustubhpatange/">LinkedIn</a> or <a href="https://x.com/KP206">X.com</a> (formerly Twitter).</p><p><a href="https://github.com/KaustubhPatange/polymarket-trade-engine">GitHub - KaustubhPatange/polymarket-trade-engine: Automated trading engine for Polymarket binary prediction markets (e.g. BTC Up/Down 5-minute)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=577436189a3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Importance of Self Custody in Crypto]]></title>
            <link>https://kaustubhpatange.medium.com/importance-of-self-custody-in-crypto-321429c5e789?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/321429c5e789</guid>
            <category><![CDATA[hardware-wallet]]></category>
            <category><![CDATA[cryptocurrency-investment]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[bitcoin-diy-seedsigner]]></category>
            <category><![CDATA[self-custody]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Sat, 07 Feb 2026 15:08:20 GMT</pubDate>
            <atom:updated>2026-02-07T15:08:20.796Z</atom:updated>
            <content:encoded><![CDATA[<p>A long time ago, one of my close friends told me that I should start investing to beat inflation. At that time, I laughed in his face and said, “If I want to be rich, I’ll just keep switching jobs.” Looking back, I was pretty naive as you can probably tell from that response. Three years later, I realized that the money sitting in my savings account earned around 3% interest per year, whereas if I had invested it in the equity market (simple mutual funds or index funds) or even in commodities, it could have potentially given me much higher returns over the same period. Even a government bond like FD yields a good 7% interest rate.</p><p>They say the best time to invest was yesterday, the next best time is today, and the worst time is tomorrow and I learned that lesson the hard way. This lesson is about playing the long game, rather than chasing short term gains through frequent trading.</p><p>Investing is one of those things that feels boring when you don’t understand why an asset is moving not the way you expected, and exciting when it finally does. I know many people who quit investing because they felt the market wasn’t working for them but in reality, they never took the time to try to understand how markets work. This lack of understanding often leads people to let emotions take over and, in some cases, lose significant portions of their savings.</p><p>A similar situation is happening with Bitcoin right now. After reaching an all time high (ATH) of around USD 124,000, Bitcoin’s price has since pulled back and, as of 6 February 2026, is trading under USD 70,000, with some speculating that it could go lower. Market sentiment, combined with large holders (often called whales) moving significant amounts of supply, contributes to price volatility and can create fear among investors.</p><p>Losing money to the market is still, in a way, acceptable because that’s the result of your own decisions, and you are responsible for them. But what about losses that are completely out of your control? For eg, if you follow the news, you’ve probably seen case where a hot wallet browser extension (Trust wallet) was hacked, resulting in the theft of millions of dollars (8.2m USD) worth of users crypto assets. Or if you browse communities like r/CryptoCurrency and similar forums, you’ll regularly see people sharing stories about their crypto accounts being hacked, drained, or even locked by centralized exchanges. You wouldn’t believe how many people have lost their life savings this way.</p><h3>Trust vs Ownership</h3><p>A pension fund, 401(k), or any government backed savings scheme stores your money in accounts that you technically “own” but in reality, the custodian or institution has operational control over them. In rare situations such as legal disputes, regulatory investigations, court orders, sanctions, or compliance issues funds can be frozen or access can be restricted. While this doesn’t happen arbitrarily (because that would destroy trust in the banking system), it highlights that traditional finance is fundamentally custodial; you rely on intermediaries to safeguard and grant access to your money.</p><p>The traditional financial system is built on trust. You trust that the US dollar issued by the government will continue to be accepted as money and retain purchasing power, and you trust financial instruments like government bonds and US Treasuries because they are backed by the state’s ability to tax and manage its economy.</p><p>Commodities like gold and silver are different. They are physical elements found in nature, and while they are relatively abundant in the Earth’s crust, they cannot be created at will like fiat currency. Even though, in theory, nuclear reactions could produce gold or silver from other elements, the cost is astronomically high and completely impractical. This is why, for thousands of years, people have trusted precious metals; they are scarce, durable, hard to counterfeit, visually distinctive, and have historically served as a store of value and medium of exchange.</p><h3>Bitcoin &amp; Scarcity</h3><p>Bitcoin was originally envisioned as a decentralized digital currency and an alternative payment system, that was Satoshi’s original idea. The modern narrative frames Bitcoin as “digital gold” because of its scarcity and store of value properties. It is secured by cryptography and by people around the world running Bitcoin nodes and miners.</p><p>You’ve probably heard that there will only ever be 21 million bitcoins. But is that really true? Since Bitcoin is “computer money” can’t someone just generate more?</p><p>In theory, yes but only if the entire network agrees to change the rules. Convincing 20 people is easy; convincing tens of thousands of independent node operators and miners to run modified software that inflates the supply is practically impossible. Bitcoin’s monetary policy is enforced by full nodes, which independently verify blocks and transactions according to the consensus rules. Miners produce blocks, but full nodes decide whether those blocks are valid.</p><p>A full node can be run on relatively modest hardware (a few hundred dollars for a small server or single board computer, plus storage, electricity, and bandwidth). A miner, on the other hand, is specialized hardware called an ASIC (Application Specific Integrated Circuit). High end ASIC miners can cost tens of thousands of dollars per unit, with price increasing as hash rate increases. Hash rate refers to how many cryptographic hashes a miner can compute per second, the higher your hash rate, the higher your probability of finding the next block and earning the block reward. On top of hardware costs, miners also pay significant electricity and cooling expenses.</p><p>You might be thinking; if I could somehow trick the network into trusting my chain (the longest valid chain is considered the canonical one), could I take over Bitcoin? This is the idea behind the famous “51% attack” that you might have heard about (even referenced in shows like Silicon Valley). In theory, if you control more than 50% of the network’s mining power, you could temporarily manipulate the ordering of transactions. According to recent estimates, to control the network for even a short period (say, a week), you would need around USD 4.6 billion worth of ASIC miners, roughly USD 1.2 billion in infrastructure, and about USD 130 million in electricity, totaling close to USD 6 billion for just one week. These figures are based on recent reports, as we don’t have precise public data on the number of active miners <a href="https://cryptonews.net/news/mining/31789586">https://cryptonews.net/news/mining/31789586</a>.</p><p>Even after spending that kind of money, all you could realistically do is reorganize recent transactions (for example, attempt double spends). You still cannot create more Bitcoin out of thin air, because your miners would be running the same unmodified Bitcoin software that enforces the 21 million supply cap. This naturally raises a question; if you’re spending billions of dollars, why not just mine honestly and earn rewards instead of attacking the network? Also, this attack doesn’t get cheaper over time. Bitcoin’s mining difficulty automatically adjusts as more computing power joins the network. As hardware improves and competition increases, attacking the network becomes more expensive, not less. And no, quantum computers (at least with today’s known capabilities) don’t magically solve Bitcoin mining, since mining relies on hash functions, not algebraic problems.</p><h3>Self-Custody in Practice</h3><p>So why am I telling you all this? To draw a parallel between gold and Bitcoin. In both cases, “creating” more supply is extremely costly. Producing gold through nuclear reactions is technically possible, but economically absurd. Similarly, breaking Bitcoin’s scarcity through a majority attack is so expensive that it makes no practical sense. Gold is naturally scarce; Bitcoin is mathematically scarce.</p><p>If you believe in gold as an investment or as a hedge against the banking system, it’s not unreasonable to also believe in Bitcoin. One important point; anything stored digitally like gold ETFs or gold backed funds carries custodial risk. You are trusting an institution to actually hold the gold on your behalf. In times of financial stress, you don’t get physical gold delivered to your door; you get promises, settlement delays, or cash equivalents that have already lost value to inflation.</p><p>This is one reason some people prefer holding physical gold. Of course, that comes with its own risks; storage, theft, and lack of insurance. If someone breaks into your house and steals your gold, it’s gone. That’s the trade off you accept for self custody. This is also why governments and institutions often push “digital gold” products as they’re easier to manage and control. If someone tells you digital gold is always safer, show them cases like this <a href="https://www.scmp.com/economy/china-economy/article/3341633/chinas-gold-fever-sparks-us1-billion-scandal-trading-platform-collapses">https://www.scmp.com/economy/china-economy/article/3341633/chinas-gold-fever-sparks-us1-billion-scandal-trading-platform-collapses</a>. At the end of the day, physical gold is yours if you can secure it.</p><p>Now, back to Bitcoin. How do you store it? Bitcoin itself lives on a public ledger, the blockchain that anyone can read. What you actually own is a private key. This private key proves ownership of the Bitcoin associated with certain addresses. When you send BTC, you are creating a transaction from your address to someone else’s address. Anyone can send BTC to you if they know your public address, but to spend BTC from an address, you must produce a valid digital signature using the corresponding private key. If someone tries to create a transaction from your address without your private key, the network will reject it. This cryptographic verification is what full nodes check; they validate signatures and ensure transactions follow the rules of the protocol. You can think of this like a bank account; anyone can send money to your account number, but to send money out, you need proper credentials. In Bitcoin, your private key is your credential. Anyone who has your private key can spend your BTC. If you lose your private key, your BTC is effectively gone forever. There is no password reset. This means you need to secure your private key just like you would secure physical gold.</p><p>This is where hardware wallets come in. A hardware wallet is a physical device designed to store your private keys securely and sign transactions without exposing those keys to the internet. This is called self custody; you control your keys instead of trusting a third party exchange to hold them for you. The phrase “Not your keys, not your coins” exists for a reason. Exchanges can freeze or restrict access to your funds due to legal, regulatory, or internal issues but with self custody, no one can stop you from accessing your Bitcoin. That’s why many people consider a hardware wallet a must have if you are planning to save your life savings in crypto. For trading, a hot wallet (exchange managed) with limited funds are enough.</p><p>There are many reputable hardware wallets available. I personally use Tangem and Ledger, and there are other well known brands like Trezor. The hardware wallet market is large, which is why you should be careful not to fall for pure marketing gimmicks. For example, “air-gapped” wallets are often promoted as inherently more secure, but maintaining true air gapped security in practice is hard and often reduces usability and portability. Some people disagree with me on this and that’s fine. These are my personal views. I work in a crypto company, specifically on wallet management systems for creating, signing, and broadcasting transactions, so I’ve seen how these protocols work under the hood and what good security practices look like. Security is always a trade off between threat model, usability, and human error.</p><p>If you’re a true believer and enjoy tinkering, you could even build your own setup. Not because hardware wallets are bad, but to gain deeper control and understanding of both the software and hardware. I did a small experiment with this using a Raspberry Pi 2W, by writing and integrating an ECDSA signing library, running a fully offline signing nodejs service, and exposing signing APIs to a mobile or desktop client, all without ever connecting the signing device to the internet. That said, building, extending and maintaining such a system is a lot of work which I quickly realized. In reality, I’d rather let specialists solve this problem well than reinvent everything myself and burn time and resources. That’s when I came across <a href="https://seedsigner.com/">SeedSigner</a> and for most people, well audited hardware wallets or open source projects like this strike the right balance between security and usability.</p><h3>SeedSigner</h3><p><a href="https://seedsigner.com/">SeedSigner</a> is an open source project that lets you build your own DIY signing hardware using easily available components like a Raspberry Pi, a small display, and a camera. You can even 3D print a case to make it look neat. You’ll find images of how mine looks at the end of this post, but essentially, SeedSigner is a Bitcoin only wallet that signs transactions securely. This is the final step in true self custody; building your own hardware and flashing open source firmware on it. I personally audited the OS it runs on, which is based on a Buildroot configuration, nothing fancy and the application itself is written in Python. There are plenty of detailed setup videos by different YouTubers if you want to build one yourself; <a href="https://www.youtube.com/results?search_query=setup+seed+signer">https://www.youtube.com/results?search_query=setup+seed+signer</a></p><p>The main idea behind SeedSigner is that your private key (or seed) is stored offline often written on paper or encoded as a QR, kept somewhere safe and away from the internet. You use an external wallet app like Sparrow Wallet on desktop or Nunchuk on mobile to construct a transaction. When you want to sign, you scan your seed QR into SeedSigner, sign the transaction using QR codes, and then scan the signed QR back into the app, which broadcasts it to the blockchain. This flow is designed to be highly secure because signing happens on a completely offline device. Only the signed transaction is shared back to the online wallet app; your private key is never exposed to the internet. SeedSigner also does not persist data on every reboot, the device forgets everything, so you must re-import your seed. This makes secure backup of your seed extremely important.</p><p>I could go on and on about SeedSigner, but in short, it’s a great DIY project if you’re tech savvy and enjoy building things yourself. If not, a regular hardware wallet is perfectly fine and far more convenient. There’s also a great keynote by the creator of SeedSigner on why he started the project; <a href="https://www.youtube.com/watch?v=Ss4RG59PvV4">https://www.youtube.com/watch?v=Ss4RG59PvV4</a> (It’s a really good watch).</p><h3>Volatility &amp; Long-Term View</h3><p>One key difference is that SeedSigner is Bitcoin only, whereas most commercial hardware wallets support multiple cryptocurrencies. It’s also generally cheaper than traditional hardware wallets at least for now. That said, hardware prices can fluctuate. Semiconductor supply constraints, increased demand from AI and data centers, and shifts in manufacturing priorities could drive up the cost of single board computers, storage, and other components over time. Some people call the current AI wave a bubble, and maybe parts of it are, but we’ve already seen how powerful LLMs can be. Personally, I think energy and infrastructure will be long term bottlenecks and investment themes going forward. Again, these are just my opinions take them with a grain of salt.</p><p>Also, don’t blindly listen to anyone who says Bitcoin is “a lost cause.” There are many macro factors that affect Bitcoin and crypto markets; US trade policies, interest rates, liquidity conditions, and the strength of the dollar (DXY), to name a few. From what I observe, a lot of the recent volatility has been driven by spot ETF flows, where large players and institutions move significant capital. At the same time, some long term holders take profits after an ATH, while retail investors panic sell during drawdowns. Personally, I don’t recommend holding crypto spot ETFs if your goal is self sovereignty they defeat much of the point of decentralized finance and self custody. If you treat Bitcoin like gold, a simple long term approach (like DCA and holding) often makes more sense. And if you ever need cash for an emergency, you can always liquidate part of your holdings.</p><p>Please take all my financial opinions with a grain of salt and do your own research before investing in any asset. There are many more exciting things in life than constantly worrying about market movements. Try to invest in assets you personally believe in, rather than blindly trusting any “expert” or influencer. Many so called advisors profit by playing on human psychology and emotions. I’d recommend reading about the “castle in the air” theory to understand how speculative manias form and how people exploit them for personal gain.</p><p>BTW, here are a few sneak peeks of the SeedSigner I built. And if you liked this post, feel free to connect with me on <a href="https://www.linkedin.com/in/kaustubhpatange/">LinkedIn</a> or <a href="https://x.com/KP206">X.com</a> (formerly Twitter).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QrLDOXqzi_lvwq_YmG9_ww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gLiY8hqqquisvIot6TNdhQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=321429c5e789" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Standing Out with Terminal-Based SSH Portfolio]]></title>
            <link>https://kaustubhpatange.medium.com/standing-out-with-terminal-based-ssh-portfolio-aa7b6a2eb1ad?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa7b6a2eb1ad</guid>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[resume]]></category>
            <category><![CDATA[portfolio]]></category>
            <category><![CDATA[terminal]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Sun, 09 Feb 2025 08:20:22 GMT</pubDate>
            <atom:updated>2025-02-09T08:20:22.584Z</atom:updated>
            <content:encoded><![CDATA[<p>We’ve all seen websites that mimic terminal interfaces, built with React, Next.js, and other frameworks. But what about creating a portfolio that actually lives in the terminal? I’m talking about a portfolio that can be accessed directly through a command-line interface. For example, by simply opening a terminal window and typing ssh kaustubhpatange.com (you&#39;ll need to type &quot;yes&quot; to accept the fingerprint), you can view a basic portfolio right in your terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*vPs9WSrApWyIME6kPUhfYw.gif" /><figcaption>ssh kaustubhpatange.com</figcaption></figure><p>While the example above demonstrates a basic text-based portfolio, you can enhance it by implementing interactive features such as user input, checkboxes, and forms to display dynamic content. This article will primarily focus on the hosting aspects rather than the portfolio creation process.</p><h4>Motivation</h4><p>It all began when I came across a tweet about <a href="https://terminal.shop/">terminal.shop</a> (accessible via ssh terminal.shop), where you can purchase coffee directly through your terminal. This fascinating concept sparked an idea: while I might not create something as complex as an e-commerce platform, I could build a terminal-based portfolio that displays my professional information and provides quick access to my social profiles on LinkedIn, GitHub, and other platforms.</p><h4>TLDR;</h4><p>But how do these terminal-based applications work? At their core, they are SSH applications that function similarly to when you connect to a remote machine. When you establish an SSH connection, it creates a pseudo terminal (PTY) for the session, allowing you to interact with the remote shell as if you were using a local terminal on that host. The SSH protocol operates on port 22 by default, and the SSH software (such as openssh-server) listens for incoming connections on this port to process user requests.</p><p>Instead of routing port 22 connections to a standard SSH server, we’ll direct them to our custom SSH application running on the host machine. This way, when a user executes ssh &lt;optional-username&gt;@ip, our application will display the portfolio content. After setting this up, we can configure our domain&#39;s DNS settings to resolve to the machine&#39;s public IP address, allowing users to access the portfolio simply by typing ssh your-domain.com.</p><h3>Creating SSH app</h3><p>While you can create an SSH application using any programming language, Go is particularly well-suited for this task. Rather than providing a step-by-step tutorial, we&#39;ll explore this through practical examples.</p><p><a href="http://github.com/charmbracelet/wish">Wish</a> is a Go library developed by <a href="https://charm.sh/">charm.sh</a> that simplifies the creation of SSH applications. It provides various middlewares (similar to plugins) to enhance your SSH app’s functionality. For example, <a href="https://github.com/charmbracelet/bubbletea">Bubbletea</a>, another Charm library, enables the development of Terminal User Interface (TUI) applications. By using the Bubbletea middleware, you can serve customized TUI interfaces to users. Additionally, the <a href="https://github.com/charmbracelet/wish/blob/main/accesscontrol/accesscontrol.go">access control</a> middleware helps restrict user commands within a session, preventing potentially malicious actions like sudo reboot that could disrupt your server.</p><p>You can explore numerous examples in the <a href="https://github.com/charmbracelet/wish/blob/main/examples/">wish/examples</a> repository. For a comprehensive, ready-to-use implementation, you can copy the gist below into your own Go project. This code is an excerpt from my personal portfolio repository.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c604d5d1313ecd0526d4995b5fc9bcf/href">https://medium.com/media/7c604d5d1313ecd0526d4995b5fc9bcf/href</a></iframe><p>For a quicker start, I recommend forking my <a href="https://github.com/KaustubhPatange/ssh-kaustubhpatange.com">repository</a> and customizing it to your needs. Important: make sure to replace the host key at .ssh/id_ed25519 with your own private key. The one in the repository is included only as an example and should not be used for you app.</p><pre># Execute this command in the repo root<br>mkdir .ssh &amp;&amp; ssh-keygen -t ed25519 -C &quot;your_email@example.com&quot; -f $_/id_ed25519</pre><p>Since this application will run on a Linux virtual machine, it’s recommended to set up a GitHub repository with a CI pipeline that automatically generates build artifacts (Go binaries) and creates releases with these assets. This approach eliminates the need to set up the build environment on your VM. You can reference the GitHub Actions workflow from my repository for implementation details. If you’ve already forked my repository, you can skip this step as the workflow will be included automatically.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e88a111fb3d9a333fa2e63afd4c7a2f8/href">https://medium.com/media/e88a111fb3d9a333fa2e63afd4c7a2f8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*56yVXlHJQq9_Wx8glWCfXw.png" /></figure><p>After this setup, whenever you push changes to the master branch, the workflow will automatically create a draft GitHub release with the compiled binaries. You can review these artifacts and, once verified, publish the release. Later, we’ll download these compiled binaries directly to our virtual machine.</p><h3>Hosting the SSH app</h3><p>The next step is acquiring a virtual server. While major cloud providers like AWS, GCP, or Azure are options, their costs can be significant. For instance, an AWS t3.medium instance (4GB RAM, 2 vCPU) costs approximately $30 per month. Although this isn’t particularly expensive, it might be excessive for a portfolio project of this scale.</p><p>For this project, we’ll use Oracle Cloud Infrastructure (OCI) due to their generous free tier offerings. While AWS does provide a free tier, it’s limited to one year. In contrast, OCI offers substantial ‘Always Free’ resources, including:</p><ul><li>24 GB RAM</li><li>4 vCPU compute</li><li>200 GB block volume storage</li></ul><p>These resources remain free indefinitely (barring policy changes). You can learn more about OCI’s Always Free resources in their <a href="https://docs.oracle.com/en-us/iaas/Content/FreeTier/freetier_topic-Always_Free_Resources.htm">official documentation</a>.</p><h4>Create &amp; Activate OCI Account</h4><p>Begin by creating an account on Oracle Cloud Infrastructure (OCI)<a href="https://signup.oraclecloud.com/"> </a><a href="https://signup.oraclecloud.com/">https://signup.oraclecloud.com</a>. Important: Before signing up, carefully research and select your home region based on VM.Standard.A1.Flex instance availability, as this is the only instance type eligible for the free tier. Since your home region cannot be changed after registration, it’s crucial to verify A1.Flex availability in your preferred region before proceeding.</p><p>During the registration process, OCI requires payment verification, which includes a one-time charge of approximately $4 USD. This verification fee is typically refunded in the following billing cycle. While I can’t personally confirm the refund process, this nominal fee is a small investment considering the long-term value of the free-tier resources you’ll receive.</p><h4>Creating a Compute</h4><p>After your account is activated, we’ll proceed with creating an OCI instance. However, first, we need to generate an SSH key pair that will allow secure remote access to our virtual machine.</p><pre>ssh-keygen -t rsa -b 2048 -f ~/oci-key.pem # enter passphrase when prompted</pre><p>Copy the generated public key to your clipboard. We’ll need to provide this to OCI during instance creation, as it will be used to authenticate your connection attempts using the corresponding private key.</p><pre>ssh-keygen -y -f ~/oci-key.pem # copy the output either via pbcopy or xclip</pre><p>To create your instance, navigate to Menu &gt; Compute &gt; Create Instance and follow these steps:</p><ol><li>Enter a name for your instance</li><li>Select your home region (where free tier eligibility applies)</li><li>For the operating system, choose Oracle Linux 8 (arm64)</li><li>Set the shape to VM.Standard.A1.Flex</li></ol><p>Note: While other operating systems like Ubuntu are available, choosing them may require different steps than those outlined in this article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jmo9Q-NuEiW23M5xIJRgGw.png" /><figcaption>A1 instances are equipped with arm64 CPUs</figcaption></figure><p>In the ‘Add SSH Keys’ section, paste the public key we generated earlier. Under storage options, you can optionally configure additional block storage. Note that the free tier includes up to 200 GB of block storage volume.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*01KqofVG0zFS45H_-8nRkg.png" /></figure><p>After your instance is provisioned, you can connect to it via SSH using the instance’s public IP address (visible in your dashboard).</p><pre>ssh -i ~/oci-key.pem opc@&lt;public-ip&gt; # accept the fingerprint &#39;yes&#39;</pre><p>Keep this SSH session active, as we’ll need it to configure our SSH application in the following steps.</p><h4>Setting up the app</h4><p>Before deploying our SSH application on port 22 (the default SSH port), we need to reconfigure the system’s OpenSSH server to use a different port. This step is crucial; failing to do so would lock us out of the machine once our application takes over port 22.</p><p>First, we need to configure OCI’s security rules to allow connections on our new SSH port (224).</p><ol><li>Navigate to your instance details</li><li>Select ‘Attached VNICs’</li><li>Click on the associated Subnet</li><li>Open the default Security List</li><li>Click ‘Add Ingress Rule’</li><li>Enter port 224 in the destination port field</li><li>Save the configuration</li></ol><p>This allows incoming SSH connections on port 224, which we’ll configure in the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bbcrWycVsymcGl7HmoiaOw.png" /><figcaption>I have few other ports whitelisted since I host my website &amp; some few backend services.</figcaption></figure><p>Next, we need to modify the OpenSSH server configuration to use port 224. While you can manually edit /etc/ssh/sshd_config using vim or nano (requiring root access), here&#39;s a more efficient approach using the following commands that will handle the configuration automatically.</p><pre>sudo su # switch to root user<br>echo &quot;Port 224&quot; &gt;&gt; /etc/ssh/sshd_config # append a custom port<br><br># Following is Oracle Linux specifics, you&#39;ll need to tell SELinux<br># about this ssh port change<br>semanage port -a -t ssh_port_t -p tcp 224<br>semanage port -l | grep ssh # verify<br><br># Update the system level firewall to whitelist this port<br>firewall-cmd --zone=public --permanent --add-port=224/tcp<br>firewall-cmd --reload<br>firewall-cmd --list-all # verify<br><br># restart the sshd server<br>systemctl restart sshd</pre><p>Before proceeding, verify the new SSH configuration by opening a new terminal window and connecting to the machine on port 224,</p><pre>ssh -p 224 -i ~/oci-key.pem opc@&lt;public-ip&gt; # accept the fingerprint &#39;yes&#39;</pre><p>Once you’ve confirmed the connection works, we can deploy our SSH application. Since our instance runs on ARM-based Ampere architecture (A1), we’ll need to use the arm64 binary from our GitHub release artifacts.</p><pre># Download the appropriate binary<br>mkdir -p ~/portfolio &amp;&amp; cd $_<br>wget &lt;your-github-release&gt;-arm64.tar.gz -O app.tar.gz<br>tar xvzf app.tar.gz<br><br># Run the binary in background (with tmux)<br># Requires: sudo apt install tmux<br>tmux new-session -d -s portfolio ./server<br><br># To see the logs<br>tmux a -t portfolio<br># Press Ctrl-b + d to detach</pre><p>We’ll use tmux to manage our application session. This terminal multiplexer allows us to monitor application logs in real-time, access the running session remotely, keep the application running even after disconnecting (we can attach to the session at any time), easily inspect and troubleshoot the application.</p><p>Now you can test your portfolio by connecting to the machine’s public IP address using SSH. If you’ve previously connected to this IP address, you’ll need to remove its entry from your ~/.ssh/known_hosts file to avoid host key verification errors:</p><pre>ssh opc@&lt;public-ip&gt; # you don&#39;t need to pass ssh key</pre><h4>Adding a domain</h4><p>While your portfolio is now accessible via SSH, using an IP address and username isn’t the most user-friendly approach. Ideally, users should be able to access your portfolio with a simpler, more memorable command. A domain name would provide a more professional and accessible solution.</p><p>To make your portfolio more accessible, you can associate it with a domain name, like:</p><ol><li>Use an active domain name</li><li>Add an A record in your DNS settings that points to your machine’s IP address</li><li>If using Cloudflare, set the proxy status to ‘DNS only’ since we want direct SSH connections rather than routing traffic through Cloudflare’s servers</li></ol><p>Note: For additional security, you can implement Cloudflare Tunnels, though this is optional and would require a different setup approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ZsnxTQqU1gRIl4zDOW-Dw.png" /></figure><p>With the domain configuration complete, users can now access your portfolio using a simpler, more memorable command:</p><pre>ssh kaustubhpatange.com</pre><h3>Conclusion</h3><p>To streamline updates, you can automate the deployment process using GitHub Actions. When a release is published (moves from draft to public), it triggers a GitHub Action that connects to your machine via SSH on port 224. The Action then stops the currently running application, downloads the latest release binary, and launches the new version.</p><p>This automation ensures your portfolio always runs the latest version without requiring manual intervention. You’ll need to configure GitHub Secrets with your SSH credentials and server details, I’ll leave this as an exercise for you to explore.</p><p>All code examples, configuration snippets, and implementation details discussed in this article are available in my GitHub repository <a href="https://github.com/KaustubhPatange/ssh-kaustubhpatange.com">https://github.com/KaustubhPatange/ssh-kaustubhpatange.com</a>. Feel free to use it as a reference for your own implementation.</p><p><em>If you like my way of writing, you can follow me on </em><a href="https://twitter.com/KP206"><em>Twitter (X)</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa7b6a2eb1ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Neovim helped improve my productivity]]></title>
            <link>https://kaustubhpatange.medium.com/how-neovim-helped-improve-my-productivity-4214dd099340?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/4214dd099340</guid>
            <category><![CDATA[neovim]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[vim]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Tue, 23 Jan 2024 16:49:32 GMT</pubDate>
            <atom:updated>2024-02-04T05:16:02.100Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MqGruGmWOn3RZdglXKKqkQ.png" /></figure><p>As any journey starts, mine began a year ago when I watched multiple videos/shorts from <a href="https://www.twitch.tv/theprimeagen">ThePrimeagen</a> on effectively using Vim in his workflow. The way he used shortcuts and navigated around the editor ignited a spark inside me to give Vim a try.</p><p>Before discovering Vim, I identified as a devoted WebStorm user due to my pre-existing familiarity with Android Studio — an IntelliJ-based IDE. Given my dual responsibilities of writing front-end code for an Android app and concurrently developing backend solutions, it was clear for me to embrace the JetBrains rich IDE ecosystem.</p><p>I experimented with VSCode for a few weeks, but it consistently felt distinct to the extent that it seemed like certain features were always missing, less intuitive than what Webstorm had consistently done better. Even after installing and configuring multiple plugins to enhance the VSCode experience, it always seemed like an editor rather than a fully-fledged IDE. The tooling provided by Webstorm is so potent, and my prior experiences with JetBrains products have always been positive. Choosing Webstorm over VSCode was a straightforward decision for me.</p><h3>Vim as your Editor</h3><p>During this time I was consuming a lot of tech content on YouTube/Twitch, <a href="https://www.twitch.tv/theprimeagen">ThePrimeagen</a>, <a href="https://www.twitch.tv/teej_dv">teej_dv</a>, <a href="https://www.twitch.tv/tsoding">tsoding</a> who, in their own unique ways, incorporated command line editors into their workflows whether it was Vim or Emacs. When I saw them move at such a pace within their editor I immediately realized that this is something I should learn.</p><p>The “Vim as your Editor” playlist from ThePrimeagen made me realize that you don’t need Vim software to improve your efficiency rather he focused on Vim motions. Vim motions are the idea of having Vim key bindings applied to any tool to enhance productivity. Motions like hjkl 2j dt) ciw within your editor improve the way you move around text. Several tools also support Vim motions including IDEs and browsers, but they are most commonly seen to be used within text editing software.</p><p>So, I began using the IdeaVim Plugin, which introduces Vim motions to IntelliJ-based IDEs. In the initial days, I found it challenging to adapt to the new hjkl motions instead of the familiar arrow keys. Subsequently, I configured the vim-be-good plugin with Neovim, even though I wasn’t fully using Neovim. I knew this plugin would aid in improving my proficiency in Vim motions, so I practiced it daily whenever I found the time. Within a week, these fundamental key bindings became ingrained in my muscle memory, enabling me to navigate, perform relative jumps to lines, find words, and carry out basic text replacement/deletion and other tasks within Webstorm efficiently.</p><p>A month went by, and I became extremely proficient with these motions. It’s worth noting that I used to type with only two fingers, an uncomfortable typing method that caused pain in my fingers. This meant I couldn’t type continuously for an extended period without cracking my finger knuckles. During this period of learning Vim motions, I also started learning touch typing. It was challenging because I not only had to master hjkl but also press them using the correct fingers, a habit I was not accustomed to. However, driven by determination, I practiced and became comfortable with both touch typing and Vim motions. Within a month, my typing speed increased from 60 words per minute to 100 words per minute.</p><h3>Neovim as my primary editor</h3><p>Two months passed by with Vim motions in Webstorm, and I decided it was time to go full in Vim. I chose <a href="https://github.com/neovim/neovim">Neovim</a> because it had Lua support rather than editing config in VimScript, also it is a community-driven project so releases are pretty often, and has a rich plugin ecosystem. However, rather than writing my own configuration, out of laziness, I chose to use an existing Neovim distribution. <a href="https://astronvim.com/">AstroNvim</a> caught my attention as it appeared first in Google search results for distros, and the homepage had a cool vibe. I didn’t even know about other distributions like <a href="https://www.lazyvim.org/">Lazy</a>, <a href="https://www.lunarvim.org/">Lunar</a>, and <a href="https://nvchad.com/">NvChad</a> at that time.</p><p>Like other distributions, AstroNvim has good sensible defaults. Everything is already set up for projects like Python, Node, Rust, Go etc. At first, I was very confused about how to navigate between Neovim buffers especially getting in and out of <a href="https://github.com/nvim-neo-tree/neo-tree.nvim">Neotree</a> and <a href="https://github.com/akinsho/toggleterm.nvim">Toggleterm</a>, both very good plugins but for me it was a skill issue. There were a lot of challenges I faced during the initial setup of Neovim like getting to display icons correctly, correct “TERM” variable and also on Mac you have to disable font smoothing for your terminal otherwise the edges of text look smudgy and many many things which at this point I forgot.</p><p>I then moved to <a href="https://alacritty.org/">Alacrity</a> along with <a href="https://github.com/tmux/tmux">Tmux</a> for managing my sessions, one for work and others for personal projects that never got completed, and now I have dangling unused sessions. Tmux was a great addition to my overall setup as I can have multiple windows and panes without opening a new terminal window. With plugins like tmux-resurrect (for saving sessions across computer restart), tmux-yank (for copying any text within a window) my development experience became 10x.</p><p>Several months passed with me using AstroNvim as my IDE for various projects. At this point, I found myself bragging in front of my colleagues at work about my proficiency with Vim and how quickly I had adapted. I knew there was no turning back to using a GUI editor. I became so obsessed with terminal programs that opening a GUI felt like cheating, especially since I was also using Hacker News and Spotify TUI right within my Tmux session. Even tasks like making changes to a directory, such as finding files, grouping them into a subdirectory, renaming, and zipping, were nothing more than a series of multiple grep, sed, awk, find, and tar piping operations.</p><p>What I felt was the idea of switching to Neovim made me more close to the command line.</p><h3>Problems I faced with AstroNvim</h3><p>It’s not that the distro has issues, but to me, AstroNvim felt overly configured to the point where I wasn’t utilizing many of its features. Making edits to the configuration was also quite confusing; adding a new plugin or editing existing configurations often felt challenging due to its highly abstracted code. As a beginner with limited knowledge of Lua and Vim APIs, grasping all the intricacies became very tough.</p><p>So, one weekend, I took on the challenge to rewrite my configuration from scratch while maintaining all the required feature parity with AstroNvim, including its remaps. Instead of opting for a starter kit like <a href="https://github.com/nvim-lua/kickstart.nvim">kickstart.nvim</a> or <a href="https://github.com/albingroen/quick.nvim">quick.nvim</a>, I decided to begin from scratch. The Vim RC video from The Primeagen and LazyVim served as a valuable starting point, helping me understand the directory structure and providing custom configuration settings for some Neovim plugins.</p><p>I was able to go from 6k+ lines to 1.2k lines (<a href="https://github.com/KaustubhPatange/init.lua/pull/1">reference PR</a>). However, it’s not about the number of lines that matters most to me, but the fact that I can now navigate through my entire configuration and make edits comfortably.</p><h3>Two cents for beginners planning to start Vim</h3><ul><li>Get comfortable with Vim motions within your IDE first. IdeaVim for Intellij-based IDEs and Vim for VSCode. Here is my <a href="https://gist.github.com/KaustubhPatange/b4921ccb3b89ca8390f4255b3503a706">.ideavimrc</a> file for some basic remaps.</li><li>Familiarize yourself with basic terminal commands like grep, awk, sed, and find, as some Neovim plugins like <a href="https://github.com/nvim-telescope/telescope.nvim">telescope</a> and <a href="https://github.com/nvim-pack/nvim-spectre">spectre</a> will require them. This will make it easier for you to configure basic search /replace settings.</li><li>When planning to go full NeoVim start with a distro like LazyVim, NvChad, etc., and see if you can wrap your minds around concepts like buffers, tabs and are comfortable navigating around. The switch may not be subtle; initially, you’ll face challenges and be slower in tasks like browsing files, search/replace, and other LSP refactoring tools. However, since you’re using a distribution, most of these will be configured for you.</li><li>Eventually, start understanding your distro config and get familiar with basic vim APIs and options in vim.opt. Use :help for reading API and plugin docs. Many plugin maintainers write good documentation but it would be tiresome for people who are not friendly with reading text-only docs rather than some fancy stuff like markdowns.</li><li>At any moment you feel like your distro is becoming too overwhelming to edit and configure, it may be a good sign to write your config from scratch. Don’t edit upon kickstart.nvim or quick.nvim as your base source, keep them as a reference. These starter kits are not magic so it would be best for you to understand them and start from scratch. You will be copying a bunch of config settings from other sources and it is okay.</li><li>Use a VCS for your config, so any fuck-ups can easily be reverted.</li><li>Once everything starts to feel stable try using a terminal multiplexer like tmux or zellij. It can help you keep your multiple sessions active even on a remote machine.</li><li>These all points cannot be done in a day, it will take time as it did for me “a whole year”.</li></ul><h3>Conclusion</h3><p>Vim is not a key to become productive it is just a tool (to flex). At the end of the day if you are comfortable around your editor then keep using it but just become good at it.</p><p>My job involves heavy use of EC2 machines and ssh’ing into production servers and pods. An environment where there is no display/graphical unit was the main reason for me to switch from Webstorm to Vim so I could have a consistent personalized development experience across different workstations.</p><p><em>If you like my way of writing, you can follow me on </em><a href="https://twitter.com/KP206"><em>Twitter (X)</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4214dd099340" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrating From DynamoDB to MySQL to Redis]]></title>
            <link>https://medium.com/better-programming/migrating-from-dynamodb-to-mysql-to-redis-33c137fae108?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/33c137fae108</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[dynamodb]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Sat, 29 Jul 2023 04:39:06 GMT</pubDate>
            <atom:updated>2023-08-12T06:17:59.943Z</atom:updated>
            <content:encoded><![CDATA[<h4>Mistakes and learnings from choosing various databases</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ywyhiiE0jDedHhIF" /><figcaption>Photo by <a href="https://unsplash.com/@thefredyjacob?utm_source=medium&amp;utm_medium=referral">Fredy Jacob</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I’ve been developing an open-sourced VPN application for quite a while. The idea behind this app was for me to learn various new technologies in the world of Android development, so I gave myself a time period of 14 days to bring this idea into reality. And so at the end of July 2021, I made the code public and released the app on Google Play.</p><p>The launch was a success, I got a few hundred downloads without marketing (still hasn’t done it) but it gave me an opportunity to explore some new technologies in public. I still maintain this application to this day as for me it gave me a platform to experiment with new things.</p><p>To make this idea into reality I needed two things,</p><ul><li>A list of VPN configurations which is basically metadata about servers.</li><li>A simple UI that makes use of this configuration to seamlessly authenticate and establish a VPN connection.</li></ul><p>In this article, I’ll talk about the first problem. The code is public on <a href="https://github.com/KaustubhPatange/Gear-VPN">GitHub</a> so if anyone is interested they can check it out.</p><h3>How it used to work</h3><p>There are a lot of websites that serve OpenVPN configuration files. So my initial thought was to write a web scraper that parses these websites and extracts the metadata. Since I had a limited amount of time and also during those days I had very less hands-on experience in writing APIs I decided to write a scraper within the app. It used to run a background job periodically every 7 hours, scrap those websites &amp; save the metadata into a local SQL database.</p><p>This worked fine but it had a few flaws, first the scraping logic is shipped with the app which means any external changes to those websites would break the scraper causing it to fail and would make the app unusable so the only way to fix it is to release an update with the fix. The other problems were the scrapper was extremely slow, there was no way to identify if those configurations are valid without establishing a connection to VPN server which itself is time-consuming, as it was a heavy network operation it used to consume a good amount of battery on low-end devices. These problems made the user experience not so good (I got low ratings on my app).</p><p>It was time to address these problems, but I’m not quite sure how? So like any engineer, I dropped this problem and started working on other projects.</p><p>Fast forward to 1 year, I got to work in a company where we were given an opportunity to closely work with all the systems that powered the working of the product. So I was learning many new skills (Backend, DevOps) which later helped me get this project back on track.</p><h3>Why choose AWS?</h3><p>At my current company, we use AWS and Kubernetes to deploy and manage multiple microservices. The process for provisioning is an IaC model, we write code to a repository (terraform, ansible) -&gt; changes are detected by CD pipeline (Jenkins) -&gt; does an automatic apply if the configuration is valid. This gave me enough idea to start something on my own, basically to migrate my VPN app’s scrapping logic to my own AWS infra and expose convenient APIs for the client.</p><p>The reason I went with cloud computing &amp; not with any PaaS solutions like Heroku, Digital Ocean, Vultr, etc. is that I want complete control over my infra so I can efficiently manage the cost, deployments, any downtimes due to maintenance, plus such cloud computing providers (AWS, GCP, Azure) offers many services that has seamless integration with each other to build a scalable application. However, one downside to this approach is you get yourself “vendor lock” to a single provider. Having prior experience with AWS and also I got some free credits from them made me choose AWS to deploy my microservice(s) for my application.</p><p>The current setup uses 2 EC2 machines (t2.micro &amp; t2.small, now t3 family), both these machines have the service running on port 8081 which is then exposed outside VPC (to the internet) via an Application Load Balancer.</p><h3>Choosing DynamoDB</h3><p>There is one cronjob (script) in one of those EC2 machines which runs periodically that scraps those websites and dumps the metadata into a database and my service just serves the data to the client whenever requested. This model prevents the service from performing any time taking business logic &amp; also a good practice to keep such logic isolated for any future modification.</p><p>Since the data I handle is unstructured I decided to go with a NoSQL database. At first, I thought I should go for MongoDB through their Atlas program, a fully managed service for MongoDB, but their free plan (M0 cluster) does not provide AWS VPC peering. This is needed so that database connections can only be established by trustworthy IPs (eg: my Machine on AWS).</p><p>There is something that AWS offers as an alternative to Mongo Atlas called DocumentDB, however, the pricing is too much. A single on-demand t3.medium machine costs around $0.078 per hour i.e $58 per month. Since I’m in the free tier (as well as having some free credits) I should not be spending or wasting my credit on something which is not scalable for the long term.</p><p>I then came to know about another OLTP NoSQL database called DynamoDB. DynamoDB is a serverless NoSQL KV (key-value) store database designed for high performance. In DynamoDB there is something called as Capacity Units (CU) for reads and writes, so if you read one row/document it will consume 1 CU &amp; same for writes. It is free for 25GB per month of storage and offers 25 free provisioned read/write capacity units which means I can read 25 documents at a time.</p><p>Anytime I go over this limit I will be charged. One way to solve this is to limit the max capacity units which could slow down your SCAN operation anytime you cross these limits. There are other charges like $1.25 per million write and $0.25 per million reads. Overall, DynamoDB is pretty affordable.</p><h3>Why migrate to MySQL (bad idea)?</h3><p>The setup was simple, a cronjob will dump VPN configurations metadata to the database and the service will serve this metadata from the DynamoDB database. The performance started to degrade for unbounded queries, basically scanning &amp; returning data from the entire table started exceeding the max capacity units limit resulting in slow response (the max capacity was set to 25 but the table had ~50 items).</p><p>The client UI displays all servers grouped by country, so implementing pagination was not a solution hence the unbounded queries. Since the cronjob runs every 5 hours, I started sending HTTP Cache-Control headers with a TTL of 2 hours. Any HTTP client (whether in browser or mobile) will respect this TTL and will try to serve a cached response until it becomes stale. Doing so I was able to reduce latency but the problem of slow response was still there.</p><p>Since I had ~50 items with 25 max capacity units, it use to take twice the amount of time to query the whole table. Increasing the max capacity unit would be a temporary solution as the number of table items can increase anytime. It seems for my case (unbounded queries) DynamoDB was never a solution. It is performant if your query acts on a specific index (primary or secondary) which would be blazingly fast. Also, DX for writing queries for DynamoDB is not very intuitive (but that is okay) for eg, taking a look at an update query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cfM0WDWJOlk8Ce7CnpjvlQ.png" /><figcaption>Microservice is written in NodeJS (Typescript)</figcaption></figure><p>It was time to go for SQL, any unstructured data will be stored as raw JSON/bytes. Also, the pricing for RDS is $0.016 per hour for t3.micro (2vCPU, 1GB RAM) i.e $11 per month. Though it looked like 1GB RAM was enough for my use case, it was really not. During migration, I started sending production traffic from DynamoDB to MySQL and the results were promising, there was a decrease in latency so I end up sending the entire traffic to MySQL after a couple of days.</p><p>Things were looking great for some weeks (sending Cache-Control headers was also helping) until I made the API public. Yes, I thought it would be a great idea to make the API public and monetize it. Suddenly after a few days, I started seeing spikes in the Database Connection, and now the latency is high again. My app has 100+ DAU, and though it seemed like MySQL worked well, it really did not. The only optimization that was helping me till now was sending Cache-Control headers.</p><p>When the API was made public I got an unknown amount of traffic directly hitting the database, eg: when you test API (on Postman) you generally ignore these headers resulting in a slow response.</p><p>Also, for unbounded queries, the response was over 1.5 MB i.e. each record was of size 30–40 KB so if there is enough traffic 1GB RAM machine may not be performant in scanning all those records from disk. Upgrading RAM to 2 GB basically doubles the cost ($22) which was something I did not see as an option.</p><h3>Moving to an In-memory database</h3><p>Now, I have my goals clear. I need something which has low latency, high throughput &amp; also cheaper. Databases that serve data from disk would not be suitable at least considering my budget. I would need something that is fast so my natural choice was to go with managed Redis service. Moving to AWS ElasticCache would not be cheaper, so I started looking for 3rd party managed service &amp; I stumbled upon Aiven. <a href="https://aiven.io/">Aiven</a> is another PaaS tool but for databases. Luckily, they have a free plan for managed Redis that gives 1GB RAM of the machine, also I can whitelist IPs so only machines from my AWS account can access the database. It is a manual process (since they don’t provide Network peering for free plan), I can live with it.</p><p>At the time of writing this article, it’s been 3 days since the migration. Since it is a 3rd party service &amp; I’m using a free plan there is nothing like Cloudwatch alarms which would notify me when the memory threshold is reached. So, for now, I implemented a basic health check in my service that continuously pings Redis every 10 seconds and if there are more than 3 consecutive failures the service fallbacks to the MySQL database and sends a Discord message.</p><h3>Conclusion</h3><p>In the future, I’ll move from MySQL to a NoSQL solution as a fallback database, might as well give DynamoDB another try, and would think of ways to improve the SCAN operation.</p><p>I hope this article is interesting for the reader. Anyways, if you’ve any questions feel free to message me.</p><p>PS: I’m also on <a href="https://twitter.com/KP206">Twitter</a> :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33c137fae108" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/migrating-from-dynamodb-to-mysql-to-redis-33c137fae108">Migrating From DynamoDB to MySQL to Redis</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling back press in Android 13, the correct way]]></title>
            <link>https://proandroiddev.com/handling-back-press-in-android-13-the-correct-way-be43e0ad877a?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/be43e0ad877a</guid>
            <category><![CDATA[onbackpressed]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[android-13]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Tue, 14 Feb 2023 20:52:43 GMT</pubDate>
            <atom:updated>2023-02-14T20:52:43.844Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9Gg7ijnz47jGTWMp" /><figcaption>Photo by <a href="https://unsplash.com/@pechka?utm_source=medium&amp;utm_medium=referral">Dima Pechurin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Android is known for many things, but recently looking at the platform &amp; API releases it is mainly known for its breaking changes. Considering how they notoriously broke clipboard monitoring functionality (OnPrimaryClipChangedListener) to be available <strong>only</strong> for system apps starting from Android Q breaking over <a href="https://play.google.com/store/search?q=clipboard%20manager&amp;c=apps">tons of apps</a> which used to rely on this callback are now deprecated &amp; do not even show up on Google Play searches making this <a href="https://issuetracker.google.com/issues/123461156">issue tracker</a> one of the most long-running (marked P1) stale request. You can literally see in the comments where developers of many popular apps are requesting for an alternative solution like maybe putting this change/feature behind a <a href="https://developer.android.com/training/permissions/requesting-special">Special Permission</a> like System Overlay, etc.</p><p>OnBackPressedDispatcher was introduced somewhere around 2019 through androidx.activity API. This change suggested moving away from onBackPressed callback to a new listener method similar to ActivityResultContract. Some said this was a good change as this made us move away from the activity override method &amp; rely on callbacks through listeners which means we can now write the back press logic anywhere like in Fragments. The only catch is the way it handles the callback.</p><h3>The underlying problem</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c152789b21d3582ba5687de79c4f2ec/href">https://medium.com/media/2c152789b21d3582ba5687de79c4f2ec/href</a></iframe><p>To listen for the back press you add a OnBackPressedCallback. If you look closely the callback accepts a constructor parameter enabled which says whether this callback is active i.e should handledOnBackPressed() method of this callback to be called or not. When you add a callback it creates a <a href="https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:sdk-common/src/main/java/com/android/ide/common/util/Cancelable.kt">Cancelable</a> version of this callback that has a cancel method which removes it from the back press callback list.</p><p>There is another overload which accepts LifecycleOwner as an argument that basically calls this cancel method whenever the owner’s state reaches onStop &amp; re-adds the callback when the state reaches onStart. This means you don’t have to handle the removal &amp; adding of callback manually.</p><p>All the callbacks added through addCallback will execute in the reverse order, if one is handled then it ignores the rest in the chain. The important part here is that whenever your logic execution is complete you must set the isEnabled property to false or remove() the callback so that it can be skipped next time in the chain. This is what onBackPressedDispatcher does internally,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7655c7eae066cb02d8236306b181e3b/href">https://medium.com/media/d7655c7eae066cb02d8236306b181e3b/href</a></iframe><p>If there are no callbacks to handle the back event then it defaults to use mFallbackOnBackPressed. For androidx.activity.ComponentActivity it will call super.onBackPressed() which means the overridden method of onBackPressed defined in ComponentActivity will be not called instead it will delegate to platform’s app.Activity method. So if you extend ComponentActivity &amp; write a custom back press logic by overriding onBackPressed() method, it won’t be called.</p><p>This will only happen when you opt-in to the <a href="https://developer.android.com/guide/navigation/predictive-back-gesture#opt-predictive">predictive back gesture</a> introduced in Android 13. The way this work is your window’s DecorView receives a KEYCODE_BACK which then <a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=3874">dispatches a key event to </a><a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=3874">app.Activity’s </a><a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=3874">onKeyUp() that then calls the </a><a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=3874">onBackPressed() method</a>. This delivery of events is done by <a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/view/ViewRootImpl.java;l=5883">InputStage</a> , more specifically an implementation of InputStage by name ViewPreImeInputStage. If you look at <a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/view/ViewRootImpl.java;l=6271">line 6271 of ViewRootImpl</a>, you will see exactly how it dispatches the event &amp; then trace it down to Activity’s onKeyUp() method.</p><p>For Android 13, when you opt-in to the <a href="https://developer.android.com/guide/navigation/predictive-back-gesture#opt-predictive">predictive back gesture</a> by setting android:enableOnBackInvokedCallback to true in &lt;application&gt; tag, <a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/view/ViewRootImpl.java;l=6204">NativePreImeInputStage</a> is used. Here, a special implementation of OnBackInvokedDispatcher called <a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/window/WindowOnBackInvokedDispatcher.java">WindowOnBackInvokedDispatcher</a>’s latest onBackInvokedCallback is invoked instead of dispatching the event to Activity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebd45f25cd0d6a3e6dc7bc43008be56d/href">https://medium.com/media/ebd45f25cd0d6a3e6dc7bc43008be56d/href</a></iframe><p>This registerOnBackInvokedCallback() accepts a priority argument which tells the order in which callbacks will be returned. Here in getTopCallback() it returns the latest onBackInvokedCallback. From Android 13, the app.Activity will <a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=1663">register a </a><a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=1663">PRIORITY_SYSTEM callback during </a><a href="https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=1663">onCreate()</a>. This callback does nothing much but mostly finishes/minimizes the activity.</p><p>Things started to break when it stopped calling Activity’s onBackPressed()method. This was a major drawback because now we cannot perform any action during the back press before the activity is finished. Earlier we could do that like,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9db8c6524955bb05fb13ad341edbb4bc/href">https://medium.com/media/9db8c6524955bb05fb13ad341edbb4bc/href</a></iframe><p>This, super.onBackPressed() was an important call to all the parents which in turn finishes the activity. Also, make sure to not call finish() anywhere in your OnBackPressedCallback as it will break the predictive back gesture feature.</p><h3>Supporting Predictive back gesture</h3><p>So how do we handle such a scenario?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea9697395ec12c808c0020e1ce71ad95/href">https://medium.com/media/ea9697395ec12c808c0020e1ce71ad95/href</a></iframe><p>The above change will work but any other callbacks added after this will be given more priority. From what I’ve seen,androidx.fragment ‘s 1.3.6 version FragmentManager automatically registers an onBackPressedCallback to remove the current fragment from the backstack which means during the back press, the callback which we register will not be called &amp; instead the callbacks registered by FragmentManagerwill be called. So if you are handling fragment transactions manually or using a 3rd party library where there is a need to keep track of such current fragments then you may need to update the code accordingly.</p><p>The better way in my opinion is to basically avoid onBackPressedDispatcher &amp; rely on onBackInvokedDispatcher. As you might know, each onBackPressedDispatcher has a onBackInvokedDispatcher. This onBackInvokedDispatcher is responsible for calling all the onBackPressedCallbacks in descending order (check the 2nd code snippet of this article) &amp; from the 3rd snippet, we know the latest onBackInvokedCallback which is registered with a high priority will be invoked (in the default case it will be the one that calls onBackPressedCallbacks).</p><p>What we can do is provide our custom implementation with much higher priority. Any priority greater than 0 is fine as that is the default priority with which the default onBackInvokedCallback is registered by onBackInvokedDispatcher.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed8760f1c793f298fd0437468c1e6632/href">https://medium.com/media/ed8760f1c793f298fd0437468c1e6632/href</a></iframe><p>The implementation is straightforward, after performing the action we just delegate to onBackPressed() -&gt; onBackPressedDispatcher.onBackPressed() -&gt; android.app.Activity.onBackPressed() as there is no way to directly callapp.Activity ‘s onBackPressed() from a deeply-nested class because that’s how inheritance work.</p><p>Make sure there are no onBackPressedCallbacks present in onBackPressedDispatcher. If this contract is followed then you can treat onBackPressedDispatcher.onBackPressed() as your super.onBackPressed() which will finish/minimize the activity.</p><h3>Conclusion</h3><p>Make sure to opt-in to the predictive back gesture through the manifest. If your targetSdk is 34, then you can skip the below step.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fa98f2e71e367d2c0728026f8ae4169/href">https://medium.com/media/8fa98f2e71e367d2c0728026f8ae4169/href</a></iframe><p>A complete implementation that uses onBackPressed() for versions lower than 13 &amp; onBackInvokedDispatcher for version above 13 will look like this,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc2497daca4e512fecdbe834f97ce9eb/href">https://medium.com/media/bc2497daca4e512fecdbe834f97ce9eb/href</a></iframe><p>All you have to do now is extend your Activity class with BackPressCompatActivity and call setOnBackPressListener to either return true to false. Returning true will finish/minimize the activity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ad54f8f7ed4183d3d94404a953ad0a6/href">https://medium.com/media/8ad54f8f7ed4183d3d94404a953ad0a6/href</a></iframe><p>Hope you like this discussion, if you’ve any doubts or concerns let me know through comments or <a href="https://twitter.com/KP206">Twitter</a> :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be43e0ad877a" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/handling-back-press-in-android-13-the-correct-way-be43e0ad877a">Handling back press in Android 13, the correct way</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Incrementally moving to Rust]]></title>
            <link>https://tales.pratilipi.com/incrementally-moving-to-rust-b848a1c79e32?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/b848a1c79e32</guid>
            <category><![CDATA[cron]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[pratilipi]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Wed, 10 Aug 2022 16:01:26 GMT</pubDate>
            <atom:updated>2022-08-12T13:31:27.405Z</atom:updated>
            <content:encoded><![CDATA[<h4>We at Pratilipi Comics are slowly adopting Rust in our work</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*t4HjXpGcg2ks97my" /><figcaption>Photo by <a href="https://unsplash.com/@oceanng?utm_source=medium&amp;utm_medium=referral">Ocean Ng</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As developers, the one thing we don’t have is time. When working on a feature, usually we ignore the code-style, best practices &amp; rush to develop these features as fast as we can to complete the task in the given time. This results in buggy, untested code that gets shipped onto the user’s device who faces them (at most a crash) which in turn creates a bad experience for them. The only way we get to know about these crashes is by using crash monitoring tools like Crashlytics, Bugsnag, Sentry, etc. where we try to identify what part of code has caused this crash, run a git blame &amp; blame that person! Just kidding, we then try to reproduce it on our device, come up with a patch &amp; try to fix it. Usually, it gets fixed but as said maintaining software is hard so you might have to go with trial &amp; error methods to fix a particular annoying crash that woke you up at 4 in the morning.</p><p>Can you identify what could’ve been done to prevent these bugs from ever getting shipped in the production? Some people would suggest removing this time-constraint barrier so that they can work without pressure &amp; focus more on code &amp; testing it. However, this cannot be always possible. I’ll tell you why, when you work in an organization or a startup with a small team like 10–15 engineers it is not always feasible to work slowly, what you want is a rapid growth of product so yes things will break &amp; you can’t do anything unless hire a QA person/team to test your code changes but even doing so it won’t prevent you from writing buggy code.</p><p>From the dawn of time when programming languages were evolving to improve developer productivity with things like better type-system, IDEs, and compile-time safety someone thought hey why not just develop a language that is very simple to learn &amp; write but has no types (except primitive), no runtime errors &amp; unexpected behaviors when doing basic comparison using == , yes I’m talking about Javascript.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca3ac8dfa0235b5ea5412862acd0ccae/href">https://medium.com/media/ca3ac8dfa0235b5ea5412862acd0ccae/href</a></iframe><p>Consider the above example, where we write a function to add 1 to a number. Sure this will arise a lot of questions in your mind. Will “n” always be the number? What if n is null, how are we handling that? What is the return type of the function? All of these assumptions we’ve to keep in mind even though we are just writing a function to increment a number by 1.</p><p>Even introducing TypeScript in the project &amp; relying on TypeScript’s type system is not a 100% bullet-proof solution. In my opinion, the greatest disadvantage of TypeScript is that it can bring you a false sense of security. Yes, it’s a huge benefit that the language can check types for us and warn us when there’s something wrong with our code. However, relying on this too heavily comes with significant risk. TypeScript performs type checks only during compilation. Afterward, we’re dealing with pure JavaScript that doesn’t do that. This means we may still encounter some bugs that the compiler didn’t find, though admittedly there are going to be far fewer of them than if we hadn’t used TypeScript.</p><h3>Rust will make you feel like a Genius</h3><p>One of the things I like about Rust is its compile-time safety. The errors that the compiler spit out are one of the nicest things I’ve ever seen.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c0ba04e98e7f7b397defff4e1e81e25/href">https://medium.com/media/1c0ba04e98e7f7b397defff4e1e81e25/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3da5b1fdeefa81d8b3751b7b3d00a342/href">https://medium.com/media/3da5b1fdeefa81d8b3751b7b3d00a342/href</a></iframe><p>When developing in rust it almost feels like there is a tour guide who is always keeping a watch on you &amp; corrects you if you did something wrong unknowingly. This system of communication between a developer &amp; compiler is one of the things I like about Rust.</p><p>Rust is both statically-typed and strongly-typed which means the compiler knows about the type during compile time rather than dynamically inferring types at runtime. By default, variables in Rust are immutable (cannot be modified once assigned), you can make them mutable by adding a keyword mut . This default immutability can help you save tons of debugging later when you try to identify which thread had modified your object. There are no classes/interfaces in Rust but something called as struct &amp; trait .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fcb2d6240ee21917cdcea9480e8f262/href">https://medium.com/media/7fcb2d6240ee21917cdcea9480e8f262/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/272144f18e97702842a35e7667db8867/href">https://medium.com/media/272144f18e97702842a35e7667db8867/href</a></iframe><p>But wait, hold on if there are no classes how can we do object-oriented programming in Rust? The thing about Rust is that it nearly changes your programming paradigm i.e you’ve to unlearn a lot of things not because they are wrong but languages like Java, and Python hides all these implementations from you. We are so used to encapsulating our logic behind a class that we forgot about the primitives we came from. Now, to answer the question take a look at the following code,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f794f9322cd739cd7a4a2dd52b0ef87/href">https://medium.com/media/2f794f9322cd739cd7a4a2dd52b0ef87/href</a></iframe><p>All we need is a series id to calculate no. of likes &amp; comments count. As you can see the code is pretty straightforward there is no unnecessary allocations &amp; each implementation is separated by a trait thus maintaining separation of concern. For someone, it may look like we’ve declared an extension function on an object which is a correct assumption that’s why we don’t see :: (path separator) because those methods have become part of the structure. When coming from languages like Java this may seem a little weird (don’t worry I felt the same) but once you hop on a journey to learn Rust trust me it will never be the same :)</p><p>In Rust, you don’t necessarily create structures based on your logic instead you act on it. This helps to incorporate the single responsibility principle into your codebase without you knowing it. Things like this make Rust a perfect language for almost any stack. Yes, it has a learning curve &amp; some people might find it difficult as we are so used to OOP that we forget about the fundamentals. One such example is managing memory ourselves, meaning allocating &amp; deallocated objects. How many of you know about malloc ? I’m sure most of us have. The main catch with malloc is that you need to manually free the memory when you don’t need it. What if I told you in Rust, this is done automatically not at the runtime but at the compile time.</p><p>You must’ve heard that Rust does not have a garbage collector but what does this mean? When you run a program in languages like Java, Python, etc. a small program will run alongside to free up your object allocation. This is done in two phases, <em>Mark &amp; Sweep.</em> Garbage collector (GC) will mark objects which aren’t used i.e there is no pathway to reach the given object, then it Sweeps (remove) the object that is identified earlier. This approach has many advantages but also comes with certain disadvantages that should not be overlooked,</p><ul><li>Since the <em>runtime</em> has to keep track of object reference creation/deletion, this activity requires more CPU power than the original application. It may affect the performance of requests which require large memory.</li><li>Programmers have no control over the scheduling of CPU time dedicated to freeing objects that are no longer needed.</li><li>Using some GC implementations might result in the application stopping unpredictably.</li><li>Automatized memory management won’t be as efficient as the proper manual memory allocation/deallocation.</li></ul><p>In Rust automatized memory management is solved through a concept called borrow-checker. The rules are very simple,</p><ol><li>Data has one owner.</li><li>Data may have multiple readers or one writer.</li></ol><p>Let’s see this in an example, Joey &amp; Amanda are two people who wants to eat a single pizza,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b3c3541d9d929c728804d790654698f/href">https://medium.com/media/6b3c3541d9d929c728804d790654698f/href</a></iframe><p>What happened why can’t Amanda eat the same pizza? Well for starters we should stay away from Joey because <em>“He doesn’t share his food” :)</em></p><p>In Rust, we have primitives like pass-by-reference (&amp;) called borrowing &amp; pass-by-value called transferring the ownership. In main method, the lifetime of the variable pizza, joey, amanda is bounded by the scope defined by the curly braces { } which means once this method completes Rust will automatically free up the memory allocated by pizza, joey, amanda. In the same method, we are also adding toppings &amp; eating pizza. Both take the first argument as Pizza but one takes it by reference &amp; the other by value. When passing the value by reference we are guaranteed that despite any modification happening to pizza the pointer to the memory address where pizza lives will not change. Hence, Rust allows multiple borrowing. This is not the case with passing pizza by value, one can modify the original reference to the pizza &amp; could do anything with it (you can see that in eat method where we are assigning a new value). Hence, the main method is not sure whether the reference it has is valid or not &amp; that’s why Rust throws a compile-time error.</p><p>Not only does this ensures code correctness but it also provides some runtime benefits like performance. Yes, Rust is faster in fact in many cases it can be faster than C. Take an example from <a href="https://medium.com/star-gazers/benchmarking-low-level-i-o-c-c-rust-golang-java-python-9a0d505f85f7">this</a> article, where <a href="https://medium.com/@xnuter?source=user_profile-------------------------------------"><strong>Eugene Retunsky</strong></a><strong> </strong>implemented TCP proxy servers in various languages &amp; benchmarked them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jS-1Irr0VvXB_OLl.png" /><figcaption><a href="https://medium.com/star-gazers/benchmarking-low-level-i-o-c-c-rust-golang-java-python-9a0d505f85f7">https://medium.com/star-gazers/benchmarking-low-level-i-o-c-c-rust-golang-java-python-9a0d505f85f7</a></figcaption></figure><ul><li>The blue line is tail latency (Y-axis on the left) — the lower, the better.</li><li>The grey bars are throughput (Y-axis on the right) — the higher, the better.</li></ul><p>Rust can definitely be used in a backend service &amp; you can yourself see the performance gain it brings. Even by comparing web frameworks on <a href="https://www.techempower.com/benchmarks/#section=data-r21"><em>techempower.com/benchmarks</em></a>, Rust’s <a href="https://github.com/HFQR/xitca-web">xitca-web</a>, <a href="https://salvo.rs/">salvo</a>, <a href="https://github.com/Xudong-Huang/may_minihttp">may-minihttp</a>, <a href="https://actix.rs/">actix</a> frameworks are in the top 10<em>.</em></p><p>For anyone coming from node.js background I’ve got good news, Rust’s toolchain is very similar to that of node.js.</p><ul><li><strong>cargo </strong>— packaging, building (similar to npm)</li><li><strong>cargo fmt</strong> — standard formatting (similar to tsfmt)</li><li><strong>cargo test</strong> — doc &amp; unit tests</li><li><strong>cargo bench</strong> — benchmarking</li><li><strong>cargo clippy</strong> — code listing (similar to eslint or tslint)</li><li><strong>rustup</strong> — rust version switching (similar to nvm)</li></ul><p>For the past 16 years, the Rust <a href="https://www.rust-lang.org/community">community</a> has evolved so much that the language has become the most popular language on <a href="http://stackoverflow.com/">stackoverflow.com</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-vzkK96dB4SnXgtN.png" /><figcaption><a href="https://insights.stackoverflow.com/survey/2021#most-loved-dreaded-and-wanted-language-love-dread">https://insights.stackoverflow.com/survey/2021#most-loved-dreaded-and-wanted-language-love-dread</a></figcaption></figure><h3>Rust at work</h3><p>Instead of rewriting all your microservices in Rust, we are going to slowly adopt it. At Pratilipi Comics, we experiment a lot &amp; one of such is when we wrote a cron-script in Rust. Yes, the title of this article says we are doing it in a production environment but that is not far, we will surely migrate to Rust for our backend services if that seems feasible in the near future.</p><p>As for now, we are using it for writing CronJobs that are deployed on Kubernetes. A cron job is a job that is scheduled to run at a specific time defined by the cron itself. One of the scripts that we deployed is the Category/Genre automation script, we have an optimized algorithm that calculates the top series for a genre based on likes, subscriptions &amp; reviews. The problem here is that executing this algorithm takes a good amount of time, so instead of running it every time whenever a user sends a request we store it in a Redis cache &amp; update the cache by this cron job. So, any incoming request will fetch the response from this cache &amp; process accordingly.</p><p>Now since you understood the whole process let’s see how this script is written. The algorithm I talked about is basically a long SQL query acting upon a MySQL database. So we need a library to connect &amp; execute a query on our MySQL database. In Rust, a library is called a crate. There are two types of crates, binary crate &amp; library crate. <em>Binary crates</em> are programs that are compiled to an executable so that they can run on any platform. <em>Library crates</em> don’t have a main function, and they don’t compile to an executable. They define functionality intended to be shared with multiple projects for eg: <a href="https://crates.io/crates/mysql">mysql</a> is a crate that provides MySQL database driver in pure Rust. We are also going to use <a href="https://crates.io/crates/reqwest">reqwest</a> which is a batteries included HTTP client for Rust, <a href="https://crates.io/crates/mini-redis">mini-redis</a> for Redis &amp; yes finally we will use an asynchronous runtime called <a href="https://crates.io/crates/tokio">tokio</a>. Note that I said a runtime, not a framework because async/await primitives are built in Rust &amp; <a href="https://crates.io/crates/tokio">tokio</a> is an abstraction over it with a runtime that provides the building blocks needed for writing asynchronous applications + a lot of features for writing multi-threaded applications.</p><p>A Rust project has Cargo.toml file (a manifest for a package) where you will specify all your configuration (like package.json in node.js). In the dependencies section, you can add any library crates you want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*-4WjtxHhjQ92qQBlvAGSFQ.png" /><figcaption>cargo.toml</figcaption></figure><p>Once done, similar to npm install you run cargo update which will pull up the dependencies in the cargo registry. cargo run to run the executable &amp; cargo build to build an executable for your platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*TXpMsJCb8gCror4jURm-BQ.png" /><figcaption>package-structure</figcaption></figure><p>The package structure for our cron job script looks like the above, there is no specific package structure we’ve followed because it is just a script &amp; not a full fledge application. There is main.rs which has the main function that makes use of modules defined in internals package. In Rust, there is a module system, basically here each file is a module which has a trait that executes specific logic for eg: cache.rs abstracts Redis cache implementation &amp; expose APIs to be consumed, in the same way comics_db.rs abstracts the database implementation &amp; fevicol.rswhich is the name of one of our microservices that deals with many things including likes, reviews &amp; comments. All of them expose APIs to be consumed by our main.rs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*u_hoGDewyvjSxVeZ0KBOew.png" /></figure><p>For this application we followed a pattern inspired by UDF (Uni-directional Data Flow), in our case, data i.e object references are passed to the modules &amp; errors are propagated up (only if a module fails to mitigate the issue). Such errors which cannot be recovered are thrown i.e panic ed. This is useful in cases where suppose if the program fails to initialize <em>mysq</em>l or <em>redis</em> then there is no need to proceed with the current execution as everything depends on it. There are many programming patterns but I find this to make the most sense.</p><p>By following this pattern each module has its own responsibility to do something with the data or return the processed data, this encapsulates logic from external modification thus ensuring the “single responsibility principle”. Let’s take an example of fevicol.rs where we fetch the list of genre names.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2db8fbc137bd3bf6703739237830ba38/href">https://medium.com/media/2db8fbc137bd3bf6703739237830ba38/href</a></iframe><p>From here you can see we are making an API call, just look at the code even if you don’t know how to code in Rust you can easily decode the logic. Notice the return type Result&lt;GenreData, String&gt; . io::std::Result&lt;T, E&gt; is an enum with Ok(T) and Err(E) values where you can return whether the return response is successful or not (similar to Kotlin’s Result&lt;T&gt; class). In our case the error type is String , as I said before when the error is unrecoverable we stop the process (exit code 1).</p><p>We are also using a JSON serialization/deserialization crate called <a href="https://crates.io/crates/serde">serde</a>. GenreData &amp; GenreItem are our types into which the response JSON will be deserialized. You can also omit any unwanted properties, that’s why here we’re only interested in id &amp; name. #derive[...] is a macro. Just like we’ve annotations &amp; annotation processors in JVM, Rust’s macro system is their approach for Meta-programming.</p><p>Finally, we glue all this together in our main function,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38e144ef50d4235ed18c98484f8f5720/href">https://medium.com/media/38e144ef50d4235ed18c98484f8f5720/href</a></iframe><p>Concurrency in Rust is so clean &amp; easy to use. Each await is a Future&lt;T&gt; (similar to Promise in Javascript or Job in Kotlin coroutines), so we can chain &amp; execute them in parallel; future::join_all does that for us (similar to Promise.all or awaitAll in Kotlin coroutines).</p><p>Edit: <a href="https://github.com/tokio-rs/mini-redis"><em>mini-redis</em></a> does not support selecting database numbers, as you know Redis has 0–15 database instances &amp; we use 11 to store this cache. Also, <a href="https://github.com/tokio-rs/mini-redis"><em>mini-redis</em></a><em> </em>does not support executing custom commands where after the connection we could execute SELECT db command by ourselves. Due to such issues with <a href="https://github.com/tokio-rs/mini-redis"><em>mini-redis</em></a><em>, </em>it has been replaced with <a href="https://crates.io/crates/redis"><em>redis</em></a><em>.</em></p><h3>Conclusion</h3><p>With great power, there also comes great responsibility; the same holds true for Rust’s memory management. Rust can only perform these memory optimizations within Rust code so any second you pull any 3rd party C/C++ library you are on your own to write an unsafe code that could potentially break. The good news is, that there are many libraries that are rewritten in Rust by the community so you’ll hardly have to ever write unsafe code unless you are touching OS-level bindings.</p><p>Even though the compile-time safety is so much great in Rust it takes a significant amount of time to compile the code, this could slow down your developer productivity.</p><p>One of the benefits of Rust is that it outputs a very small single executable (in release mode with strip enabled) but not so for the intermediate build files. During build time the size of these temporary build files can go up to 2 GB in size. This is one of the problems we faced because our cron machine has a max 8 GB space where spitting out this 2 GB build is not great every time you run cargo build . The good news is, that with --release flag enabled this shrinks down to 300 MB but is still much larger. What we’ve done to mitigate this issue is we wrote a shell script that builds &amp; stores the executable upon the first run so that any subsequent run will use the same executable. This shell script additionally does one more job which is to rebuild the executable if there is a change in the source code (since we store all the sources in the cron machine for now), this is done by comparing md5 hashes of files with the previous one which was stored when the build was run for the first time. You can take a look at the code of the script <a href="https://gist.github.com/KaustubhPatange/743683d5b5e2d25c5304d6c59bb1a4fe">here</a>.</p><p>Lastly, I want to say that Rust has so many benefits &amp; to me personally, it feels like a perfect language designed for developers not only just to write CLI tools but also for backend &amp; other stacks. If you are still unsure whether to start learning Rust, just take a look at the <a href="https://github.com/topics/rust">projects on Github that are built with Rust</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b848a1c79e32" width="1" height="1" alt=""><hr><p><a href="https://tales.pratilipi.com/incrementally-moving-to-rust-b848a1c79e32">Incrementally moving to Rust</a> was originally published in <a href="https://tales.pratilipi.com/">Team Pratilipi</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The State of Navigation in Jetpack Compose]]></title>
            <link>https://proandroiddev.com/the-state-of-navigation-in-jetpack-compose-cc13eb6ac3d9?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc13eb6ac3d9</guid>
            <category><![CDATA[jetpack-navigation]]></category>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[ui-toolkit]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Wed, 08 Jun 2022 12:18:19 GMT</pubDate>
            <atom:updated>2022-06-09T10:39:13.728Z</atom:updated>
            <content:encoded><![CDATA[<h4>Investing in good navigation framework from start will help you save tons of migration work afterwards</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*24HOuEEp7E6wrCu7" /><figcaption>Photo by <a href="https://unsplash.com/@rocinante_11?utm_source=medium&amp;utm_medium=referral">Mick Haupt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>It’s been about a year since Google announced Jetpack Compose’s 1.0 stable release, meaning developers can now create production-ready apps using its UI toolkit but, should you? Keep in mind that whatever I’ve said in this article is purely my opinion, so if there is anything you don’t agree then let me know through comments or Twitter :)</p><p>Over the course of time, the term “stable” has been misused a lot &amp; it feels to be true for Jetpack Compose. Even though they (Google) say it is production-ready you should really do your research when using it for a big business project like checking active issues on the <a href="https://issuetracker.google.com/issues?q=componentid:610764%20status:open&amp;s=modified_time:desc">issue-tracker</a>, write sample apps to see how it performs i.e whether the toolkit provides the necessary widgets? Does it affect the size &amp; performance of the release app? And most importantly does it affect developer productivity &amp; what about tooling support? This is not only the case for Jetpack Compose but for any library/framework you wish to use. One of the techniques they’ve used to get away with not being called as “alpha” is by introducing @Experimental annotations to mark some features as unstable (not that these features don’t work, but the API could change breaking the binary compatibility with the previous versions). You should really take such things into account when choosing a completely new framework for writing UIs.</p><p>So when will it get stable for production use? Don’t get me wrong you can still use it but there are some gotchas you should really know if you are using it in apps that are consumed by millions of users. Keep in mind that the existing View-based toolkit had 10 years of time to mature so practically it makes sense we should at least expect around 3–4 years for Jetpack Compose to catch up &amp; become stable so as to be adopted by teams on a larger scale. Even today if you see, the adoption of Kotlin is ~70% for Android &amp; these are metrics captured from Google Play apps only, there is still a gap of ~30%. My point is don’t just become a fanboy, do your research, for every nice thing about the framework you should be able to list at least 2 bad things. If you really fail to do so (after extensive researching), congratulations the framework/library is ready for your use.</p><p>Now coming back to Jetpack Compose, if you are willing to adopt Jetpack Compose then the correct way to introduce this framework is to transition slowly and keep using Fragments instead of pure composable function. One of the things about Jetpack Compose is its interoperability with the existing View system through many <a href="https://developer.android.com/jetpack/compose/interop/interop-apis/">interoperability APIs</a> like ComposeView &amp; AndroidView to support libraries like Exoplayer/Media3, etc. within a composable function.</p><p>The reason I brought this topic to attention is that when you create an application it is more likely that it will be composed of more than one screen so as to move onto a different screen you will need some kind of navigation system. On Desktop, this was not challenging due to concepts like Primary &amp; Secondary Windows (dependent or independent) which OS manages for you. This changes when you develop apps for mobile devices or any device that is not a desktop, in order to save memory &amp; support smaller screens lot of optimizations are put in place for eg: you only see one screen at a time, the other might be sleeping (Lifecycle.onStop); multi-tasking is still in its early stage considering the current state of foldable devices &amp; are not much powerful as Desktop systems. So in order to move from one screen to another, a navigation system should do things like, cancel any ongoing job in the previous screen, release all the objects which are no longer in use so they can be garbage collected (in order to save memory) &amp; a smooth transition that catches user’s attention so that they know there is a context switch.</p><h3>What you should expect from a Navigation system?</h3><p>When you are developing Android apps that have multiple screens, the one thing you should focus more on is managing object allocation &amp; how much memory they are taking. Consider a ViewModel scoped to a Fragment, so when you move/navigate to a different Fragment whatever you are observing in the previous Fragment through ViewModel should be canceled to avoid unnecessary CPU, memory utilization &amp; memory leaks. Luckily components like Fragment, ComponentActivity are lifecycle aware (LifecycleOwner) &amp; have a SavedStateRegistry so any off-thread jobs you execute using LifecycleOwner.lifecycleScope will be automatically canceled when the owner moves to the destroyed state (navigating to another screen, application closed, etc.) &amp; all the states you put in SavedStateHandle in ViewModel would be safely stored in a Bundle (onSaveInstanceState) for situations like process death.</p><p>In Compose UI you don’t have such solid components that provide these optimization benefits out of the box, all you do is describe your UI in a function &amp; it just renders that on the screen. So effectively each destination in Compose is a composable function &amp; what you want to achieve is to make them aware of Lifecycle, ViewModelStore &amp; SavedStateRegistry by either managing yourself or through a navigation library that does it for you.</p><p>Now jumping to the actual topic the first thing you should look at in a navigation library is whether each screen manages Lifecycle &amp; SavedStateRegistry so that you can scope ViewModels to the screen &amp; ensure that when the screen is destroyed the ViewModels are also cleared from the memory. Compose has rememberSaveable which stores the value across configuration change &amp; process death. The way they work is by hooking to LocalSaveableStateRegistry which is a composition local for your nearest SaveableStateHolder. This interface is responsible for storing rememberSaveable‘s value to the Activity’s or Fragment&#39;s SavedStateRegistry with a unique string key, it does this by registering a lambda using SavedStateRegistry.registerSavedStateProvider that saves all the values of rememberSaveables’ in a bundle during onSaveInstanceState &amp; restores during the first call to the rememberSaveable. You must make sure that the navigation library of your choice is correctly implementing SaveableStateHolder, to verify this just make sure the following pseudo implementation exists somewhere in the internals of the navigation library of your choice,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc31946daedc721e8dab44ad8cd8f7f0/href">https://medium.com/media/cc31946daedc721e8dab44ad8cd8f7f0/href</a></iframe><p>In Compose, we describe UI in Kotlin which is a statically typed programming language, so it is natural that we should use this type system extensively even for navigation i.e passing &amp; resolving typed arguments when moving from one screen to another. The destination should also be strongly typed i.e represented by some entity object which then becomes easier for us to specify it during navigation (nice IDE auto-complete). However, this is not the case with some libraries (we will talk about it later).</p><p>Another thing you should expect from a navigation library is a good animation system. In Compose we have great animation APIs one of which is AnimatedContent. Even though it is experimental, it is quite powerful i.e you can chain multiple enter/exit transitions fadeIn() + scaleIn(), etc. Since screens in Compose is nothing but a @Composable function, when you are moving from one screen to other you are basically changing the entire content of the screen. AnimatedContent is one best use-case for such transition.</p><h3>Problems with navigation-compose</h3><p>Now that you know the context we are in, let’s talk about the official support of the Navigation Component for Jetpack Compose i.e “navigation-compose”.</p><p>In “navigation-compose”, each destination is a NavBackStackEntry which is a Lifecycle, ViewModelStore &amp; SavedStateRegistry owner. This helps to incorporate support for scoped ViewModels along with SavedStateHandleas SavedStateRegistry performs save &amp; restore operations whenever an appropriate lifecycle event occurs i.e when a destination goes from onStart -&gt; onStop (destination in backstack due to forward navigation, all states are saved including rememberSaveables’) -&gt; onDestroy (the destination is removed from the backstack due to backward navigation). You can confirm this by adding a LifecycleEventObserver to NavBackStackEntry.lifecycle. Though I would still like built-in support for navGraph scoped ViewModels (just like Navigation Component) without me manually finding parent NavBackStackEntry &amp; scoping ViewModel to it (an extension function would be great).</p><p>The navigation here is string (URI) based which is a major drawback. I get it why they took this approach to have built-in support for deeplinks just like Navigation Component for Fragments but they’ve overthrown type-safety while navigating &amp; as well as passing arguments to the destination. Deeplinks are important but an implicit handling is not needed, one can always write a helper class to manage deeplinks &amp; route them to the required destination with a few lines of code. This way you can separate &amp; customize the logic according to your need.</p><p>Here you pass arguments &amp; required destination by concatenating strings as destination/{arg1}&amp;{arg2}. So you first have to convert your typed object to a URI based representation ignoring escape characters like “&amp;”, <br>“%20” (space), “/”, “?”, “@”, etc. i.e bye-bye to passing parcelables, serializables as arguments. It clearly seems that they want us to avoid passing typed objects as parameters &amp; use ViewModel or any state object to retrieve data from the cache (database) i.e you pass an “id” as an argument &amp; retrieve the object from the database, however, sometimes this is not feasible &amp; there may be a case where you want to show an in-memory temporary cache of an object &amp; destroy it immediately when not needed. For eg: showing a payment success response in a dialog with some additional info so that when a user closes the dialog that information no longer exists.</p><p>Currently, “navigation-compose” has a default Cross-fade animation (with no ability to disable them) when navigating to other destinations. Full support for animations is supported through an incubation library project “Accompanist” (navigation-animation) &amp; its setup looks like the below,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a400419d56f243b73ff57e35b72e86e/href">https://medium.com/media/4a400419d56f243b73ff57e35b72e86e/href</a></iframe><p>You can declare enter, exit, popEnter, or popExit transition as arguments to composable functions. These animations are implemented using AnimatedContent which is why you get lambda of type AnimatedContentScope. The only problem I feel with this system is you lose flexibility when defining animations. As above, if you see for enterTransition we’ve declared fadeIn() + slideIn() when the starting destination is “Second” (i.e coming from “Second” to “First”) &amp; fadeIn for other cases. There is no flexibility apart from knowing the current (initialState) or target (targetState) destination &amp; declaring animations according to it. This hugely promotes an imperative style where you must pre-assume animations based on state (destination) values.</p><p>If you take an example from the Fragments world, we useFragmentTransaction.setCustomAnimations(..) to define animations for any followed fragment operations (like replace, add, remove). This chain gives us enough flexibility to set animations based on any logical conditions &amp; you can also refer to any local variable in those conditions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aac735ee09a4add864bdd58bdb029d04/href">https://medium.com/media/aac735ee09a4add864bdd58bdb029d04/href</a></iframe><p>You are defining animations during navigating to a destination (a more flexible &amp; declarative approach i.e not just only limited to doing conditional animations based on initial or target destination but a lot more), this is not possible with Accompanist’s navigation-animation. On the positive side, I think they went with this imperative approach because then you have all animations defined in one place. For a reader, this would become easy to take a look at which animation will trigger just by looking at the navigation setup.</p><p><a href="https://github.com/raamcosta/compose-destinations"><strong>Compose Destinations</strong></a><strong>:</strong></p><p>If you are using “navigation-compose” then chances are you already have heard about this library (if you haven’t then take a look <a href="https://composedestinations.rafaelcosta.xyz/">here</a>). In a nutshell, Compose Destinations is a code generating library based on KSP which attempts to solve the problems with “navigation-compose”. The resulting navigation setup using this library removes most of the boilerplate which would you have to then write manually. The library solves the string-based navigation with typed-based by generating destinations annotated with @Destination on a composable function. The typed parameter of the composable function becomes the argument for the navigation with an implicit DestinationsNavigator argument that helps to manage navigation for the given NavGraph.</p><p>The library is built on top of navigation-compose so it offers all features but is done better, passing typed arguments is way better (sure there are some base64 conversions happening but it is completely hidden). Nested navigation or defining your custom navigation is also <a href="https://composedestinations.rafaelcosta.xyz/defining-navgraphs">straightforward</a>, just make sure you don’t forget to annotate your nested navigation graph with the parent’s annotation (eg:@RootNavGraph) or be ready to get runtime crash when moving to any nested destination. This is the only problem I’ve faced when using this library, even though we have got typed navigation it is not typed to the current navigation graph (they all are of type Direction) so you can easily navigate to any child navigation’s destination without the navigation graph being nested to the parent navigation.</p><p>The other problem for me is that it is a code generation library, no matter what you do it will always affect build time (even a little bit) + when cloning a fresh project there will be always some types, properties, or methods which does not exist unless you run build for the first time. For any change in the navigation structure, you need to run build or kspDebugKotlin, this may slow down your developer productivity as well as code reviews will become harder to review on the web since destinations are scattered across the project so without a cumulative search for @Destination (using a Find tool) it will be hard to know which destinations contribute to which navigation graph.</p><p>You may feel like I might be over-exaggerating but again for me, I would generally avoid such code generation solutions in a big business project that has many developers contributing to it &amp; technically would move to other navigation libraries or might come up with our own navigation system if that is feasible. Nevertheless, the library offers a great solution on top of navigation-compose, so if you are someone who wants to use navigation-compose &amp; avoid its pain points, you should really check out this <a href="https://github.com/raamcosta/compose-destinations">project</a>.</p><p>My main motive behind this discussion (above) is to not convey any hate towards the “navigation-compose” library. It has its pros &amp; its cons but here cons basically outlive the pros so yeah it is bad. Libraries like Compose Destinations can help you avoid some of the pain points but it is still generating the same code which you otherwise have to write manually. My suggestion would be to explore some other options (given below), there are some great developers who have made their custom solutions for navigation in compose &amp; I think you should really check them out.</p><h3>Exploring some navigation libraries</h3><h4>1. <a href="https://github.com/olshevski/compose-navigation-reimagined">compose-navigation-reimagined</a></h4><p>If you are coming from “navigation-compose” &amp; want to switch over to a similar library, this would be most likely your first choice. The API is somewhat similar to that of “navigation-compose” (as the name suggests it’s “navigation-compose” but reimagined). The <a href="https://olshevski.github.io/compose-navigation-reimagined/">documentation</a> is pretty good in my opinion.</p><p>Here you’ve NavComponentEntry instead of NavBackStackEntry , you’ve built-in typed navigation with typed arguments (parcelable &amp; serializable are also supported). Here, NavController has a generic type &lt;T&gt; so it can inherit &amp; will only allow navigation of type T. Destinations can be of any type but for the best use case, we should use enums (if you don’t want to pass arguments for eg: in a bottom navigation setup) or a sealed class where each constructor parameters of the class become the arguments for the destination.</p><p>Just like “navigation-compose” you’ve NavHost&amp;AnimatedNavHost. Dialogs are also supported through a DialogNavHost &amp; its implementation/usage differs a lot that of “navigation-compose”. I like this idea where dialogs are treated as separate navigation &amp; not composed as a part of NavHosti.e one of its child destinations so you’ve separate NavController to manage them. The APIs provided by the NavController are much more flexible &amp; explicit, you can easily get a snapshot of the backstack using NavController.backstack. Basically, it has all features of the “navigation-compose” library (excluding built-in support for deeplinks).</p><p>Here is a simple example of how to set up this library,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b27482446af7ef1ad0a4f4a99701a58/href">https://medium.com/media/0b27482446af7ef1ad0a4f4a99701a58/href</a></iframe><p>The only problem I found with this library is that it does not have built-in support for ViewModels injected using Hilt, however, this is not a big problem because luckily you can copy-paste <a href="https://github.com/olshevski/compose-navigation-reimagined/discussions/4">this snippet</a> into your source code &amp; use the new hiltViewModel() function for creating ViewModels annotated with @HiltViewModel. For Dagger, you’ve to override your Activity’s or Fragment’s getDefaultViewModelProviderFactory() method to propagate your custom ViewModelFactory as currently NavComponentEntry does not provide a way to set custom ViewModelFactory.</p><h4>2. <a href="https://github.com/adrielcafe/voyager">voyager</a></h4><p>The first multiplatform navigation library for Jetpack Compose currently supports Android &amp; Desktop yet to have support for IOS for a true KMM.</p><p>The navigation system is stack-based somewhat similar to Fragments, there is no well defined typed navigation instead you define your destination by creating a class &amp; extend it either byScreen or AndroidScreen , so any parameters to this class become the arguments for that destination &amp; will be saved across configuration change &amp; process death due to Screen being serializable. AndroidScreen is a special implementation of Screen which is Lifecycle, ViewModelStore &amp; SavedStateRegisty aware, so it provides support for scoped ViewModels as well (for the multiplatform applications you would use ScreenModel instead of ViewModel).</p><p>There is a Navigator class for managing navigation hosted in aNavigator composable, I know its confusing with the names but a simple example with voyager looks like below,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1939115836fb70aab53fe540462ad206/href">https://medium.com/media/1939115836fb70aab53fe540462ad206/href</a></iframe><p>Navigator class has various operations defined by its Stack API, there is great <a href="https://voyager.adriel.cafe/stack-api">documentation</a> written by the author.</p><p>Implementing animations/transitions are simple, just wrap the content with either FadeTransition , ScaleTransition , SlideTransition or you can create your own using the raw ScreenTransition API.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f45ba513237014111b6fe93904675e95/href">https://medium.com/media/f45ba513237014111b6fe93904675e95/href</a></iframe><p>The library has support for deeplinks however its usage is quite different, basically, once you know the destination where you want to navigate (through intent.getExtras()) for eg: ThirdScreen, you’ll need to chain the previous screens (eg: FirstScreen,SecondScreen) to the Navigator(..)composable as well (one overload accepts List&lt;Screen&gt;), this way the last added item in the list will be the first one to be displayed &amp; pop will actually go back to previous screens. One problem with this approach is that suppose you’ve to navigate to a deeply nested navigation with arguments, chances are you’ve to pass them through every Navigator(..) composable &amp; also have to main separate logic for them.</p><p>Apart from this, the library has built-in support for bottom sheet, tab &amp; bottom-bar navigation which is very easy to set up &amp; very well illustrated in the <a href="https://voyager.adriel.cafe/navigation">docs</a>.</p><p>One of the things I like about this library is that the documentation is really good covering most of its API usage neatly with proper linking to code samples. The library is ready to use in production &amp; I think at this point some people already have. The <a href="https://github.com/adrielcafe/voyager/issues">issues</a> listed on Github are more likely feature requests &amp; bugs related to some very specific use cases. If you check the closed issues you’ll understand how much the library has matured before coming to its stable 1.0 release.</p><h4><a href="https://github.com/KaustubhPatange/navigator/tree/master/navigator-compose">3. navigator-compose</a></h4><p>Another library for handling navigation is navigator-compose. The library also supports Fragment navigation through its parent library called navigator.</p><p>The navigation setup is similar to that of “compose-navigation-reimaged”. Just like that, it supports type-safe navigation through only sealed classes whose constructor parameters become the arguments for that destination. Here you’ve got a Controller&lt;T&gt; class which you would use to manage navigation of type T. It also provides a wide range of flexible APIs for managing the navigation backstack.</p><p>In this, each destination that you would create by extending Route interface has a LifecycleController . This controller is Lifecycle, ViewModelStore &amp; SavedStateRegisty owner which means the library has support for scoped ViewModels &amp; also supports ViewModel injected through Hilt using a separate add-on library. There is great <a href="https://github.com/KaustubhPatange/navigator/wiki/Compose-Navigator-Tutorials#lifecycle-events-in-navigation-routes">documentation here</a> that shows how Lifecycle Event change occurs when navigation backstack changes.</p><p>Apart from Controller&lt;T&gt; you’ve got a gigantic class called ComposeNavigator which has a global view of the navigation backstack which is why the navigation here is queue-based (in a map) i.e it knows about its parent &amp; child navigation. ComposeNavigator class provides similar APIs to that of Controller&lt;T&gt; but any change could also affect the parent navigation, for eg: there is an API in ComposeNavigator class called <a href="https://kaustubhpatange.github.io/navigator/reference/navigator-compose/navigator-compose/com.kpstv.navigation.compose/-compose-navigator/go-back-until.html">goBackUntil</a> which as the name suggests pops to the required destination (including the one from multiple hierarchies of parent navigations as well) &amp; it does this by looking through the whole navigation queue.</p><p>Here is a simple example of how to set up this library,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e96b82368d0e49811c6fa9ccc0fc682b/href">https://medium.com/media/e96b82368d0e49811c6fa9ccc0fc682b/href</a></iframe><p>Animations in this library are also pretty straightforward but the only thing is that they are not implemented using AnimatedContent API but are by manually interpolating between current -&gt; target destination &amp; modifying the graphics layer. This actually provides much more flexibility but could take away simplicity when defining your custom animations (although there is a <a href="https://github.com/KaustubhPatange/navigator/pull/12">PR</a> to upgrade the animation system to AnimatedContent so if you are a user of the library you can convince the author to upgrade it). Currently, the library provides built-in Fade &amp; Slide transitions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5324df31860b360142cdc5386c4bd6a9/href">https://medium.com/media/5324df31860b360142cdc5386c4bd6a9/href</a></iframe><p>As you can see the animations here are much more flexible i.e logic can be much more dynamic. Similar DSL is provided for popUpTo i.e popping to the required destination.</p><p>The library is still in alpha which means the API change could break the backward compatibility.</p><h4>4. <a href="https://github.com/Zhuinden/simple-stack-compose-integration">simple-stack-compose-integration</a></h4><p>The library is an add-on to the <a href="https://github.com/Zhuinden/simple-stack">simple-stack</a> library. simple-stack is a backstack library &amp; has been there for quite a long time, so you can say it is one of the most matured navigation frameworks among all of them. A lot of concepts are carried over from simple-stack library to its compose-integration. The library is an abstracted navigation framework, which means it does not directly depend on the implementation detail on what component is used for navigation but rather how navigation should proceed (state change), which is why the library has separate implementations for navigation of plain Views, Fragments &amp; Composable functions. This is done through theStateChanger interface (ViewStateChanger for View, FragmentStateChangerfor Fragment &amp; ComposeStateChanger for Compose).</p><p>Each destination here is a class extending from either DefaultViewKey (for View-based navigation), DefaultFragmentKey (for Fragment-based navigation) or DefaultComposeKey (for Composable-based navigation). For Compose, you can override a method called ScreenComposable where you would normally put your composable content. Similar to other navigation libraries we saw, the constructor parameters of this class become the arguments for that destination.</p><p>There is no ViewModel here but a concept called Scoped Services. The idea is you bind services (which is usually a class) to a destination with a tag scoped to that destination (very similar to scoped ViewModels) &amp; use it using a lookup(...) function in your component (could be a Fragment or a Composable function). The services aka the bounded classes will live as long as the destination is in the backstack. So suppose you set up bottom-navigation in a destination A, you can then lookup(..) for a service scoped to A in any child destination of tab &amp; suddenly you’ve automatically shared the given service with the children of bottom navigation. In the Navigation Component world, this is what you refer to as navigation graph scoped ViewModels (created using navGraphViewModel&lt;T&gt;()), the only difference is the implementation is platform agnostic. Apart from this, the scoped services can also implement interfaces like Bundleable to have support for save/retrieve state to/from a StateBundle (similar to SavedStateHandle).</p><p>A simple setup using this library would look like this,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35c5da471912cb75c12e7e0cb4041444/href">https://medium.com/media/35c5da471912cb75c12e7e0cb4041444/href</a></iframe><p>Animation between navigation is done by modifying the graphics layer through interpolating between the current &amp; target destination (similar to 3. navigator-compose). Unfortunately, the library does not provide any built-in transitions but the default transition here is Slide. You can customize the animation through an optional parameter of ComposeStateChanger constructor, where you can specify your custom implementation of AnimationConfiguration. You can customize the animation for different destinations using stateChange parameter (topPreviousKey() &amp; topNewKey()).</p><p>Apart from the animation system, the library is excellent. Yes, it has some learning curve but sure it&#39;s worth investing in. Like I said the library can become platform-agnostic &amp; if so there is a high chance we can see its usage in a multiplatform project.</p><h3>Conclusion</h3><p>I know there are many navigation libraries &amp; I’ve might only covered a few. The reason I picked these libraries is that I’ve seen them grow &amp; also used them for doing POC to decide which one to use for my next personal projects, so I can say I’m familiar with most of the concepts each of them has to offer. Also, in the discussion above I’ve only covered a few of the top features that I like/dislike about the libraries, it is important that you check them out yourself &amp; maybe get familiar with the internal workings of them.</p><p>Another major aspect is that each of these libraries is tested through unit testing, instrumentation testing, or both (on CI as well). This is very important as it tells how serious the maintainer/author is about the project.</p><p>If you ask for my opinion on which navigation library should you use for your next project that will be in pure Compose &amp; no fragments then my suggestion would be to go with <a href="https://github.com/adrielcafe/voyager">voyager</a> (2nd). The reason being it is the only library that is used by some of the people I know in the community for a serious production app that has a very high user base (around ~5M+ downloads). If they’ve chosen this library then they must surely have done their research. Also, in my opinion, this is the only library I feel is close to stable covering a variety of use cases (soon 1.0 stable release).</p><p>Hope you like this discussion, if you’ve any doubts or concerns let me know through comments or <a href="https://twitter.com/KP206">Twitter</a> :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc13eb6ac3d9" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/the-state-of-navigation-in-jetpack-compose-cc13eb6ac3d9">The State of Navigation in Jetpack Compose</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scalable Navigation in multi-module projects]]></title>
            <link>https://proandroiddev.com/scalable-navigation-in-multi-module-projects-c3c44dc1b913?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3c44dc1b913</guid>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[multi-module-project]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Sat, 20 Nov 2021 12:34:51 GMT</pubDate>
            <atom:updated>2022-02-23T05:49:24.192Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gek8uMpOGH7sQ-7PDWIWFg.png" /></figure><p>This is an opinionated article on how I think navigation should be handled in multi-module single-activity projects. Also, this is not “yet another tutorial on Navigation Component”. In fact, I’m not going to use Navigation Component at all but will demonstrate the approach by using our good old FragmentManager .</p><p>The main problem with the Navigation Component IMO is that its logic cannot be abstracted away easily, there are graphs, ids for the destination, NavActions with some vile way on how to pass some arguments. Your navigation is represented in XML rather than in the code you write (Java/Kotlin). There are DSLs that allow you to write NavGraph in code but who uses that :p. Luckily by using the “safe-args” gradle plugin we can solve the long-awaited problem of not being able to pass <em>parcelables</em> as arguments and talking about “safe args”, there is a lot of magic involved when using the “safe-args” gradle plugin &amp; the code it generates for deeplinks (mainly by manifest merging) &amp; &lt;argument/&gt; which sometimes becomes hard to reason about.</p><p>Despite being these issues (&amp; a lot more), I think for any beginner in Android development he/she should go with the Navigation Component as their first choice for navigation library since it offers a lot more simplicity &amp; hides most of the implementation details of FragmentManager methods (with a simple navigateTo call) visually (using Navigation Graph Editor in Studio) &amp; programmatically. Later on, they can then decide if they should migrate to another navigation library to solve some problems that can’t be solved through Navigation Component or maybe want to reduce coupling between the navigation logic. Likewise, the problem of multi-module navigation is already solved by Navigation Component whether by using <a href="https://developer.android.com/guide/navigation/navigation-multi-module#across">deeplinks</a> (I wouldn&#39;t recommend it since it’s not typesafe &amp; could cause issues). There is a great video on the “Android Developers” YouTube channel which <a href="https://www.youtube.com/watch?v=IvFSJVTGDb4">covers this topic</a>. You can see how simple the approach is when using the Navigation Component to solve multi-module navigation but then why shouldn’t we use it?</p><h3>Problems with abstraction</h3><p>In Android, FragmentManager is the first-class citizens API to handle navigation with fragments &amp; is much flexible to use. You have more control over your navigation, backstack, animation, etc.</p><p>There are a lot of third-party navigation libraries for Android (<a href="https://github.com/Zhuinden/simple-stack">simple-stack</a>, <a href="https://github.com/ncapdevi/FragNav">FragNav</a>, <a href="https://github.com/KaustubhPatange/navigator">navigator</a>, <a href="https://github.com/aartikov/Alligator">Alligator</a>, name a few) which offers similar or maybe more functionality but the one thing they have in common is “go-to screen”, “go back” &amp; such functionalities similar to FragmentManager due to which their logic can be abstracted away so in the future if you decide to change your navigation library it shouldn’t be that burdensome.</p><p>Also, when migrating your app to pure Jetpack Compose (no fragments at all) such library or pure FragmentManager approach is far better than the “Navigation Component” one to ensure a somewhat smooth migration.</p><h3>The actual solution</h3><p>So how are we going to solve the problem of multi-module navigation if the idea is to have an abstracted navigation logic &amp; to also have slower build-times (that’s why we do modularization right :p)? There is an article by <a href="https://medium.com/u/7a8d96da8cb6">Gabor Varadi</a> “<a href="https://proandroiddev.com/structural-and-navigation-anti-patterns-in-modularized-android-applications-a7d667e35cd6">Structural and navigation anti-patterns in multi-module and modularized applications: The case against “Android Clean Architecture” and the “domain”</a> wherein one of the sections (anti-pattern #3) he outlined the theoretical approach on how to do it. Fortunately a sample by <a href="https://medium.com/u/dd7d3efa41de">Nick Rose</a> in the article “<a href="https://medium.com/@nicholas.rose/multi-module-navigation-with-dagger2-f747db186343">Multi-module navigation with Dagger2</a>” shows how to do it. I tried to implement this solution by having one nested navigation in one of the child modules &amp; had pretty good success with it. If you want to see a similar approach check the dagger branch on my <a href="https://github.com/KaustubhPatange/multi-module-navigation-sample">sample repository</a> which does the same thing including a solution for carrying out nested navigation in one of the child modules. For any future references, I’m going to talk or take some excerpts from this repository itself.</p><p>As you noticed we are going to use a DI framework for solving multi-module navigation. In the <a href="https://github.com/KaustubhPatange/multi-module-navigation-sample">sample</a>, I tried two approaches one with Dagger 2 &amp; the other with Hilt (goes by many names like “The opinionated Dagger framework”). DI frameworks help us to provide an implementation for an interface when requested which is crucial for navigating between modules.</p><h4>But why Dagger or similar frameworks?</h4><p>As mentioned in <a href="https://medium.com/u/7a8d96da8cb6">Gabor Varadi</a>’s <a href="https://medium.com/@nicholas.rose/multi-module-navigation-with-dagger2-f747db186343">article</a> the approach with DI is similar to what we would do if we were to use FQNs instead where there is no guarantee whether classes exist at runtime (similar to how multi-module navigation is handled using deeplinks using Navigation Component). With DI especially the ones which provide compile-time safety, we can ensure if our code breaks our build will also break (plus refactoring becomes simple) which is why I think Dagger or Hilt is the best-suited tool for our use case (sorry <a href="https://insert-koin.io/">Koin</a>). But this does not mean you cannot use other DI frameworks, you definitely can but you lose type-safety &amp; some build overhead (through <em>kapt</em> if using Kotlin, although I heard <a href="https://github.com/google/ksp#supported-libraries">Dagger is getting a KSP version, no ETA yet</a>).</p><p>Anyway, I like a system that will nag me if I do something wrong during compile time rather than crashing on random devices during production just because one binding in module {}doesn’t resolve at runtime. Having said this,</p><h3>Let’s see the approach</h3><p>As I said we are going to use Dagger! But Dagger is complex not because of the difficulty but its verbosity. Since setting it up requires a lot of boilerplate code it may not be ideal especially when injecting in Android specific components like ViewModel, WorkManager where you requires solutions like @AssistedInject . Luckily injecting with Hilt into these components <a href="https://developer.android.com/training/dependency-injection/hilt-jetpack">is fairly simple</a>, so what we want is the power of Hilt with a little bit of Dagger magic (in terms of Hilt).</p><p>Let’s look at the app’s module architecture,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CdSZSWWw-itDCZWLyJt2iA.png" /></figure><p>The architecture follows a single-activity pattern where each child module contains one or more Fragments &amp; we are navigating to them using Dagger/Hilt. For eg: Navigation from “WelcomeFragment” to “HomeFragment” i.e from module :welcome to :home is not happening directly since :welcome module doesn’t know anything about the :home module.</p><h4>Navigating between child modules (top-level)</h4><p>As :welcome &amp; :home are the child modules of :app , it knows every public class &amp; method exposed by them. So what we do here is,</p><ul><li>Expose an interface in:welcome module which contains a method called goToNext().</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/962858265d2c5863bfe0aba289110e3d/href">https://medium.com/media/962858265d2c5863bfe0aba289110e3d/href</a></iframe><ul><li>Implement this interface in :app module where we can then provide an actual logic on how to navigate to the “HomeFragment”.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa351107abed13069d80c58cb368493a/href">https://medium.com/media/fa351107abed13069d80c58cb368493a/href</a></iframe><ul><li>Bind the interface with our implementation in the :app module with the help of a DI framework. Note, we need to scope the module to the ActivityComponent . In hilt, it is straightforward by using @InstalIn annotation but in Dagger, we need to create our custom Activity subcomponent (all shown in the “<a href="https://github.com/KaustubhPatange/multi-module-navigation-sample/tree/dagger">dagger” branch of the sample</a>).</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4439cfbc5998081665a75eff5d1db045/href">https://medium.com/media/4439cfbc5998081665a75eff5d1db045/href</a></iframe><ul><li>Now for the navigation part, you need to <strong>@Inject</strong> “WelcomeButtonClick” at the site (in :welcome module) where you want to navigate to the “HomeFragment”.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6a29ce566486c242c0d791c208f8457/href">https://medium.com/media/d6a29ce566486c242c0d791c208f8457/href</a></iframe><p>Here, Dagger (Hilt) provides an instance of “WelcomeButtonClickImpl” whenever we request “WelcomeButtonClick”. We have then used it in the button’s OnClick listener. This will properly navigate the current destination to “HomeFragment” whenever that button is clicked. “HomeFragment” here is nothing but aFragmentContainerViewwhose default destination is “HomeStartFragment”.</p><p>Some of you might ask me, how did we get an instance of Activity in “WelcomeButtonClickedImpl”? Well this is Dagger helping us, all ActivityComponent have a reference to its Activity (@BindInstance) and is available through the component/subcomponent hierarchy so we can reference it anytime we want which is what we did in the “WelcomeButtonClickedImpl”.</p><p>This solution works for top-level navigation, the real problem arises when you have nested navigation in one of the child modules.</p><h4>Navigating between child modules (nested navigation)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tuuJQLrjEunLgs4BDhFh3Q.png" /></figure><p>If you look at the diagram again, we have a nested-navigation in :home module which depends on two other modules :home-internal &amp; :home-internal2 containing Fragments to navigate. What happens here, :home module now acts as:app module managing navigation for its child modules.</p><p>In :home module “HomeStartFragment” can directly navigate to “HomeInternalFragment” which is defined in :home-internal as it is one of the child modules so it definitely knows about it.</p><p>The problem is navigating from :home-internal to :home-internal2! Surely we can reuse the same solution mentioned above which we have implemented for navigating between:welcome &amp;:home right? Let’s try &amp; see what happens,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dca8f5a841498ff360a81d948bcdccbd/href">https://medium.com/media/dca8f5a841498ff360a81d948bcdccbd/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a9517993e7e8a6bc13542e85d8e7007/href">https://medium.com/media/9a9517993e7e8a6bc13542e85d8e7007/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cadef36a1dc71bfd3156ec2b4244e2e7/href">https://medium.com/media/cadef36a1dc71bfd3156ec2b4244e2e7/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ace264aa4a9a1eeb14faca8ef13c90db/href">https://medium.com/media/ace264aa4a9a1eeb14faca8ef13c90db/href</a></iframe><p>We’ve done exactly like previous, exposing interface from child module, implementing it in the parent module &amp; using it in the child module where navigation is required. This should work, right?</p><h4>Crash Crash Crash…</h4><p>Well it does not, in fact, your app will <strong>crash </strong>especially in “HomeButtonClickedImpl” at line 7 (above) saying that this fragment is not attached to any container. What does this mean? Well what happened in “HomeButtonClickedImpl” is we have injected “HomeFragment” thinking that it will provide us the same instance of “HomeFragment” which @AndroidEntryPoint i.e hilt has created for us but rather it is creating &amp; providing a new instance of the “HomeFragment” which is not present in our navigation backstack hence the crash. BTW you can test this crash from the sample in the <a href="https://github.com/KaustubhPatange/multi-module-navigation-sample/tree/hilt-crash"><em>hilt-crash</em></a> branch.</p><p>This crash did not happen when Dagger was used because there we’ve created a multi-map binding to create and instantiate (while constructor inject) Fragments using our own custom FragmentFactory called DaggerFragmentFactory . The problem when using hilt (along with its gradle plugin) is injection is completely handled by itself, we’ve no control over the individual component or subcomponent in sense. Somehow we’ve to provide the same instance of “HomeFragment” which is currently on backstack. But can we do that?</p><h4>Solution with hilt Qualifiers</h4><p>If you want to check the solution that fixed this issue make sure to check out the <a href="https://github.com/KaustubhPatange/multi-module-navigation-sample/tree/hilt"><em>hilt</em></a><em> </em>branch from the sample or diff the branches.</p><pre>git difftool hilt-crash hilt</pre><p><em>Edit: The logic is updated to fix crashes due to configuration change &amp; process death as pointed by </em><a href="https://medium.com/u/7a8d96da8cb6"><em>Gabor Varadi</em></a><em> </em><a href="https://www.reddit.com/r/androiddev/comments/qy4q37/comment/hlhq2d5/?utm_source=share&amp;utm_medium=web2x&amp;context=3"><em>here</em></a><em>.</em></p><p>What we want to do is to provide the same instance of “HomeFragment” every time any navigation logic requests i.e in one of the child modules of :home. For that, we define a qualifier to restrict the injection of HomeFragment to navigation purposes only.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aad5557403351ffedaf164adc2c0d3f6/href">https://medium.com/media/aad5557403351ffedaf164adc2c0d3f6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9706bb4c80060c770b068dc2e22bbe99/href">https://medium.com/media/9706bb4c80060c770b068dc2e22bbe99/href</a></iframe><p>And by doing that all we’ve to change in “HomeInternalButtonClickedImpl” is to annotate homeFragment parameter with @HomeQualiferwhich will provide us the required instance of “HomeFragment” from the backstack instead of creating a new one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23adccadd912305d50260beca9c16a63/href">https://medium.com/media/23adccadd912305d50260beca9c16a63/href</a></iframe><p>This way we’ve fixed our crash (you can check it out in the <a href="https://github.com/KaustubhPatange/multi-module-navigation-sample/tree/hilt"><em>hilt</em></a> branch of the sample) &amp; everything will work perfectly.</p><p>So the real logic is defined in HomeDependencyModule where we’ve installed a provider scoped to FragmentComponent that looks up the HomeFragment in the Activity’s backstack &amp; returns it. It will work across configuration change &amp; process death but will break if Activity has multiple instances of HomeFragment . To solve this problem we can use tags to identify the required fragment from the backstack (findFragmentByTag). Also, we’ve restricted the injection of HomeFragment by adding @HomeQualifier , this will ensure that we cannot just inject HomeFragment anywhere we want &amp; should only be used for navigation purposes only.</p><h3>Conclusion</h3><p>The solution may not be correct in fact many of you might consider it as a hack so if anyone knows a better solution other than this please do share with us.</p><p>One other solution that comes to my mind is in “HomeInternalButtonClickedImpl” instead of requesting “HomeFragment” we should request just Fragment which if I’m not wrong should provide the current instance of the fragment (since the injection is scoped to FragmentComponent) &amp; then using parentFragmentManager property we can commit a new fragment transaction. But this solution assumes that there must &amp; should be a parent fragment which I think will always be true. I don’t know if this approach is correct or even better than what I suggested above. If you have any suggestions let me know!</p><p>Also, the sample could be improved a lot more especially in structuring the code &amp; logic but my main idea was to demonstrate this use case with DI.</p><p>I’ve not talked anything about Jetpack Compose yet but I know for sure that this problem can be easily solved. In fact, if you had looked into the <a href="https://github.com/chrisbanes/tivi">tivi</a> sample, each screen is modularized into a new gradle module &amp; is wired up in the app module where each destination is added to the navigation graph (using “Navigation Component for Compose”). So if any screen wants to navigate to another screen (which it does not know about) instead of injecting an interface we just have to pass a lambda to do so, since destinations in Compose are nothing but a function we can always add an argument to the function &amp; invoke it when required. So far this seems a good way to approach modularization &amp; handle navigation in a pure Jetpack Compose app without using any DI. But who knows? It’s still too early to make any assumptions. For now, stick to Fragment + Compose approach which is the safest bet I can see.</p><p>Finally, I’m closing by linking the sample that was used to demonstrate everything here. Also, thanks to <a href="https://medium.com/u/7a8d96da8cb6">Gabor Varadi</a> for answering my questions over Twitter DM.</p><p><a href="https://github.com/KaustubhPatange/multi-module-navigation-sample">GitHub - KaustubhPatange/multi-module-navigation-sample: A sample that demonstrates navigation in child modules with the help of DI frameworks like Dagger &amp; Hilt.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3c44dc1b913" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/scalable-navigation-in-multi-module-projects-c3c44dc1b913">Scalable Navigation in multi-module projects</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Navigating with Animations in Jetpack Compose]]></title>
            <link>https://kaustubhpatange.medium.com/navigating-with-animations-in-jetpack-compose-5655820dedb8?source=rss-124f1e86b15------2</link>
            <guid isPermaLink="false">https://medium.com/p/5655820dedb8</guid>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[backstack]]></category>
            <dc:creator><![CDATA[Kaustubh Patange]]></dc:creator>
            <pubDate>Fri, 25 Jun 2021 06:49:08 GMT</pubDate>
            <atom:updated>2021-09-15T16:06:21.873Z</atom:updated>
            <content:encoded><![CDATA[<h4>Simplified Navigation in Jetpack Compose</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AOnl3E00grDfs_AyL7FUsg.png" /><figcaption>Yup, straightaway stealing Zhuinden’s style</figcaption></figure><p>I bet most of the readers came here thinking about how can we achieve this using the official <a href="https://developer.android.com/jetpack/compose/navigation">Navigation Component</a>, but believe me the issue is currently being tracked on the issue tracker for a <a href="https://issuetracker.google.com/172112072">very long time</a> (Edit: now possible using <a href="https://google.github.io/accompanist/navigation-animation/">Accompanist</a>).</p><p>This article is not another Navigation Component tutorial (though the title might seem clickbait), here I will show you (or make you aware of) my new navigation library which is an implementation of <a href="https://github.com/KaustubhPatange/navigator">navigator</a> for Jetpack Compose to handle navigation along with many features (including support for animations).</p><h3>Why we need another navigation library?</h3><p>I’m not going to list down the pros-cons of the official navigation library instead I’ll point out something that I don’t like. Remember, these are just my opinions… in the end, you are free to choose whatever suits your needs.</p><p>First of all, I want to say that the way of writing &amp; representing UI/Screen using Jetpack Compose is completely different than the traditional way so any knowledge of handling navigation may not be carried over to this new system. Having said that, the way the official Navigation library handles passing of arguments to the other composable screens is not ideal (a lot of unwanted things has to be done just to pass a String), this is because of how it represents a destination (i.e using a String which is also the reason that it has support for handling deeplinks). Naturally, we can create a class to store all their names but when it comes to passing an argument to that destination it would then become something like controller.navigate(“${destinations.profile}/$argument”) which could easily become a source of bug if not defined carefully. Animations are a <a href="https://issuetracker.google.com/172112072">WIP</a> so there is nothing to talk about (Edit: they are now possible using <a href="https://google.github.io/accompanist/navigation-animation/">Accompanist</a>).</p><p>Navigation was never a problem for Jetpack compose, by using some combination of remember ,mutableStateOf anyone can pretty much define a basic navigation system. What is challenging is how should we manage the backstack? That’s why there are libraries like <a href="https://github.com/zsoltk/compose-router">compose-router</a>, <a href="https://github.com/Zhuinden/simple-stack-compose-integration">simple-stack</a> which does that for you, so why another one? The answer is to facilitate navigation so that developers can focus on other important things, that&#39;s the one thing I like about the official navigation library is how simple is it to configure &amp; use.</p><h3>Introducing Navigator Compose</h3><p>This library is my take on how navigation should be done in Jetpack Compose. So in this article, I’ll give you a simple introduction about my navigation library, what problem does it solve? And most importantly how to use it for complex/deeply nested screens as well. If you are interested &amp; want to read more, check the <a href="https://github.com/KaustubhPatange/navigator/tree/master/navigator-compose">Github</a> project.</p><p>All the code snippets which are shown below are taken from the <a href="https://github.com/KaustubhPatange/navigator/blob/master/samples/basic-sample">sample app</a> &amp; <a href="https://github.com/KaustubhPatange/navigator/tree/master/navigator-compose#tutorials">documentation</a> (wiki) available on Github.</p><p>The first thing we need is to add the required navigator-compose dependency.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4238e34c6096191c81986ca1de3be01c/href">https://medium.com/media/4238e34c6096191c81986ca1de3be01c/href</a></iframe><p>Now we need to initialize the ComposeNavigator which is the root class for managing all the navigation related to @Composable screens inside an Activity (no Fragments are required at all).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bed8bd61894aa755200e72c4a669553/href">https://medium.com/media/7bed8bd61894aa755200e72c4a669553/href</a></iframe><p>Once initialized, the ComposeNavigator will attach itself to the lifecycle of the activity which will ensure that the navigation states will be properly restored across configuration &amp; process death.</p><p>This setup will also handle the backpress logic on the activity to go up the navigation stack (i.e backward navigation). ComposeNavigator provide APIs for handling backstack like goBack() ,canGoBack() so if you want some custom behavior for handling backpress on the Activity <a href="https://github.com/KaustubhPatange/navigator/issues/8">you can use them</a>.</p><h4>Define a Route</h4><p>Destinations can be represented through inheritance using a sealed class.</p><p>The constructor parameters become the arguments for that destination screen. Note, only data types that can be stored in a bundle are supported.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2196a9eb79b3e2f9e8100b62fb4328b8/href">https://medium.com/media/2196a9eb79b3e2f9e8100b62fb4328b8/href</a></iframe><p>The MainRoute must implement com.kpstv.navigation.compose.Route interface which internally implements Parcelable to ensure that the state of navigation will persist across configuration change.</p><p>We also have declared a key variable in the companion object of the MainRoute (used later during the <em>Setup</em>). This key is used to uniquely identify the Route in the composition tree. This is how findController&lt;T&gt;(key) was able to find &amp; retrieve the Controller associated with that specified key.</p><p>From the above snippet, you can notice that we have declared a private noArg parameter for theSecond screen. That’s because we should not use object or simple class to represent an empty argument destination because SaveStateProvider does not restore rememberSaveable’s value after process death (there is a lint-check that will fail the build if such usages are found). Read more about this issue <a href="https://issuetracker.google.com/191059138">here</a>.</p><h4>Setup Navigation in MainScreen</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cee7405fe57216753bbbda8fe8b505f/href">https://medium.com/media/0cee7405fe57216753bbbda8fe8b505f/href</a></iframe><p>The key argument for Setup becomes the key for that navigation which is also used to identify the backstack associated with it.</p><p>Navigation to other destinations are managed within the respective @Composable screens. To go back programmatically you can call controller.goBack(). It also provides an API controller.getAllHistory() to get the current snapshot of the history associated with the current key.</p><p>Each Setup provides a unique instance of Controller&lt;T&gt; for managing that specific destination type &amp; can be accessed by using findController&lt;T&gt;() within the child composables.</p><p>A when statement is used to switch between different @Composable screens based on the target destination dest.</p><h4>Customizing Navigation with NavOptions</h4><p>Navigator allows you to customize NavOptions while navigating to other screens. This includes setting enter &amp; exit animations, singleTop or a custom popUpTo logic. More information is available in the <a href="https://github.com/KaustubhPatange/navigator/wiki/Compose-Navigator-Tutorials">documentation</a>.</p><p>Below is a small snippet on “navigating to another screen with animations”. There are also some other animations like Slide, Shrink which the library provides out of the box.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd1f89bfd95b683cd5ebd3f9f5da8897/href">https://medium.com/media/bd1f89bfd95b683cd5ebd3f9f5da8897/href</a></iframe><h4>Nested Navigation</h4><p>When you call navigator.Setup, it binds the current ComposeNavigator to the CompositionLocal which can be retrieved using findComposeNavigator(). It also binds the Controller&lt;T&gt; associated with the destination T for all the child composables which can be retrieved using findController&lt;T&gt;().</p><p>All you have to do is implement navigator.Setup for another screen where you want nested-navigation.</p><p>Check out the <a href="https://github.com/KaustubhPatange/navigator/blob/master/samples/basic-sample">Basic Sample</a> for a more clear example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/180ce45bf7a1967e2dd9c23c4a243bf4/href">https://medium.com/media/180ce45bf7a1967e2dd9c23c4a243bf4/href</a></iframe><h4>Support for Dialogs</h4><p>When you set up navigation with navigator.Setup, the controller that is used to manage the navigation for that Route is used to create, show &amp; close dialogs.</p><p>Each dialog must extend from com.kpstv.navigation.compose.DialogRoute, they can be a data class where the constructor parameters becomes the argument for the dialog destination or object for no argument dialog destination.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7aa0fbd5adb1b9d3504be369b268382/href">https://medium.com/media/e7aa0fbd5adb1b9d3504be369b268382/href</a></iframe><h3>What’s next?</h3><p>As you can see from the examples, there is no special magic happening inside the library. Everything is straightforward which it should be!</p><p>Animations are there, arguments are properly being passed through destination constructor parameters. What it can’t do yet is handle deeplinks which the official navigation library supports.</p><p>Some people may ask, how can we set up Bottom Navigation with this library? This is fairly simple, like said all you have to do is implement navigator.Setup for that screen to act like a nested-navigation. If you take the implementation from the <a href="https://developer.android.com/jetpack/compose/navigation#bottom-nav">offical navigation library</a>, you just have to replace NavHost with navigator.Setup &amp; that’s it. The <a href="https://github.com/KaustubhPatange/navigator/blob/master/samples/basic-sample">sample</a> app showcases some different setup &amp; not the material’s BottomNavigation.</p><p>The library is in alpha (not that some features don’t work but the name of some APIs may be changed in the future versions) so you can try it out or check out the <a href="https://github.com/KaustubhPatange/navigator/tree/master/navigator-compose">official sample</a> app. Also if you have any issues or feature requests just let me know.</p><p><a href="https://github.com/KaustubhPatange/navigator/tree/master/navigator-compose">KaustubhPatange/navigator</a></p><h4>Bonus: My thoughts on accompanist’s navigation-animation</h4><p>So it seems like transitions between composable are now possible using the <a href="https://github.com/google/accompanist">accompanist</a>’s navigation-animation artifact. It’s been over a month since there are no changes in the library in terms of API design. So, here are some of my opinions &amp; things I dislike.</p><p>No issue the library is great &amp; works out of the box by just replacing standard NavHost with AnimatedNavHost which then allows you to specify enter, exit &amp; popEnter, popExit transitions on composable (extension functions on NavHost that represents destinations). This was a deal-breaker for me, what if I want to change the transition depending on the initial &amp; target destination? Since these enter, exit, etc. transitions provide initial, target destination as parameters one can easily wrap the logic in if,else or when but is it worth it? Look at all the code smell with those conditional switches (something like below).</p><pre>composable(&quot;Route1&quot;, <br>  enterTransition = { initial, target -&gt;<br>     when(intial) {<br>       &quot;Route2&quot; -&gt; ...<br>       &quot;Route3&quot; -&gt; ...<br>       &quot;Route4&quot; -&gt; <br>        ...<br>     }<br>  },<br>  exitTransition = { initial, target -&gt;<br>     when(target) {<br>       &quot;RouteN&quot; -&gt; ...<br>        ...<br>     }<br>  }<br>) {<br>    // your composable content<br>}</pre><p>Suppose I need to add animation for a new destination for “Route1” I would then have to come back to this composable &amp; edit these transitions. This will eventually get out of hand if not properly managed. With my navigator-compose it just becomes an optional argument of NavOptions (see <a href="https://github.com/KaustubhPatange/navigator/wiki/Compose-Navigator-Tutorials#navigating-with-animation">here</a>) &amp; this is for me the idiomatic way as the logic for animation becomes the part of navigation so we can customize it to the full extent.</p><pre>// with navigator-compose</pre><pre>controller.navigateTo(&quot;Route1&quot;) {<br>    withAnimation {<br>        target = SlideRight<br>        current = Fade<br>    }<br>}</pre><p>Another thing is with the popEnter&amp; popExit, I just don’t see its need in Compose world. Yes, for the fragments it was needed because the backstack was entirely managed by FragmentManager internally (we cannot manually edit it) plus even though Fragments are nothing but a view in a container it has its own lifecycle &amp; is itself a lifecycleOwner so changing states internally might trigger some different behavior especially for popping (more specifically the order in which they are popped).</p><p>In Compose, everything is a function &amp; are render based on state changes due to which navigation is predictable. So, with navigator-compose it is basically just a navigateTo call with optional popUpTo DSL (see <a href="https://github.com/KaustubhPatange/navigator/wiki/Compose-Navigator-Tutorials#navigate-with-single-top-instance-or-popupto">here</a>) along with withAnimation for customizing animations.</p><p>I think the problem is not with the <a href="https://github.com/google/accompanist">accompanist</a>’s navigation-animation but the way Navigation Component for Compose was designed. Anyway, these are just my opinions &amp; you are always free to use whatever satisfies your needs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5655820dedb8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>