Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo app changes for pinned and archived channels #683

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions DemoAppSwiftUI/ChannelHeader/ChannelListQueryIdentifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright © 2024 Stream.io Inc. All rights reserved.
//

import Foundation
import StreamChat

enum ChannelListQueryIdentifier: String, CaseIterable, Identifiable {
case initial
case archived
case pinned
case unarchivedAndPinnedSorted

var id: String {
rawValue
}

var title: String {
switch self {
case .initial: "Initial Channels"
case .archived: "Archived Channels"
case .pinned: "Pinned Channels"
case .unarchivedAndPinnedSorted: "Sort by Pinned and Ignore Archived Channels"
}
}
}
21 changes: 21 additions & 0 deletions DemoAppSwiftUI/ChannelHeader/ChooseChannelQueryView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright © 2024 Stream.io Inc. All rights reserved.
//

import StreamChat
import StreamChatSwiftUI
import SwiftUI

struct ChooseChannelQueryView: View {
static let queryIdentifiers = ChannelListQueryIdentifier.allCases.sorted(using: KeyPathComparator(\.title))

var body: some View {
ForEach(Self.queryIdentifiers) { queryIdentifier in
Button {
AppState.shared.setChannelQueryIdentifier(queryIdentifier)
} label: {
Text(queryIdentifier.title)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct CustomChannelModifier: ChannelListHeaderViewModifier {

var title: String

@State var isChooseChannelQueryShown = false
@State var isNewChatShown = false
@State var logoutAlertShown = false
@State var actionsPopupShown = false
Expand Down Expand Up @@ -99,18 +100,26 @@ struct CustomChannelModifier: ChannelListHeaderViewModifier {
)
}
.confirmationDialog("", isPresented: $actionsPopupShown) {
Button("Blocked users") {
Button("Choose Channel Query") {
isChooseChannelQueryShown = true
}
Button("Show Blocked Users") {
blockedUsersShown = true
}

Button("Logout") {
Button("Logout", role: .destructive) {
logoutAlertShown = true
}

Button("Cancel", role: .cancel) {}
} message: {
Text("Select an action")
}
.confirmationDialog("", isPresented: $isChooseChannelQueryShown) {
ChooseChannelQueryView()
} message: {
Text("Choose a channel query")
}
}
}
}
116 changes: 85 additions & 31 deletions DemoAppSwiftUI/DemoAppSwiftUIApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright © 2024 Stream.io Inc. All rights reserved.
//

import Combine
import StreamChat
import StreamChatSwiftUI
import SwiftUI
Expand Down Expand Up @@ -39,25 +40,11 @@ struct DemoAppSwiftUIApp: App {
.tabItem { Label("Threads", systemImage: "text.bubble") }
.badge(appState.unreadCount.threads)
}
.id(appState.contentIdentifier)
}
}
.onChange(of: appState.userState) { newValue in
if newValue == .loggedIn {
/*
if let currentUserId = chatClient.currentUserId {
let pinnedByKey = ChatChannel.isPinnedBy(keyForUserId: currentUserId)
let channelListQuery = ChannelListQuery(
filter: .containMembers(userIds: [currentUserId]),
sort: [
.init(key: .custom(keyPath: \.isPinned, key: pinnedByKey), isAscending: true),
.init(key: .lastMessageAt),
.init(key: .updatedAt)
]
)
appState.channelListController = chatClient.channelListController(query: channelListQuery)
}
*/
appState.currentUserController = chatClient.currentUserController()
notificationsHandler.setupRemoteNotifications()
}
}
Expand Down Expand Up @@ -86,28 +73,55 @@ struct DemoAppSwiftUIApp: App {
}

class AppState: ObservableObject, CurrentChatUserControllerDelegate {
@Injected(\.chatClient) var chatClient: ChatClient

@Published var userState: UserState = .launchAnimation {
willSet {
if newValue == .notLoggedIn && userState == .loggedIn {
channelListController = nil
}
}
}

// Recreate the content view when channel query changes.
@Published private(set) var contentIdentifier: String = ""

@Published var userState: UserState = .launchAnimation
@Published var unreadCount: UnreadCount = .noUnread

var channelListController: ChatChannelListController?
var currentUserController: CurrentChatUserController? {
didSet {
currentUserController?.delegate = self
currentUserController?.synchronize()
}
}
private(set) var channelListController: ChatChannelListController?
private(set) var currentUserController: CurrentChatUserController?
private var cancellables = Set<AnyCancellable>()

static let shared = AppState()

private init() {}
private init() {
$userState
.removeDuplicates()
.filter { $0 == .notLoggedIn }
.sink { [weak self] _ in
self?.didLogout()
}
.store(in: &cancellables)
$userState
.removeDuplicates()
.filter { $0 == .loggedIn }
.sink { [weak self] _ in
self?.didLogin()
}
.store(in: &cancellables)
}

private func didLogout() {
channelListController = nil
currentUserController = nil
}

private func didLogin() {
setChannelQueryIdentifier(.initial)

currentUserController = chatClient.currentUserController()
currentUserController?.delegate = self
currentUserController?.synchronize()
}

func setChannelQueryIdentifier(_ identifier: ChannelListQueryIdentifier) {
let query = AppState.channelListQuery(forIdentifier: identifier, chatClient: chatClient)
channelListController = chatClient.channelListController(query: query)
contentIdentifier = identifier.rawValue
}

func currentUserController(_ controller: CurrentChatUserController, didChangeCurrentUserUnreadCount: UnreadCount) {
unreadCount = didChangeCurrentUserUnreadCount
Expand All @@ -125,3 +139,43 @@ enum UserState {
case notLoggedIn
case loggedIn
}

extension AppState {
private static func channelListQuery(
forIdentifier identifier: ChannelListQueryIdentifier,
chatClient: ChatClient
) -> ChannelListQuery {
guard let currentUserId = chatClient.currentUserId else { fatalError("Not logged in") }
switch identifier {
case .initial:
return ChannelListQuery(
filter: .containMembers(userIds: [currentUserId])
)
case .unarchivedAndPinnedSorted:
return ChannelListQuery(
filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.archived, to: false)
]),
sort: [
.init(key: .pinnedAt, isAscending: false),
.init(key: .default)
]
)
case .archived:
return ChannelListQuery(
filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.archived, to: true)
])
)
case .pinned:
return ChannelListQuery(
filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.pinned, to: true)
])
)
}
}
}
12 changes: 0 additions & 12 deletions DemoAppSwiftUI/PinChannelHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ import StreamChat
import StreamChatSwiftUI
import SwiftUI

extension ChatChannel {
static func isPinnedBy(keyForUserId userId: UserId) -> String {
"is_pinned_by_\(userId)"
}

var isPinned: Bool {
guard let userId = membership?.id else { return false }
let key = Self.isPinnedBy(keyForUserId: userId)
return extraData[key]?.boolValue ?? false
}
}

struct DemoAppChatChannelListItem: View {

@Injected(\.fonts) private var fonts
Expand Down
59 changes: 51 additions & 8 deletions DemoAppSwiftUI/ViewFactoryExamples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class DemoAppFactory: ViewFactory {
onDismiss: onDismiss,
onError: onError
)
let archiveChannel = archiveChannelAction(for: channel, onDismiss: onDismiss, onError: onError)
let pinChannel = pinChannelAction(for: channel, onDismiss: onDismiss, onError: onError)
actions.insert(archiveChannel, at: actions.count - 2)
actions.insert(pinChannel, at: actions.count - 2)
return actions
}
Expand Down Expand Up @@ -76,6 +78,40 @@ class DemoAppFactory: ViewFactory {
ShowProfileModifier(messageModifierInfo: messageModifierInfo, mentionsHandler: mentionsHandler)
}

private func archiveChannelAction(
for channel: ChatChannel,
onDismiss: @escaping () -> Void,
onError: @escaping (Error) -> Void
) -> ChannelAction {
ChannelAction(
title: channel.isArchived ? "Unarchive Channel" : "Archive Channel",
iconName: "archivebox",
action: { [weak self] in
guard let self else { return }
let channelController = self.chatClient.channelController(for: channel.cid)
if channel.isArchived {
channelController.unarchive { error in
if let error = error {
onError(error)
} else {
onDismiss()
}
}
} else {
channelController.archive { error in
if let error = error {
onError(error)
} else {
onDismiss()
}
}
}
},
confirmationPopup: nil,
isDestructive: false
)
}

private func pinChannelAction(
for channel: ChatChannel,
onDismiss: @escaping () -> Void,
Expand All @@ -87,14 +123,21 @@ class DemoAppFactory: ViewFactory {
action: { [weak self] in
guard let self else { return }
let channelController = self.chatClient.channelController(for: channel.cid)
let userId = channelController.channel?.membership?.id ?? ""
let pinnedKey = ChatChannel.isPinnedBy(keyForUserId: userId)
let newState = !channel.isPinned
channelController.partialChannelUpdate(extraData: [pinnedKey: .bool(newState)]) { error in
if let error = error {
onError(error)
} else {
onDismiss()
if channel.isPinned {
channelController.unpin { error in
if let error = error {
onError(error)
} else {
onDismiss()
}
}
} else {
channelController.pin { error in
if let error = error {
onError(error)
} else {
onDismiss()
}
}
}
},
Expand Down
Loading
Loading