2020-12-03 06:19:15 +01:00
//
// A d d N e w s B l u r A c c o u n t V i e w . s w i f t
// M u l t i p l a t f o r m m a c O S
//
// C r e a t e d b y S t u a r t B r e c k e n r i d g e o n 0 3 / 1 2 / 2 0 2 0 .
// C o p y r i g h t © 2 0 2 0 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
import SwiftUI
import Account
2020-12-05 09:46:03 +01:00
import RSCore
import RSWeb
import Secrets
2020-12-03 06:19:15 +01:00
struct AddNewsBlurAccountView : View {
@ Environment ( \ . presentationMode ) var presentationMode
2020-12-05 09:46:03 +01:00
@ StateObject private var model = AddNewsBlurViewModel ( )
2020-12-03 06:19:15 +01:00
var body : some View {
2020-12-05 15:18:10 +01:00
#if os ( macOS )
macBody
#else
2020-12-07 12:22:35 +01:00
NavigationView {
iosBody
}
2020-12-05 15:18:10 +01:00
#endif
}
#if os ( iOS )
var iosBody : some View {
List {
2020-12-07 12:22:35 +01:00
Section ( header : formHeader , content : {
TextField ( " Email " , text : $ model . username )
2020-12-06 01:00:34 +01:00
if model . showPassword = = false {
ZStack {
HStack {
SecureField ( " Password " , text : $ model . password )
Spacer ( )
Image ( systemName : " eye.fill " )
. foregroundColor ( . accentColor )
. onTapGesture {
model . showPassword = true
}
}
}
}
else {
ZStack {
HStack {
TextField ( " Password " , text : $ model . password )
Spacer ( )
Image ( systemName : " eye.slash.fill " )
. foregroundColor ( . accentColor )
. onTapGesture {
model . showPassword = false
}
}
}
}
2020-12-07 12:22:35 +01:00
2020-12-05 15:18:10 +01:00
} )
2020-12-07 12:22:35 +01:00
Section ( footer : formFooter , content : {
Button ( action : {
model . authenticateNewsBlur ( )
} , label : {
HStack {
Spacer ( )
2020-12-07 12:52:35 +01:00
Text ( " Add Account " )
2020-12-07 12:22:35 +01:00
Spacer ( )
}
} ) . disabled ( model . username . isEmpty || model . password . isEmpty )
2020-12-05 15:18:10 +01:00
} )
2020-12-07 12:22:35 +01:00
}
. navigationBarItems ( leading :
2020-12-05 15:18:10 +01:00
Button ( action : {
2020-12-07 12:22:35 +01:00
presentationMode . wrappedValue . dismiss ( )
2020-12-05 15:18:10 +01:00
} , label : {
2020-12-07 12:22:35 +01:00
Text ( " Cancel " )
} ) )
. listStyle ( InsetGroupedListStyle ( ) )
. navigationBarTitleDisplayMode ( . inline )
. navigationTitle ( Text ( " NewsBlur " ) )
. alert ( isPresented : $ model . showError , content : {
Alert ( title : Text ( " Sign In Error " ) , message : Text ( model . accountUpdateError . description ) , dismissButton : . cancel ( Text ( " Dismiss " ) ) )
} )
. onReceive ( model . $ canDismiss , perform : { value in
if value = = true {
presentationMode . wrappedValue . dismiss ( )
}
} )
2020-12-05 15:18:10 +01:00
}
#endif
#if os ( macOS )
var macBody : some View {
2020-12-03 06:19:15 +01:00
VStack {
HStack ( spacing : 16 ) {
VStack ( alignment : . leading ) {
AccountType . newsBlur . image ( )
. frame ( width : 50 , height : 50 )
Spacer ( )
}
VStack ( alignment : . leading , spacing : 8 ) {
Text ( " Sign in to your NewsBlur account. " )
. font ( . headline )
HStack {
2021-06-01 01:01:01 +02:00
Text ( " Don’ t have a NewsBlur account? " )
2020-12-03 06:19:15 +01:00
. font ( . callout )
Button ( action : {
2020-12-06 00:58:20 +01:00
model . presentSignUpOption ( . newsBlur )
2020-12-03 06:19:15 +01:00
} , label : {
Text ( " Sign up here. " ) . font ( . callout )
} ) . buttonStyle ( LinkButtonStyle ( ) )
}
HStack {
VStack ( alignment : . trailing , spacing : 14 ) {
Text ( " Email " )
Text ( " Password " )
}
VStack ( spacing : 8 ) {
2020-12-05 09:46:03 +01:00
TextField ( " me@email.com " , text : $ model . username )
SecureField ( " ••••••••••• " , text : $ model . password )
2020-12-03 06:19:15 +01:00
}
}
Text ( " Your username and password will be encrypted and stored in Keychain. " )
. foregroundColor ( . secondary )
. font ( . callout )
. lineLimit ( 2 )
. padding ( . top , 4 )
Spacer ( )
HStack ( spacing : 8 ) {
Spacer ( )
2020-12-05 09:46:03 +01:00
ProgressView ( )
. scaleEffect ( CGSize ( width : 0.5 , height : 0.5 ) )
. hidden ( ! model . isAuthenticating )
2020-12-03 06:19:15 +01:00
Button ( action : {
presentationMode . wrappedValue . dismiss ( )
} , label : {
Text ( " Cancel " )
. frame ( width : 60 )
} ) . keyboardShortcut ( . cancelAction )
Button ( action : {
2020-12-05 15:18:10 +01:00
model . authenticateNewsBlur ( )
2020-12-03 06:19:15 +01:00
} , label : {
2020-12-05 14:09:34 +01:00
Text ( " Sign In " )
2020-12-03 06:19:15 +01:00
. frame ( width : 60 )
} )
. keyboardShortcut ( . defaultAction )
2020-12-05 09:46:03 +01:00
. disabled ( model . username . isEmpty || model . password . isEmpty )
2020-12-03 06:19:15 +01:00
}
}
}
}
. padding ( )
2020-12-05 14:09:34 +01:00
. frame ( minWidth : 400 , maxWidth : 400 , minHeight : 230 , maxHeight : 260 )
2020-12-03 06:19:15 +01:00
. textFieldStyle ( RoundedBorderTextFieldStyle ( ) )
2020-12-05 14:09:34 +01:00
. alert ( isPresented : $ model . showError , content : {
Alert ( title : Text ( " Sign In Error " ) , message : Text ( model . accountUpdateError . description ) , dismissButton : . cancel ( ) )
} )
2020-12-05 15:18:10 +01:00
. onReceive ( model . $ canDismiss , perform : { value in
if value = = true {
presentationMode . wrappedValue . dismiss ( )
}
} )
}
#endif
2020-12-05 09:46:03 +01:00
2020-12-05 15:18:10 +01:00
var formHeader : some View {
HStack {
2020-12-07 12:22:35 +01:00
Spacer ( )
2020-12-05 15:18:10 +01:00
VStack ( alignment : . center ) {
AccountType . newsBlur . image ( )
. resizable ( )
. frame ( width : 50 , height : 50 )
2020-12-05 09:46:03 +01:00
}
2020-12-07 12:22:35 +01:00
Spacer ( )
} . padding ( . vertical )
}
var formFooter : some View {
HStack {
Spacer ( )
VStack ( spacing : 8 ) {
2021-06-01 00:52:56 +02:00
Text ( " Sign in to your NewsBlur account and sync your feeds across your devices. Your username and password and password will be encrypted and stored in Keychain. " ) . foregroundColor ( . secondary )
2021-06-01 01:01:01 +02:00
Text ( " Don’ t have a NewsBlur account? " ) . foregroundColor ( . secondary )
2020-12-07 12:22:35 +01:00
Button ( action : {
model . presentSignUpOption ( . newsBlur )
} , label : {
Text ( " Sign Up Here " ) . foregroundColor ( . blue ) . multilineTextAlignment ( . center )
} )
ProgressView ( ) . hidden ( ! model . isAuthenticating )
}
. multilineTextAlignment ( . center )
. font ( . caption2 )
Spacer ( )
} . padding ( . vertical )
2020-12-05 09:46:03 +01:00
}
2020-12-03 06:19:15 +01:00
}
struct AddNewsBlurAccountView_Previews : PreviewProvider {
static var previews : some View {
AddNewsBlurAccountView ( )
}
}