Skip to content

Non tc support #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions ios/Classes/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,23 @@ struct Constants {
struct Error {
static let methodNotImplemented = "Method not implemented"
}

struct Arguments {
static let firstName = "fname"
static let lastName = "lname"
static let otp = "otp"
static let phone = "ph"
static let countryCode = "ci"
}

struct CountryCode {
static let india = "IN"
}

struct ErrorDescription {
static let unableToParseArguments = "Unable to parse arguments"
static let phoneNumberEmpty = "Phone number cannot be empty"
static let firstNameEmpty = "Firstname should not be empty"
static let otpEmpty = "OTP should not be empty"
}
}
2 changes: 1 addition & 1 deletion ios/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension TCTrueProfile {
extension TCError {
var toDict: [String: AnyHashable] {
var dict = [String: AnyHashable]()
dict["code"] = getCode()
dict["code"] = getCode().rawValue
dict["message"] = description
return dict
}
Expand Down
97 changes: 94 additions & 3 deletions ios/Classes/SwiftTruecallerSdkPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ private enum MethodCalls: String {
case verifyMissedCall
}

//Disclaimer should be manually shown while using iOS flutter SDK.
public class Controller: UIViewController, TCTrueSDKViewDelegate { }

public class SwiftTruecallerSdkPlugin: NSObject,
FlutterPlugin {
private var mainChannel: FlutterMethodChannel?
private var eventChannel: FlutterEventChannel?
private var eventSink: FlutterEventSink?

private var controller = Controller()

private var trueSdk = TCTrueSDK.sharedManager()

public static func register(with registrar: FlutterPluginRegistrar) {
Expand Down Expand Up @@ -59,21 +64,90 @@ public class SwiftTruecallerSdkPlugin: NSObject,
switch method {
case .initiateSDK:
trueSdk.delegate = self
trueSdk.viewDelegate = controller
result(true)
case .isUsable:
result(trueSdk.isSupported())
case .requestVerification:
requestVerification(call: call,
result: result)
result(true)
case .verifyOtp:
verifyOtpAndName(call: call,
result: result)
case .setDarkTheme,
.setLocale,
.requestVerification,
.verifyOtp,
.verifyMissedCall:
result(Constants.Error.methodNotImplemented)
result(true)
case .getProfile:
trueSdk.requestTrueProfile()
result(true)
case .none:
result(Constants.Error.methodNotImplemented)
}
}

private func requestVerification(call: FlutterMethodCall,
result: @escaping FlutterResult) {
switch getVerificationArguments(from: call) {
case .success(let arguments):
trueSdk.requestVerification(forPhone: arguments.phoneNumber,
countryCode: arguments.isoCountryCode)
case .failure(let error):
result(error)
}
}

private typealias VerificationArguments = (phoneNumber: String, isoCountryCode: String)

private func getVerificationArguments(from call: FlutterMethodCall) -> Result<VerificationArguments, Error> {
guard let arguments = call.arguments as? [String: Any] else {
return .failure(TCError(code: TCTrueSDKErrorCode.badRequest,
description: Constants.ErrorDescription.unableToParseArguments))
}
guard let phone = arguments[Constants.Arguments.phone] as? String else {
return .failure(TCError(code: TCTrueSDKErrorCode.badRequest,
description: Constants.ErrorDescription.phoneNumberEmpty))
}

let countryCode = arguments[Constants.Arguments.countryCode] as? String
?? Constants.CountryCode.india
return .success((phone,countryCode))
}

private func verifyOtpAndName(call: FlutterMethodCall,
result: @escaping FlutterResult) {
switch getCodeAndNames(from: call) {
case .success(let arguments):
trueSdk.verifySecurityCode(arguments.code,
andUpdateFirstname: arguments.firstName,
lastName: arguments.lastname)
case .failure(let error):
result(error)
}
}

private typealias CodeAndName = (firstName: String, lastname: String, code: String)

private func getCodeAndNames(from call: FlutterMethodCall) -> Result<CodeAndName, Error> {
guard let arguments = call.arguments as? [String: Any] else {
return .failure(TCError(code: TCTrueSDKErrorCode.badRequest,
description: Constants.ErrorDescription.unableToParseArguments))
}
guard let code = arguments[Constants.Arguments.otp] as? String else {
return .failure(TCError(code: TCTrueSDKErrorCode.badRequest,
description: Constants.ErrorDescription.otpEmpty))
}

guard let firstName = arguments[Constants.Arguments.firstName] as? String else {
return .failure(TCError(code: TCTrueSDKErrorCode.badRequest,
description: Constants.ErrorDescription.firstNameEmpty))
}

let lastName = arguments[Constants.Arguments.lastName] as? String ?? ""
return .success((firstName, lastName, code))
}
}

// MARK: - App delegate methods -
Expand Down Expand Up @@ -124,6 +198,23 @@ extension SwiftTruecallerSdkPlugin: TCTrueSDKDelegate {
eventSink?(map)
}

public func verificationStatusChanged(to verificationState: TCVerificationState) {
var map = [String: Any]()
map[Constants.String.result] = verificationState.rawValue
switch verificationState {
case .otpInitiated:
map[Constants.String.data] = trueSdk.tokenTtl()
case .otpReceived,
Copy link
Contributor

@shubhral shubhral Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see in case of otpReceived, we are not doing anything here. In Android, we pass the OTP to the callback, so shouldn't there be a similar behaviour?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, in case of verifiedBefore, we must pass the profile back as the call back data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of iOS we cannot know if the otp is received or not.
In cas2 of verified before, we will have to tweak the documentation a little bit, so that the app waits for the profile call back, similar to in native sdk @shubhral

.verifiedBefore:
break
case .verificationComplete:
map[Constants.String.data] = trueSdk.accessTokenForOTPVerification
@unknown default:
break
}
eventSink?(map)
}

public func didReceive(_ profileResponse: TCTrueProfileResponse) {
trueProfileResponse = profileResponse
}
Expand All @@ -133,6 +224,6 @@ extension SwiftTruecallerSdkPlugin: TCTrueSDKDelegate {
map[Constants.String.result] = Constants.String.failure
map[Constants.String.data] = error.toDict.tojsonString
trueProfileResponse = nil
eventSink?(error)
eventSink?(map)
}
}