Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F14171354
AccountModel.swift
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
AccountModel.swift
View Options
import
Foundation
import
WriteFreely
enum
AccountError
:
Error
{
case
invalidPassword
case
usernameNotFound
case
serverNotFound
}
extension
AccountError
:
LocalizedError
{
public
var
errorDescription
:
String
?
{
switch
self
{
case
.
serverNotFound
:
return
NSLocalizedString
(
"The server could not be found. Please check the information you've entered and try again."
,
comment
:
""
)
case
.
invalidPassword
:
return
NSLocalizedString
(
"Invalid password. Please check that you've entered your password correctly and try logging in again."
,
comment
:
""
)
case
.
usernameNotFound
:
return
NSLocalizedString
(
"Username not found. Did you use your email address by mistake?"
,
comment
:
""
)
}
}
}
struct
AccountModel
{
private
let
defaults
=
UserDefaults
.
standard
let
isLoggedInFlag
=
"isLoggedInFlag"
let
usernameStringKey
=
"usernameStringKey"
let
serverStringKey
=
"serverStringKey"
var
server
:
String
=
""
var
username
:
String
=
""
private
(
set
)
var
user
:
WFUser
?
private
(
set
)
var
isLoggedIn
:
Bool
=
false
mutating
func
login
(
_
user
:
WFUser
)
{
self
.
user
=
user
self
.
username
=
user
.
username
??
""
self
.
isLoggedIn
=
true
defaults
.
set
(
true
,
forKey
:
isLoggedInFlag
)
defaults
.
set
(
user
.
username
,
forKey
:
usernameStringKey
)
defaults
.
set
(
server
,
forKey
:
serverStringKey
)
}
mutating
func
logout
()
{
self
.
user
=
nil
self
.
isLoggedIn
=
false
defaults
.
set
(
false
,
forKey
:
isLoggedInFlag
)
defaults
.
removeObject
(
forKey
:
usernameStringKey
)
defaults
.
removeObject
(
forKey
:
serverStringKey
)
}
mutating
func
restoreState
()
{
isLoggedIn
=
defaults
.
bool
(
forKey
:
isLoggedInFlag
)
server
=
defaults
.
string
(
forKey
:
serverStringKey
)
??
""
username
=
defaults
.
string
(
forKey
:
usernameStringKey
)
??
""
}
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Thu, Apr 2, 2:11 AM (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3673272
Attached To
rWFSUI WriteFreely SwiftUI
Event Timeline
Log In to Comment