init
This commit is contained in:
116
lib/backend/DataManager.dart
Normal file
116
lib/backend/DataManager.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
import 'dart:convert';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:queue_client/backend/DebugHelper.dart';
|
||||
// DateFormat dateFormat;
|
||||
|
||||
final String API_ENDPOINT= "https://vps.playpoolstudios.com/qms/api/";
|
||||
|
||||
final dateTimeFormat = DateFormat("yyyy-MM-dd hh:mm");
|
||||
|
||||
class DataManager{
|
||||
static _DataManager? m_instance = null;
|
||||
static _DataManager instance(){
|
||||
m_instance ??= _DataManager();
|
||||
return m_instance!;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class _DataManager{
|
||||
_DataManager();
|
||||
|
||||
List<dynamic> services = [];
|
||||
List<Map<String, dynamic>> AvailableServices = [];
|
||||
List<Map<String, dynamic>> JoinedServices = [];
|
||||
|
||||
int userId = -1;
|
||||
|
||||
Future<String> Login(String username,String password) async{
|
||||
String responseTxt = "";
|
||||
try{
|
||||
var response = (await http.post(
|
||||
Uri.parse('${API_ENDPOINT}login.php'),
|
||||
body: <String, String>{
|
||||
'username':username,
|
||||
'password':password
|
||||
}));
|
||||
responseTxt = response.body.toString();
|
||||
int result = int.parse(response.body.toString());
|
||||
userId = result;
|
||||
return "0";
|
||||
}catch(e){
|
||||
Debug.LogError(e);
|
||||
return responseTxt;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> GetData() async{
|
||||
try{
|
||||
var response = (await http.post(
|
||||
Uri.parse('${API_ENDPOINT}get_services.php'),
|
||||
body: <String, String>{}));
|
||||
Debug.LogResponse(response.body.toString());
|
||||
services = jsonDecode(response.body.toString());
|
||||
Debug.LogResponse(services);
|
||||
}catch(e){
|
||||
Debug.LogError(e);
|
||||
}
|
||||
|
||||
ProcessServices();
|
||||
}
|
||||
|
||||
void ProcessServices(){
|
||||
AvailableServices=[];
|
||||
JoinedServices=[];
|
||||
for (var m_service in services) {
|
||||
Map<String,dynamic> service= jsonDecode(m_service);
|
||||
List<String> members = service['members'].toString().split(',');
|
||||
|
||||
if(members.contains(userId.toString())){
|
||||
int tokenId = 0;
|
||||
for(int i=0;i<members.length; i++){
|
||||
if(members[i] == userId.toString()){
|
||||
tokenId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
service.putIfAbsent("tokenId", () => tokenId);
|
||||
JoinedServices.add(service);
|
||||
}else{
|
||||
AvailableServices.add(service);
|
||||
}
|
||||
|
||||
m_service = jsonEncode(service);
|
||||
}
|
||||
}
|
||||
|
||||
dynamic GetServiceFromID(int id){
|
||||
for (var service in services) {
|
||||
Map<String, dynamic> serviceData = jsonDecode(service);
|
||||
if(serviceData['id']==id.toString()){
|
||||
return serviceData;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> JoinService(int id) async {
|
||||
try{
|
||||
var response = (await http.post(
|
||||
Uri.parse('${API_ENDPOINT}join_service.php'),
|
||||
body: <String, String>{
|
||||
'service_id':id.toString(),
|
||||
'user_id':userId.toString(),
|
||||
}));
|
||||
Debug.LogResponse(response.body.toString());
|
||||
services = jsonDecode(response.body.toString());
|
||||
Debug.LogResponse(services);
|
||||
}catch(e){
|
||||
Debug.LogError(e);
|
||||
}
|
||||
|
||||
ProcessServices();
|
||||
}
|
||||
}
|
||||
32
lib/backend/DebugHelper.dart
Normal file
32
lib/backend/DebugHelper.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'dart:developer';
|
||||
|
||||
class Debug{
|
||||
static bool enableLogging = true;
|
||||
static bool enableResponseLogging = true;
|
||||
static bool enableErrorLoggin = true;
|
||||
static bool enableTestLogging = true;
|
||||
|
||||
static void LogResponse(Object? response, {Object src= ''}){
|
||||
if(!enableLogging){return;}
|
||||
if(!enableResponseLogging) {return;}
|
||||
print('\x1B[32m$src response\n$response\x1B[0m');
|
||||
}
|
||||
|
||||
static void LogError(Object? msg){
|
||||
if(!enableLogging){return;}
|
||||
if(!enableErrorLoggin) {return;}
|
||||
print('\x1B[31m$msg\x1B[0m');
|
||||
}
|
||||
|
||||
static void Log(Object? msg){
|
||||
if(!enableLogging) {return;}
|
||||
print('\x1B[36m$msg\x1B[0m');
|
||||
}
|
||||
|
||||
static void LogTest(Object? msg){
|
||||
if(!enableLogging){return;}
|
||||
if(!enableTestLogging) {return;}
|
||||
print('\x1B[35m$msg\x1B[0m');
|
||||
}
|
||||
|
||||
}
|
||||
76
lib/backend/Dialogs.dart
Normal file
76
lib/backend/Dialogs.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
// import 'package:fhub/backend/login_mgr.dart';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
// import 'package:flutter_spinkit/flutter_spinkit.dart';
|
||||
// import 'package:shared_preferences/shared_preferences.dart';
|
||||
// import 'package:url_launcher/url_launcher.dart';
|
||||
import '../main.dart';
|
||||
|
||||
class Dialogs{
|
||||
|
||||
static showAlertDialog(BuildContext context, String title, String message) {
|
||||
// set up the button
|
||||
Widget okButton = TextButton(
|
||||
child: Text("OK"),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
);
|
||||
|
||||
// set up the AlertDialog
|
||||
AlertDialog alert = AlertDialog(
|
||||
backgroundColor: Color(0xFF1F1F1F),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
|
||||
title: Text(title,textAlign: TextAlign.center,),
|
||||
content: Text(message,textAlign: TextAlign.center,),
|
||||
actions: [
|
||||
okButton,
|
||||
],
|
||||
|
||||
);
|
||||
|
||||
// show the dialog
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return alert;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static bool showing = false;
|
||||
static BuildContext? context;
|
||||
static waiting(){
|
||||
showing=true;
|
||||
// context=navigatorKey.currentContext;
|
||||
if(context!=null) {
|
||||
return showDialog(
|
||||
context: context!,
|
||||
barrierDismissible: false,
|
||||
routeSettings: const RouteSettings(name: "Progress"),
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
|
||||
backgroundColor: Color(0xaa101010),
|
||||
title: Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
// SpinKitChasingDots(color: Colors.green),
|
||||
Expanded(child: Text("Loading",textAlign: TextAlign.center,)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
// static hide(){
|
||||
// showing=false;
|
||||
// Navigator.of(navigatorKey.currentContext!).popUntil((route){
|
||||
// return route.settings.name!="Progress";
|
||||
// });
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user