chabok_flutter 1.0.0 chabok_flutter: ^1.0.0 copied to clipboard
Chabok Push & Realtime Messaging Service.
import 'dart:collection';
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:chabok_flutter/Chabok.dart';
import 'package:chabok_flutter/user/Profile.dart';
import 'package:chabok_flutter/user/Gender.dart';
import 'package:flutter/foundation.dart';
import 'dart:developer' as developer;
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
final TextEditingController loginKey = TextEditingController();
final TextEditingController loginValue = TextEditingController();
final TextEditingController attributeKey = TextEditingController();
final TextEditingController attributeValue = TextEditingController();
final TextEditingController eventKey = TextEditingController();
final TextEditingController eventValue = TextEditingController();
final TextEditingController tag = TextEditingController();
final TextEditingController profileName = TextEditingController();
final TextEditingController profileFamily = TextEditingController();
final TextEditingController profileEmail= TextEditingController();
final TextEditingController profileMobile = TextEditingController();
Color connectionColor = Colors.red;
String connectionString = "UNKNOWN";
String logString = "";
@override
void initState() {
WidgetsBinding.instance!.addObserver(this);
Chabok.user().isLoggedIn().then((isLoggedIn) {
setState(() {
// Map<String, dynamic> responseJson = json.decode(response);
// final String message = responseJson['message'];
// final bool success = responseJson['success'];
if(isLoggedIn) {
print('(CHABOK): Flutter ~~~~~~ User is loggedIn!');
} else {
print('(CHABOK): Flutter ~~~~~~ User is not loggedIn!');
}
});
});
Chabok.user().getUsername().then((username) {
setState(() {
print('(CHABOK): Flutter ~~~~~~ Username: $username!');
});
});
// Timer(Duration(seconds: 10), () {
// Chabok.setNotificationPermissionStatus(true);
// });
addNotificationHandler();
super.initState();
//debugPrint('(CHABOK)');
}
@override
void deactivate() {
// print('deactivate() invoked.');
super.deactivate();
}
@override
void dispose() {
// print('dispose() invoked.');
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// print('state = $state');
}
//======================
addNotificationHandler() {
print('(CHABOK): Flutter ~~~~~~ Notification handler called ~~~~~~');
Chabok.message().addNotificationHandler((data) {
print('(CHABOK): Flutter ~~~~~~ New notification data received ~~~> $data');
Map<String, dynamic> responseJson = json.decode(data);
});
}
login() {
print('(CHABOK): Flutter ~~~~~~ login called');
Chabok.user().login(loginKey.text.toString()).then((response) {
setState(() {
Map<String, dynamic> responseJson = json.decode(response);
final String message = responseJson['message'];
final bool success = responseJson['success'];
if(success) {
print('(CHABOK): Flutter ~~~~~~ logged in suucessfully');
} else {
print('(CHABOK): Flutter ~~~~~~ error in login $message');
}
});
});
}
logout() {
Chabok.user().logout().then((response) {
setState(() {
Map<String, dynamic> responseJson = json.decode(response);
final String message = responseJson['message'];
final bool success = responseJson['success'];
if(success) {
print('(CHABOK): Flutter ~~~~~~ logged out suucessfully');
} else {
print('(CHABOK): Flutter ~~~~~~ error in logout $message');
}
});
});
}
setUserTag() {
Chabok.user().setTag(tag.text.toString()).then((response) {
setState(() {
Map<String, dynamic> responseJson = json.decode(response);
final String message = responseJson['message'];
final bool success = responseJson['success'];
if(success) {
print('(CHABOK): Flutter ~~~~~~ set user tag suucessfully');
} else {
print('(CHABOK): Flutter ~~~~~~ error in set user tag $message');
}
});
});
}
unsetUserTag() {
Chabok.user().unsetTag(tag.text.toString()).then((response) {
setState(() {
Map<String, dynamic> responseJson = json.decode(response);
final String message = responseJson['message'];
final bool success = responseJson['success'];
if(success) {
print('(CHABOK): Flutter ~~~~~~ unset user tag suucessfully');
} else {
print('(CHABOK): Flutter ~~~~~~ error in unset user tag $message');
}
});
});
}
setUserAttribute() {
Chabok.user().setAttribute(attributeKey.text.toString(), attributeValue.text.toString()).then((response) {
setState(() {
Map<String, dynamic> responseJson = json.decode(response);
final String message = responseJson['message'];
final bool success = responseJson['success'];
if(success) {
print('(CHABOK): Flutter ~~~~~~ set user attribute suucessfully');
} else {
print('(CHABOK): Flutter ~~~~~~ error in set user attribute $message');
}
});
});
}
unsetUserAttribute() {
Chabok.user().unsetAttribute(attributeKey.text.toString()).then((response) {
setState(() {
Map<String, dynamic> responseJson = json.decode(response);
final String message = responseJson['message'];
final bool success = responseJson['success'];
if(success) {
print('(CHABOK): Flutter ~~~~~~ unset user attribute suucessfully');
} else {
print('(CHABOK): Flutter ~~~~~~ error in unset user attribute $message');
}
});
});
}
setUserProfile() {
final profile = Profile();
profile.email = profileEmail.text.toString();
profile.phoneNumber = profileMobile.text.toString();
profile.firstName = profileName.text.toString();
profile.lastName = profileFamily.text.toString();
profile.birthDate = 76540911;
profile.gender = Gender.MALE;
Chabok.user().setProfile(profile).then((response) {
setState(() {
Map<String, dynamic> responseJson = json.decode(response);
final String message = responseJson['message'];
final bool success = responseJson['success'];
if(success) {
print('(CHABOK): Flutter ~~~~~~ set user profile suucessfully');
} else {
print('(CHABOK): Flutter ~~~~~~ error in set user profile $message');
}
});
});
}
@override
Widget build(BuildContext context) {
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 10.0);
Color color = Theme.of(context).primaryColor;
Widget loginSection = Container(
padding: const EdgeInsets.only(left: 15,right: 15),
child: Column(
children: [
Container(
margin: const EdgeInsets.only(top: 15),
child: Row(
children: [
Text(
'Login',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
fontWeight: FontWeight.bold
)
)
]
),
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 15),
height: 30,
child: Padding(
padding: const EdgeInsets.only(right: 15),
child: TextField(
controller: loginKey,
decoration: InputDecoration(
hintText: 'key'
)
),
),
),
),
],
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
width: 200,
padding: const EdgeInsets.all(8),
child: MaterialButton(
color: color,
onPressed: login,
child: Text("LOGIN",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold
)
)
)
),
),
Expanded(
flex: 1,
child: Container(
width: 200,
padding: const EdgeInsets.all(8),
child: MaterialButton(
color: color,
onPressed: logout,
child: Text("LOGOUT",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold
)
)
)
),
),
],
),
],
),
);
Widget attribueSection = Container(
padding: const EdgeInsets.only(left: 15,right: 15),
child: Column(
children: [
Container(
margin: const EdgeInsets.only(top: 15),
child: Row(
children: [
Text(
'Attribute',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
fontWeight: FontWeight.bold
)
)
]
),
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 15),
height: 30,
child: Padding(
padding: const EdgeInsets.only(right: 15),
child: TextField(
controller: attributeKey,
decoration: InputDecoration(
hintText: 'key'
)
),
),
),
),
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 15),
height: 30,
child: Padding(
padding: const EdgeInsets.only(left: 15),
child: TextField(
controller: attributeValue,
decoration: InputDecoration(
hintText: 'value'
)
),
),
),
),
],
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
width: 200,
padding: const EdgeInsets.all(8),
child: MaterialButton(
color: color,
onPressed: setUserAttribute,
child: Text("SET ATTRIBUTE",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold
)
)
)
),
),
Expanded(
flex: 1,
child: Container(
width: 200,
padding: const EdgeInsets.all(8),
child: MaterialButton(
color: color,
onPressed: unsetUserAttribute,
child: Text("UNSET ATTRIBUTE",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold
)
)
)
),
),
],
),
],
),
);
Widget tagSection = Container(
padding: const EdgeInsets.only(left: 15,right: 15),
child: Column(
children: [
Container(
margin: const EdgeInsets.only(top: 15),
child: Row(
children: [
Text(
'Tag',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
fontWeight: FontWeight.bold
)
)
]
),
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 15),
height: 30,
child: Padding(
padding: const EdgeInsets.only(right: 15),
child: TextField(
controller: tag,
decoration: InputDecoration(
hintText: 'tag'
)
),
),
),
),
],
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
width: 200,
padding: const EdgeInsets.all(8),
child: MaterialButton(
color: color,
onPressed: setUserTag,
child: Text("SET TAG",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold
)
)
)
),
),
Expanded(
flex: 1,
child: Container(
width: 200,
padding: const EdgeInsets.all(8),
child: MaterialButton(
color: color,
onPressed: unsetUserTag,
child: Text("UNSET TAG",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold
)
)
)
),
),
],
),
],
),
);
Widget profileSection = Container(
padding: const EdgeInsets.only(left: 15,right: 15),
child: Column(
children: [
Container(
margin: const EdgeInsets.only(top: 15),
child: Row(
children: [
Text(
'Profile',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
fontWeight: FontWeight.bold
)
)
]
),
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 15),
height: 30,
child: Padding(
padding: const EdgeInsets.only(right: 15),
child: TextField(
controller: profileName,
decoration: InputDecoration(
hintText: 'name'
)
),
),
),
),
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 15),
height: 30,
child: Padding(
padding: const EdgeInsets.only(left: 15),
child: TextField(
controller: profileFamily,
decoration: InputDecoration(
hintText: 'family'
)
),
),
),
),
],
),
Row(
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 30),
height: 30,
child: Padding(
padding: const EdgeInsets.only(right: 15),
child: TextField(
controller: profileEmail,
decoration: InputDecoration(
hintText: 'email'
)
),
),
),
),
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(top: 30),
height: 30,
child: Padding(
padding: const EdgeInsets.only(left: 15),
child: TextField(
controller: profileMobile,
decoration: InputDecoration(
hintText: 'mobile'
)
),
),
),
),
],
),
Container(
width: 200,
padding: const EdgeInsets.all(8),
child: MaterialButton(
color: color,
onPressed: setUserProfile,
child: Text("SET PROFILE",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold
)
)
)
)
],
),
);
return MaterialApp(
title: 'Chabok Demo Flutter',
home: Scaffold(
// appBar: AppBar(
// title: Text('Chabok Demo'),
// ),
body: ListView(
children: [
loginSection,
Divider(),
attribueSection,
Divider(),
tagSection,
Divider(),
profileSection,
],
),
),
);
}
}