Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to work null safe on modern android mobile and migrate to AndroidX. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {
clientSecret: "XXX_CLIENT_SECRET_XXX",
redirectUrl: "https://kunstmaan.github.io/flutter_slack_oauth/success.html",
onSuccess: () async {
String accessToken = await Token.getLocalAccessToken();
String accessToken = (await Token.getLocalAccessToken())!;
UserList users = await slack.getUsers(accessToken);

Scaffold.of(context).showSnackBar(new SnackBar(
Expand Down
10 changes: 5 additions & 5 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 27
compileSdkVersion 30

lintOptions {
disable 'InvalidPackage'
Expand All @@ -25,10 +25,10 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "kunstmaan.be.example"
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

buildTypes {
Expand All @@ -46,6 +46,6 @@ flutter {

dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
22 changes: 18 additions & 4 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="example"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -23,17 +22,32 @@
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specify that the launch screen should continue being displayed -->
<!-- until Flutter renders its first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background" />

<!-- Theme to apply as soon as Flutter begins rendering frames -->
<!--
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
-->
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package kunstmaan.be.example;

import android.os.Bundle;

import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}

}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:4.1.0'
}
}

Expand Down
2 changes: 2 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
3 changes: 2 additions & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip

1 change: 1 addition & 0 deletions example/android/settings_aar.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':app'
20 changes: 9 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_slack_oauth/flutter_slack_oauth.dart';

import 'package:flutter_slack_oauth/oauth/slack.dart' as slack;

void main() {
Expand All @@ -18,24 +17,23 @@ void main() {
redirectUrl:
"https://kunstmaan.github.io/flutter_slack_oauth/success.html",
onSuccess: () async {
String accessToken = await Token.getLocalAccessToken();
String accessToken = (await Token.getLocalAccessToken())!;
UserList users = await slack.getUsers(accessToken);

Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text('We found ' +
users.users.length.toString() +
' users'),
));
content: new Text(
'We found ' + users.users.length.toString() + ' users'),
));
print((await slack.getUserIdentity(accessToken)).user.name);
},
onFailure: () {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text('Slack Login Failed'),
));
content: new Text('Slack Login Failed'),
));
},
onCancelledByUser: () {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text('Slack Login Cancelled by user'),
));
content: new Text('Slack Login Cancelled by user'),
));
},
),
);
Expand Down
5 changes: 4 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
name: example
description: flutter_slack_oauth example

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.0
cupertino_icons: ^1.0.3

flutter_slack_oauth:
path: ..
Expand Down
45 changes: 22 additions & 23 deletions lib/flutter_slack_oauth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ class SlackButton extends StatelessWidget {
final List scope;

const SlackButton(
{@required this.clientId,
@required this.clientSecret,
@required this.onSuccess,
@required this.onCancelledByUser,
@required this.onFailure,
this.scope = const [
'identity.basic',
'identity.team',
'identity.email'
],
this.redirectUrl});
{required this.clientId,
required this.clientSecret,
required this.onSuccess,
required this.onCancelledByUser,
required this.onFailure,
this.scope = const ['identity.basic', 'identity.team', 'identity.email'],
required this.redirectUrl});

bool get enabled => onSuccess != null;

Expand All @@ -53,24 +49,27 @@ class SlackButton extends StatelessWidget {
}

onTap(BuildContext context) async {
bool success = await Navigator.of(context).push(new MaterialPageRoute<bool>(
bool? success =
await Navigator.of(context).push(new MaterialPageRoute<bool>(
builder: (BuildContext context) => new SlackLoginWebViewPage(
clientId: clientId,
clientSecret: clientSecret,
scope: scope,
redirectUrl: redirectUrl == null
? "https://kunstmaan.github.io/flutter_slack_oauth/success.html"
: redirectUrl,
),
clientId: clientId,
clientSecret: clientSecret,
scope: scope,
redirectUrl: redirectUrl == null
? "https://kunstmaan.github.io/flutter_slack_oauth/success.html"
: redirectUrl,
),
));

// if success == null, user just closed the webview
if (success == null) {
onCancelledByUser();
} else if (success == false) {
onFailure();
} else if (success) {
onSuccess();
} else {
if (success) {
onSuccess();
} else {
onFailure();
}
}
}
}
14 changes: 6 additions & 8 deletions lib/oauth/generic_slack_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ class GenericSlackButton extends StatelessWidget {

final String clientId;
final String clientSecret;
final String redirectUrl;

const GenericSlackButton(
{@required this.clientId,
@required this.clientSecret,
@required this.onSuccess,
@required this.onCancelledByUser,
@required this.onFailure,
@required this.onTap,
this.redirectUrl});
{required this.clientId,
required this.clientSecret,
required this.onSuccess,
required this.onCancelledByUser,
required this.onFailure,
required this.onTap});

bool get enabled => onSuccess != null;

Expand Down
11 changes: 4 additions & 7 deletions lib/oauth/model/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ import 'package:shared_preferences/shared_preferences.dart';
/// Recommended to store the access token in LocalStorage, so users don't need to log in every time
class Token {
String accessToken;
String tokenType;

Token.fromMap(Map json) {
accessToken = json['access_token'];
tokenType = json['token_type'];
}
Token.fromMap(Map json)
: this.accessToken = json['access_token'];

static Future<String> getLocalAccessToken() async {
static Future<String?> getLocalAccessToken() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String accessToken = prefs.getString("slack_access_token");
String? accessToken = prefs.getString("slack_access_token");
return accessToken;
}

Expand Down
35 changes: 16 additions & 19 deletions lib/oauth/model/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ class UserList {
bool ok;
List<User> users;

UserList.fromMap(Map json) {
ok = json['ok'];
users = new List<User>();

UserList.fromMap(Map json)
: this.ok = json['ok'],
this.users = new List.empty(growable: true) {
List<Map> members = (json['members'] as List).cast();

for (int i = 0; i < members.length; ++i) {
Expand All @@ -21,27 +20,25 @@ class User {
bool isBot;
bool deleted;

User.fromMap(Map json) {
profile = new Profile.fromMap(json['profile']);
id = json['id'];
name = json['name'];
isBot = json['is_bot'];
deleted = json['deleted'];
}
User.fromMap(Map json)
: this.profile = new Profile.fromMap(json['profile']),
this.id = json['id'],
this.name = json['name'],
this.isBot = json['is_bot'],
this.deleted = json['deleted'];
}

class Profile {
String statusText;
String realName;
String email;
// String email;
String image192;
String image512;

Profile.fromMap(Map json) {
statusText = json['status_text'];
realName = json['real_name'];
email = json['email'];
image192 = json['image_192'];
image512 = json['image_512'];
}
Profile.fromMap(Map json)
: this.statusText = json['status_text'],
this.realName = json['real_name'],
// this.email = json['email'],
this.image192 = json['image_192'],
this.image512 = json['image_512'];
}
20 changes: 7 additions & 13 deletions lib/oauth/model/user_identity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ class UserIdentity {
User user;
Team team;

UserIdentity.fromMap(Map json) {
ok = json['ok'];
user = new User.fromMap(json['user']);
team = new Team.fromMap(json['team']);
}
UserIdentity.fromMap(Map json) : this.ok = json['ok'],
this.user = new User.fromMap(json['user']),
this.team = new Team.fromMap(json['team']);
}

class User {
String name;
String id;

User.fromMap(Map json) {
id = json['id'];
name = json['name'];
}
User.fromMap(Map json)
: this.id = json['id'],
this.name = json['name'];
}

class Team {
String id;

Team.fromMap(Map json) {
id = json['id'];
}
Team.fromMap(Map json) : this.id = json['id'];
}
Loading