carp_webservices 1.1.1
carp_webservices: ^1.1.1 copied to clipboard
Flutter API for accessing the CARP web services - authentication, file management, data points, and app-specific collections of documents.
example/lib/main.dart
import 'package:carp_mobile_sensing/carp_mobile_sensing.dart';
import 'package:flutter/material.dart';
import 'package:carp_webservices/carp_services/carp_services.dart';
import 'package:carp_webservices/carp_auth/carp_auth.dart';
import 'package:carp_core/carp_core.dart';
void main() {
CarpMobileSensing.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'CARP Backend Demo',
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
void initState() {
super.initState();
bloc.init();
}
void dispose() {
bloc.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
height: 80,
width: 200,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
child: TextButton.icon(
onPressed: () => bloc.authenticate(
context,
username: 'user@dtu.dk',
),
icon: Icon(Icons.login),
label: Text(
'LOGIN',
style: TextStyle(fontSize: 35),
),
),
),
Container(
height: 80,
width: 400,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
child: TextButton.icon(
onPressed: () => bloc.getStudyInvitation(context),
icon: Icon(Icons.mail),
label: Text(
'GET STUDY',
style: TextStyle(fontSize: 35),
),
),
),
StreamBuilder(
stream: CarpService().authStateChanges,
builder: (BuildContext context, AsyncSnapshot<AuthEvent> event) =>
Padding(
padding: EdgeInsets.fromLTRB(10, 30, 10, 0),
child: Text(
'Authentication status: ${(CarpService().authenticated) ? 'Authenticated' : 'Not authenticated'}')),
)
],
)),
);
}
}
class AppBLoC {
final String uri = "https://cans.cachet.dk/dev";
ActiveParticipationInvitation? _invitation;
String? get studyId => _invitation?.studyId;
String? get studyDeploymentId => _invitation?.studyDeploymentId;
CarpApp? _app;
CarpApp? get app => _app;
Future<void> init() async {
_app = CarpApp(
name: 'carp_backend_example_app',
uri: Uri.parse(uri),
oauth: OAuthEndPoint(clientID: 'carp', clientSecret: 'carp'),
);
CarpService().configure(app!);
}
void dispose() async {}
Future<void> authenticate(BuildContext context, {String? username}) async =>
await CarpService().authenticateWithDialog(
context,
username: username,
allowClose: true,
);
Future<ActiveParticipationInvitation?> getStudyInvitation(
BuildContext context) async {
// configure a participant service based on the carp service already configured
CarpParticipationService().configureFrom(CarpService());
_invitation = await CarpParticipationService().getStudyInvitation(context);
print('CARP Study Invitation: $_invitation');
// check that the app has been updated to reflect the study id and deployment id
print(
'Study ID: ${app?.studyId}, Deployment ID: ${app?.studyDeploymentId}');
return _invitation;
}
}
final bloc = AppBLoC();