#1098 - Added user suggestions to the main app timeline.

This commit is contained in:
Stefan Ceriu
2021-10-04 11:37:37 +03:00
parent ccfdfa1fb8
commit 388e521a89
8 changed files with 129 additions and 27 deletions
@@ -20,6 +20,7 @@ import Foundation
import UIKit
import SwiftUI
@available(iOS 14.0, *)
final class UserSuggestionCoordinator: Coordinator {
// MARK: - Properties
@@ -44,27 +45,22 @@ final class UserSuggestionCoordinator: Coordinator {
init(parameters: UserSuggestionCoordinatorParameters) {
self.parameters = parameters
userSuggestionService = UserSuggestionService(session: parameters.room)
userSuggestionService = UserSuggestionService(room: parameters.room)
userSuggestionViewModel = UserSuggestionViewModel.makeUserSuggestionViewModel(userSuggestionService: userSuggestionService)
let view = UserSuggestionList(viewModel: viewModel.context)
.addDependency(AvatarService.instantiate(mediaManager: parameters.session.mediaManager))
let view = UserSuggestionList(viewModel: userSuggestionViewModel.context)
.addDependency(AvatarService.instantiate(mediaManager: parameters.mediaManager))
userSuggestionHostingController = VectorHostingController(rootView: view)
userSuggestionHostingController = UIHostingController(rootView: view)
}
func processPartialUserName(_ userName: String) {
userSuggestionService.processPartialUserName(userName)
}
// MARK: - Public
func start() {
MXLog.debug("[UserSuggestionCoordinator] did start.")
userSuggestionViewModel.completion = { [weak self] result in
MXLog.debug("[UserSuggestionCoordinator] UserSuggestionViewModel did complete with result: \(result).")
guard let self = self else { return }
switch result {
case .cancel, .done:
self.completion?()
break
}
}
}
func toPresentable() -> UIViewController {
@@ -0,0 +1,48 @@
//
// 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
@objcMembers
final class UserSuggestionCoordinatorBridge: NSObject {
private var _userSuggestionCoordinator: Any? = nil
@available(iOS 14.0, *)
fileprivate var userSuggestionCoordinator: UserSuggestionCoordinator {
return _userSuggestionCoordinator as! UserSuggestionCoordinator
}
init(mediaManager: MXMediaManager, room: MXRoom) {
let parameters = UserSuggestionCoordinatorParameters(mediaManager: mediaManager, room: room)
if #available(iOS 14.0, *) {
self._userSuggestionCoordinator = UserSuggestionCoordinator(parameters: parameters)
}
}
func processPartialUserName(_ userName: String) {
if #available(iOS 14.0, *) {
return self.userSuggestionCoordinator.processPartialUserName(userName)
}
}
func toPresentable() -> UIViewController? {
if #available(iOS 14.0, *) {
return self.userSuggestionCoordinator.toPresentable()
}
return nil
}
}
@@ -19,5 +19,6 @@
import Foundation
struct UserSuggestionCoordinatorParameters {
let mediaManager: MXMediaManager
let room: MXRoom
}
@@ -28,15 +28,38 @@ class UserSuggestionService: UserSuggestionServiceProtocol {
private let room: MXRoom
private var suggestionItems: [UserSuggestionItemProtocol] = []
// MARK: Public
var items: CurrentValueSubject<[UserSuggestionItemProtocol], Never>
// MARK: - Setup
init(room: MXRoom) {
self.room = room
self.items = CurrentValueSubject([])
generateUsersWithCount(10)
items.send(suggestionItems)
}
func processPartialUserName(_ userName: String) {
guard userName.count > 0 else {
items.send(suggestionItems)
return
}
items.send(suggestionItems.filter({ userSuggestion in
return (userSuggestion.displayName?.lowercased().range(of: userName.lowercased()) != .none)
}))
}
private func generateUsersWithCount(_ count: UInt) {
suggestionItems.removeAll()
for _ in 0..<count {
let identifier = "@" + UUID().uuidString
suggestionItems.append(MockUserSuggestionServiceItem(userId: identifier, displayName: identifier, avatarUrl: "mxc://matrix.org/VyNYAgahaiAzUoOeZETtQ"))
}
}
}
@@ -22,8 +22,6 @@ import SwiftUI
@available(iOS 14.0, *)
enum MockUserSuggestionScreenState: MockScreenState, CaseIterable {
case multipleResults
case oneResult
case empty
var screenType: Any.Type {
MockUserSuggestionScreenState.self
@@ -32,10 +30,6 @@ enum MockUserSuggestionScreenState: MockScreenState, CaseIterable {
var screenView: ([Any], AnyView) {
let service: MockUserSuggestionService
switch self {
case .empty:
service = MockUserSuggestionService(userCount: 0)
case .oneResult:
service = MockUserSuggestionService(userCount: 1)
case .multipleResults:
service = MockUserSuggestionService(userCount: 10)
}
@@ -30,6 +30,7 @@ struct UserSuggestionList: View {
// MARK: Public
@ObservedObject var viewModel: UserSuggestionViewModel.Context
// FIXME: This should be dynamic
let rowHeight: CGFloat = 60.0
let maxHeight: CGFloat = 300.0
@@ -67,6 +68,7 @@ private struct BackgroundView<Content: View>: View {
var content: () -> Content
@Environment(\.theme) private var theme: ThemeSwiftUI
private let shadowRadius: CGFloat = 20.0
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
@@ -75,8 +77,9 @@ private struct BackgroundView<Content: View>: View {
var body: some View {
VStack(content: content)
.background(theme.colors.background)
.clipShape(RoundedCornerShape(radius: 20.0, corners: [.topLeft, .topRight]))
.clipShape(RoundedCornerShape(radius: shadowRadius, corners: [.topLeft, .topRight]))
.shadow(color: .black.opacity(0.20), radius: 20.0, x: 0.0, y: 3.0)
.mask(Rectangle().padding(.init(top: -(shadowRadius * 2), leading: 0.0, bottom: 0.0, trailing: 0.0)))
.edgesIgnoringSafeArea(.all)
}
}