xmartlabs / XLForm

XLForm is the most flexible and powerful iOS library to create dynamic table-view forms. Fully compatible with Swift & Obj-C.
MIT License
5.77k stars 954 forks source link

Update Documentation / README / Examples for Swift #359

Open ericcgu opened 9 years ago

ericcgu commented 9 years ago

Let me know if want help with this. @mtnbarreto

mtnbarreto commented 9 years ago

@ericcgu It would be great if you can help with the swift doc and examples migration. I have been working on swift examples and almost got all the examples migrated to swift.

Since you want to help I just pushed it here: https://github.com/xmartlabs/XLForm/tree/examples/swift . Could you review these swift code and make changes if needed ? This is my first swift code. :) I'm open to any contribution such as improve documentation, feature implementation, bug fixes, answer questions, add more test cases, etc.

ericcgu commented 9 years ago

https://github.com/xmartlabs/XLForm/pull/360

ericcgu commented 9 years ago

https://github.com/xmartlabs/XLForm/pull/361

ericcgu commented 9 years ago

@mtnbarreto It looks good. I don't know this repo well enough to add a lot of code change but here are some of the things I noticed:

  1. Is it possible to use enums of Type Int instead of static lets. I believe you can write custom functions on enums. They are much more powerful:
class XLFormWeekDaysCell : XLFormBaseCell {

    static let kSunday = "sunday"
    static let kMonday = "monday"
    static let kTuesday = "tuesday"
    static let kWednesday = "wednesday"
    static let kThursday = "thursday"
    static let kFriday = "friday"
    static let kSaturday = "saturday"

  enum kWeekDay: Int {
        case
        Sunday = 1,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday

        func description() -> String {
            switch self {
            case .Sunday:
                return "Sunday"
            case .Monday:
                return "Monday"
            case .Tuesday:
                return "Tuesday"
            case .Wednesday:
                return "Wednesday"
            case .Thursday:
                return "Thursday"
            case .Friday:
                return "Friday"
            case .Saturday:
                return "Saturday"
            }
        }

        //Add Custom Function to Enum

        //Allows you loop and iterate as needed
        static let allValues = [Sunday,
                                Monday,
                                Tuesday,
                                Wednesday,
                                Thursday,
                                Friday,]
}
  1. Maybe using an array of IBOutlets can clean up some redundancy. You just identify them using tags and match the tags with enum raw value (Int) from above. Sunday is view.tag = 1, Monday, tag = 2, etc. This way you can change alpha in one line instead of 7.
    @IBOutlet weak var sundayButton: UIButton!
    @IBOutlet weak var mondayButton: UIButton!
    @IBOutlet weak var tuesdayButton: UIButton!
    @IBOutlet weak var wednesdayButton: UIButton!
    @IBOutlet weak var thursdayButton: UIButton!
    @IBOutlet weak var fridayButton: UIButton!
    @IBOutlet weak var saturdayButton: UIButton!

    //Consider changing tags and using Array of UIButton
    @IBOutlet var weekdayButton = [UIButton]()
  1. In Swift, unless you are using it in a closure, custom extension or have naming convention conflict, you don't need to write self.
    func updateButtons() {
        let value = self.rowDescriptor.value as! Dictionary<String, Bool>
        sundayButton.selected = value[XLFormWeekDaysCell.kSunday]!
        self.mondayButton.selected = value[XLFormWeekDaysCell.kMonday]!
        self.tuesdayButton.selected = value[XLFormWeekDaysCell.kTuesday]!
        self.wednesdayButton.selected = value[XLFormWeekDaysCell.kWednesday]!
        self.thursdayButton.selected = value[XLFormWeekDaysCell.kThursday]!
        self.fridayButton.selected = value[XLFormWeekDaysCell.kFriday]!
        self.saturdayButton.selected = value[XLFormWeekDaysCell.kSaturday]!

    }

    func updateButtons() {
        let value = rowDescriptor.value as! Dictionary<String, Bool>
        sundayButton.selected = value[XLFormWeekDaysCell.kSunday]!
        mondayButton.selected = value[XLFormWeekDaysCell.kMonday]!
        tuesdayButton.selected = value[XLFormWeekDaysCell.kTuesday]!
        wednesdayButton.selected = value[XLFormWeekDaysCell.kWednesday]!
        thursdayButton.selected = value[XLFormWeekDaysCell.kThursday]!
        fridayButton.selected = value[XLFormWeekDaysCell.kFriday]!
        saturdayButton.selected = value[XLFormWeekDaysCell.kSaturday]!
    }
  1. Perhaps you can leverage some kind of hybrid /custom struct that will allow you to loop through Dictionaries

struct OrderedDictionary<Tk: Hashable, Tv> {
    var keys: Array<Tk>
    var values: Dictionary<Tk,Tv>

    // 1
    mutating func insert(value: Tv, forKey key: Tk, atIndex index: Int) -> Tv?
    {
        var adjustedIndex = index

        // 2
        let existingValue = self.values[key]
        if existingValue != nil {
            // 3
            let existingIndex = find(self.keys, key)!

            // 4
            if existingIndex < index {
                adjustedIndex--
            }
            self.keys.removeAtIndex(existingIndex)
        }

        // 5
        self.keys.insert(key, atIndex:adjustedIndex)
        self.values[key] = value

        // 6
        return existingValue
    }

}
mtnbarreto commented 9 years ago

@ericcgu, thanks for your contributions!

I agree that functions inside an enum is a better approach (swift has this capability, let's use it). Can you update the code? Regarding the collection IBOutlet I think it can reduce the amount of code but at the same time is hard to understand. I don't have a strong position. I let you make the decision.

Notice that the swift and obj-c examples are the same, I tried to have the same examples (on both languages) so anyone can compare and i would like to continue with this correspondence.

ericcgu commented 9 years ago

My pleasure.

I also changed this Switch Looking If Statement to a Switch Statement. Hope that is OK.

    func getDayFormButton(button: UIButton) -> String
    {
        if button == self.sundayButton { return XLFormWeekDaysCell.kSunday }
        if button == self.mondayButton { return XLFormWeekDaysCell.kMonday }
        if button == self.tuesdayButton { return XLFormWeekDaysCell.kTuesday }
        if button == self.wednesdayButton { return XLFormWeekDaysCell.kWednesday }
        if button == self.thursdayButton { return XLFormWeekDaysCell.kThursday }
        if button == self.fridayButton { return XLFormWeekDaysCell.kFriday }
        return XLFormWeekDaysCell.kSaturday
    }

    func getDayFormButton(button: UIButton) -> String {
        switch button {
        case sundayButton:
            return XLFormWeekDaysCell.kWeekDay.Sunday.description()
        case mondayButton:
            return XLFormWeekDaysCell.kWeekDay.Monday.description()
        case tuesdayButton:
            return XLFormWeekDaysCell.kWeekDay.Tuesday.description()
        case wednesdayButton:
            return XLFormWeekDaysCell.kWeekDay.Wednesday.description()
        case thursdayButton:
            return XLFormWeekDaysCell.kWeekDay.Thursday.description()
        case fridayButton:
            return XLFormWeekDaysCell.kWeekDay.Friday.description()
        default:
            return XLFormWeekDaysCell.kWeekDay.Saturday.description()
        }
    }
ericcgu commented 9 years ago

https://github.com/xmartlabs/XLForm/pull/362

I will try to help out as I study XLForms more.

ericcgu commented 9 years ago

@mtnbarreto

Do you want me to change this for UICustomization?

Let me know.

image

image

mtnbarreto commented 9 years ago

@ericcgu Go ahead! Please update objective-c example as well.

ericcgu commented 9 years ago

@mtnbarreto

If you think this is OK, I will work on converting Obj C right away.

https://github.com/xmartlabs/XLForm/pull/368

untitled