Skip to content

Commit

Permalink
Log out and log in work
Browse files Browse the repository at this point in the history
  • Loading branch information
0teh committed Aug 31, 2022
1 parent db68725 commit b627a54
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 15 deletions.
7 changes: 0 additions & 7 deletions app/android/app/src/debug/AndroidManifest.xml

This file was deleted.

1 change: 1 addition & 0 deletions app/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<application
android:label="PharMe"
android:icon="@mipmap/launcher_icon"
android:theme="@style/NormalTheme"
android:usesCleartextTraffic="false"
android:name="${applicationName}"
>
Expand Down
63 changes: 62 additions & 1 deletion app/android/app/src/main/kotlin/de/hpi/frasecys/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import care.data4life.sdk.Data4LifeClient
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.os.Bundle
import care.data4life.sdk.listener.ResultListener
import care.data4life.sdk.lang.D4LException

class MainActivity : FlutterActivity() {

Expand All @@ -23,9 +25,68 @@ class MainActivity : FlutterActivity() {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "chdp")
.setMethodCallHandler { call, result ->
when (call.method) {
"isLoggedIn" -> result.success(true)
"isLoggedIn" -> Data4LifeClient.getInstance().isUserLoggedIn(
object : ResultListener<Boolean> {
override fun onSuccess(t: Boolean) = result.success(t)

override fun onError(exception: D4LException) =
result.error(
"D4LException",
"Failed to check login status",
exception
)
}
)
"login" -> {
val intent = Data4LifeClient.getInstance().getLoginIntent(
this@MainActivity,
setOf(
"perm:r",
"rec:r",
"rec:w",
"attachment:r",
"attachment:w",
"user:r",
"user:w",
"user:q",
)
)

this@MainActivity.startActivityForResult(intent, Data4LifeClient.D4L_AUTH)

result.success(0)
}
"logout" -> {
Data4LifeClient.getInstance().logout(
object : care.data4life.sdk.listener.Callback {
override fun onSuccess() = result.success(0)

override fun onError(exception: D4LException) =
result.error(
"D4LException",
"Failed to logout",
exception
)
}
)

}
else -> result.notImplemented()
}
}
}

override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: android.content.Intent?,
) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == Data4LifeClient.D4L_AUTH) {
if (resultCode == android.app.Activity.RESULT_OK) {
android.util.Log.i("PHARME", "login successful")
}
}
}
}
4 changes: 2 additions & 2 deletions app/android/app/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is on -->
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<style name="LaunchTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
Expand All @@ -12,7 +12,7 @@
running.
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<style name="NormalTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowBackground">?android:colorBackground</item>
</style>
</resources>
4 changes: 2 additions & 2 deletions app/android/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off -->
<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
Expand All @@ -12,7 +12,7 @@
running.
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
<style name="NormalTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">?android:colorBackground</item>
</style>
</resources>
48 changes: 45 additions & 3 deletions app/lib/settings/chdp_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,48 @@ class ChdpListTile extends StatefulWidget {
State<ChdpListTile> createState() => _ChdpState();
}

class _ChdpState extends State<ChdpListTile> {
class _ChdpState extends State<ChdpListTile> with WidgetsBindingObserver {
static const platform = MethodChannel('chdp');

bool _isLoggedIn = false;

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
getIsLoggedIn().whenComplete(() => null);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
getIsLoggedIn().whenComplete(() => null);
}
}

@override
Widget build(BuildContext context) {
final title = _isLoggedIn
? Text('Log out of Smart4Health')
: Text('Log in to Smart4Health');

return ListTile(
title: Text('Is logged in? $_isLoggedIn'),
);
title: title,
trailing: _isLoggedIn ? null : Icon(Icons.chevron_right),
onTap: () => {
// are you okay intellij autoformatter????
if (_isLoggedIn)
{logout().whenComplete(getIsLoggedIn)}
else
{login().whenComplete(() => null)}
},
); // ListTile
}

Future<void> getIsLoggedIn() async {
Expand All @@ -39,4 +65,20 @@ class _ChdpState extends State<ChdpListTile> {
_isLoggedIn = isLoggedIn;
});
}

Future<void> login() async {
try {
await platform.invokeMethod('login');
} on PlatformException catch (e) {
debugPrint('platform exception in login');
}
}

Future<void> logout() async {
try {
await platform.invokeMethod('logout');
} on PlatformException catch (e) {
debugPrint('platform exception in logout');
}
}
}

0 comments on commit b627a54

Please sign in to comment.