to laptptop

This commit is contained in:
ImBenji
2024-02-28 04:56:50 +00:00
parent 2ef40e6b27
commit 675c42d2da
19 changed files with 1758 additions and 151 deletions

View File

@@ -0,0 +1,31 @@
class ApiConstants {
static const String APPWRITE_ENDPOINT = "https://cloud.imbenji.net/v1";
static const String APPWRITE_PROJECT_ID = "65de530c1c0a7ffc0c3f";
static const String INFO_Q_DATABASE_ID = "65de5cab16717444527b";
static const String DEST_Q_COLLECTION_ID = "65de9f2f925562a2eda8";
static const String MANUAL_Q_COLLECTION_ID = "65de9f1b6282fd209bdb";
static const String BUSSTOP_Q_COLLECTION_ID = "65de9ef464bfa5a0693d";
// function to convert date time to something that looks like: Today at 12:00 PM or Yesterday at 12:00 PM or 12/31/2021 at 12:00 PM, make sure to always use double digits for the time so 01 not 1
static String formatDateTime(DateTime dateTime) {
DateTime now = DateTime.now();
DateTime today = DateTime(now.year, now.month, now.day);
DateTime yesterday = DateTime(now.year, now.month, now.day - 1);
if (dateTime.isAfter(today)) {
return "Today at ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')} ${dateTime.hour > 12 ? "PM" : "AM"}";
} else if (dateTime.isAfter(yesterday)) {
return "Yesterday at ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')} ${dateTime.hour > 12 ? "PM" : "AM"}";
} else {
return "${dateTime.month.toString().padLeft(2, '0')}/${dateTime.day.toString().padLeft(2, '0')}/${dateTime.year.toString().padLeft(2, '0')} at ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')} ${dateTime.hour > 12 ? "PM" : "AM"}";
}
}
}

171
lib/auth/auth_api.dart Normal file
View File

@@ -0,0 +1,171 @@
import 'package:appwrite/appwrite.dart' as appwrite;
import 'package:appwrite/models.dart' as models;
import 'package:bus_infotainment/auth/api_constants.dart';
import 'package:bus_infotainment/utils/delegates.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
enum AuthStatus {
UNINITIALIZED,
AUTHENTICATED,
UNAUTHENTICATED,
}
class AuthAPI extends ChangeNotifier {
appwrite.Client client = appwrite.Client();
late final appwrite.Account account;
late models.User _currentUser;
AuthStatus _status = AuthStatus.UNINITIALIZED;
// late UserInfo _userInfo;
// Getter methods
models.User get currentUser => _currentUser;
// UserInfo get userInfo => _userInfo;
AuthStatus get status => _status;
String? get username => _currentUser.name;
String? get email => _currentUser.email;
String? get userID => _currentUser.$id;
AuthAPI() {
init();
loadUser();
}
init() {
client
.setEndpoint(ApiConstants.APPWRITE_ENDPOINT)
.setProject(ApiConstants.APPWRITE_PROJECT_ID)
.setSelfSigned();
account = appwrite.Account(client);
}
loadUser() async {
try {
{
SharedPreferences prefs = await SharedPreferences.getInstance();
String? username = prefs.getString("APPWRITE_SESSIONID_USERNAME");
String? password = prefs.getString("APPWRITE_SESSIONID_PASSWORD");
if (username != null && password != null) {
await createEmailSession(email: username, password: password);
}
}
final user = await account.get();
_status = AuthStatus.AUTHENTICATED;
// _userInfo = await UserInfo.getFromId(this, userID!);
_currentUser = user;
} catch (e) {
_status = AuthStatus.UNAUTHENTICATED;
} finally {
notifyListeners();
}
}
Future<models.User> createUser({
required String displayName,
required String username,
required String email,
required String password,
}) async {
notifyListeners();
try {
final user = await account.create(
userId: appwrite.ID.unique(),
email: email,
password: password,
name: username,
);
final appwrite.Databases databases = appwrite.Databases(client);
// await databases.createDocument(
// databaseId: ApiConstants.SOCIALS_DATABASE_ID,
// collectionId: ApiConstants.USERS_COLLECTION_ID,
// documentId: user!.$id,
// data: {
// "username": username,
// "display_name": displayName,
// "bio": "",
// "status": "",
// "profilepicture_id": "default",
// "profilebanner_id": "default",
// "last_activity": DateTime.now().toIso8601String(),
// "guild_ids": [],
//
// }
// );
return user;
} finally {
notifyListeners();
}
}
/* Login */
Future<models.Session> createEmailSession({
required String email,
required String password,
}) async {
try {
final session = await account.createEmailSession(
email: email,
password: password,
);
_currentUser = await account.get();
_status = AuthStatus.AUTHENTICATED;
// _userInfo = await UserInfo.getFromId(this, userID!);
// _userInfo.username = _currentUser.name!;
// _userInfo.pushChanges(this);
// store session in shared preferences
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("APPWRITE_SESSIONID_USERNAME", email);
prefs.setString("APPWRITE_SESSIONID_PASSWORD", password);
return session;
} finally {
onLogin.trigger(1);
notifyListeners();
}
}
/* Logout */
Future<void> deleteSession() async {
try {
await account.deleteSession(sessionId: "current");
{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove("APPWRITE_SESSIONID_USERNAME");
prefs.remove("APPWRITE_SESSIONID_PASSWORD");
}
_status = AuthStatus.UNAUTHENTICATED;
} finally {
notifyListeners();
}
}
bool isAuthenticated() {
return _status == AuthStatus.AUTHENTICATED;
}
// Events
EventDelegate<int> _onLogin = EventDelegate();
EventDelegate get onLogin => _onLogin;
}