lundi 22 octobre 2018

Swift: pattern for ViewController that can be acessed from many places

In my app, I've a "details" screen/VC that can be accessed from 3 different screens.

I'm currently using constructor dependency injection and 3 different initializers to setup correct variables based on where user comes from.

Still, most of logic is in ViewDidLoad under pretty ugly ifs

Wrote a little sample to give you an idea of how it looks like now:

if object != nil {

    WebApi.fetchDropDown1 { items in
        dropdown.selectedItem = items.first{$0.id == object.UnitId}
    }

    currentDateLabel = object.date
} else if object == nil && currentDate == nil {
    // came to add from list of objects

    // object doesn't exist yet

    WebApi.fetchDropDown1 { items in
        dropdown.selectedItem = items[0] // select first availabile
    }

    currentDateLabel = Date() // set today as default date
    deleteButton.isEnabled = false
    // something like that for every element

} else if { currentDate != nil && object == nil} {
    // came here from calendar pick

    WebApi.fetchDropDown1 { items in
        dropdown.selectedItem = items[0] // select first availabile
    }

    currentDateLabel = currentDate

}

This isn't actual code, but just to clarify what I'm trying to solve.

Many tutorials on design patterns just do the simplest use-cases and I haven't been able to find some useful advice for more complex cases.

Thanks.

Aucun commentaire:

Enregistrer un commentaire