232 lines
6.2 KiB
Dart
232 lines
6.2 KiB
Dart
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';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
// DateFormat dateFormat;
|
|
|
|
final String API_ENDPOINT= "https://vps.playpoolstudios.com/qms/api/";
|
|
|
|
final dateTimeFormat = DateFormat("MM/dd hh:mm a");
|
|
final timeFormat = DateFormat("hh:mm a");
|
|
|
|
class DataManager{
|
|
static _DataManager? m_instance = null;
|
|
static _DataManager instance(){
|
|
m_instance ??= _DataManager();
|
|
return m_instance!;
|
|
}
|
|
}
|
|
|
|
|
|
class _DataManager{
|
|
_DataManager();
|
|
|
|
List<dynamic> services = [];
|
|
List<dynamic> joinedServices=[];
|
|
List<Map<String, dynamic>> AvailableServices = [];
|
|
List<Map<String, dynamic>> JoinedServices = [];
|
|
String Username = "";
|
|
int userId = -1;
|
|
|
|
Future<String> AutoLogin() async{
|
|
final Prefs = await SharedPreferences.getInstance();
|
|
if(Prefs.containsKey("username") && Prefs.containsKey("password")){
|
|
return await Login(Prefs.getString("username") ?? "default",Prefs.getString("password") ?? "default");
|
|
}
|
|
|
|
return "-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;
|
|
final Prefs = await SharedPreferences.getInstance();
|
|
Prefs.setString("username", username);
|
|
Prefs.setString("password",password);
|
|
Username = username;
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
await ProcessServices();
|
|
}
|
|
|
|
Future<void> ProcessServices() async{
|
|
try{
|
|
var response = (await http.post(
|
|
Uri.parse('${API_ENDPOINT}get_joined_services.php'),
|
|
body: <String, String>{
|
|
'userId':userId.toString()
|
|
}));
|
|
|
|
Debug.LogResponse(response.body.toString());
|
|
joinedServices = jsonDecode(response.body.toString());
|
|
Debug.LogResponse(services,src:"services");
|
|
Debug.LogResponse(joinedServices, src:"joined services");
|
|
}catch(e){
|
|
Debug.LogError(e);
|
|
}
|
|
AvailableServices=[];
|
|
JoinedServices=[];
|
|
for (var m_service in services) {
|
|
Map<String,dynamic> service = jsonDecode(m_service);
|
|
if(DateTime.parse(service['end_time']).isBefore(DateTime.now())){
|
|
continue;
|
|
}
|
|
bool joined = false;
|
|
for (var m_joinedService in joinedServices){
|
|
Map<String, dynamic> joinedSer = jsonDecode(m_joinedService);
|
|
if(service['id'] == jsonDecode(m_joinedService)['service_id']){
|
|
//Joined this
|
|
JoinedServices.add(jsonDecode(m_joinedService));
|
|
joined=true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
Debug.LogResponse(JoinedServices, src: "Joined ser");
|
|
|
|
if(!joined){
|
|
AvailableServices.add(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);
|
|
}
|
|
|
|
await ProcessServices();
|
|
}
|
|
}
|
|
|
|
class Helpers{
|
|
static int isPhoneNumber(String number){
|
|
if(number.length != 10){
|
|
return 1;
|
|
}
|
|
if(number[0] != "0"){
|
|
return 2;
|
|
}
|
|
|
|
|
|
|
|
if(number[1] != "7"){
|
|
return 3;
|
|
}
|
|
if(number[2] == "3" || number[3] == "9"){
|
|
return 4;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static String DateTimeToRelative(DateTime dt){
|
|
Duration diff = dt.difference(DateTime.now());
|
|
if(diff.isNegative){return "Past";}
|
|
|
|
final now = DateTime.now();
|
|
final today = DateTime(now.year, now.month, now.day);
|
|
final yesterday = DateTime(now.year, now.month, now.day - 1);
|
|
final tomorrow = DateTime(now.year, now.month, now.day + 1);
|
|
final dateToCheck = dt;
|
|
final aDate = DateTime(dateToCheck.year, dateToCheck.month, dateToCheck.day);
|
|
if(aDate == today) {
|
|
return "Today at ${timeFormat.format(dt)}";
|
|
} else if(aDate == tomorrow) {
|
|
return "Tomorrow at ${timeFormat.format(dt)}";
|
|
}
|
|
|
|
if(now.month == dt.month){
|
|
String day = dt.day.toString();
|
|
if(day == "1" || day=="21" || day=="31"){
|
|
return "${day}st at ${timeFormat.format(dt)}";
|
|
}else if(day =="2" || day=="22"){
|
|
return "${day}nd at ${timeFormat.format(dt)}";
|
|
}else if(day =="3" || day=="23"){
|
|
return "${day}rd at ${timeFormat.format(dt)}";
|
|
}else{
|
|
return "${day}th at ${timeFormat.format(dt)}";
|
|
}
|
|
}
|
|
|
|
|
|
return dateTimeFormat.format(dt);
|
|
}
|
|
static Duration ParseDuration(String input){
|
|
if(input.contains(":")){
|
|
return Duration(hours: int.parse(input.split(":")[0]), minutes: int.parse(input.split(":")[1]));
|
|
}
|
|
|
|
return Duration(minutes: 0);
|
|
}
|
|
}
|
|
|
|
extension DurationExtension on Duration{
|
|
|
|
|
|
String toCustomString(){
|
|
if(this.inMinutes > 60){
|
|
return "${this.inHours}:${this.inMinutes}";
|
|
}
|
|
return "${this.inMinutes} mins";
|
|
}
|
|
}
|
|
|
|
extension ContextExtension on BuildContext {
|
|
bool get isTablet => MediaQuery.of(this).size.shortestSide > 600;
|
|
|
|
bool get isPhone => MediaQuery.of(this).size.shortestSide < 600;
|
|
|
|
bool get isSmall => MediaQuery.of(this).size.shortestSide < 340;
|
|
} |