<?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 Balya Elfata on Medium]]></title>
        <description><![CDATA[Stories by Balya Elfata on Medium]]></description>
        <link>https://medium.com/@balyaelfata?source=rss-d4576e261e58------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lOXIpkmm18r16UZaYyILeg.jpeg</url>
            <title>Stories by Balya Elfata on Medium</title>
            <link>https://medium.com/@balyaelfata?source=rss-d4576e261e58------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 14:41:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@balyaelfata/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[Rive Animations in Swift 101]]></title>
            <link>https://medium.com/@balyaelfata/rive-animations-in-swift-101-4b4e94f19915?source=rss-d4576e261e58------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b4e94f19915</guid>
            <category><![CDATA[rive]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Balya Elfata]]></dc:creator>
            <pubDate>Sat, 05 Apr 2025 14:02:19 GMT</pubDate>
            <atom:updated>2025-04-05T14:02:19.327Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5o61zWqnPQZs8hfc7HPMCQ.png" /></figure><p>Recently, I implemented Rive animations in Swift for my visa preparation app, <strong>Visaku</strong>. It was a fun challenge, but I quickly realized that there weren’t many resources out there for integrating Rive into Swift projects.</p><p>So, I decided to write the guide I wish I had — something hands-on, beginner-friendly, and comprehensive. Let’s dive right in!</p><h3>🔧 Setting Up the Environment</h3><p>Here’s how to get started:</p><ol><li>Go to File &gt; Add Package Dependencies in Xcode.</li><li>Enter the Rive iOS package URL: <a href="https://github.com/rive-app/rive-ios">https://github.com/rive-app/rive-ios</a><br>If you encounter package resolution errors, try using version <strong>6.7.0</strong> (the one I used), or any recent stable version.</li><li>In your Swift file, import the Rive module:</li><li>Add your .riv file to the project workspace. (or you can use a web URL)</li></ol><pre>import RiveRuntime</pre><p>Your package dependencies window should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EiM_uu0ZBsRzArl2kySkWw.png" /></figure><h4>🎬 Displaying Rive Animations in SwiftUI</h4><p>Here’s the most basic way to display a Rive animation:</p><pre>import SwiftUI<br>import RiveRuntime<br><br>struct ContentView: View {<br>    var body: some View {<br>        RiveViewModel(<br>            filename: &quot;rive_animation&quot; // without the .riv extension<br>        ).view()<br>    }<br>}<br><br>#Preview {<br>    ContentView()<br>}</pre><p>📌 Note: Replace &quot;rive_animation&quot; with the name of your own .riv file.</p><p>You can also load animations from a URL:</p><pre>RiveViewModel(<br>    webURL: &quot;https://cdn.rive.app/animations/off_road_car_v7.riv&quot;<br>).view()</pre><h3>⏯️ Playing and Pausing Animations</h3><p>To control the animation (play/pause), you’ll want to store the RiveViewModel in a variable. Let’s also hook it up with buttons:</p><pre>import SwiftUI<br>import RiveRuntime<br><br>struct ContentView: View {<br>    private let riveViewModel: RiveViewModel  <br><br>    init () {<br>        riveViewModel = RiveViewModel(<br>            webURL: &quot;https://cdn.rive.app/animations/off_road_car_v7.riv&quot;<br>        )<br>    }<br>    <br>    var body: some View {<br>        riveViewModel.view()<br>        Button(&quot;Play&quot;) {<br>            riveViewModel.play()<br>        }<br>        Button(&quot;Pause&quot;) {<br>            riveViewModel.pause()<br>        }<br>    }<br>}<br><br>#Preview {<br>    ContentView()<br>}</pre><p>The animation here is played automatically by default. You can change it by adding the autoplay parameter to the initialize RiveViewModel and setting it to false:</p><pre>init () {<br>    riveViewModel = RiveViewModel(<br>        webURL: &quot;https://cdn.rive.app/animations/off_road_car_v7.riv&quot;,<br>        autoPlay: false<br>    )<br>}</pre><h3>⏪ Playing Animations Backwards</h3><p>Want your animation to go in reverse? Just add the direction parameter:</p><pre>import SwiftUI<br>import RiveRuntime<br><br>struct ContentView: View {<br>    private let riveViewModel: RiveViewModel<br>    <br>    init () {<br>        riveViewModel = RiveViewModel(<br>            webURL: &quot;https://cdn.rive.app/animations/off_road_car_v7.riv&quot;<br>        )<br>    }<br>    <br>    var body: some View {<br>        riveViewModel.view()<br>        Button(&quot;Play&quot;) {<br>            riveViewModel.play()<br>        }<br>        Button(&quot;Pause&quot;) {<br>            riveViewModel.pause()<br>        }<br>        Button(&quot;Reverse&quot;) {<br>            riveViewModel.play(direction: .backwards)<br>        }<br>    }<br>}<br><br>#Preview {<br>    ContentView()<br>}</pre><p>Notice that once you set the direction to .backwards by clicking the Reverse button, you can’t play it forward by clicking the Play button — it will stay backwards.</p><p>This is because the play() method without an argument will take the previously set argument. So you need to explicitly switch the direction to .forwards in order to fix this:</p><pre>import SwiftUI<br>import RiveRuntime<br><br>struct ContentView: View {<br>    private let riveViewModel: RiveViewModel<br>    <br>    init () {<br>        riveViewModel = RiveViewModel(<br>            webURL: &quot;https://cdn.rive.app/animations/off_road_car_v7.riv&quot;<br>        )<br>    }<br>    <br>    var body: some View {<br>        riveViewModel.view()<br>        Button(&quot;Play&quot;) {<br>            riveViewModel.play(direction: .forwards)<br>        }<br>        Button(&quot;Pause&quot;) {<br>            riveViewModel.pause()<br>        }<br>        Button(&quot;Reverse&quot;) {<br>            riveViewModel.play(direction: .backwards)<br>        }<br>    }<br>}<br><br>#Preview {<br>    ContentView()<br>}</pre><p>Now it’s working properly! we can move the truck forwards and backwards seamlessly.</p><h3>🔁 Looping Animations</h3><p>Clicking button to make the truck go back and forth are fun, but there’s a way to make it by just one line of code.</p><p>This truck animation is looping forever by default, but you might encounter other Rive animations that has no looping. You can loop a Rive animation forever, once, or back and forth:</p><pre>import SwiftUI<br>import RiveRuntime<br><br>struct ContentView: View {<br>    private let riveViewModel: RiveViewModel    <br><br>    init () {<br>        riveViewModel = RiveViewModel(<br>            webURL: &quot;https://cdn.rive.app/animations/off_road_car_v7.riv&quot;<br>        )<br>    }<br>    <br>    var body: some View {<br>        riveViewModel.view()<br>        Button(&quot;Play Once&quot;) {<br>            riveViewModel.play(loop: .oneShot)<br>        }<br>        Button(&quot;Loop Forever&quot;) {<br>            riveViewModel.play(loop: .loop)<br>        }<br>        Button(&quot;Loop Back and Forth&quot;) {<br>            riveViewModel.play(loop: .pingPong)<br>        }<br>    }<br>}<br><br>#Preview {<br>    ContentView()<br>}</pre><p>Remember that using the default play() method would play the previously set parameters, so don’t get yourself stuck in a loop. (pun intended)</p><h3>🕰 Working with Rive Timelines</h3><p>Some .riv files have <strong>multiple timelines</strong> — think of them as different animation sequences within the same file.</p><p>You’ll need to ask your designer (or check your Rive project if you have access) for the timeline names, since .riv files can’t be reverse-engineered for security reasons.</p><p>Once you get all the timeline names, you can pass a specific timeline into the animationName parameter to play it. It’s tricky how we call the timeline name by the parameter animationName — I spent some time figuring it out.</p><p>Let’s try to change the play and pause button to play different timelines of the .riv file (this need a .riv file with different timeline to work):</p><pre>import SwiftUI<br>import RiveRuntime<br><br>struct ContentView: View {<br>    private let riveViewModel: RiveViewModel  <br><br>    init () {<br>        riveViewModel = RiveViewModel(<br>            filename: &quot;rive_animation&quot;<br>        )<br>    }<br>    <br>    var body: some View {<br>        riveViewModel.view()<br>        Button(&quot;Play Animation 1&quot;) {<br>            riveViewModel.play(animationName: &quot;Timeline 1&quot;) // existing timeline name<br>        }<br>        Button(&quot;Play Animation 2&quot;) {<br>            riveViewModel.play(animationName: &quot;Timeline 2&quot;) // existing timeline name<br>        }<br>    }<br>}<br><br>#Preview {<br>    ContentView()<br>}</pre><p>On my case, we have 6 different timelines that are connected in one flow with different captions to highlight the features. You can use looping like I did to iterate through all the timelines. Here’s what it looks like:</p><figure><img alt="Visaku Rive Animation" src="https://cdn-images-1.medium.com/max/800/1*3v2s3w65aRfngLWKtunVVA.gif" /></figure><h3>🎮 Creating a Simple Truck Game</h3><p>Let’s have some fun and build a mini-game with our truck animation!</p><p>We’ll control the truck’s movement with buttons and track how far it has traveled:</p><p>💡 Preview:</p><ul><li>Forward button = drives the truck forward</li><li>Backward button = rewinds it</li><li>Pause button = stops everything</li><li>A cute distance meter = shows progress</li></ul><p>Code:</p><pre>import SwiftUI<br>import RiveRuntime<br><br>struct ContentView: View {<br>    private let riveViewModel: RiveViewModel<br>    @State private var distance: Int<br>    @State private var timer: Timer? = nil<br>    @State private var direction: Direction = .paused<br>    @State var finished: Bool = false<br>    <br>    enum Direction {<br>        case forwards, backwards, paused<br>    }<br><br>    init () {<br>        distance = 0<br>        riveViewModel = RiveViewModel(<br>            webURL: &quot;https://cdn.rive.app/animations/off_road_car_v7.riv&quot;,<br>            autoPlay: false<br>        )<br>    }<br>    <br>    var body: some View {<br>        ZStack {<br>            LinearGradient(<br>                colors: [Color.purple, Color.blue, Color.cyan],<br>                startPoint: .topLeading,<br>                endPoint: .bottomTrailing<br>            )<br>            .ignoresSafeArea()<br>            <br>            VStack(spacing: 30) {<br>                Text(&quot;🚗 Distance&quot;)<br>                    .font(.system(size: 40, weight: .black, design: .rounded))<br>                    .foregroundColor(.white)<br>                    .shadow(color: .yellow.opacity(0.6), radius: 4, x: 0, y: 2)<br><br>                HStack {<br>                    Text(&quot;\(distance)&quot;)<br>                        .font(.system(size: 60, weight: .heavy, design: .rounded))<br>                        .foregroundColor(.yellow)<br>                        .shadow(color: .white.opacity(0.6), radius: 4)<br>                    Text(&quot;m&quot;)<br>                        .font(.system(size: 40, weight: .black, design: .rounded))<br>                        .foregroundColor(.white)<br>                        .shadow(color: .yellow.opacity(0.6), radius: 4, x: 0, y: 2)<br>                        .animation(.bouncy, value: distance)<br>                }<br>                .padding(.bottom, 10)<br><br>                Text(distance == 100 ? &quot;🎉 Yayy you&#39;re finished!&quot; : &quot; &quot;)<br>                    .font(.title)<br>                    .fontWeight(.heavy)<br>                    .foregroundStyle(.green.gradient)<br>                    .scaleEffect(distance == 100 ? 1 : 0)<br>                    .animation(.bouncy, value: distance)<br><br>                riveViewModel.view()<br>                    .frame(height: 400)<br>                    .padding()<br><br>                // 🎮 Control Buttons<br>                HStack(spacing: 40) {<br>                    Button {<br>                        if distance &gt; 0 {<br>                            riveViewModel.play(direction: .backwards)<br>                            startTimer(for: .backwards)<br>                        }<br>                    } label: {<br>                        Image(systemName: &quot;arrowtriangle.backward.circle.fill&quot;)<br>                            .resizable()<br>                            .frame(width: 60, height: 60)<br>                            .foregroundStyle(.pink)<br>                            .shadow(color: .black.opacity(0.6), radius: 2)<br>                    }<br><br>                    Button {<br>                        riveViewModel.pause()<br>                        stopTimer()<br>                    } label: {<br>                        Image(systemName: &quot;pause.circle.fill&quot;)<br>                            .resizable()<br>                            .frame(width: 60, height: 60)<br>                            .foregroundStyle(.orange)<br>                            .shadow(color: .black.opacity(0.6), radius: 2)<br>                    }<br><br>                    Button {<br>                        if distance &lt; 100 {<br>                            riveViewModel.play(direction: .forwards)<br>                            startTimer(for: .forwards)<br>                        }<br>                    } label: {<br>                        Image(systemName: &quot;play.circle.fill&quot;)<br>                            .resizable()<br>                            .frame(width: 60, height: 60)<br>                            .foregroundStyle(.green)<br>                            .shadow(color: .black.opacity(0.6), radius: 2)<br>                    }<br>                }<br>            }<br>            .padding()<br>        }<br>    }<br><br>    func startTimer(for newDirection: Direction) {<br>        stopTimer()<br>        direction = newDirection<br>        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in<br>            switch direction {<br>            case .forwards:<br>                distance = min(distance + 1, 100)<br>                if distance == 100 {<br>                    riveViewModel.pause()<br>                    stopTimer()<br>                }<br>            case .backwards:<br>                distance = max(0, distance - 1)<br>                if distance == 0 {<br>                    riveViewModel.pause()<br>                    stopTimer()<br>                }<br>            case .paused:<br>                break<br>            }<br>        }<br>    }<br><br>    func stopTimer() {<br>        timer?.invalidate()<br>        timer = nil<br>        direction = .paused<br>    }<br>}<br><br>#Preview {<br>    ContentView()<br>}</pre><p>And boom! You’ve got a truck game with animation, state, and controls — powered by Rive!</p><figure><img alt="Simple Rive Truck Game" src="https://cdn-images-1.medium.com/max/295/1*myuwvWZhF-9nrD54RcFa1w.gif" /></figure><p>Now you can add music and create a better design to enhance the game!</p><h3>🎉 Wrapping Up</h3><p>We’ve gone through:</p><p>✅ Setting up Rive in Swift<br> ✅ Displaying .riv animations<br> ✅ Playing, pausing, and reversing<br> ✅ Looping animations<br> ✅ Working with multiple timelines<br> ✅ Building a mini interactive game</p><p>I hope this guide helped you feel more confident using Rive in your Swift projects. If you have any questions or ideas on how to extend this further, I’d love to hear them!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b4e94f19915" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Access Control in Swift]]></title>
            <link>https://medium.com/@balyaelfata/understanding-access-control-in-swift-without-the-headache-ef916d96b255?source=rss-d4576e261e58------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef916d96b255</guid>
            <category><![CDATA[uikit]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[access-control]]></category>
            <dc:creator><![CDATA[Balya Elfata]]></dc:creator>
            <pubDate>Wed, 26 Mar 2025 07:22:39 GMT</pubDate>
            <atom:updated>2025-03-26T07:33:05.879Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fuuq--vNv-SoASi2DZBCnA.png" /></figure><p>Hey there! If you’re like me, when you first heard about <strong>access control</strong> in Swift, you probably thought, <em>Do I really need to know this?</em> (Spoiler: Yes, you do! ). But don’t worry, I’ll try to break it down in a way that actually makes sense.</p><p>When I first started working on larger Swift projects, I often discussed with my colleagues whether my code was either <strong>too exposed</strong> or <strong>too restrictive</strong>. That’s when I realized that mastering access control is <strong>the key to writing clean, maintainable code</strong>. I really grasped this concept when I was building <strong>Visaku</strong>, an app for Indonesian visa document preparation, using a <strong>modular architecture</strong>. I had separate packages / modules for <strong>UI Components</strong> and <strong>Repository</strong>, and I had to carefully decide which classes and methods should be public, internal, or private. It was a great learning experience!</p><p>So, let’s dive in with real-world examples and some fun comparisons!</p><h3>What Is Access Control?</h3><p>In simple terms, access control determines <strong>who</strong> can access <strong>what</strong> in your Swift code. Think of it like permission levels in a company:</p><ul><li>Some files are <strong>public</strong> (everyone can see them)</li><li>Some are <strong>internal</strong> (only employees can see them)</li><li>Some are <strong>private</strong> (only you can see them)</li></ul><p>Swift gives us <strong>five</strong> levels of access control:</p><ol><li>open – Maximum accessibility (mainly for frameworks)</li><li>public – Accessible everywhere within the module</li><li>internal – Default level, accessible within the module</li><li>fileprivate – Accessible only within the same file</li><li>private – Accessible only within the same scope (class, struct, or extension)</li></ol><p>Let’s break each of them down with code examples for better understanding.</p><h3>1. Open &amp; Public: Share with the World</h3><p>When I was first building a Swift package, I learned (the hard way) that I had to make certain classes <strong>public</strong> so that I could use them when I add them as package dependencies.</p><pre>public class Car {<br>    public var brand: String<br>    <br>    public init(brand: String) {<br>        self.brand = brand<br>    }<br>    <br>    public func drive() {<br>        print(&quot;Driving a \(brand)!&quot;)<br>    }<br>}</pre><p>Let’s say we save this file as in a module and import it elsewhere. A<strong>nyone</strong> using this module can create a Car object and call its drive() method.</p><p><strong>But what’s the difference between </strong><strong>open and </strong><strong>public?</strong></p><ul><li><strong>open</strong>: The class and its methods can be subclassed and overridden <strong>even outside the module</strong>.</li><li><strong>public</strong>: The class and methods can be accessed but <strong>cannot</strong> be subclassed outside the module.</li></ul><p>So, when you make a class public instead of open, and someone using your package tried to subclass it—they would run into an error.</p><h3>2. Internal: The Default Access Level</h3><p>Most of the time, you don’t have to specify access control, because Swift sets it to internal by default. That means everything in the same module (app or package) can access it.</p><pre>class Engine {<br>    var horsepower: Int = 150<br>}<br><br>let engine = Engine()<br>print(engine.horsepower) // Works fine within the module</pre><p>If you don’t specify public or private, Swift assumes internal. It’s like a company policy—if you work here, you have access. But outsiders? Nope.</p><h3>3. Fileprivate: Keeping Things Local (to a File)</h3><p>Once, I had multiple classes in a single file that needed to share some properties. But I didn’t want <strong>everything</strong> in the module to access them. That’s where fileprivate came in handy!</p><pre>class RaceCar {<br>    fileprivate var nitroBoost = 100<br>    <br>    func useBoost() {<br>        nitroBoost -= 10<br>        print(&quot;Boost remaining: \(nitroBoost)&quot;)<br>    }<br>}<br><br>let car = RaceCar()<br>car.useBoost() // ✅ Works fine because it&#39;s in the same file</pre><p>But if you try to access nitroBoost from another file, it won’t work!</p><h3>4. Private: The Tightest Lock</h3><p>A variable shouldn’t be accessed directly. Normally this would be my go to access control and I would change it when I needed specific access. This relate closely to one of OOP concepts — <strong>Encapsulation</strong>.</p><pre>class BankAccount {<br>    private var balance: Double = 1000.0<br>    <br>    func withdraw(amount: Double) {<br>        if amount &lt;= balance {<br>            balance -= amount<br>            print(&quot;Withdrew $\(amount). New balance: $\(balance)&quot;)<br>        } else {<br>            print(&quot;Insufficient funds!&quot;)<br>        }<br>    }<br>}<br><br>let account = BankAccount()<br>// account.balance = 500.0 // ❌ Error: &#39;balance&#39; is private<br>account.withdraw(amount: 200) // ✅ Works through method access</pre><p>Using private ensures that balance <strong>can only be modified through specific methods</strong>. No accidental balance changes here!</p><h3>When Should You Use Each One?</h3><ul><li>Use <strong>open or </strong><strong>public</strong> when writing frameworks that others will use.</li><li>Use <strong>internal</strong> for most app code (the default in Swift).</li><li>Use <strong>fileprivate</strong> when different classes in the same file need access but the rest of the module shouldn’t see it.</li><li>Use <strong>private</strong> when you want to <strong>strictly</strong> encapsulate data within a class or struct.</li></ul><h3>Wrapping Up</h3><p>If there’s one thing I’ve learned, it’s that <strong>access control is all about balancing security and usability</strong>. Once I started using it properly, my code became <strong>more structured, easier to maintain, and way less error-prone</strong>.</p><p>I especially appreciated access controls when working on Visaku, where I had to organize modules efficiently. Ensuring that UI components were public but keeping repository internals private helped me maintain a clean architecture.</p><p>I hope this guide makes access control less intimidating for you! If you’ve ever had a funny (or frustrating) experience with access control in Swift, let me know in the comments. Let’s keep learning Swift together!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef916d96b255" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>