Skip to content

Commit 16b8a5f

Browse files
committed
1.1.0
1 parent 5de7c46 commit 16b8a5f

File tree

6 files changed

+27
-53
lines changed

6 files changed

+27
-53
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# VK SDK Android
2-
![cover](images/android_cover.png)
2+
![cover](docs/images/android_cover.png)
33

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

@@ -17,7 +17,7 @@ See the documentation: [https://vksdk.github.io/vk-sdk-android](https://vksdk.gi
1717
## Auth
1818
[![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)
1919

20-
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)
20+
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)
2121

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

4343
// before activity.onCreate
44-
VkAuth.register(activity, callback)
44+
val launcher = VkAuth.register(activity, callback)
4545

4646
// somewhere onClick
47-
VkAuth.login(activity, params)
47+
VkAuth.login(activity, launcher, params)
4848
```
4949

5050
## Install

auth/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change log
22

3+
Version 1.1.0 *(2022-12-29)*
4+
----------------------------
5+
6+
* Expose the activity result launcher and take it as a param.
7+
* Fix `AuthMode.RequireWebView` ignored for `ResponseType.Code`.
8+
39
Version 1.0.1 *(2022-12-29)*
410
----------------------------
511

auth/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
POM_ARTIFACT_ID=auth
22
POM_NAME=VK SDK Android Auth
33
POM_DESCRIPTION=VK SDK Android, module Auth
4-
VERSION_NAME=1.0.1
4+
VERSION_NAME=1.1.0

auth/src/main/kotlin/com/petersamokhin/vksdk/android/auth/VkAuth.kt

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.petersamokhin.vksdk.android.auth.error.VkAuthException
2424
import com.petersamokhin.vksdk.android.auth.model.VkAuthResult
2525
import com.petersamokhin.vksdk.android.auth.utils.toMap
2626
import kotlinx.parcelize.Parcelize
27-
import java.util.WeakHashMap
2827

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

55-
private var resultLaunchers: MutableMap<ComponentActivity, ActivityResultLauncher<Intent>> = WeakHashMap()
56-
5754
/**
5855
* Register the given activity for auth result.
5956
* See [ComponentActivity.registerForActivityResult]
@@ -65,13 +62,8 @@ public object VkAuth {
6562
@JvmStatic
6663
public fun register(
6764
activity: ComponentActivity,
68-
overrideLaunchersMap: MutableMap<ComponentActivity, ActivityResultLauncher<Intent>>? = null,
6965
listener: (Result<VkAuthResult>) -> Unit,
70-
) {
71-
if (overrideLaunchersMap != null) {
72-
resultLaunchers = overrideLaunchersMap.also { map -> map.putAll(resultLaunchers) }
73-
}
74-
66+
): ActivityResultLauncher<Intent> {
7567
activity.addOnNewIntentListener { intent ->
7668
try {
7769
listener(
@@ -86,7 +78,7 @@ public object VkAuth {
8678
}
8779
}
8880

89-
val resultLauncher = activity.registerForActivityResult(
81+
return activity.registerForActivityResult(
9082
/* contract = */ ActivityResultContracts.StartActivityForResult(),
9183
) { result ->
9284
val vkResult = try {
@@ -105,22 +97,6 @@ public object VkAuth {
10597
listener(Result.failure(VkAuthException("Failed to parse the VK auth result: $result")))
10698
}
10799
}
108-
resultLaunchers[activity] = resultLauncher
109-
}
110-
111-
/**
112-
* Unregister the activity result listeners.
113-
*
114-
* @param activity An activity for which to unregister the listeners.
115-
* If null, all listeners will be unregistered.
116-
*/
117-
@JvmStatic
118-
public fun unregister(activity: ComponentActivity? = null) {
119-
if (activity != null) {
120-
resultLaunchers.remove(activity)
121-
} else {
122-
resultLaunchers.clear()
123-
}
124100
}
125101

126102
/**
@@ -173,6 +149,7 @@ public object VkAuth {
173149
@JvmStatic
174150
public fun login(
175151
activity: ComponentActivity,
152+
launcher: ActivityResultLauncher<Intent>,
176153
authParams: AuthParams,
177154
mode: AuthMode = AuthMode.Auto,
178155
) {
@@ -223,26 +200,20 @@ public object VkAuth {
223200
}
224201

225202
if (intent != null) {
226-
launchLogin(
227-
activity = activity,
228-
intent = intent,
229-
)
203+
launcher.launch(intent)
230204
}
231205
}
232206

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

236210
when {
237-
activity.customTabsSupported() -> {
211+
mode != AuthMode.RequireWebView && activity.customTabsSupported() -> {
238212
activity.startActivity(loadCustomTabsAuthUrlIntent(authParams.asQuery()))
239213
}
240214

241215
else -> {
242-
launchLogin(
243-
activity = activity,
244-
intent = VkAuthActivity.intent(activity, authParams),
245-
)
216+
launcher.launch(VkAuthActivity.intent(activity, authParams))
246217
}
247218
}
248219
}
@@ -365,10 +336,6 @@ public object VkAuth {
365336
}
366337
}
367338

368-
private fun launchLogin(activity: ComponentActivity, intent: Intent) {
369-
resultLaunchers[activity]?.launch(intent)
370-
}
371-
372339
/**
373340
* Response type: access_token or code
374341
*

docs/auth.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ The first param is `androidx.activity.ComponentActivity` (e.g. `androidx.appcomp
3232
class MainActivity : AppCompatActivity(R.layout.activity_main) {
3333
override fun onCreate(savedInstanceState: Bundle?) {
3434
// must be called before onCreate
35-
VkAuth.register(this) { result ->
35+
val launcher = VkAuth.register(this) { result ->
3636
Log.e("vk_auth", result.toString())
3737
}
3838
super.onCreate(savedInstanceState)
3939

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

8484
```xml
85+
8586
<activity android:name=".auth.YourAuthActivity" android:exported="true" android:launchMode="singleTop">
8687

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

100101
#### 4. Prepare your activity
101102

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

104105
#### Et voila!
105106

@@ -179,7 +180,7 @@ val params = VkAuth.AuthParams(
179180
apiVersion = apiVersion,
180181
)
181182

182-
VkAuth.login(activity, params, authMode)
183+
VkAuth.login(activity, launcher, params, authMode)
183184
```
184185

185186
Auth modes:
@@ -251,10 +252,10 @@ val callback = { result: VkAuthResult ->
251252
}
252253

253254
// before activity.onCreate
254-
VkAuth.register(activity, callback)
255+
val launcher = VkAuth.register(activity, callback)
255256

256257
// somewhere onClick
257-
VkAuth.login(activity, params)
258+
VkAuth.login(activity, launcher, params)
258259
```
259260

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

279280
// before activity.onCreate
280-
VkAuth.register(activity, callback)
281+
val launcher = VkAuth.register(activity, callback)
281282

282283
// somewhere onClick
283-
VkAuth.login(activity, params)
284+
VkAuth.login(activity, launcher, params)
284285
```

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
GROUP=com.petersamokhin.vksdk.android
2-
VERSION_NAME=1.0.1
2+
VERSION_NAME=1.1.0
33

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

0 commit comments

Comments
 (0)