Skip to content

Question about making changes to the inner datasource which is a MutableDataSource #20

@Isuru-Nanayakkara

Description

@Isuru-Nanayakkara

There is a simple static tableview where it shows profile data. Since this data was not changing, it has used the StaticDataSource.

final class ProfileInfoCardViewModel {
    let dataSource: DataSource
    let profileViewModel: ProfileViewModel

    init(profileViewModel: ProfileViewModel) {
        self.profileViewModel = profileViewModel
        let user = profileViewModel.user.producer.skipNil()

        let proxyDataSource = ProxyDataSource()
        self.dataSource = proxyDataSource
        proxyDataSource.innerDataSource <~ user.map { user in
            if user.isLoggedInUser {
                if user.userLikes ?? 0 > 0 {
                    items.append(ProfileInfoRatingCellViewModel(userLikes : user.userLikes ?? 0))
                } else {
                    items.append(ProfileInfoNoRatingsYetCellViewModel())
                }
                items.append(ProfileInfoUpdateCellViewModel())
            }
            return StaticDataSource(items: items)
        }
    }
}

There is a new requirement to make this tableview dynamic. Upon a tap of a button, I need to insert/remove a cell from this tableview. So I opted to use the MutableDataSource. Also I changed the dataSource class variable from DataSource type to ProxyDataSource directly.

final class ProfileInfoCardViewModel {
    let dataSource: ProxyDataSource
    let profileViewModel: ProfileViewModel

    init(profileViewModel: ProfileViewModel) {
        self.profileViewModel = profileViewModel
        let user = profileViewModel.user.producer.skipNil()
        
        self.dataSource = ProxyDataSource()
        self.dataSource.innerDataSource <~ user.map { user in
            if user.isLoggedInUser {
                if user.userLikes ?? 0 > 0 {
                    items.append(ProfileInfoRatingCellViewModel(userLikes : user.userLikes ?? 0))
                } else {
                    items.append(ProfileInfoNoRatingsYetCellViewModel())
                }
                items.append(ProfileInfoUpdateCellViewModel())
            }
            return MutableDataSource(items)
        }
    }
    
    func userTappedToggleButton(_ show: Bool) {
        // do changes (insert/remove) to the inner mutable data source here

    }
}

The data loads fine still. However I'm having trouble figuring out how to make changes to the inner data source (add/remove an item).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions