<commit_before>// File created from ScreenTemplate
// $ createScreen.sh Spaces/SpaceRoomList/ExploreRoom ShowSpaceExploreRoom
/*
 Copyright 2021 New Vector Ltd
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import Foundation

final class SpaceExploreRoomViewModel: SpaceExploreRoomViewModelType {
    
    // MARK: - Properties
    
    // MARK: Private

    private let session: MXSession
    private let spaceId: String
    private let spaceName: String?

    private var currentOperation: MXHTTPOperation?
    private var nextBatch: String?
    private var rootSpaceChildInfo: MXSpaceChildInfo?
    
    private var itemDataList: [SpaceExploreRoomListItemViewData] = [] {
        didSet {
            self.updateFilteredItemList()
        }
    }
    private var searchKeyword: String? {
        didSet {
            self.updateFilteredItemList()
        }
    }
    private var filteredItemDataList: [SpaceExploreRoomListItemViewData] = [] {
        didSet {
            if self.filteredItemDataList.isEmpty {
                if self.itemDataList.isEmpty {
                    self.update(viewState: .emptySpace)
                } else {
                    self.update(viewState: .emptyFilterResult)
                }
            } else {
                self.update(viewState: .loaded(self.filteredItemDataList, self.nextBatch != nil && (self.searchKeyword ?? "").isEmpty))
            }
        }
    }
    
    // MARK: Public

    weak var viewDelegate: SpaceExploreRoomViewModelViewDelegate?
    weak var coordinatorDelegate: SpaceExploreRoomViewModelCoordinatorDelegate?
    
    // MARK: - Setup
    
    init(parameters: SpaceExploreRoomCoordinatorParameters) {
        self.session = parameters.session
        self.spaceId = parameters.spaceId
        self.spaceName = parameters.spaceName
    }
    
    deinit {
        self.cancelOperations()
    }
    
    // MARK: - Public
    
    func process(viewAction: SpaceExploreRoomViewAction) {
        switch viewAction {
        case .loadData:
            self.loadData()
        case .complete(let selectedItem, let sourceView):
            self.coordinatorDelegate?.spaceExploreRoomViewModel(self, didSelect: selectedItem, from: sourceView)
        case .cancel:
            self.cancelOperations()
            self.coordinatorDelegate?.spaceExploreRoomViewModelDidCancel(self)
        case .searchChanged(let newText):
            self.searchKeyword = newText
        }
    }
    
    // MARK: - Private
    
    private func loadData() {
        guard self.currentOperation == nil else {
            return
        }
        
        if let spaceName = self.spaceName {
            self.update(viewState: .spaceNameFound(spaceName))
        }

        if self.nextBatch == nil {
            self.update(viewState: .loading)
        }
        
        self.currentOperation = self.session.spaceService.getSpaceChildrenForSpace(withId: self.spaceId, suggestedOnly: false, limit: 20, maxDepth: 1, from: nil, completion: { [weak self] response in
            guard let self = self else {
                return
            }<commit_msg>Support pagination in the Space Summary API<commit_after>// File created from ScreenTemplate
// $ createScreen.sh Spaces/SpaceRoomList/ExploreRoom ShowSpaceExploreRoom
/*
 Copyright 2021 New Vector Ltd
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import Foundation

final class SpaceExploreRoomViewModel: SpaceExploreRoomViewModelType {
    
    // MARK: - Properties
    
    // MARK: Private

    private let session: MXSession
    private let spaceId: String
    private let spaceName: String?

    private var currentOperation: MXHTTPOperation?
    private var nextBatch: String?
    private var rootSpaceChildInfo: MXSpaceChildInfo?
    
    private var itemDataList: [SpaceExploreRoomListItemViewData] = [] {
        didSet {
            self.updateFilteredItemList()
        }
    }
    private var searchKeyword: String? {
        didSet {
            self.updateFilteredItemList()
        }
    }
    private var filteredItemDataList: [SpaceExploreRoomListItemViewData] = [] {
        didSet {
            if self.filteredItemDataList.isEmpty {
                if self.itemDataList.isEmpty {
                    self.update(viewState: .emptySpace)
                } else {
                    self.update(viewState: .emptyFilterResult)
                }
            } else {
                self.update(viewState: .loaded(self.filteredItemDataList, self.nextBatch != nil && (self.searchKeyword ?? "").isEmpty))
            }
        }
    }
    
    // MARK: Public

    weak var viewDelegate: SpaceExploreRoomViewModelViewDelegate?
    weak var coordinatorDelegate: SpaceExploreRoomViewModelCoordinatorDelegate?
    
    // MARK: - Setup
    
    init(parameters: SpaceExploreRoomCoordinatorParameters) {
        self.session = parameters.session
        self.spaceId = parameters.spaceId
        self.spaceName = parameters.spaceName
    }
    
    deinit {
        self.cancelOperations()
    }
    
    // MARK: - Public
    
    func process(viewAction: SpaceExploreRoomViewAction) {
        switch viewAction {
        case .loadData:
            self.loadData()
        case .complete(let selectedItem, let sourceView):
            self.coordinatorDelegate?.spaceExploreRoomViewModel(self, didSelect: selectedItem, from: sourceView)
        case .cancel:
            self.cancelOperations()
            self.coordinatorDelegate?.spaceExploreRoomViewModelDidCancel(self)
        case .searchChanged(let newText):
            self.searchKeyword = newText
        }
    }
    
    // MARK: - Private
    
    private func loadData() {
        guard self.currentOperation == nil else {
            return
        }
        
        if let spaceName = self.spaceName {
            self.update(viewState: .spaceNameFound(spaceName))
        }

        if self.nextBatch == nil {
            self.update(viewState: .loading)
        }
        
        self.currentOperation = self.session.spaceService.getSpaceChildrenForSpace(withId: self.spaceId, suggestedOnly: false, limit: nil, maxDepth: 1, from: self.nextBatch, completion: { [weak self] response in
            guard let self = self else {
                return
            }
{'fraction_non_alphanumeric': 0.07401770890979524, 'fraction_numerical': 0.002767017155506364, 'mean_word_length': 2.3009132420091323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746375', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1817, 'n_words': 510}