2021-02-05 10:53:00 +01:00
//
// M a s t o d o n R e g i s t e r V i e w M o d e l . s w i f t
// M a s t o d o n
//
// C r e a t e d b y M a i n a s u K C i r n o o n 2 0 2 1 - 2 - 5 .
//
import Combine
2021-02-20 06:55:06 +01:00
import Foundation
2021-02-05 10:53:00 +01:00
import MastodonSDK
2021-02-20 06:55:06 +01:00
import UIKit
2021-02-05 10:53:00 +01:00
final class MastodonRegisterViewModel {
2021-02-20 10:13:16 +01:00
var disposeBag = Set < AnyCancellable > ( )
2021-02-05 10:53:00 +01:00
// i n p u t
let domain : String
let applicationToken : Mastodon . Entity . Token
let isRegistering = CurrentValueSubject < Bool , Never > ( false )
2021-02-20 10:13:16 +01:00
let username = CurrentValueSubject < String ? , Never > ( nil )
2021-02-05 10:53:00 +01:00
// o u t p u t
let applicationAuthorization : Mastodon . API . OAuth . Authorization
2021-02-20 10:13:16 +01:00
let isUsernameValid = CurrentValueSubject < Bool ? , Never > ( nil )
2021-02-05 10:53:00 +01:00
let error = CurrentValueSubject < Error ? , Never > ( nil )
init ( domain : String , applicationToken : Mastodon . Entity . Token ) {
self . domain = domain
self . applicationToken = applicationToken
self . applicationAuthorization = Mastodon . API . OAuth . Authorization ( accessToken : applicationToken . accessToken )
2021-02-20 10:13:16 +01:00
username
. map { username in
guard let username = username else {
return nil
}
return ! username . isEmpty
}
. assign ( to : \ . value , on : isUsernameValid )
. store ( in : & disposeBag )
2021-02-05 10:53:00 +01:00
}
2021-02-20 06:55:06 +01:00
}
extension MastodonRegisterViewModel {
func isValidEmail ( _ email : String ) -> Bool {
let emailRegEx = " [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+ \\ .[A-Za-z]{2,64} "
let emailPred = NSPredicate ( format : " SELF MATCHES %@ " , emailRegEx )
return emailPred . evaluate ( with : email )
}
2021-02-05 10:53:00 +01:00
2021-02-20 06:55:06 +01:00
func validatePassword ( text : String ) -> ( Bool , Bool , Bool ) {
let trimmedText = text . trimmingCharacters ( in : . whitespaces )
let isEightCharacters = trimmedText . count >= 8
let isOneNumber = trimmedText . range ( of : " .*[0-9] " , options : . regularExpression ) != nil
let isOneSpecialCharacter = trimmedText . trimmingCharacters ( in : . decimalDigits ) . trimmingCharacters ( in : . letters ) . count > 0
return ( isEightCharacters , isOneNumber , isOneSpecialCharacter )
}
func attributeStringForUsername ( ) -> NSAttributedString {
let resultAttributeString = NSMutableAttributedString ( )
let redImage = NSTextAttachment ( )
let font = UIFont . preferredFont ( forTextStyle : . caption1 )
let configuration = UIImage . SymbolConfiguration ( font : font )
redImage . image = UIImage ( systemName : " xmark.octagon.fill " , withConfiguration : configuration ) ? . withTintColor ( Asset . Colors . lightDangerRed . color )
let imageAttribute = NSAttributedString ( attachment : redImage )
let stringAttribute = NSAttributedString ( string : " This username is taken. " , attributes : [ NSAttributedString . Key . font : font , NSAttributedString . Key . foregroundColor : Asset . Colors . lightDangerRed . color ] )
resultAttributeString . append ( imageAttribute )
resultAttributeString . append ( stringAttribute )
return resultAttributeString
}
func attributeStringForPassword ( eightCharacters : Bool = false , oneNumber : Bool = false , oneSpecialCharacter : Bool = false ) -> NSAttributedString {
let font = UIFont . preferredFont ( forTextStyle : . caption1 )
let color = UIColor . black
let falseColor = UIColor . clear
let attributeString = NSMutableAttributedString ( )
let start = NSAttributedString ( string : " Your password needs at least: \n " , attributes : [ NSAttributedString . Key . font : font , NSAttributedString . Key . foregroundColor : color ] )
attributeString . append ( start )
attributeString . append ( checkImage ( color : eightCharacters ? color : falseColor ) )
let eightCharactersDescription = NSAttributedString ( string : " Eight characters \n " , attributes : [ NSAttributedString . Key . font : font , NSAttributedString . Key . foregroundColor : color ] )
attributeString . append ( eightCharactersDescription )
attributeString . append ( checkImage ( color : oneNumber ? color : falseColor ) )
let oneNumberDescription = NSAttributedString ( string : " One number \n " , attributes : [ NSAttributedString . Key . font : font , NSAttributedString . Key . foregroundColor : color ] )
attributeString . append ( oneNumberDescription )
attributeString . append ( checkImage ( color : oneSpecialCharacter ? color : falseColor ) )
let oneSpecialCharacterDescription = NSAttributedString ( string : " One special character \n " , attributes : [ NSAttributedString . Key . font : font , NSAttributedString . Key . foregroundColor : color ] )
attributeString . append ( oneSpecialCharacterDescription )
return attributeString
}
func checkImage ( color : UIColor ) -> NSAttributedString {
let checkImage = NSTextAttachment ( )
let font = UIFont . preferredFont ( forTextStyle : . caption1 )
let configuration = UIImage . SymbolConfiguration ( font : font )
checkImage . image = UIImage ( systemName : " checkmark.circle.fill " , withConfiguration : configuration ) ? . withTintColor ( color )
return NSAttributedString ( attachment : checkImage )
}
2021-02-05 10:53:00 +01:00
}