Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.vk.api.sdk.VK.login
import com.vk.api.sdk.VK.logout
import com.vk.api.sdk.auth.VKAccessToken
import com.vk.api.sdk.auth.VKScope
import com.vk.api.sdk.utils.VKUtils
import com.vk.sdk.api.account.AccountService
import com.vk.sdk.api.users.UsersService
import com.vk.sdk.api.users.dto.UsersUserFull
Expand All @@ -27,6 +28,7 @@ class MethodCallHandler(private val context: Context, private val loginCallback:
private const val getAccessToken = "getAccessToken"
private const val getUserProfile = "getUserProfile"
private const val getSdkVersion = "getSdkVersion"
private const val getCertificateFingerprint = "getCertificateFingerprint"
private const val initSdk = "initSdk"
private const val scopeLoginArg = "scope"
private const val scopeInitArg = "scope"
Expand All @@ -52,6 +54,7 @@ class MethodCallHandler(private val context: Context, private val loginCallback:
getAccessToken -> sendResult(getAccessToken(), r)
getUserProfile -> getUserProfile(r)
getSdkVersion -> sendResult(getSdkVersion(), r)
getCertificateFingerprint -> sendResult(getCertificateFingerprint(), r)
initSdk -> {
val initScope = call.argument<List<String>?>(scopeInitArg)
initSdk(initScope, r)
Expand Down Expand Up @@ -155,6 +158,11 @@ class MethodCallHandler(private val context: Context, private val loginCallback:
return metaData.metaData["VKSdkVersion"].toString()
}

private fun getCertificateFingerprint(): String? {
val fingerprints: Array<String?>? = VKUtils.getCertificateFingerprint(context, context.packageName)
return fingerprints?.joinToString()
}

private fun sendResult(data: Any?, r: MethodChannel.Result) {
r.success(data)
}
Expand Down
49 changes: 27 additions & 22 deletions lib/src/vk_login.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:async/async.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
Expand All @@ -12,6 +14,7 @@ class VKLogin {
static const _methodGetAccessToken = 'getAccessToken';
static const _methodGetUserProfile = 'getUserProfile';
static const _methodGetSdkVersion = 'getSdkVersion';
static const _methodGetCertificateFingerprint = 'getCertificateFingerprint';

// TODO: rename to `permissions`?
static const _argLogInScope = 'scope';
Expand All @@ -35,16 +38,12 @@ class VKLogin {
///
/// If user is now logged in, than returns `null`.
Future<VKAccessToken?> get accessToken async {
assert(_initialized,
'SDK is not initialized. You should call initSdk() first');
assert(_initialized, 'SDK is not initialized. You should call initSdk() first');
if (!_initialized) return null;

final tokenResult = await _channel
.invokeMethod<Map<dynamic, dynamic>>(_methodGetAccessToken);
final tokenResult = await _channel.invokeMethod<Map<dynamic, dynamic>>(_methodGetAccessToken);

return tokenResult != null
? VKAccessToken.fromMap(tokenResult.cast<String, dynamic>())
: null;
return tokenResult != null ? VKAccessToken.fromMap(tokenResult.cast<String, dynamic>()) : null;
}

/// Returns currently used VK SDK.
Expand All @@ -68,8 +67,7 @@ class VKLogin {
/// You can pass [scope] (and/or [customScope], see [logIn])
/// to require listed permissions. If user logged in,
/// but doesn't have all of this permissions - he will be logged out.
Future<Result<bool>> initSdk(
{List<VKScope>? scope, List<String>? customScope}) async {
Future<Result<bool>> initSdk({List<VKScope>? scope, List<String>? customScope}) async {
final scopeArg = _getScope(scope: scope, customScope: customScope);

if (debug) {
Expand Down Expand Up @@ -108,14 +106,11 @@ class VKLogin {
}

try {
final result = await _channel
.invokeMethod<Map<dynamic, dynamic>>(_methodGetUserProfile);
final result = await _channel.invokeMethod<Map<dynamic, dynamic>>(_methodGetUserProfile);

if (debug) _log('User profile: $result');

return Result.value(result != null
? VKUserProfile.fromMap(result.cast<String, dynamic>())
: null);
return Result.value(result != null ? VKUserProfile.fromMap(result.cast<String, dynamic>()) : null);
} on PlatformException catch (e) {
if (debug) _log('Get profile error: $e');
return Result.error(e);
Expand Down Expand Up @@ -151,19 +146,16 @@ class VKLogin {
///
/// If error occure during log in process, than error result
/// will be returned. And [Result.error] may
Future<Result<VKLoginResult>> logIn(
{List<VKScope> scope = const [], List<String>? customScope}) async {
assert(_initialized,
'SDK is not initialized. You should call initSdk() first');
Future<Result<VKLoginResult>> logIn({List<VKScope> scope = const [], List<String>? customScope}) async {
assert(_initialized, 'SDK is not initialized. You should call initSdk() first');
if (!_initialized) throw Exception('SDK is not initialized.');

final scopeArg = _getScope(scope: scope, customScope: customScope);

if (debug) _log('Log In with scope $scopeArg');

try {
final res = await _channel.invokeMethod<Map<dynamic, dynamic>>(
_methodLogIn, {_argLogInScope: scopeArg});
final res = await _channel.invokeMethod<Map<dynamic, dynamic>>(_methodLogIn, {_argLogInScope: scopeArg});

if (res == null) {
return Result.error('Invalid null result');
Expand All @@ -176,9 +168,22 @@ class VKLogin {
}
}

/// Get fingerprints for android
Future<String?> getCertificateFingerprint() async {
assert(_initialized, 'SDK is not initialized. You should call initSdk() first');
assert(Platform.isAndroid, 'This method is only available for android.');
if (!_initialized) throw Exception('SDK is not initialized.');
if (!Platform.isAndroid) return null;

try {
return await _channel.invokeMethod<String?>(_methodGetCertificateFingerprint);
} on PlatformException catch (e) {
rethrow;
}
}

Future<void> logOut() async {
assert(_initialized,
'SDK is not initialized. You should call initSdk() first');
assert(_initialized, 'SDK is not initialized. You should call initSdk() first');
if (!_initialized) return;

if (debug) _log('Log Out');
Expand Down