Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
petersamokhin committed Dec 29, 2022
1 parent 5de7c46 commit 16b8a5f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 53 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# VK SDK Android
![cover](images/android_cover.png)
![cover](docs/images/android_cover.png)

<p align="center">Some Android-specific features built into Unofficial VK SDK for Android</p>

Expand All @@ -17,7 +17,7 @@ See the documentation: [https://vksdk.github.io/vk-sdk-android](https://vksdk.gi
## Auth
[![Android minSdkVersion](https://img.shields.io/badge/minSdkVersion-21-yellowgreen)](https://img.shields.io/badge/minSdkVersion-16-yellowgreen) [![Android targetSdkVersion](https://img.shields.io/badge/targetSdkVersion-33-green)](https://img.shields.io/badge/targetSdkVersion-33-green)

Latest version: [![maven-central](https://img.shields.io/badge/Maven%20Central-1.0.0-yellowgreen?style=flat)](https://search.maven.org/search?q=g:com.petersamokhin.vksdk.android)
Latest version: [![maven-central](https://img.shields.io/badge/Maven%20Central-1.1.0-yellowgreen?style=flat)](https://search.maven.org/search?q=g:com.petersamokhin.vksdk.android)

[Authorization code flow](https://vk.com/dev/authcode_flow_user) is not supported by the official VK SDK and by the official app.
But it is supported by this auth feature.
Expand All @@ -41,10 +41,10 @@ val callback = { result: VkAuthResult ->
}

// before activity.onCreate
VkAuth.register(activity, callback)
val launcher = VkAuth.register(activity, callback)

// somewhere onClick
VkAuth.login(activity, params)
VkAuth.login(activity, launcher, params)
```

## Install
Expand Down
6 changes: 6 additions & 0 deletions auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change log

Version 1.1.0 *(2022-12-29)*
----------------------------

* Expose the activity result launcher and take it as a param.
* Fix `AuthMode.RequireWebView` ignored for `ResponseType.Code`.

Version 1.0.1 *(2022-12-29)*
----------------------------

Expand Down
2 changes: 1 addition & 1 deletion auth/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POM_ARTIFACT_ID=auth
POM_NAME=VK SDK Android Auth
POM_DESCRIPTION=VK SDK Android, module Auth
VERSION_NAME=1.0.1
VERSION_NAME=1.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.petersamokhin.vksdk.android.auth.error.VkAuthException
import com.petersamokhin.vksdk.android.auth.model.VkAuthResult
import com.petersamokhin.vksdk.android.auth.utils.toMap
import kotlinx.parcelize.Parcelize
import java.util.WeakHashMap

/**
* VK authorization handler.
Expand Down Expand Up @@ -52,8 +51,6 @@ public object VkAuth {
private const val SERVICE_ACTION = "android.support.customtabs.action.CustomTabsService"
private const val CHROME_PACKAGE = "com.android.chrome"

private var resultLaunchers: MutableMap<ComponentActivity, ActivityResultLauncher<Intent>> = WeakHashMap()

/**
* Register the given activity for auth result.
* See [ComponentActivity.registerForActivityResult]
Expand All @@ -65,13 +62,8 @@ public object VkAuth {
@JvmStatic
public fun register(
activity: ComponentActivity,
overrideLaunchersMap: MutableMap<ComponentActivity, ActivityResultLauncher<Intent>>? = null,
listener: (Result<VkAuthResult>) -> Unit,
) {
if (overrideLaunchersMap != null) {
resultLaunchers = overrideLaunchersMap.also { map -> map.putAll(resultLaunchers) }
}

): ActivityResultLauncher<Intent> {
activity.addOnNewIntentListener { intent ->
try {
listener(
Expand All @@ -86,7 +78,7 @@ public object VkAuth {
}
}

val resultLauncher = activity.registerForActivityResult(
return activity.registerForActivityResult(
/* contract = */ ActivityResultContracts.StartActivityForResult(),
) { result ->
val vkResult = try {
Expand All @@ -105,22 +97,6 @@ public object VkAuth {
listener(Result.failure(VkAuthException("Failed to parse the VK auth result: $result")))
}
}
resultLaunchers[activity] = resultLauncher
}

/**
* Unregister the activity result listeners.
*
* @param activity An activity for which to unregister the listeners.
* If null, all listeners will be unregistered.
*/
@JvmStatic
public fun unregister(activity: ComponentActivity? = null) {
if (activity != null) {
resultLaunchers.remove(activity)
} else {
resultLaunchers.clear()
}
}

/**
Expand Down Expand Up @@ -173,6 +149,7 @@ public object VkAuth {
@JvmStatic
public fun login(
activity: ComponentActivity,
launcher: ActivityResultLauncher<Intent>,
authParams: AuthParams,
mode: AuthMode = AuthMode.Auto,
) {
Expand Down Expand Up @@ -223,26 +200,20 @@ public object VkAuth {
}

if (intent != null) {
launchLogin(
activity = activity,
intent = intent,
)
launcher.launch(intent)
}
}

ResponseType.Code -> {
Log.w(LOG_TAG, INFO_RESPONSE_TYPE_NOT_SUPPORTED)

when {
activity.customTabsSupported() -> {
mode != AuthMode.RequireWebView && activity.customTabsSupported() -> {
activity.startActivity(loadCustomTabsAuthUrlIntent(authParams.asQuery()))
}

else -> {
launchLogin(
activity = activity,
intent = VkAuthActivity.intent(activity, authParams),
)
launcher.launch(VkAuthActivity.intent(activity, authParams))
}
}
}
Expand Down Expand Up @@ -365,10 +336,6 @@ public object VkAuth {
}
}

private fun launchLogin(activity: ComponentActivity, intent: Intent) {
resultLaunchers[activity]?.launch(intent)
}

/**
* Response type: access_token or code
*
Expand Down
17 changes: 9 additions & 8 deletions docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ The first param is `androidx.activity.ComponentActivity` (e.g. `androidx.appcomp
class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreate(savedInstanceState: Bundle?) {
// must be called before onCreate
VkAuth.register(this) { result ->
val launcher = VkAuth.register(this) { result ->
Log.e("vk_auth", result.toString())
}
super.onCreate(savedInstanceState)

someButton.setOnClickListener {
VkAuth.login(this, params)
VkAuth.login(this, launcher, params)
}
}
}
Expand Down Expand Up @@ -82,6 +82,7 @@ You need to make a JSON file available here: `https://domain.com/.well-known/ass
Make your auth activity (from where you will do the auth) discoverable:

```xml

<activity android:name=".auth.YourAuthActivity" android:exported="true" android:launchMode="singleTop">

<intent-filter android:autoVerify="true" tools:targetApi="m">
Expand All @@ -99,7 +100,7 @@ Make your auth activity (from where you will do the auth) discoverable:

#### 4. Prepare your activity

In your auth activity, just before `onCreate`, call `VkAuth.register(this)`.
In your auth activity, just before `onCreate`, call `VkAuth.register(this)` and use the returned `ActivityResultLauncher`.

#### Et voila!

Expand Down Expand Up @@ -179,7 +180,7 @@ val params = VkAuth.AuthParams(
apiVersion = apiVersion,
)

VkAuth.login(activity, params, authMode)
VkAuth.login(activity, launcher, params, authMode)
```

Auth modes:
Expand Down Expand Up @@ -251,10 +252,10 @@ val callback = { result: VkAuthResult ->
}

// before activity.onCreate
VkAuth.register(activity, callback)
val launcher = VkAuth.register(activity, callback)

// somewhere onClick
VkAuth.login(activity, params)
VkAuth.login(activity, launcher, params)
```

Example for `Code`:
Expand All @@ -277,8 +278,8 @@ val callback = { result: VkAuthResult ->
}

// before activity.onCreate
VkAuth.register(activity, callback)
val launcher = VkAuth.register(activity, callback)

// somewhere onClick
VkAuth.login(activity, params)
VkAuth.login(activity, launcher, params)
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.petersamokhin.vksdk.android
VERSION_NAME=1.0.1
VERSION_NAME=1.1.0

REPOSITORY_URL_MAVEN_STAGING_DEFAULT=https://oss.sonatype.org/service/local/staging/deploy/maven2/
REPOSITORY_URL_MAVEN_SNAPSHOT_DEFAULT=https://oss.sonatype.org/content/repositories/snapshots/
Expand Down

0 comments on commit 16b8a5f

Please sign in to comment.