-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
169 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Util.swift | ||
// XMTPiOSExample | ||
// | ||
// Created by Pat Nakajima on 2/6/24. | ||
// | ||
|
||
import Foundation | ||
|
||
enum Util { | ||
static func abbreviate(address: String) -> String { | ||
if address.count > 6 { | ||
let start = address.index(address.startIndex, offsetBy: 6) | ||
let end = address.index(address.endIndex, offsetBy: -5) | ||
return address.replacingCharacters(in: start ... end, with: "...") | ||
} else { | ||
return address | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
XMTPiOSExample/XMTPiOSExample/Views/GroupSettingsView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// GroupSettingsView.swift | ||
// XMTPiOSExample | ||
// | ||
// Created by Pat Nakajima on 2/6/24. | ||
// | ||
|
||
import SwiftUI | ||
import XMTPiOS | ||
|
||
struct GroupSettingsView: View { | ||
var client: XMTPiOS.Client | ||
var group: XMTPiOS.Group | ||
|
||
@Environment(\.dismiss) var dismiss | ||
@EnvironmentObject var coordinator: EnvironmentCoordinator | ||
|
||
@State private var groupMembers: [String] = [] | ||
@State private var newGroupMember = "" | ||
@State private var isAddingMember = false | ||
@State private var groupError = "" | ||
|
||
init(client: Client, group: XMTPiOS.Group) { | ||
self.client = client | ||
self.group = group | ||
} | ||
|
||
var body: some View { | ||
NavigationStack { | ||
List { | ||
Section("Members") { | ||
ForEach(groupMembers, id: \.self) { member in | ||
HStack { | ||
Text(Util.abbreviate(address: member)) | ||
Spacer() | ||
if client.address.lowercased() == member.lowercased() { | ||
Text("You") | ||
.foregroundStyle(.secondary) | ||
} | ||
} | ||
.swipeActions { | ||
if client.address.lowercased() == member.lowercased() { | ||
Button("Leave", role: .destructive) { | ||
Task { | ||
try await group.removeMembers(addresses: [client.address]) | ||
coordinator.path = NavigationPath() | ||
dismiss() | ||
} | ||
} | ||
} else { | ||
Button("Remove", role: .destructive) { | ||
Task { | ||
try await group.removeMembers(addresses: [member]) | ||
await syncGroupMembers() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
HStack { | ||
TextField("Add member", text: $newGroupMember) | ||
Button("Add") { | ||
if newGroupMember.lowercased() == client.address { | ||
self.groupError = "You cannot add yourself to a group" | ||
return | ||
} | ||
|
||
isAddingMember = true | ||
|
||
Task { | ||
do { | ||
if try await self.client.canMessageV3(address: newGroupMember) { | ||
try await group.addMembers(addresses: [newGroupMember]) | ||
try await syncGroupMembers() | ||
|
||
await MainActor.run { | ||
self.groupError = "" | ||
self.newGroupMember = "" | ||
self.isAddingMember = false | ||
} | ||
} else { | ||
await MainActor.run { | ||
self.groupError = "Member address not registered" | ||
self.isAddingMember = false | ||
} | ||
} | ||
} catch { | ||
self.groupError = error.localizedDescription | ||
self.isAddingMember = false | ||
} | ||
} | ||
} | ||
.opacity(isAddingMember ? 0 : 1) | ||
.overlay { | ||
if isAddingMember { | ||
ProgressView() | ||
} | ||
} | ||
} | ||
} | ||
|
||
if groupError != "" { | ||
Text(groupError) | ||
.foregroundStyle(.red) | ||
.font(.subheadline) | ||
} | ||
} | ||
.navigationTitle("Group Settings") | ||
.task { | ||
await syncGroupMembers() | ||
} | ||
} | ||
} | ||
|
||
private func syncGroupMembers() async { | ||
try? await group.sync() | ||
await MainActor.run { | ||
self.groupMembers = group.members | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters