2022-01-31 21:28:07 +00:00
//
// U s e r A d d r e s s . s w i f t
// S i m p l e X
//
// C r e a t e d b y E v g e n y P o b e r e z k i n o n 3 1 / 0 1 / 2 0 2 2 .
// C o p y r i g h t © 2 0 2 2 S i m p l e X C h a t . A l l r i g h t s r e s e r v e d .
//
import SwiftUI
2022-09-21 17:18:48 +04:00
import SimpleXChat
2022-01-31 21:28:07 +00:00
struct UserAddress : View {
2022-09-22 08:36:39 +01:00
@ EnvironmentObject private var chatModel : ChatModel
2022-09-21 17:18:48 +04:00
@ State private var alert : UserAddressAlert ?
2022-10-24 11:25:36 +01:00
@ State private var showAcceptRequests = false
2022-09-21 17:18:48 +04:00
private enum UserAddressAlert : Identifiable {
case deleteAddress
case error ( title : LocalizedStringKey , error : String = " " )
var id : String {
switch self {
case . deleteAddress : return " deleteAddress "
case let . error ( title , _ ) : return " error \( title ) "
}
}
}
2022-02-01 17:34:06 +00:00
2022-01-31 21:28:07 +00:00
var body : some View {
2022-07-06 15:01:41 +01:00
ScrollView {
VStack ( alignment : . leading ) {
Text ( " You can share your address as a link or as a QR code - anybody will be able to connect to you. You won't lose your contacts if you later delete it. " )
. padding ( . bottom )
if let userAdress = chatModel . userAddress {
2022-10-23 11:16:56 +01:00
QRCode ( uri : userAdress . connReqContact )
2022-07-06 15:01:41 +01:00
HStack {
Button {
2022-10-24 11:25:36 +01:00
showShareSheet ( items : [ userAdress . connReqContact ] )
2022-07-06 15:01:41 +01:00
} label : {
2022-10-24 11:25:36 +01:00
HStack {
Image ( systemName : " square.and.arrow.up " )
Text ( " Share link " )
}
2022-07-06 15:01:41 +01:00
}
. padding ( )
2022-10-24 11:25:36 +01:00
NavigationLink {
if let contactLink = chatModel . userAddress {
AcceptRequestsView ( contactLink : contactLink )
. navigationTitle ( " Contact requests " )
. navigationBarTitleDisplayMode ( . large )
}
} label : {
HStack {
Text ( " Contact requests " )
Image ( systemName : " chevron.right " )
}
2022-07-06 15:01:41 +01:00
}
. padding ( )
2022-02-01 17:34:06 +00:00
}
2022-07-06 15:01:41 +01:00
. frame ( maxWidth : . infinity )
2022-10-24 11:25:36 +01:00
Button ( role : . destructive ) { alert = . deleteAddress } label : {
Label ( " Delete address " , systemImage : " trash " )
}
. frame ( maxWidth : . infinity )
2022-07-06 15:01:41 +01:00
} else {
Button {
Task {
do {
2022-10-23 11:16:56 +01:00
let connReqContact = try await apiCreateUserAddress ( )
2022-07-06 15:01:41 +01:00
DispatchQueue . main . async {
2022-10-23 11:16:56 +01:00
chatModel . userAddress = UserContactLink ( connReqContact : connReqContact )
2022-07-06 15:01:41 +01:00
}
} catch let error {
2022-10-15 18:09:25 +04:00
logger . error ( " UserAddress apiCreateUserAddress: \( responseError ( error ) ) " )
2022-10-06 15:02:58 +01:00
let a = getErrorAlert ( error , " Error creating address " )
alert = . error ( title : a . title , error : " \( a . message ) " )
2022-02-24 17:16:41 +00:00
}
}
2022-07-06 15:01:41 +01:00
} label : { Label ( " Create address " , systemImage : " qrcode " ) }
. frame ( maxWidth : . infinity )
}
2022-02-01 17:34:06 +00:00
}
2022-07-06 15:01:41 +01:00
. padding ( )
2022-09-22 08:36:39 +01:00
. frame ( maxWidth : . infinity , maxHeight : . infinity , alignment : . top )
2022-10-24 11:25:36 +01:00
. sheet ( isPresented : $ showAcceptRequests ) {
if let contactLink = chatModel . userAddress {
AcceptRequestsView ( contactLink : contactLink )
}
}
2022-09-21 17:18:48 +04:00
. alert ( item : $ alert ) { alert in
switch alert {
case . deleteAddress :
return Alert (
title : Text ( " Delete address? " ) ,
message : Text ( " All your contacts will remain connected " ) ,
primaryButton : . destructive ( Text ( " Delete " ) ) {
Task {
do {
try await apiDeleteUserAddress ( )
DispatchQueue . main . async {
chatModel . userAddress = nil
}
} catch let error {
2022-10-15 18:09:25 +04:00
logger . error ( " UserAddress apiDeleteUserAddress: \( responseError ( error ) ) " )
2022-09-21 17:18:48 +04:00
}
}
} , secondaryButton : . cancel ( )
)
case let . error ( title , error ) :
return Alert ( title : Text ( title ) , message : Text ( " \( error ) " ) )
}
}
2022-02-01 17:34:06 +00:00
}
2022-01-31 21:28:07 +00:00
}
}
struct UserAddress_Previews : PreviewProvider {
static var previews : some View {
2022-02-01 17:34:06 +00:00
let chatModel = ChatModel ( )
2022-10-23 11:16:56 +01:00
chatModel . userAddress = UserContactLink ( connReqContact : " https://simplex.chat/contact#/?v=1&smp=smp%3A%2F%2FPQUV2eL0t7OStZOoAsPEV2QYWt4-xilbakvGUGOItUo%3D%40smp6.simplex.im%2FK1rslx-m5bpXVIdMZg9NLUZ_8JBm8xTt%23MCowBQYDK2VuAyEALDeVe-sG8mRY22LsXlPgiwTNs9dbiLrNuA7f3ZMAJ2w%3D " )
2022-02-24 12:58:59 +04:00
return Group {
UserAddress ( )
. environmentObject ( chatModel )
UserAddress ( )
. environmentObject ( ChatModel ( ) )
}
2022-01-31 21:28:07 +00:00
}
}