@@ -8,17 +8,57 @@ enum IntentType: String {
88 case holding = " HOLDINGS_IMPORT "
99 case fetchFunds = " FETCH_FUNDS "
1010 case sipSetup = " SIP_SETUP "
11+ case imrSetup = " IMR_SETUP "
1112 case authoriseHoldings = " AUTHORISE_HOLDINGS "
1213}
1314
15+ @MainActor
1416public class SwiftScgatewayFlutterPlugin : NSObject , FlutterPlugin , FlutterStreamHandler {
1517
16- @MainActor
17- let currentViewController : UIViewController = ( UIApplication . shared. delegate? . window?? . rootViewController) !
18-
1918 private var eventSink : FlutterEventSink ?
2019 private var notificationObserver : NSObjectProtocol ?
2120
21+ var currentViewController : UIViewController {
22+ let foregroundScene = UIApplication . shared. connectedScenes
23+ . filter ( { $0. activationState == . foregroundActive } )
24+ . compactMap ( { $0 as? UIWindowScene } )
25+ . first
26+
27+ if let scene = foregroundScene {
28+ let keyWindow : UIWindow ?
29+ if #available( iOS 15 . 0 , * ) {
30+ keyWindow = scene. keyWindow
31+ } else {
32+ keyWindow = scene. windows. first ( where: { $0. isKeyWindow } )
33+ }
34+ if let rootVC = keyWindow? . rootViewController {
35+ return rootVC
36+ }
37+ }
38+
39+ // Any connected scene fallback (covers edge cases where no scene is .foregroundActive yet)
40+ if let windowScene = UIApplication . shared. connectedScenes
41+ . compactMap ( { $0 as? UIWindowScene } )
42+ . first {
43+ let window : UIWindow ?
44+ if #available( iOS 15 . 0 , * ) {
45+ window = windowScene. keyWindow ?? windowScene. windows. first
46+ } else {
47+ window = windowScene. windows. first ( where: { $0. isKeyWindow } ) ?? windowScene. windows. first
48+ }
49+ if let rootVC = window? . rootViewController {
50+ return rootVC
51+ }
52+ }
53+
54+ // Fallback (AppDelegate-based window)
55+ if let rootVC = UIApplication . shared. delegate? . window?? . rootViewController {
56+ return rootVC
57+ }
58+
59+ fatalError ( " SwiftScgatewayFlutterPlugin: No root view controller found. Ensure a UIWindow is set up in SceneDelegate or AppDelegate. " )
60+ }
61+
2262 public static func register( with registrar: FlutterPluginRegistrar ) {
2363 let channel = FlutterMethodChannel ( name: " scgateway_flutter_plugin " , binaryMessenger: registrar. messenger ( ) )
2464 let instance = SwiftScgatewayFlutterPlugin ( )
@@ -342,6 +382,40 @@ public class SwiftScgatewayFlutterPlugin: NSObject, FlutterPlugin, FlutterStream
342382 /// Catch exception
343383 }
344384
385+ // MARK: IMR_SETUP
386+ case . imrSetup( let smallcaseAuthToken, let imrAction, let transactionId, let signup) :
387+
388+ do {
389+ let jsonEncoder = JSONEncoder ( )
390+ let jsonData = try jsonEncoder. encode ( imrAction)
391+
392+ let data = try ? JSONSerialization . jsonObject ( with: jsonData, options: [ ] )
393+
394+ if let imrResponse = data as? [ String : Any ] {
395+
396+ print ( " IMR_SETUP response: \( imrResponse) " )
397+
398+ var resDict : [ String : Any ] = [ : ]
399+
400+ resDict [ " success " ] = true
401+ resDict [ " data " ] = imrResponse
402+ resDict [ " smallcaseAuthToken " ] = smallcaseAuthToken
403+ resDict [ " transaction " ] = " IMR_SETUP "
404+ resDict [ " transactionId " ] = transactionId
405+ resDict [ " signup " ] = signup
406+
407+ let jsonData = try JSONSerialization . data ( withJSONObject: resDict, options: [ ] )
408+ let jsonString = String ( data: jsonData, encoding: . utf8)
409+
410+ result ( jsonString)
411+
412+ return
413+ }
414+
415+ } catch {
416+ result ( FlutterError ( code: " IMR_SETUP_ERROR " , message: error. localizedDescription, details: nil ) )
417+ }
418+
345419 default :
346420 return
347421 }
0 commit comments