Month: April 2021

  • Git worktree example

    Git worktree example

    Git worktree is great a way to separate your code without having to create a new branch when you are not yet ready to commit.

    I have created a new repo in GitHub and cloned in a dir ‘worktree-example’.

    git clone [email protected]:sminrana/git-worktree-example.git

    Open the dir with code . command and update the readme.md file. Now we can push it to main branch.

    git add README.md 
    git commit -m "message"
    git push origin main

    It’s time to create our first worktree. We will put our worktree one dir up from our current dir where our main branch is. The documentation says -d flag with single hyphen but two hypen worked for me.

    git worktree add --d ../test 

    Now open test folder and open it in code . and make some changes in readme file then commit. Since worktree is detached HEAD you can’t commit to anywhere, for this we have create a new branch from here, run following command.

    git switch -c dev
    git push origin dev

    You can remove worktree

    git worktree remove test

    and remove completely from ref.

    git worktree prune

    That’s all you needed. More git worktree commands

    git worktree add --d ../hotfix 
    git worktree remove hotfix
    git worktree prune
    
    Throwaway working tree
    git worktree add --d <path>

    Download source code

    Spread the love
  • Radio Buttons group in SwiftUI

    Radio Buttons group in SwiftUI

    Radio Buttons group in SwiftUI

    Radio Buttons group in SwiftUI, is not hard to implement classic radio button group in your SwiftUI. Though this project is in very early stage, I will work on this in coming days to enhance its features.

    Installation: Add RadioButtonGroup.swift in your SwiftUI project.

    Usages: Create model class, must conform to RadioModelable protocol

     class RadioModel: RadioModelable {
        var id: Int
        var isChecked: Bool
        var label: String
        
        required init(id: Int, isChecked: Bool, label: String) {
            self.id = id
            self.isChecked = isChecked
            self.label = label
        }
    }

    Create data provider class, must conform to RadioDataProviding protocol

    class DataProvider<T>: RadioDataProviding where T: RadioModelable {
        @Published var items: [RItem] = []
        
        init() {
            
            self.items = getItems()
        }
        
        func getItems() -> [T] {
            return [T(id: 1, isChecked: true, label: "Radio 1"),
                    T(id: 2, isChecked: false, label: "Radio 2"),
                    T(id: 3, isChecked: false, label: "Radio 3")]
        }
        
        func toggle(id: Int) {
            for var item in self.items {
                if item.id == id {
                    item.isChecked = true
                } else {
                    item.isChecked = false
                }
            }
            
            self.objectWillChange.send()
        }
    }

    Finally add in your view

    struct ContentView: View {
        var body: some View {
            SRadioButtonViewGroup(dataProvider: DataProvider<RadioModel>(), selectedItem: getSelectedItemLabel)
        }
        
        func getSelectedItemLabel<T>(item: T) {
            print("selected item : \((item as! RadioModel).label)")
        }
    }

    Preview

    Download source code

    Spread the love