Я создаю функцию общего доступа, используя uicollectionView
и ReactorKit
Моя настройка
PhotoListViewReactor.class
class PhotoListViewReactor : Reactor {
enum Action {
case shareInit
case select(photo: Photo)
case deselect(photo: Photo)
case shareConfirm
case shareFinish
}
enum Mutation {
case selectShare(_ photo: Photo)
case deselectShare(_ photo: Photo)
case setSharingState(Bool)
case triggerShareAction
case shareComplete
}
struct State {
var sharePhotos: [Photo] = []
var isSharing: Bool = false
var shareAction: Bool = false
}
var initialState = State()
// init() { }
func mutate(action: Action) -> Observable<Mutation> {
switch action {
case .select(photo: let photo):
return Observable.just(Mutation.selectShare(photo)).takeUntil(self.action.filter(isSharingAction))
case .deselect(photo: let photo):
return Observable.just(Mutation.deselectShare(photo)).takeUntil(self.action.filter(isSharingAction))
case .shareInit:
return Observable.just(Mutation.setSharingState(true))
case .shareConfirm:
return Observable.concat([Observable.just(Mutation.triggerShareAction), Observable.just(Mutation.setSharingState(false))])
case .shareFinish:
return Observable.concat([Observable.just(Mutation.shareComplete),Observable.just(Mutation.setSharingState(false))])
}
}
func reduce(state: State, mutation: Mutation) -> State {
switch mutation {
case let .selectShare(photo):
var newState = state
newState.sharePhotos.append(photo)
return newState
case let .deselectShare(photo):
var newState = state
newState.sharePhotos.removeAll(where: { $0.id == photo.id })
return newState
case let .setSharingState(isSharing):
var newState = state
newState.isSharing = isSharing
return newState
case .triggerShareAction:
var newState = state
newState.shareAction = true
return newState
case .shareComplete:
var newState = state
newState.shareAction = false
newState.isSharing = false
newState.sharePhotos = []
return newState
}
}
private func isSharingAction(_ action: Action) -> Bool {
if case .shareInit = action {
return true
} else {
return false
}
}
}
и внутри PhotoListViewController
self.collectionView.rx.modelSelected(Photo.self).share()
.filter(if: reactor.state.map{$0.isSharing})
.map {Reactor.Action.select(photo: $0)}
.bind(to: reactor.action)
.disposed(by: disposeBag)
self.collectionView.rx.modelDeselected(Photo.self).share()
.filter(if: reactor.state.map{$0.isSharing})
.map {Reactor.Action.deselect(photo: $0)}
.bind(to: reactor.action)
.disposed(by: disposeBag)
Чтобы очистить мой фильтр, если Оператор:
extension ObservableType {
/**
Filters the source observable sequence using a trigger observable sequence producing Bool values.
Elements only go through the filter when the trigger has not completed and its last element was true. If either source or trigger error's, then the source errors.
- parameter trigger: Triggering event sequence.
- returns: Filtered observable sequence.
*/
func filter(if trigger: Observable<Bool>) -> Observable<E> {
return self.withLatestFrom(trigger) { ($0, $1) }
.filter { $0.1 }
.map { $0.0 }
}
}
Моя проблема заключается в том, что выбор и отмена выбора не работают должным образом (после выбора пользователь не может отменить выбор ячейки, щелкнув снова),Я включил многосекционный режим в uicollectionview.