Skip to content

Commit

Permalink
refactor/OMHD-454: Change Native Modules structure (#83)
Browse files Browse the repository at this point in the history
* refactor: change native modules structure

Signed-off-by: Maciej Budziński <[email protected]>

feat: wip

Signed-off-by: Maciej Budziński <[email protected]>

feat: wip

Signed-off-by: Maciej Budziński <[email protected]>

* refactor: changes after self CR

Signed-off-by: Maciej Budziński <[email protected]>

* refactor: changes after self CR, part 2

Signed-off-by: Maciej Budziński <[email protected]>

* refactor: changes after CR

Signed-off-by: Maciej Budziński <[email protected]>

---------

Signed-off-by: Maciej Budziński <[email protected]>
  • Loading branch information
maciekBudzinski authored Aug 12, 2024
1 parent 5f1c923 commit 1699dbe
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,34 @@

package com.openmobilehub.reactnative.auth.core

import android.app.Activity
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.openmobilehub.android.auth.core.OmhAuthClient

class OmhAuthModule(
reactContext: ReactApplicationContext,
private val name: String,
private val createOmhAuthClient: (config: HashMap<String, Any>) -> OmhAuthClient
) : ReactContextBaseJavaModule(reactContext) {
private var omhAuthClient: OmhAuthClient? = null
private fun getAuthClient(): OmhAuthClient {
if (omhAuthClient == null) {
throw Exception("OmhAuthClient is not initialized")
}
class OmhAuthModuleImpl(
context: ReactApplicationContext,
private val createAuthClient: (config: HashMap<String, Any>) -> OmhAuthClient
) {
private var _authClient: OmhAuthClient? = null

val authClient: OmhAuthClient
get() = _authClient ?: throw IllegalStateException("Accessing OmhAuthClient before OmhAuthModule is initialized")

return omhAuthClient!!
}

private val loginActivityEventListener = OmhLoginActivityListener()

init {
reactContext.addActivityEventListener(loginActivityEventListener)
}

override fun getName(): String {
return name
context.addActivityEventListener(loginActivityEventListener)
}

@ReactMethod
fun initialize(config: ReadableMap, promise: Promise) {
omhAuthClient = createOmhAuthClient(config.toHashMap())
_authClient = createAuthClient(config.toHashMap())

try {
getAuthClient().initialize().addOnSuccess {
authClient.initialize().addOnSuccess {
promise.resolve(null)
}.addOnFailure {
promise.reject(E_INITIALIZED_FAILED, it.message)
Expand All @@ -62,29 +52,26 @@ class OmhAuthModule(
}
}

@ReactMethod
fun signIn(promise: Promise) {
val activity = currentActivity
fun signIn(currentActivity: Activity?, promise: Promise) {

if (activity == null) {
if (currentActivity == null) {
promise.reject(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist")
return
}

loginActivityEventListener.loginPromise = promise

try {
val loginIntent = getAuthClient().getLoginIntent()
activity.startActivityForResult(loginIntent, LOGIN_REQUEST)
val loginIntent = authClient.getLoginIntent()
currentActivity.startActivityForResult(loginIntent, LOGIN_REQUEST)
} catch (e: Exception) {
promise.reject(E_SIGN_IN_FAILED, e.message)
}
}

@ReactMethod
fun getUser(promise: Promise) {
try {
getAuthClient().getUser()
authClient.getUser()
.addOnSuccess {
val jsonObject = Arguments.createMap().apply {
putString("name", it.name)
Expand All @@ -104,21 +91,19 @@ class OmhAuthModule(
}
}

@ReactMethod
fun getAccessToken(promise: Promise) {
try {
val credentials = getAuthClient().getCredentials()
val credentials = authClient.getCredentials()

promise.resolve(credentials.accessToken)
} catch (e: Exception) {
promise.reject(E_GET_ACCESS_TOKEN_FAILED, e.message)
}
}

@ReactMethod
fun refreshAccessToken(promise: Promise) {
try {
val credentials = getAuthClient().getCredentials()
val credentials = authClient.getCredentials()

credentials.refreshAccessToken()
.addOnSuccess {
Expand All @@ -133,10 +118,9 @@ class OmhAuthModule(
}
}

@ReactMethod
fun revokeAccessToken(promise: Promise) {
try {
getAuthClient().revokeToken()
authClient.revokeToken()
.addOnSuccess {
promise.resolve(null)
}
Expand All @@ -149,10 +133,9 @@ class OmhAuthModule(
}
}

@ReactMethod
fun signOut(promise: Promise) {
try {
getAuthClient().signOut()
authClient.signOut()
.addOnSuccess {
promise.resolve(null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OmhLoginActivityListener : BaseActivityEventListener() {
Activity.RESULT_CANCELED -> {
val error = intent?.getStringExtra("errorMessage")

promise.reject(OmhAuthModule.E_SIGN_IN_CANCELED, error)
promise.reject(OmhAuthModuleImpl.E_SIGN_IN_CANCELED, error)
}

Activity.RESULT_OK ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@

package com.openmobilehub.reactnative.auth.plugin.dropbox

import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.annotations.ReactModule
import com.openmobilehub.android.auth.core.OmhAuthClient
import com.openmobilehub.android.auth.plugin.dropbox.DropboxAuthClient
import com.openmobilehub.reactnative.auth.core.OmhAuthModule

class OmhDropboxModule(private val reactContext: ReactApplicationContext) {
fun getOmhDropboxModule(): OmhAuthModule {
return OmhAuthModule(
reactContext = reactContext,
name = NAME,
createOmhAuthClient = ::createOmhAuthClient
)
}
import com.openmobilehub.reactnative.auth.core.OmhAuthModuleImpl

@ReactModule(name = OmhDropboxModule.NAME)
class OmhDropboxModule(private val reactContext: ReactApplicationContext):
ReactContextBaseJavaModule(reactContext) {
private val moduleImpl = OmhAuthModuleImpl(reactContext, this::createOmhAuthClient)

val authClient: OmhAuthClient
get() = moduleImpl.authClient

private fun createOmhAuthClient(config: HashMap<String, Any>): OmhAuthClient {
val scopes = config["scopes"] as ArrayList<String>
Expand All @@ -39,6 +43,45 @@ class OmhDropboxModule(private val reactContext: ReactApplicationContext) {
)
}

@ReactMethod
fun initialize(config: ReadableMap, promise: Promise) {
moduleImpl.initialize(config, promise)
}

@ReactMethod
fun signIn(promise: Promise) {
moduleImpl.signIn(currentActivity, promise)
}

@ReactMethod
fun getUser(promise: Promise) {
moduleImpl.getUser(promise)
}

@ReactMethod
fun getAccessToken(promise: Promise) {
moduleImpl.getAccessToken(promise)
}

@ReactMethod
fun refreshAccessToken(promise: Promise) {
moduleImpl.refreshAccessToken(promise)
}

@ReactMethod
fun revokeAccessToken(promise: Promise) {
moduleImpl.revokeAccessToken(promise)
}

@ReactMethod
fun signOut(promise: Promise) {
moduleImpl.signOut(promise)
}

override fun getName(): String {
return NAME
}

companion object {
const val NAME = "OmhDropbox"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.facebook.react.uimanager.ViewManager

class OmhDropboxPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(OmhDropboxModule(reactContext).getOmhDropboxModule())
return listOf(OmhDropboxModule(reactContext))
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@

package com.openmobilehub.reactnative.auth.plugin.facebook

import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.annotations.ReactModule
import com.openmobilehub.android.auth.core.OmhAuthClient
import com.openmobilehub.android.auth.plugin.facebook.FacebookAuthClient
import com.openmobilehub.reactnative.auth.core.OmhAuthModule

class OmhFacebookModule(private val reactContext: ReactApplicationContext) {
fun getOmhFacebookModule(): OmhAuthModule {
return OmhAuthModule(
reactContext = reactContext,
name = NAME,
createOmhAuthClient = ::createOmhAuthClient
)
}
import com.openmobilehub.reactnative.auth.core.OmhAuthModuleImpl

@ReactModule(name = OmhFacebookModule.NAME)
class OmhFacebookModule(private val reactContext: ReactApplicationContext):
ReactContextBaseJavaModule(reactContext) {
private val moduleImpl = OmhAuthModuleImpl(reactContext, this::createOmhAuthClient)

val authClient: OmhAuthClient
get() = moduleImpl.authClient

private fun createOmhAuthClient(config: HashMap<String, Any>): OmhAuthClient {
val scopes = config["scopes"] as ArrayList<String>
Expand All @@ -38,6 +42,45 @@ class OmhFacebookModule(private val reactContext: ReactApplicationContext) {
)
}

@ReactMethod
fun initialize(config: ReadableMap, promise: Promise) {
moduleImpl.initialize(config, promise)
}

@ReactMethod
fun signIn(promise: Promise) {
moduleImpl.signIn(currentActivity, promise)
}

@ReactMethod
fun getUser(promise: Promise) {
moduleImpl.getUser(promise)
}

@ReactMethod
fun getAccessToken(promise: Promise) {
moduleImpl.getAccessToken(promise)
}

@ReactMethod
fun refreshAccessToken(promise: Promise) {
moduleImpl.refreshAccessToken(promise)
}

@ReactMethod
fun revokeAccessToken(promise: Promise) {
moduleImpl.revokeAccessToken(promise)
}

@ReactMethod
fun signOut(promise: Promise) {
moduleImpl.signOut(promise)
}

override fun getName(): String {
return NAME
}

companion object {
const val NAME = "OmhFacebook"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.facebook.react.uimanager.ViewManager

class OmhFacebookPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(OmhFacebookModule(reactContext).getOmhFacebookModule())
return listOf(OmhFacebookModule(reactContext))
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
Expand Down
Loading

0 comments on commit 1699dbe

Please sign in to comment.