Skip to content

Commit

Permalink
fix pagination ciclyc dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kloenk committed Jun 15, 2021
1 parent 7178dfe commit fbc1498
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 127 deletions.
6 changes: 6 additions & 0 deletions Nio.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
9525D2EC267659A2004055B6 /* SettingsContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9525D2EB267659A2004055B6 /* SettingsContainerView.swift */; };
9525D2ED267659BD004055B6 /* SettingsContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9525D2E62676594F004055B6 /* SettingsContainerView.swift */; };
9525D2EE26765C8C004055B6 /* RecentRoomsContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39C932062384BB13004449E1 /* RecentRoomsContainerView.swift */; };
95CAC7B82678F64E001D71DC /* MXEventTimeLine+Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95CAC7B72678F64E001D71DC /* MXEventTimeLine+Async.swift */; };
95CAC7B92678F64E001D71DC /* MXEventTimeLine+Async.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95CAC7B72678F64E001D71DC /* MXEventTimeLine+Async.swift */; };
A51BF8CE254C2FE5000FB0A4 /* NioApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51BF8CD254C2FE5000FB0A4 /* NioApp.swift */; };
A51F762C25D6E9950061B4A4 /* MessageTextViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51F762B25D6E9950061B4A4 /* MessageTextViewWrapper.swift */; };
A58352A625A667AB00533363 /* MatrixSDK in Frameworks */ = {isa = PBXBuildFile; productRef = A58352A525A667AB00533363 /* MatrixSDK */; };
Expand Down Expand Up @@ -383,6 +385,7 @@
9525D2E326765850004055B6 /* RoomView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomView.swift; sourceTree = "<group>"; };
9525D2E62676594F004055B6 /* SettingsContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsContainerView.swift; sourceTree = "<group>"; };
9525D2EB267659A2004055B6 /* SettingsContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsContainerView.swift; sourceTree = "<group>"; };
95CAC7B72678F64E001D71DC /* MXEventTimeLine+Async.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MXEventTimeLine+Async.swift"; sourceTree = "<group>"; };
A51BF8CD254C2FE5000FB0A4 /* NioApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NioApp.swift; sourceTree = "<group>"; };
A51F762B25D6E9950061B4A4 /* MessageTextViewWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageTextViewWrapper.swift; sourceTree = "<group>"; };
CAAF5BF72478696F006FDC33 /* UITextViewWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITextViewWrapper.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -864,6 +867,7 @@
393411C823904428003B49B8 /* MXEvent+Extensions.swift */,
4BEB8C03250403D200E90699 /* UserDefaults.swift */,
E897AA3325F2716F00D11427 /* UXKit.swift */,
95CAC7B72678F64E001D71DC /* MXEventTimeLine+Async.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1427,6 +1431,7 @@
E8B4726125F26D5A00ACEFCB /* AccountStore.swift in Sources */,
E8B4725825F26D5A00ACEFCB /* CustomEvent.swift in Sources */,
E8B4725F25F26D5A00ACEFCB /* Configuration.swift in Sources */,
95CAC7B82678F64E001D71DC /* MXEventTimeLine+Async.swift in Sources */,
E8B4725D25F26D5A00ACEFCB /* EventCollection.swift in Sources */,
9525D2D426763DD0004055B6 /* MXAutoDiscovery+Async.swift in Sources */,
E8B4725E25F26D5A00ACEFCB /* MXEvent+Extensions.swift in Sources */,
Expand Down Expand Up @@ -1516,6 +1521,7 @@
E897AA2325F2707C00D11427 /* Reaction.swift in Sources */,
E897AA1925F2707C00D11427 /* EditEvent.swift in Sources */,
E897AA1F25F2707C00D11427 /* MX+Identifiable.swift in Sources */,
95CAC7B92678F64E001D71DC /* MXEventTimeLine+Async.swift in Sources */,
E897AA1D25F2707C00D11427 /* MXCredentials+Keychain.swift in Sources */,
9525D2D326763DD0004055B6 /* MXAutoDiscovery+Async.swift in Sources */,
E897AA2425F2707C00D11427 /* MXEvent+Extensions.swift in Sources */,
Expand Down
49 changes: 32 additions & 17 deletions Nio/Conversations/RoomView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI
import Combine
import MatrixSDK
import SwiftUI

import NioKit

Expand Down Expand Up @@ -29,7 +29,7 @@ struct RoomView: View {
@State private var shouldPaginate = false

private var areOtherUsersTyping: Bool {
return !(room.room.typingUsers?.filter { $0 != userId }.isEmpty ?? true)
!(room.room.typingUsers?.filter { $0 != userId }.isEmpty ?? true)
}

var body: some View {
Expand All @@ -41,18 +41,19 @@ struct RoomView: View {
showSender: !self.isDirect,
edits: self.events.relatedEvents(of: event).filter { $0.isEdit() },
contextMenuModel: EventContextMenuModel(
event: event,
userId: self.userId,
onReact: { self.onReact(event.eventId) },
onReply: { },
onEdit: { self.edit(event: event) },
onRedact: {
if event.sentState == MXEventSentStateFailed {
room.removeOutgoingMessage(event)
} else {
self.eventToRedact = event.eventId
}
}))
event: event,
userId: self.userId,
onReact: { self.onReact(event.eventId) },
onReply: {},
onEdit: { self.edit(event: event) },
onRedact: {
if event.sentState == MXEventSentStateFailed {
room.removeOutgoingMessage(event)
} else {
self.eventToRedact = event.eventId
}
}
))
.padding(.horizontal)
}

Expand All @@ -72,12 +73,26 @@ struct RoomView: View {
onCancel: cancelEdit,
onCommit: send
)
.padding(.horizontal)
.padding(.bottom, 10)
.padding(.horizontal)
.padding(.bottom, 10)
}
.onChange(of: shouldPaginate) { newValue in
if newValue, let topEvent = events.renderableEvents.first {
store.paginate(room: self.room, event: topEvent)
asyncDetached {
print("paginating")
await room.paginate(topEvent)
}
}
}
.onAppear {
asyncDetached {
if let topEvent = events.renderableEvents.first {
// await room.paginate(topEvent)
await room.createPagination()
} else {
print("did not find topEvent")
await room.createPagination()
}
}
}
.alert(item: $eventToRedact) { eventId in
Expand Down
6 changes: 3 additions & 3 deletions Nio/NewConversation/NewConversationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private struct NewConversationView: View {
// Seems to be a bug in Xcode, currently needs to be asnyc, to be able to await actor
Button(action: {
async {
createRoom
await createRoom()
}
}) {
Text(verbatim: L10n.NewConversation.createRoom)
Expand Down Expand Up @@ -151,7 +151,7 @@ private struct NewConversationView: View {
}
}

@MainActor private func createRoom() {
private func createRoom() async {
isWaiting = true

let parameters = MXRoomCreationParameters()
Expand All @@ -173,7 +173,7 @@ private struct NewConversationView: View {
}
}

store?.session?.createRoom(parameters: parameters) { response in
await store?.session?.createRoom(parameters: parameters) { response in
switch response {
case .success(let room):
createdRoomId = room.id
Expand Down
2 changes: 1 addition & 1 deletion Nio/Shared Views/ReverseList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct ReverseList<Element, Content>: View where Element: Identifiable, Content:
}
.frame(height: 30) // FIXME: Frame height shouldn't be hard-coded
.onPreferenceChange(IsVisibleKey.self) {
hasReachedTop = $0
if $0 != hasReachedTop { hasReachedTop = $0 }
}
}
.scaleEffect(x: -1.0, y: 1.0)
Expand Down
2 changes: 1 addition & 1 deletion NioKit/Extensions/MX+Identifiable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SwiftUI
import MatrixSDK
import SwiftUI

extension MXPublicRoom: Identifiable {}
extension MXRoom: Identifiable {}
Expand Down
8 changes: 4 additions & 4 deletions NioKit/Extensions/MXAutoDiscovery+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import Foundation
import MatrixSDK

extension MXAutoDiscovery {
public func findClientConfig() async throws -> MXDiscoveredClientConfig {
return try await withCheckedThrowingContinuation {continuation in
self.findClientConfig({ continuation.resume(returning: $0)}, failure: {continuation.resume(throwing: $0)})
public extension MXAutoDiscovery {
func findClientConfig() async throws -> MXDiscoveredClientConfig {
try await withCheckedThrowingContinuation { continuation in
self.findClientConfig({ continuation.resume(returning: $0) }, failure: { continuation.resume(throwing: $0) })
}
}
}
10 changes: 5 additions & 5 deletions NioKit/Extensions/MXClient+Publisher.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Foundation
import Combine
import Foundation
import MatrixSDK

extension MXRestClient {
public func nio_publicRooms(onServer: String? = nil, limit: UInt? = nil) -> AnyPublisher<MXPublicRoomsResponse, Error> {
public extension MXRestClient {
func nio_publicRooms(onServer: String? = nil, limit: UInt? = nil) -> AnyPublisher<MXPublicRoomsResponse, Error> {
Future<MXPublicRoomsResponse, Error> { promise in
self.publicRooms(onServer: onServer, limit: limit) { response in
switch response {
case .failure(let error):
case let .failure(error):
promise(.failure(error))
case .success(let publicRoomsResponse):
case let .success(publicRoomsResponse):
promise(.success(publicRoomsResponse))
@unknown default:
fatalError("Unexpected Matrix response: \(response)")
Expand Down
12 changes: 6 additions & 6 deletions NioKit/Extensions/MXCredentials+Keychain.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import MatrixSDK
import KeychainAccess
import MatrixSDK

extension MXCredentials {
public func save(to keychain: Keychain) {
public extension MXCredentials {
func save(to keychain: Keychain) {
guard
let homeserver = self.homeServer,
let homeserver = homeServer,
let userId = self.userId,
let accessToken = self.accessToken,
let deviceId = self.deviceId
Expand All @@ -17,14 +17,14 @@ extension MXCredentials {
keychain["deviceId"] = deviceId
}

public func clear(from keychain: Keychain) {
func clear(from keychain: Keychain) {
keychain["homeserver"] = nil
keychain["userId"] = nil
keychain["accessToken"] = nil
keychain["deviceId"] = nil
}

public static func from(_ keychain: Keychain) -> MXCredentials? {
static func from(_ keychain: Keychain) -> MXCredentials? {
guard
let homeserver = keychain["homeserver"],
let userId = keychain["userId"],
Expand Down
14 changes: 7 additions & 7 deletions NioKit/Extensions/MXEvent+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import Foundation
import MatrixSDK

extension MXEvent {
public var timestamp: Date {
Date(timeIntervalSince1970: TimeInterval(self.originServerTs / 1000))
public extension MXEvent {
var timestamp: Date {
Date(timeIntervalSince1970: TimeInterval(originServerTs / 1000))
}

public func content<T>(valueFor key: String) -> T? {
if let value = self.content?[key] as? T {
func content<T>(valueFor key: String) -> T? {
if let value = content?[key] as? T {
return value
}
return nil
}

public func prevContent<T>(valueFor key: String) -> T? {
if let value = self.unsignedData?.prevContent?[key] as? T {
func prevContent<T>(valueFor key: String) -> T? {
if let value = unsignedData?.prevContent?[key] as? T {
return value
}
return nil
Expand Down
27 changes: 27 additions & 0 deletions NioKit/Extensions/MXEventTimeLine+Async.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// MXEventTimeLine+Async.swift
// Nio
//
// Created by Finn Behrens on 15.06.21.
// Copyright © 2021 Kilian Koeltzsch. All rights reserved.
//

import Foundation
import MatrixSDK

extension MXEventTimeline {
func paginate(_ numItems: UInt, direction: MXTimelineDirection = .backwards, onlyFromStore: Bool = false) async throws {
return try await withCheckedThrowingContinuation {continuation in
self.paginate(numItems, direction: direction, onlyFromStore: onlyFromStore, completion: {resp in
switch resp {
case .success(_):
continuation.resume()
case .failure(let e):
continuation.resume(throwing: e)
@unknown default:
continuation.resume(throwing: NioUnknownContinuationSwitchError(value: resp))
}
})
}
}
}
14 changes: 7 additions & 7 deletions NioKit/Extensions/MXRestClient+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import MatrixSDK

extension MXRestClient {
func login(type loginType: MatrixSDK.MXLoginFlowType = .password, username: String, password: String) async throws -> MXCredentials {
return try await withCheckedThrowingContinuation {continuation in
self.login(type: loginType, username: username, password: password, completion: {resp in
try await withCheckedThrowingContinuation { continuation in
self.login(type: loginType, username: username, password: password, completion: { resp in
switch resp {
case .success(let v):
case let .success(v):
continuation.resume(returning: v)
case .failure(let e):
case let .failure(e):
continuation.resume(throwing: e)
@unknown default:
continuation.resume(throwing: NioUnknownContinuationSwitchError(value: resp))
}
})
}
}

func wellKnown() async throws -> MXWellKnown {
return try await withCheckedThrowingContinuation {continuation in
self.wellKnow({continuation.resume(returning: $0!)}, failure: {continuation.resume(throwing: $0!)})
try await withCheckedThrowingContinuation { continuation in
self.wellKnow({ continuation.resume(returning: $0!) }, failure: { continuation.resume(throwing: $0!) })
}
}
}
16 changes: 12 additions & 4 deletions NioKit/Extensions/MXRoom+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ import MatrixSDK

extension MXRoom {
func members() async throws -> MXRoomMembers? {
return try await withCheckedThrowingContinuation {continuation in
self.members(completion: {resp in
try await withCheckedThrowingContinuation { continuation in
self.members(completion: { resp in
switch resp {
case .success(let v):
case let .success(v):
continuation.resume(returning: v)
case .failure(let e):
case let .failure(e):
continuation.resume(throwing: e)
@unknown default:
continuation.resume(throwing: NioUnknownContinuationSwitchError(value: resp))
}
})
}
}

var liveTimeline: MXEventTimeline {
get async {
await withCheckedContinuation { continuation in
self.liveTimeline { continuation.resume(returning: $0!) }
}
}
}
}
Loading

0 comments on commit fbc1498

Please sign in to comment.