Skip to content

Commit

Permalink
T10203244: archiving of chat/past meetings seems to be broken [4fec198]
Browse files Browse the repository at this point in the history
  • Loading branch information
lancy-mega committed May 18, 2023
1 parent aef99af commit 9495625
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 51 deletions.
15 changes: 8 additions & 7 deletions MEGAData/Repository/Chat/ChatRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,14 @@ public final class ChatRepository: ChatRepositoryProtocol {
chatSDK.firstActiveCall?.toCallEntity()
}

public func chatsList(ofType type: ChatTypeEntity) -> [ChatListItemEntity]? {
guard let chatList = chatSDK.chatListItems(by: type.toMEGAChatType()) else { return nil }
var chatListItems = [ChatListItemEntity]()
for i in 0 ..< chatList.size {
chatListItems.append(chatList.chatListItem(at: i).toChatListItemEntity())
}
return chatListItems
public func fetchMeetings() -> [ChatListItemEntity]? {
guard let chatList = chatSDK.chatListItems(by: [.meetingOrNonMeeting, .archivedOrNonArchived], filter: .meeting) else { return nil }
return (0..<chatList.size).map { chatList.chatListItem(at: $0).toChatListItemEntity() }
}

public func fetchNonMeetings() -> [ChatListItemEntity]? {
guard let chatList = chatSDK.chatListItems(by: [.meetingOrNonMeeting, .archivedOrNonArchived], filter: []) else { return nil }
return (0..<chatList.size).map { chatList.chatListItem(at: $0).toChatListItemEntity() }
}

public func isCallInProgress(for chatRoomId: HandleEntity) -> Bool {
Expand Down
73 changes: 41 additions & 32 deletions MEGAUnitTests/Chat/ChatRoomsListViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import Combine

final class ChatRoomsListViewModelTests: XCTestCase {
var subscription: AnyCancellable?
let chatsListMock = [ChatListItemEntity(chatId: 1, title: "Chat1"), ChatListItemEntity(chatId: 3, title: "Chat2"), ChatListItemEntity(chatId: 67, title: "Chat3")]
let meetingsListMock = [ChatListItemEntity(chatId: 11, title: "Meeting 1"), ChatListItemEntity(chatId: 14, title: "Meeting 2"), ChatListItemEntity(chatId: 51, title: "Meeting 3")]
let chatsListMock = [ChatListItemEntity(chatId: 1, title: "Chat1"),
ChatListItemEntity(chatId: 3, title: "Chat2"),
ChatListItemEntity(chatId: 67, title: "Chat3")]
let meetingsListMock = [ChatListItemEntity(chatId: 11, title: "Meeting 1", meeting: true),
ChatListItemEntity(chatId: 14, title: "Meeting 2", meeting: true),
ChatListItemEntity(chatId: 51, title: "Meeting 3", meeting: true)]

func test_remoteChatStatusChange() {
let userHandle: HandleEntity = 100
Expand Down Expand Up @@ -53,25 +57,6 @@ final class ChatRoomsListViewModelTests: XCTestCase {
XCTAssert(router.presentStartConversation_calledTimes == 1)
}

func testAction_selectChatsMode() {
let viewModel = ChatRoomsListViewModel(
chatUseCase: MockChatUseCase(items: chatsListMock),
chatViewMode: .meetings
)

viewModel.loadChatRoomsIfNeeded()
viewModel.selectChatMode(.chats)

guard let chatRoomsCount = viewModel.displayChatRooms?.count else {
XCTFail("No Chat Rooms, count should be equal to chatsListMock.count")
return
}

for index in 0..<chatRoomsCount {
XCTAssert(viewModel.displayChatRooms?[index].chatListItem.chatId == chatsListMock[index].chatId)
}
}

func testSelectChatMode_inviteContactNow_shouldMatch() throws {
let router = MockChatRoomsListRouter()
let viewModel = ChatRoomsListViewModel(router: router, chatViewMode: .meetings)
Expand Down Expand Up @@ -117,23 +102,47 @@ final class ChatRoomsListViewModelTests: XCTestCase {
XCTAssertTrue(state.description == Strings.Localizable.seeWhoSAlreadyOnMEGA)
}

func testAction_selectMeetingsMode() {
func testSelectChatsMode_inputAsChats_viewModelesShouldMatch() {
let mockList = chatsListMock
let viewModel = ChatRoomsListViewModel(
chatUseCase: MockChatUseCase(items: meetingsListMock),
chatUseCase: MockChatUseCase(items: mockList),
chatViewMode: .meetings
)

viewModel.loadChatRoomsIfNeeded()
viewModel.selectChatMode(.chats)


let expectation = expectation(description: "Compare the past meetings")
subscription = viewModel
.$displayChatRooms
.dropFirst()
.sink {
XCTAssert(mockList.map { ChatRoomViewModel(chatListItem: $0) } == $0)
expectation.fulfill()
}

guard let chatRoomsCount = viewModel.displayChatRooms?.count else {
XCTFail("No Chat Rooms, count should be equal to chatsListMock.count")
return
}
viewModel.selectChatMode(.chats)
wait(for: [expectation], timeout: 6)
}

func testSelectChatsMode_inputAsMeeting_viewModelsShouldMatch() {
let mockList = meetingsListMock
let viewModel = ChatRoomsListViewModel(
chatUseCase: MockChatUseCase(items: mockList),
chatViewMode: .chats
)
viewModel.loadChatRoomsIfNeeded()

for index in 0..<chatRoomsCount {
XCTAssert(viewModel.displayChatRooms?[index].chatListItem.chatId == meetingsListMock[index].chatId)
}
let expectation = expectation(description: "Compare the past meetings")
subscription = viewModel
.$displayPastMeetings
.filter { $0?.count == 3 }
.sink {
XCTAssert(mockList.map { ChatRoomViewModel(chatListItem: $0) } == $0)
expectation.fulfill()
}

viewModel.selectChatMode(.meetings)
wait(for: [expectation], timeout: 6)
}

func test_EmptyChatsList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension ChatRoomViewModel {
megaHandleUseCase: MEGAHandleUseCaseProtocol = MockMEGAHandleUseCase(),
callUseCase: CallUseCaseProtocol = MockCallUseCase(),
audioSessionUseCase: AudioSessionUseCaseProtocol = MockAudioSessionUseCase(),
scheduledMeetingUseCase: ScheduledMeetingUseCaseProtocol,
scheduledMeetingUseCase: ScheduledMeetingUseCaseProtocol = MockScheduledMeetingUseCase(),
chatNotificationControl: ChatNotificationControl = ChatNotificationControl(delegate: MockPushNotificationControl()),
notificationCenter: NotificationCenter = .default,
isTesting: Bool = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ final class ScheduleMeetingCreationRecurrenceOptionsViewModelTests: XCTestCase {
let router = ScheduleMeetingCreationRecurrenceOptionsRouter(
presenter: UINavigationController(),
rules: inputRules[0],
startDate: Date.now
startDate: date
)

let expectation = expectation(description: "Selection updated from monthly to daily")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public protocol ChatRepositoryProtocol: RepositoryProtocol {
func monitorChatListItemUpdate() -> AnyPublisher<[ChatListItemEntity], Never>
func existsActiveCall() -> Bool
func activeCall() -> CallEntity?
func chatsList(ofType type: ChatTypeEntity) -> [ChatListItemEntity]?
func fetchMeetings() -> [ChatListItemEntity]?
func fetchNonMeetings() -> [ChatListItemEntity]?
func isCallInProgress(for chatRoomId: HandleEntity) -> Bool
func myFullName() -> String?
func archivedChatListCount() -> UInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public protocol ChatUseCaseProtocol {
func monitorChatListItemUpdate() -> AnyPublisher<[ChatListItemEntity], Never>
func existsActiveCall() -> Bool
func activeCall() -> CallEntity?
func chatsList(ofType type: ChatTypeEntity) -> [ChatListItemEntity]?
func fetchNonMeetings() -> [ChatListItemEntity]?
func fetchMeetings() -> [ChatListItemEntity]?
func isCallInProgress(for chatRoomId: HandleEntity) -> Bool
func myFullName() -> String?
func archivedChatListCount() -> UInt
Expand Down Expand Up @@ -59,9 +60,13 @@ public struct ChatUseCase<T: ChatRepositoryProtocol>: ChatUseCaseProtocol {
public func activeCall() -> CallEntity? {
chatRepo.activeCall()
}

public func fetchMeetings() -> [ChatListItemEntity]? {
chatRepo.fetchMeetings()?.sorted(by: { $0.lastMessageDate.compare($1.lastMessageDate) == .orderedDescending })
}

public func chatsList(ofType type: ChatTypeEntity) -> [ChatListItemEntity]? {
chatRepo.chatsList(ofType: type)?.sorted(by: { $0.lastMessageDate.compare($1.lastMessageDate) == .orderedDescending })
public func fetchNonMeetings() -> [ChatListItemEntity]? {
chatRepo.fetchNonMeetings()?.sorted(by: { $0.lastMessageDate.compare($1.lastMessageDate) == .orderedDescending })
}

public func isCallInProgress(for chatRoomId: HandleEntity) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public final class MockChatUseCase: ChatUseCaseProtocol {
activeCallEntity
}

public func chatsList(ofType type: ChatTypeEntity) -> [ChatListItemEntity]? {
public func fetchNonMeetings() -> [ChatListItemEntity]? {
items
}

public func fetchMeetings() -> [ChatListItemEntity]? {
items
}

Expand Down
19 changes: 16 additions & 3 deletions iMEGA/Chat/ChatRoomsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ - (void)viewDidLoad {

switch (self.chatRoomsType) {
case ChatRoomsTypeDefault:
[self configureSelectorViewForChatType:MEGAChatTypeNonMeeting];
self.chatListItemList = [[MEGASdkManager sharedMEGAChatSdk] chatListItemsByType:self.chatTypeSelected];
[self loadChatListItemListForSelectedChatType];
self.archivedChatListItemList = [[MEGASdkManager sharedMEGAChatSdk] archivedChatListItems];
self.addBarButtonItem.enabled = [MEGAReachabilityManager isReachable] && MEGASdkManager.sharedMEGASdk.businessStatus != BusinessStatusExpired;
break;
Expand Down Expand Up @@ -646,8 +645,22 @@ - (void)presentGroupOrContactDetailsForChatListItem:(MEGAChatListItem *)chatList
}
}

- (void)loadChatListItemListForSelectedChatType {
if (self.chatTypeSelected == MEGAChatTypeMeeting) {
self.chatListItemList = [MEGAChatSdk.shared chatListItemsByMask:MEGAChatListMaskMeetingOrNonMeeting | MEGAChatListMaskArchivedOrNonArchived
filter:MEGAChatListFilterMeeting | MEGAChatListFilterNonArchived];
} else {
self.chatListItemList = [MEGAChatSdk.shared chatListItemsByMask:MEGAChatListMaskMeetingOrNonMeeting | MEGAChatListMaskArchivedOrNonArchived
filter:MEGAChatListFilterNonMeeting | MEGAChatListFilterNonArchived];
}
}

- (void)reloadData {
self.chatListItemList = self.chatRoomsType == ChatRoomsTypeDefault ? [[MEGASdkManager sharedMEGAChatSdk] chatListItemsByType:self.chatTypeSelected] : [[MEGASdkManager sharedMEGAChatSdk] archivedChatListItems];
if (self.chatRoomsType == ChatRoomsTypeDefault) {
[self loadChatListItemListForSelectedChatType];
} else {
self.chatListItemList = [MEGAChatSdk.shared archivedChatListItems];
}
self.archivedChatListItemList = [[MEGASdkManager sharedMEGAChatSdk] archivedChatListItems];
[self reorderList];
[self updateChatIdIndexPathDictionary];
Expand Down
4 changes: 2 additions & 2 deletions iMEGA/ChatRoomsListScene/ChatRoomsListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ final class ChatRoomsListViewModel: ObservableObject {
private func fetchNonMeetingChats() async {
guard isViewOnScreen else { return }

let chatListItems = chatUseCase.chatsList(ofType: .nonMeeting) ?? []
let chatListItems = chatUseCase.fetchNonMeetings() ?? []
chatRooms = chatListItems.map(constructChatRoomViewModel)

await filterChats()
Expand All @@ -310,7 +310,7 @@ final class ChatRoomsListViewModel: ObservableObject {
return
}

let chatListItems = chatUseCase.chatsList(ofType: .meeting) ?? []
let chatListItems = chatUseCase.fetchMeetings() ?? []

fetchFutureScheduledMeetings()

Expand Down

0 comments on commit 9495625

Please sign in to comment.