react-native-spike-sdk 4.3.43 → 4.3.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/android/build.gradle +2 -2
  2. package/android/src/main/java/com/spikesdk/SpikeSdkModule.kt +454 -456
  3. package/lib/commonjs/index.js +7 -0
  4. package/lib/commonjs/index.js.map +1 -1
  5. package/lib/commonjs/v3/DataModels/MetricType.js +3 -3
  6. package/lib/commonjs/v3/DataModels/Provider.js +1 -0
  7. package/lib/commonjs/v3/DataModels/Provider.js.map +1 -1
  8. package/lib/commonjs/v3/DataModels/ProviderSource.js +1 -0
  9. package/lib/commonjs/v3/DataModels/ProviderSource.js.map +1 -1
  10. package/lib/commonjs/v3/DataModels/SamsungHealthDataAvailability.js +6 -0
  11. package/lib/commonjs/v3/DataModels/SamsungHealthDataAvailability.js.map +1 -0
  12. package/lib/commonjs/v3/DataModels/SamsungHealthDataAvailabilityStatus.js +37 -0
  13. package/lib/commonjs/v3/DataModels/SamsungHealthDataAvailabilityStatus.js.map +1 -0
  14. package/lib/commonjs/v3/SpikeRNConnectionAPIv3.js +111 -1
  15. package/lib/commonjs/v3/SpikeRNConnectionAPIv3.js.map +1 -1
  16. package/lib/commonjs/version.js +1 -1
  17. package/lib/module/index.js +2 -1
  18. package/lib/module/index.js.map +1 -1
  19. package/lib/module/v3/DataModels/MetricType.js +3 -3
  20. package/lib/module/v3/DataModels/Provider.js +1 -0
  21. package/lib/module/v3/DataModels/Provider.js.map +1 -1
  22. package/lib/module/v3/DataModels/ProviderSource.js +1 -0
  23. package/lib/module/v3/DataModels/ProviderSource.js.map +1 -1
  24. package/lib/module/v3/DataModels/SamsungHealthDataAvailability.js +4 -0
  25. package/lib/module/v3/DataModels/SamsungHealthDataAvailability.js.map +1 -0
  26. package/lib/module/v3/DataModels/SamsungHealthDataAvailabilityStatus.js +33 -0
  27. package/lib/module/v3/DataModels/SamsungHealthDataAvailabilityStatus.js.map +1 -0
  28. package/lib/module/v3/SpikeRNConnectionAPIv3.js +111 -1
  29. package/lib/module/v3/SpikeRNConnectionAPIv3.js.map +1 -1
  30. package/lib/module/version.js +1 -1
  31. package/lib/typescript/index.d.ts +4 -2
  32. package/lib/typescript/index.d.ts.map +1 -1
  33. package/lib/typescript/v3/DataModels/BackgroundDeliveryConfig.d.ts +2 -0
  34. package/lib/typescript/v3/DataModels/BackgroundDeliveryConfig.d.ts.map +1 -1
  35. package/lib/typescript/v3/DataModels/MetricType.d.ts +3 -3
  36. package/lib/typescript/v3/DataModels/MetricType.d.ts.map +1 -1
  37. package/lib/typescript/v3/DataModels/Provider.d.ts +1 -0
  38. package/lib/typescript/v3/DataModels/Provider.d.ts.map +1 -1
  39. package/lib/typescript/v3/DataModels/ProviderSource.d.ts +1 -0
  40. package/lib/typescript/v3/DataModels/ProviderSource.d.ts.map +1 -1
  41. package/lib/typescript/v3/DataModels/SamsungHealthDataAvailability.d.ts +7 -0
  42. package/lib/typescript/v3/DataModels/SamsungHealthDataAvailability.d.ts.map +1 -0
  43. package/lib/typescript/v3/DataModels/SamsungHealthDataAvailabilityStatus.d.ts +28 -0
  44. package/lib/typescript/v3/DataModels/SamsungHealthDataAvailabilityStatus.d.ts.map +1 -0
  45. package/lib/typescript/v3/SpikeRNConnectionAPIv3.d.ts +57 -1
  46. package/lib/typescript/v3/SpikeRNConnectionAPIv3.d.ts.map +1 -1
  47. package/lib/typescript/version.d.ts +1 -1
  48. package/package.json +1 -1
  49. package/src/index.ts +4 -0
  50. package/src/v3/DataModels/BackgroundDeliveryConfig.ts +2 -0
  51. package/src/v3/DataModels/MetricType.ts +3 -3
  52. package/src/v3/DataModels/Provider.ts +1 -0
  53. package/src/v3/DataModels/ProviderSource.ts +1 -0
  54. package/src/v3/DataModels/SamsungHealthDataAvailability.ts +9 -0
  55. package/src/v3/DataModels/SamsungHealthDataAvailabilityStatus.ts +34 -0
  56. package/src/v3/SpikeRNConnectionAPIv3.ts +127 -1
  57. package/src/version.ts +1 -1
@@ -597,21 +597,16 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
597
597
  customerEndUserId: String,
598
598
  promise: Promise
599
599
  ) {
600
- scope.launch {
601
- try {
602
- val connection = SpikeConnectionAPIv3.createConnectionLegacy(
603
- appId = appId,
604
- authToken = authToken,
605
- customerEndUserId = customerEndUserId,
606
- context = reactApplicationContext
607
- )
608
-
609
- addConnectionV3(connection, uuid)
610
- promise.resolve(uuid)
600
+ executeV3Method(promise) {
601
+ val connection = SpikeConnectionAPIv3.createConnectionLegacy(
602
+ appId = appId,
603
+ authToken = authToken,
604
+ customerEndUserId = customerEndUserId,
605
+ context = reactApplicationContext
606
+ )
611
607
 
612
- } catch (e: SpikeExceptions) {
613
- promise.reject(e.mapException(), e.message)
614
- }
608
+ addConnectionV3(connection, uuid)
609
+ promise.resolve(uuid)
615
610
  }
616
611
  }
617
612
 
@@ -623,21 +618,16 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
623
618
  endUserId: String,
624
619
  promise: Promise
625
620
  ) {
626
- scope.launch {
627
- try {
628
- val connection = SpikeConnectionAPIv3.createConnection(
629
- applicationId = applicationId,
630
- signature = signature,
631
- endUserId = endUserId,
632
- context = reactApplicationContext
633
- )
634
-
635
- addConnectionV3(connection, uuid)
636
- promise.resolve(uuid)
621
+ executeV3Method(promise) {
622
+ val connection = SpikeConnectionAPIv3.createConnection(
623
+ applicationId = applicationId,
624
+ signature = signature,
625
+ endUserId = endUserId,
626
+ context = reactApplicationContext
627
+ )
637
628
 
638
- } catch (e: SpikeExceptions) {
639
- promise.reject(e.mapException(), e.message)
640
- }
629
+ addConnectionV3(connection, uuid)
630
+ promise.resolve(uuid)
641
631
  }
642
632
  }
643
633
 
@@ -648,20 +638,9 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
648
638
  connectionUUID: String,
649
639
  promise: Promise
650
640
  ) {
651
- scope.launch {
652
- try {
653
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
654
- SpikeExceptions.SpikeException("Connection not found").mapException(),
655
- "Connection not found (getUserInfo)"
656
- )
657
-
658
- val result = connection.getUserInfo()
659
-
660
- promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
661
-
662
- } catch (e: SpikeExceptions) {
663
- promise.reject(e.mapException(), e.message)
664
- }
641
+ executeV3MethodWithConnection("getUserInfo", connectionUUID, promise) { connection ->
642
+ val result = connection.getUserInfo()
643
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
665
644
  }
666
645
  }
667
646
 
@@ -674,26 +653,15 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
674
653
  config: String?,
675
654
  promise: Promise
676
655
  ) {
677
- scope.launch {
678
- try {
679
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
680
- SpikeExceptions.SpikeException("Connection not found").mapException(),
681
- "Connection not found (getIntegrationInitUrl)"
682
- )
683
-
684
- val providerObject = Provider.values().find { it.value == provider } ?: Provider.UNKNOWN
685
- val configObject = if (config != null) {
686
- IntegrationInitConfigUtils.fromValue(config)
687
- } else {
688
- null
689
- }
690
- val result = connection.getIntegrationInitUrl(providerObject, configObject)
691
-
692
- promise.resolve(result)
693
-
694
- } catch (e: SpikeExceptions) {
695
- promise.reject(e.mapException(), e.message)
656
+ executeV3MethodWithConnection("getIntegrationInitUrl", connectionUUID, promise) { connection ->
657
+ val providerObject = Provider.values().find { it.value == provider } ?: Provider.UNKNOWN
658
+ val configObject = if (config != null) {
659
+ IntegrationInitConfigUtils.fromValue(config)
660
+ } else {
661
+ null
696
662
  }
663
+ val result = connection.getIntegrationInitUrl(providerObject, configObject)
664
+ promise.resolve(result)
697
665
  }
698
666
  }
699
667
 
@@ -709,33 +677,24 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
709
677
  filter: String?, // StatisticsFilter?
710
678
  promise: Promise
711
679
  ) {
712
- scope.launch {
713
- try {
714
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
715
- SpikeExceptions.SpikeException("Connection not found").mapException(),
716
- "Connection not found (getStatistics)"
717
- )
718
- val statTypes = types.toArrayList().mapNotNull {
719
- StatisticsTypeUtils.fromValue(it as String)
720
- }.toSet()
721
- val dateFrom = convertIsoStringToInstant(from)
722
- val dateTo = convertIsoStringToInstant(to)
723
- val statsInterval = StatisticsIntervalUtils.fromValue(interval)
724
- val statsFilter = StatisticsFilter.fromValue(filter)
725
-
726
- val result = connection.getStatistics(
727
- types = statTypes,
728
- from = dateFrom,
729
- to = dateTo,
730
- interval = statsInterval,
731
- filter = statsFilter
732
- )
733
-
734
- promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
680
+ executeV3MethodWithConnection("getStatistics", connectionUUID, promise) { connection ->
681
+ val statTypes = types.toArrayList().mapNotNull {
682
+ StatisticsTypeUtils.fromValue(it as String)
683
+ }.toSet()
684
+ val dateFrom = convertIsoStringToInstant(from)
685
+ val dateTo = convertIsoStringToInstant(to)
686
+ val statsInterval = StatisticsIntervalUtils.fromValue(interval)
687
+ val statsFilter = StatisticsFilter.fromValue(filter)
688
+
689
+ val result = connection.getStatistics(
690
+ types = statTypes,
691
+ from = dateFrom,
692
+ to = dateTo,
693
+ interval = statsInterval,
694
+ filter = statsFilter
695
+ )
735
696
 
736
- } catch (e: SpikeExceptions) {
737
- promise.reject(e.mapException(), e.message)
738
- }
697
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
739
698
  }
740
699
  }
741
700
 
@@ -748,31 +707,22 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
748
707
  filter: String?, // StatisticsFilter?
749
708
  promise: Promise
750
709
  ) {
751
- scope.launch {
752
- try {
753
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
754
- SpikeExceptions.SpikeException("Connection not found").mapException(),
755
- "Connection not found (getRecords)"
756
- )
757
- val statTypes = types.toArrayList().mapNotNull {
758
- MetricTypeUtils.fromValue(it as String)
759
- }.toSet()
760
- val dateFrom = convertIsoStringToInstant(from)
761
- val dateTo = convertIsoStringToInstant(to)
762
- val statsFilter = StatisticsFilter.fromValue(filter)
763
-
764
- val result = connection.getRecords(
765
- types = statTypes,
766
- from = dateFrom,
767
- to = dateTo,
768
- filter = statsFilter
769
- )
710
+ executeV3MethodWithConnection("getRecords", connectionUUID, promise) { connection ->
711
+ val statTypes = types.toArrayList().mapNotNull {
712
+ MetricTypeUtils.fromValue(it as String)
713
+ }.toSet()
714
+ val dateFrom = convertIsoStringToInstant(from)
715
+ val dateTo = convertIsoStringToInstant(to)
716
+ val statsFilter = StatisticsFilter.fromValue(filter)
717
+
718
+ val result = connection.getRecords(
719
+ types = statTypes,
720
+ from = dateFrom,
721
+ to = dateTo,
722
+ filter = statsFilter
723
+ )
770
724
 
771
- promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
772
-
773
- } catch (e: SpikeExceptions) {
774
- promise.reject(e.mapException(), e.message)
775
- }
725
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
776
726
  }
777
727
  }
778
728
 
@@ -785,29 +735,20 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
785
735
  filter: String?, // StatisticsFilter?
786
736
  promise: Promise
787
737
  ) {
788
- scope.launch {
789
- try {
790
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
791
- SpikeExceptions.SpikeException("Connection not found").mapException(),
792
- "Connection not found (getActivities)"
793
- )
794
- val dateFrom = convertIsoStringToInstant(from)
795
- val dateTo = convertIsoStringToInstant(to)
796
- val statsFilter = StatisticsFilter.fromValue(filter)
797
- val configObject = ActivityConfigUtils.fromValue(configJson) ?: ActivityConfig()
798
-
799
- val result = connection.getActivities(
800
- config = configObject,
801
- from = dateFrom,
802
- to = dateTo,
803
- filter = statsFilter
804
- )
738
+ executeV3MethodWithConnection("getActivities", connectionUUID, promise) { connection ->
739
+ val dateFrom = convertIsoStringToInstant(from)
740
+ val dateTo = convertIsoStringToInstant(to)
741
+ val statsFilter = StatisticsFilter.fromValue(filter)
742
+ val configObject = ActivityConfigUtils.fromValue(configJson) ?: ActivityConfig()
743
+
744
+ val result = connection.getActivities(
745
+ config = configObject,
746
+ from = dateFrom,
747
+ to = dateTo,
748
+ filter = statsFilter
749
+ )
805
750
 
806
- promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
807
-
808
- } catch (e: SpikeExceptions) {
809
- promise.reject(e.mapException(), e.message)
810
- }
751
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
811
752
  }
812
753
  }
813
754
 
@@ -820,29 +761,20 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
820
761
  filter: String?, // StatisticsFilter?
821
762
  promise: Promise
822
763
  ) {
823
- scope.launch {
824
- try {
825
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
826
- SpikeExceptions.SpikeException("Connection not found").mapException(),
827
- "Connection not found (getSleep)"
828
- )
829
- val dateFrom = convertIsoStringToInstant(from)
830
- val dateTo = convertIsoStringToInstant(to)
831
- val statsFilter = StatisticsFilter.fromValue(filter)
832
- val configObject = SleepConfigUtils.fromValue(configJson) ?: SleepConfig()
833
-
834
- val result = connection.getSleep(
835
- config = configObject,
836
- from = dateFrom,
837
- to = dateTo,
838
- filter = statsFilter
839
- )
764
+ executeV3MethodWithConnection("getSleep", connectionUUID, promise) { connection ->
765
+ val dateFrom = convertIsoStringToInstant(from)
766
+ val dateTo = convertIsoStringToInstant(to)
767
+ val statsFilter = StatisticsFilter.fromValue(filter)
768
+ val configObject = SleepConfigUtils.fromValue(configJson) ?: SleepConfig()
769
+
770
+ val result = connection.getSleep(
771
+ config = configObject,
772
+ from = dateFrom,
773
+ to = dateTo,
774
+ filter = statsFilter
775
+ )
840
776
 
841
- promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
842
-
843
- } catch (e: SpikeExceptions) {
844
- promise.reject(e.mapException(), e.message)
845
- }
777
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
846
778
  }
847
779
  }
848
780
 
@@ -852,24 +784,13 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
852
784
  metrics: ReadableArray,
853
785
  promise: Promise
854
786
  ) {
855
- scope.launch {
856
- try {
857
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
858
- SpikeExceptions.SpikeException("Connection not found").mapException(),
859
- "Connection not found (getUserProperties)"
860
- )
861
-
862
- val mTypes = metrics.toArrayList().mapNotNull {
863
- MetricTypeUtils.fromValue(it as String)
864
- }.toSet()
865
-
866
- val result = connection.getUserProperties(types = mTypes)
867
-
868
- promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
787
+ executeV3MethodWithConnection("getUserProperties", connectionUUID, promise) { connection ->
788
+ val mTypes = metrics.toArrayList().mapNotNull {
789
+ MetricTypeUtils.fromValue(it as String)
790
+ }.toSet()
869
791
 
870
- } catch (e: SpikeExceptions) {
871
- promise.reject(e.mapException(), e.message)
872
- }
792
+ val result = connection.getUserProperties(types = mTypes)
793
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
873
794
  }
874
795
  }
875
796
 
@@ -878,126 +799,65 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
878
799
 
879
800
  @ReactMethod
880
801
  fun checkHealthConnectAvailability(connectionUUID: String, promise: Promise) {
881
- scope.launch {
882
- try {
883
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
884
- SpikeExceptions.SpikeException("Connection not found").mapException(),
885
- "Connection not found (checkHealthConnectAvailability)"
886
- )
887
-
888
- val result = connection.checkHealthConnectAvailability()
889
- promise.resolve(result.value)
890
-
891
- } catch (e: SpikeExceptions) {
892
- promise.reject(e.mapException(), e.message)
893
- }
802
+ executeV3MethodWithConnection("checkHealthConnectAvailability", connectionUUID, promise) { connection ->
803
+ val result = connection.checkHealthConnectAvailability()
804
+ promise.resolve(result.value)
894
805
  }
895
806
  }
896
807
 
897
808
  @ReactMethod
898
809
  fun openHealthConnectInstallation(connectionUUID: String, promise: Promise) {
899
- scope.launch {
900
- try {
901
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
902
- SpikeExceptions.SpikeException("Connection not found").mapException(),
903
- "Connection not found (openHealthConnectInstallation)"
904
- )
905
-
906
- connection.openHealthConnectInstallation()
907
- promise.resolve(true)
908
-
909
- } catch (e: SpikeExceptions) {
910
- promise.reject(e.mapException(), e.message)
911
- }
810
+ executeV3MethodWithConnection("openHealthConnectInstallation", connectionUUID, promise) { connection ->
811
+ connection.openHealthConnectInstallation()
812
+ promise.resolve(true)
912
813
  }
913
814
  }
914
815
 
915
816
  @ReactMethod
916
817
  fun enableHealthConnectIntegration(connectionUUID: String, promise: Promise) {
917
- scope.launch {
918
- try {
919
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
920
- SpikeExceptions.SpikeException("Connection not found").mapException(),
921
- "Connection not found (enableHealthConnectIntegration)"
922
- )
923
-
924
- connection.enableHealthConnectIntegration()
925
- Log.d("SpikeSdkModule", "Health Connect integration enabled")
926
- promise.resolve(true)
927
- } catch (e: SpikeExceptions) {
928
- promise.reject(e.mapException(), e.message)
929
- }
818
+ executeV3MethodWithConnection("enableHealthConnectIntegration", connectionUUID, promise) { connection ->
819
+ connection.enableHealthConnectIntegration()
820
+ Log.d("SpikeSdkModule", "Health Connect integration enabled")
821
+ promise.resolve(true)
930
822
  }
931
823
  }
932
824
 
933
825
  @ReactMethod
934
826
  fun disableHealthConnectIntegration(connectionUUID: String, promise: Promise) {
935
- scope.launch {
936
- try {
937
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
938
- SpikeExceptions.SpikeException("Connection not found").mapException(),
939
- "Connection not found (enableHealthConnectIntegration)"
940
- )
941
-
942
- connection.disableHealthConnectIntegration()
943
- Log.d("SpikeSdkModule", "Health Connect integration disabled")
944
- promise.resolve(true)
945
-
946
- } catch (e: SpikeExceptions) {
947
- promise.reject(e.mapException(), e.message)
948
- }
827
+ executeV3MethodWithConnection("disableHealthConnectIntegration", connectionUUID, promise) { connection ->
828
+ connection.disableHealthConnectIntegration()
829
+ Log.d("SpikeSdkModule", "Health Connect integration disabled")
830
+ promise.resolve(true)
949
831
  }
950
832
  }
951
833
 
952
834
  @ReactMethod
953
835
  fun isHealthConnectIntegrationEnabled(connectionUUID: String, promise: Promise) {
954
- scope.launch {
955
- try {
956
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
957
- SpikeExceptions.SpikeException("Connection not found").mapException(),
958
- "Connection not found (enableHealthConnectIntegration)"
959
- )
960
-
961
- val result = connection.isHealthConnectIntegrationEnabled()
962
- Log.d("SpikeSdkModule", "Health Connect integration is " + if (result) { "enabled" } else ( "disabled" ))
963
- promise.resolve(result)
964
-
965
- } catch (e: SpikeExceptions) {
966
- promise.reject(e.mapException(), e.message)
967
- }
836
+ executeV3MethodWithConnection("isHealthConnectIntegrationEnabled", connectionUUID, promise) { connection ->
837
+ val result = connection.isHealthConnectIntegrationEnabled()
838
+ Log.d("SpikeSdkModule", "Health Connect integration is " + if (result) { "enabled" } else ( "disabled" ))
839
+ promise.resolve(result)
968
840
  }
969
841
  }
970
842
 
971
843
  @ReactMethod
972
844
  fun getGrantedHealthConnectPermissions(connectionUUID: String, promise: Promise) {
973
- scope.launch {
974
- try {
975
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
976
- SpikeExceptions.SpikeException("Connection not found").mapException(),
977
- "Connection not found (getGrantedHealthConnectPermissions)"
978
- )
979
- if (!connection.isHealthConnectIntegrationEnabled()) {
980
- return@launch promise.reject(
981
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
982
- "Health Connect should be enabled before calling getGrantedHealthConnectPermissions"
983
- )
984
- }
845
+ executeV3MethodWithConnection("getGrantedHealthConnectPermissions", connectionUUID, promise) { connection ->
846
+ if (!connection.isHealthConnectIntegrationEnabled()) {
847
+ throw SpikeExceptions.SpikeException("Health Connect should be enabled before calling getGrantedHealthConnectPermissions")
848
+ }
985
849
 
986
- val permissions = connection
987
- .getHealthConnectPermissionManager()
988
- .getGrantedPermissions()
850
+ val permissions = connection
851
+ .getHealthConnectPermissionManager()
852
+ .getGrantedPermissions()
989
853
 
990
- val result = Arguments.createArray()
991
- permissions.forEach {
992
- result.pushString(it)
993
- }
994
-
995
- Log.w("SpikeSdkModule", "$permissions")
996
- promise.resolve(result)
997
-
998
- } catch (e: SpikeExceptions) {
999
- promise.reject(e.mapException(), e.message)
854
+ val result = Arguments.createArray()
855
+ permissions.forEach {
856
+ result.pushString(it)
1000
857
  }
858
+
859
+ Log.w("SpikeSdkModule", "$permissions")
860
+ promise.resolve(result)
1001
861
  }
1002
862
  }
1003
863
 
@@ -1012,122 +872,85 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
1012
872
  includeBackgroundDelivery: Boolean,
1013
873
  promise: Promise
1014
874
  ) {
1015
- scope.launch {
1016
- try {
1017
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
1018
- SpikeExceptions.SpikeException("Connection not found").mapException(),
1019
- "Connection not found (getHealthConnectPermissions)"
875
+ executeV3MethodWithConnection("getHealthConnectPermissions", connectionUUID, promise) { connection ->
876
+ if (!connection.isHealthConnectIntegrationEnabled()) {
877
+ throw SpikeExceptions.SpikeException("Health Connect should be enabled before calling getHealthConnectPermissionsForStatisticTypes")
878
+ }
879
+ val permissionManager = connection.getHealthConnectPermissionManager()
880
+
881
+ val statTypes = statisticTypes.toArrayList().mapNotNull {
882
+ StatisticsTypeUtils.fromValue(it as String)
883
+ }.toSet()
884
+
885
+ val mTypes = metricTypes.toArrayList().mapNotNull {
886
+ MetricTypeUtils.fromValue(it as String)
887
+ }.toSet()
888
+
889
+ val aConfigs = activityConfigs.toArrayList().mapNotNull {
890
+ ActivityConfigUtils.fromValue(it as String)
891
+ }.toSet()
892
+
893
+ val sConfigs = sleepConfigs.toArrayList().mapNotNull {
894
+ SleepConfigUtils.fromValue(it as String)
895
+ }.toSet()
896
+
897
+ val permissions = permissionManager
898
+ .getPermissions(
899
+ statisticsTypes = statTypes,
900
+ metricTypes = mTypes,
901
+ activityConfigs = aConfigs,
902
+ sleepConfigs = sConfigs,
903
+ includeEnhancedPermissions = includeEnhancedPermissions,
904
+ includeBackgroundDelivery = includeBackgroundDelivery
1020
905
  )
1021
- if (!connection.isHealthConnectIntegrationEnabled()) {
1022
- return@launch promise.reject(
1023
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
1024
- "Health Connect should be enabled before calling getHealthConnectPermissionsForStatisticTypes"
1025
- )
1026
- }
1027
- val permissionManager = connection.getHealthConnectPermissionManager()
1028
-
1029
- val statTypes = statisticTypes.toArrayList().mapNotNull {
1030
- StatisticsTypeUtils.fromValue(it as String)
1031
- }.toSet()
1032
-
1033
- val mTypes = metricTypes.toArrayList().mapNotNull {
1034
- MetricTypeUtils.fromValue(it as String)
1035
- }.toSet()
1036
-
1037
- val aConfigs = activityConfigs.toArrayList().mapNotNull {
1038
- ActivityConfigUtils.fromValue(it as String)
1039
- }.toSet()
1040
-
1041
- val sConfigs = sleepConfigs.toArrayList().mapNotNull {
1042
- SleepConfigUtils.fromValue(it as String)
1043
- }.toSet()
1044
-
1045
- val permissions = permissionManager
1046
- .getPermissions(
1047
- statisticsTypes = statTypes,
1048
- metricTypes = mTypes,
1049
- activityConfigs = aConfigs,
1050
- sleepConfigs = sConfigs,
1051
- includeEnhancedPermissions = includeEnhancedPermissions,
1052
- includeBackgroundDelivery = includeBackgroundDelivery
1053
- )
1054
906
 
1055
- val result = Arguments.createArray()
1056
- permissions.forEach {
1057
- result.pushString(it)
1058
- }
1059
-
1060
- // Log.w("SpikeSdkModule", "$permissions")
1061
- promise.resolve(result)
1062
-
1063
- } catch (e: SpikeExceptions) {
1064
- promise.reject(e.mapException(), e.message)
907
+ val result = Arguments.createArray()
908
+ permissions.forEach {
909
+ result.pushString(it)
1065
910
  }
911
+
912
+ // Log.w("SpikeSdkModule", "$permissions")
913
+ promise.resolve(result)
1066
914
  }
1067
915
  }
1068
916
 
1069
917
  @ReactMethod
1070
918
  fun revokeAllHealthConnectPermissions(connectionUUID: String, promise: Promise) {
1071
- scope.launch {
1072
- try {
1073
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
1074
- SpikeExceptions.SpikeException("Connection not found").mapException(),
1075
- "Connection not found (revokeAllHealthConnectPermissions)"
1076
- )
1077
- if (!connection.isHealthConnectIntegrationEnabled()) {
1078
- return@launch promise.reject(
1079
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
1080
- "Health Connect should be enabled before calling revokeAllHealthConnectPermissions"
1081
- )
1082
- }
1083
-
1084
- connection
1085
- .getHealthConnectPermissionManager()
1086
- .revokeAllPermissions()
1087
-
1088
- promise.resolve(true)
1089
-
1090
- } catch (e: SpikeExceptions) {
1091
- promise.reject(e.mapException(), e.message)
919
+ executeV3MethodWithConnection("revokeAllHealthConnectPermissions", connectionUUID, promise) { connection ->
920
+ if (!connection.isHealthConnectIntegrationEnabled()) {
921
+ throw SpikeExceptions.SpikeException("Health Connect should be enabled before calling revokeAllHealthConnectPermissions")
1092
922
  }
923
+
924
+ connection
925
+ .getHealthConnectPermissionManager()
926
+ .revokeAllPermissions()
927
+
928
+ promise.resolve(true)
1093
929
  }
1094
930
  }
1095
931
 
1096
932
  @ReactMethod
1097
933
  fun requestPermissionsFromHealthConnect(connectionUUID: String, permissions: ReadableArray, promise: Promise) {
1098
- scope.launch {
1099
- try {
1100
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
1101
- SpikeExceptions.SpikeException(
1102
- "Connection not found"
1103
- ).mapException(), "Connection not found"
1104
- )
1105
- if (!connection.isHealthConnectIntegrationEnabled()) {
1106
- return@launch promise.reject(
1107
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
1108
- "Health Connect should be enabled before calling requestPermissionsFromHealthConnect"
1109
- )
1110
- }
1111
-
1112
- val permissionManager = connection.getHealthConnectPermissionManager()
1113
- val permissionsStrings = permissions.toArrayList().map{ it as String }.toSet()
934
+ executeV3MethodWithConnection("requestPermissionsFromHealthConnect", connectionUUID, promise) { connection ->
935
+ if (!connection.isHealthConnectIntegrationEnabled()) {
936
+ throw SpikeExceptions.SpikeException("Health Connect should be enabled before calling requestPermissionsFromHealthConnect")
937
+ }
1114
938
 
1115
- if (checkIfAllPermissionsAreGranted(permissionManager, permissionsStrings)) {
1116
- // All permissions granted, nothing to do here
1117
- promise.resolve(true)
1118
- return@launch
1119
- }
1120
-
1121
- Log.d("SpikeSdkModule", "Will ask for following Health Connect permissions: $permissions")
939
+ val permissionManager = connection.getHealthConnectPermissionManager()
940
+ val permissionsStrings = permissions.toArrayList().map{ it as String }.toSet()
1122
941
 
1123
- if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU) {
1124
- startHealthConnectRequest_oldAndroid(connectionUUID, permissionManager, permissionsStrings, promise)
1125
- } else {
1126
- startHealthConnectRequest(connectionUUID, permissionManager, permissionsStrings, promise)
1127
- }
942
+ if (checkIfAllPermissionsAreGranted(permissionManager, permissionsStrings)) {
943
+ // All permissions granted, nothing to do here
944
+ promise.resolve(true)
945
+ return@executeV3MethodWithConnection
946
+ }
947
+
948
+ Log.d("SpikeSdkModule", "Will ask for following Health Connect permissions: $permissions")
1128
949
 
1129
- } catch (e: SpikeExceptions) {
1130
- promise.reject(e.mapException(), e.message)
950
+ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU) {
951
+ startHealthConnectRequest_oldAndroid(connectionUUID, permissionManager, permissionsStrings, promise)
952
+ } else {
953
+ startHealthConnectRequest(connectionUUID, permissionManager, permissionsStrings, promise)
1131
954
  }
1132
955
  }
1133
956
  }
@@ -1176,27 +999,14 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
1176
999
  }
1177
1000
 
1178
1001
  fun areAllPermissionsGranted(connectionUUID: String, permissions: Set<String>, promise: Promise) {
1179
- scope.launch {
1180
- try {
1181
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
1182
- SpikeExceptions.SpikeException(
1183
- "Connection not found"
1184
- ).mapException(), "Connection not found"
1185
- )
1186
- if (!connection.isHealthConnectIntegrationEnabled()) {
1187
- return@launch promise.reject(
1188
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
1189
- "Health Connect should be enabled before calling areAllPermissionsGranted"
1190
- )
1191
- }
1192
- val permissionManager = connection.getHealthConnectPermissionManager()
1193
-
1194
- val permissionsGranted: Boolean = checkIfAllPermissionsAreGranted(permissionManager, permissions)
1195
- promise.resolve(permissionsGranted)
1196
-
1197
- } catch (e: SpikeExceptions) {
1198
- promise.reject(e.mapException(), e.message)
1002
+ executeV3MethodWithConnection("areAllPermissionsGranted", connectionUUID, promise) { connection ->
1003
+ if (!connection.isHealthConnectIntegrationEnabled()) {
1004
+ throw SpikeExceptions.SpikeException("Health Connect should be enabled before calling areAllPermissionsGranted")
1199
1005
  }
1006
+ val permissionManager = connection.getHealthConnectPermissionManager()
1007
+
1008
+ val permissionsGranted: Boolean = checkIfAllPermissionsAreGranted(permissionManager, permissions)
1009
+ promise.resolve(permissionsGranted)
1200
1010
  }
1201
1011
  }
1202
1012
 
@@ -1215,47 +1025,38 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
1215
1025
  sleepConfigs: ReadableArray,
1216
1026
  promise: Promise
1217
1027
  ) {
1218
- scope.launch {
1219
- try {
1220
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
1221
- SpikeExceptions.SpikeException("Connection not found").mapException(),
1222
- "Connection not found (enableBackgroundDelivery)"
1223
- )
1224
- if (!connection.isHealthConnectIntegrationEnabled()) {
1225
- return@launch promise.reject(
1226
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
1227
- "Health Connect should be enabled before calling enableBackgroundDelivery"
1228
- )
1229
- }
1230
-
1231
- val statTypes = statisticTypes.toArrayList().mapNotNull {
1232
- StatisticsTypeUtils.fromValue(it as String)
1233
- }.toSet()
1028
+ executeV3MethodWithConnection("enableBackgroundDelivery", connectionUUID, promise) { connection ->
1029
+ val healthConnectEnabled = connection.isHealthConnectIntegrationEnabled()
1030
+ val samsungHealthDataEnabled = connection.isSamsungHealthDataIntegrationEnabled()
1031
+
1032
+ if (!healthConnectEnabled && !samsungHealthDataEnabled) {
1033
+ throw SpikeExceptions.SpikeException("Either Health Connect or Samsung Health Data should be enabled before calling enableBackgroundDelivery")
1034
+ }
1234
1035
 
1235
- val mTypes = metricTypes.toArrayList().mapNotNull {
1236
- MetricTypeUtils.fromValue(it as String)
1237
- }.toSet()
1036
+ val statTypes = statisticTypes.toArrayList().mapNotNull {
1037
+ StatisticsTypeUtils.fromValue(it as String)
1038
+ }.toSet()
1238
1039
 
1239
- val aConfigs = activityConfigs.toArrayList().mapNotNull {
1240
- ActivityConfigUtils.fromValue(it as String)
1241
- }.toSet()
1040
+ val mTypes = metricTypes.toArrayList().mapNotNull {
1041
+ MetricTypeUtils.fromValue(it as String)
1042
+ }.toSet()
1242
1043
 
1243
- val sConfigs = sleepConfigs.toArrayList().mapNotNull {
1244
- SleepConfigUtils.fromValue(it as String)
1245
- }.toSet()
1044
+ val aConfigs = activityConfigs.toArrayList().mapNotNull {
1045
+ ActivityConfigUtils.fromValue(it as String)
1046
+ }.toSet()
1246
1047
 
1247
- connection.enableBackgroundDelivery(
1248
- statisticsTypes = statTypes,
1249
- metricTypes = mTypes,
1250
- activityConfigs = aConfigs,
1251
- sleepConfigs = sConfigs
1252
- )
1048
+ val sConfigs = sleepConfigs.toArrayList().mapNotNull {
1049
+ SleepConfigUtils.fromValue(it as String)
1050
+ }.toSet()
1253
1051
 
1254
- promise.resolve(true)
1052
+ connection.enableBackgroundDelivery(
1053
+ statisticsTypes = statTypes,
1054
+ metricTypes = mTypes,
1055
+ activityConfigs = aConfigs,
1056
+ sleepConfigs = sConfigs
1057
+ )
1255
1058
 
1256
- } catch (e: SpikeExceptions) {
1257
- promise.reject(e.mapException(), e.message)
1258
- }
1059
+ promise.resolve(true)
1259
1060
  }
1260
1061
  }
1261
1062
 
@@ -1264,24 +1065,15 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
1264
1065
  connectionUUID: String,
1265
1066
  promise: Promise
1266
1067
  ) {
1267
- scope.launch {
1268
- try {
1269
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
1270
- SpikeExceptions.SpikeException("Connection not found").mapException(),
1271
- "Connection not found (disableBackgroundDelivery)"
1272
- )
1273
- if (!connection.isHealthConnectIntegrationEnabled()) {
1274
- return@launch promise.reject(
1275
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
1276
- "Health Connect should be enabled before calling disableBackgroundDelivery"
1277
- )
1278
- }
1279
- connection.disableBackgroundDelivery()
1280
- promise.resolve(true)
1281
-
1282
- } catch (e: SpikeExceptions) {
1283
- promise.reject(e.mapException(), e.message)
1068
+ executeV3MethodWithConnection("disableBackgroundDelivery", connectionUUID, promise) { connection ->
1069
+ val healthConnectEnabled = connection.isHealthConnectIntegrationEnabled()
1070
+ val samsungHealthDataEnabled = connection.isSamsungHealthDataIntegrationEnabled()
1071
+
1072
+ if (!healthConnectEnabled && !samsungHealthDataEnabled) {
1073
+ throw SpikeExceptions.SpikeException("Either Health Connect or Samsung Health Data should be enabled before calling disableBackgroundDelivery")
1284
1074
  }
1075
+ connection.disableBackgroundDelivery()
1076
+ promise.resolve(true)
1285
1077
  }
1286
1078
  }
1287
1079
 
@@ -1290,27 +1082,18 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
1290
1082
  connectionUUID: String,
1291
1083
  promise: Promise
1292
1084
  ) {
1293
- scope.launch {
1294
- try {
1295
- val connection = getConnectionV3(connectionUUID) ?: return@launch promise.reject(
1296
- SpikeExceptions.SpikeException("Connection not found").mapException(),
1297
- "Connection not found (getBackgroundDeliveryConfig)"
1298
- )
1299
- if (!connection.isHealthConnectIntegrationEnabled()) {
1300
- return@launch promise.reject(
1301
- SpikeExceptions.SpikeException("Health Connect is disabled").mapException(),
1302
- "Health Connect should be enabled before calling getBackgroundDeliveryConfig"
1303
- )
1304
- }
1305
- val result = connection.getBackgroundDeliveryConfig()
1306
- if (result == null) {
1307
- promise.resolve(null)
1308
- } else {
1309
- promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
1310
- }
1311
-
1312
- } catch (e: SpikeExceptions) {
1313
- promise.reject(e.mapException(), e.message)
1085
+ executeV3MethodWithConnection("getBackgroundDeliveryConfig", connectionUUID, promise) { connection ->
1086
+ val healthConnectEnabled = connection.isHealthConnectIntegrationEnabled()
1087
+ val samsungHealthDataEnabled = connection.isSamsungHealthDataIntegrationEnabled()
1088
+
1089
+ if (!healthConnectEnabled && !samsungHealthDataEnabled) {
1090
+ throw SpikeExceptions.SpikeException("Either Health Connect or Samsung Health Data should be enabled before calling getBackgroundDeliveryConfig")
1091
+ }
1092
+ val result = connection.getBackgroundDeliveryConfig()
1093
+ if (result == null) {
1094
+ promise.resolve(null)
1095
+ } else {
1096
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
1314
1097
  }
1315
1098
  }
1316
1099
  }
@@ -1336,6 +1119,221 @@ class SpikeSdkModule(reactContext: ReactApplicationContext) :
1336
1119
  }
1337
1120
  }
1338
1121
 
1122
+
1123
+ /**
1124
+ * Centralized error handling for V3 methods
1125
+ * Executes the provided block in a coroutine scope with comprehensive error handling
1126
+ */
1127
+ private fun executeV3Method(
1128
+ promise: Promise,
1129
+ block: suspend () -> Unit
1130
+ ) {
1131
+ scope.launch {
1132
+ try {
1133
+ block()
1134
+ } catch (e: SpikeExceptions) {
1135
+ promise.reject(e.mapException(), e.message)
1136
+ } catch (e: Exception) {
1137
+ promise.reject(e.toString(), e.message)
1138
+ }
1139
+ }
1140
+ }
1141
+
1142
+ /**
1143
+ * Centralized error handling for V3 methods that need connection validation
1144
+ * Executes the provided block in a coroutine scope with connection validation and comprehensive error handling
1145
+ */
1146
+ private fun executeV3MethodWithConnection(
1147
+ methodName: String,
1148
+ connectionUUID: String,
1149
+ promise: Promise,
1150
+ block: suspend (SpikeConnectionAPIv3) -> Unit
1151
+ ) {
1152
+ executeV3Method(promise) {
1153
+ val connection = getConnectionV3(connectionUUID) ?: throw SpikeExceptions.SpikeException(
1154
+ "Connection not found ($methodName)"
1155
+ )
1156
+ block(connection)
1157
+ }
1158
+ }
1159
+
1160
+ /// --- Samsung Health Data permissions ---
1161
+
1162
+ @ReactMethod
1163
+ fun checkSamsungHealthDataAvailability(connectionUUID: String, promise: Promise) {
1164
+ executeV3MethodWithConnection("checkSamsungHealthDataAvailability", connectionUUID, promise) { connection ->
1165
+ val activity = reactApplicationContext.currentActivity
1166
+ if (activity == null) {
1167
+ throw SpikeExceptions.SpikeException("Activity is required to check Samsung Health Data availability")
1168
+ }
1169
+ val result = connection.checkSamsungHealthDataAvailability(activity)
1170
+ promise.resolve(SpikeConnectionAPIv3.convertToJson(result))
1171
+ }
1172
+ }
1173
+
1174
+ @ReactMethod
1175
+ fun enableSamsungHealthDataIntegration(connectionUUID: String, promise: Promise) {
1176
+ executeV3MethodWithConnection("enableSamsungHealthDataIntegration", connectionUUID, promise) { connection ->
1177
+ val activity = reactApplicationContext.currentActivity
1178
+ if (activity == null) {
1179
+ throw SpikeExceptions.SpikeException("Activity is required to enable Samsung Health Data integration")
1180
+ }
1181
+ connection.enableSamsungHealthDataIntegration(activity)
1182
+ Log.d("SpikeSdkModule", "Samsung Health Data integration enabled")
1183
+ promise.resolve(true)
1184
+ }
1185
+ }
1186
+
1187
+ @ReactMethod
1188
+ fun disableSamsungHealthDataIntegration(connectionUUID: String, promise: Promise) {
1189
+ executeV3MethodWithConnection("disableSamsungHealthDataIntegration", connectionUUID, promise) { connection ->
1190
+ connection.disableSamsungHealthDataIntegration()
1191
+ Log.d("SpikeSdkModule", "Samsung Health Data integration disabled")
1192
+ promise.resolve(true)
1193
+ }
1194
+ }
1195
+
1196
+ @ReactMethod
1197
+ fun isSamsungHealthDataIntegrationEnabled(connectionUUID: String, promise: Promise) {
1198
+ executeV3MethodWithConnection("isSamsungHealthDataIntegrationEnabled", connectionUUID, promise) { connection ->
1199
+ val result = connection.isSamsungHealthDataIntegrationEnabled()
1200
+ Log.d("SpikeSdkModule", "Samsung Health Data integration is " + if (result) { "enabled" } else ( "disabled" ))
1201
+ promise.resolve(result)
1202
+ }
1203
+ }
1204
+
1205
+ @ReactMethod
1206
+ fun getGrantedSamsungHealthDataPermissions(connectionUUID: String, promise: Promise) {
1207
+ executeV3MethodWithConnection("getGrantedSamsungHealthDataPermissions", connectionUUID, promise) { connection ->
1208
+ if (!connection.isSamsungHealthDataIntegrationEnabled()) {
1209
+ throw SpikeExceptions.SpikeException("Samsung Health Data should be enabled before calling getGrantedSamsungHealthDataPermissions")
1210
+ }
1211
+
1212
+ val permissions = connection
1213
+ .getSamsungHealthDataPermissionManager()
1214
+ .getGrantedPermissions()
1215
+
1216
+ val result = Arguments.createArray()
1217
+ permissions.forEach {
1218
+ result.pushString(it)
1219
+ }
1220
+
1221
+ Log.w("SpikeSdkModule", "$permissions")
1222
+ promise.resolve(result)
1223
+ }
1224
+ }
1225
+
1226
+ @ReactMethod
1227
+ fun getSamsungHealthDataPermissions(
1228
+ connectionUUID: String,
1229
+ statisticTypes: ReadableArray,
1230
+ metricTypes: ReadableArray,
1231
+ activityConfigs: ReadableArray,
1232
+ sleepConfigs: ReadableArray,
1233
+ promise: Promise
1234
+ ) {
1235
+ executeV3MethodWithConnection("getSamsungHealthDataPermissions", connectionUUID, promise) { connection ->
1236
+ if (!connection.isSamsungHealthDataIntegrationEnabled()) {
1237
+ throw SpikeExceptions.SpikeException("Samsung Health Data should be enabled before calling getSamsungHealthDataPermissions")
1238
+ }
1239
+ val permissionManager = connection.getSamsungHealthDataPermissionManager()
1240
+
1241
+ val statTypes = statisticTypes.toArrayList().mapNotNull {
1242
+ StatisticsTypeUtils.fromValue(it as String)
1243
+ }.toSet()
1244
+
1245
+ val mTypes = metricTypes.toArrayList().mapNotNull {
1246
+ MetricTypeUtils.fromValue(it as String)
1247
+ }.toSet()
1248
+
1249
+ val aConfigs = activityConfigs.toArrayList().mapNotNull {
1250
+ ActivityConfigUtils.fromValue(it as String)
1251
+ }.toSet()
1252
+
1253
+ val sConfigs = sleepConfigs.toArrayList().mapNotNull {
1254
+ SleepConfigUtils.fromValue(it as String)
1255
+ }.toSet()
1256
+
1257
+ val permissions = permissionManager
1258
+ .getPermissions(
1259
+ statisticsTypes = statTypes,
1260
+ metricTypes = mTypes,
1261
+ activityConfigs = aConfigs,
1262
+ sleepConfigs = sConfigs
1263
+ )
1264
+
1265
+ val result = Arguments.createArray()
1266
+ permissions.forEach {
1267
+ result.pushString(it)
1268
+ }
1269
+
1270
+ promise.resolve(result)
1271
+ }
1272
+ }
1273
+
1274
+ @ReactMethod
1275
+ fun requestPermissionsFromSamsungHealthData(
1276
+ connectionUUID: String,
1277
+ statisticTypes: ReadableArray,
1278
+ metricTypes: ReadableArray,
1279
+ activityConfigs: ReadableArray,
1280
+ sleepConfigs: ReadableArray,
1281
+ promise: Promise
1282
+ ) {
1283
+ executeV3MethodWithConnection("requestPermissionsFromSamsungHealthData", connectionUUID, promise) { connection ->
1284
+ if (!connection.isSamsungHealthDataIntegrationEnabled()) {
1285
+ throw SpikeExceptions.SpikeException("Samsung Health Data should be enabled before calling requestPermissionsFromSamsungHealthData")
1286
+ }
1287
+
1288
+ val permissionManager = connection.getSamsungHealthDataPermissionManager()
1289
+
1290
+ val statTypes = statisticTypes.toArrayList().mapNotNull {
1291
+ StatisticsTypeUtils.fromValue(it as String)
1292
+ }.toSet()
1293
+
1294
+ val mTypes = metricTypes.toArrayList().mapNotNull {
1295
+ MetricTypeUtils.fromValue(it as String)
1296
+ }.toSet()
1297
+
1298
+ val aConfigs = activityConfigs.toArrayList().mapNotNull {
1299
+ ActivityConfigUtils.fromValue(it as String)
1300
+ }.toSet()
1301
+
1302
+ val sConfigs = sleepConfigs.toArrayList().mapNotNull {
1303
+ SleepConfigUtils.fromValue(it as String)
1304
+ }.toSet()
1305
+
1306
+ Log.d("SpikeSdkModule", "Will ask for Samsung Health Data permissions with: statTypes=$statTypes, metricTypes=$mTypes, activityConfigs=$aConfigs, sleepConfigs=$sConfigs")
1307
+
1308
+ val activity = reactApplicationContext.currentActivity
1309
+ if (activity == null) {
1310
+ throw SpikeExceptions.SpikeException("Activity is required to request Samsung Health Data permissions")
1311
+ }
1312
+
1313
+ try {
1314
+ val grantedPermissions = permissionManager.requestPermissions(
1315
+ statisticsTypes = statTypes,
1316
+ metricTypes = mTypes,
1317
+ activityConfigs = aConfigs,
1318
+ sleepConfigs = sConfigs
1319
+ )
1320
+
1321
+ val result = Arguments.createArray()
1322
+ grantedPermissions.forEach {
1323
+ result.pushString(it)
1324
+ }
1325
+
1326
+ promise.resolve(result)
1327
+ } catch (e: Exception) {
1328
+ promise.reject(e.toString(), e.message)
1329
+ }
1330
+ }
1331
+ }
1332
+
1333
+
1334
+
1335
+ /// ---
1336
+
1339
1337
  }
1340
1338
 
1341
1339
  private fun convertIsoStringToInstant(isoString: String): Instant {