First commit for ltx-flutter
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import 'package:appwrite/appwrite.dart';
|
||||
import 'package:appwrite/models.dart';
|
||||
import 'package:ltx_flutter/constants/constants.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
enum AuthStatus {
|
||||
uninitialized,
|
||||
authenticated,
|
||||
unauthenticated,
|
||||
}
|
||||
|
||||
class AuthAPI extends ChangeNotifier {
|
||||
Client client = Client();
|
||||
late final Account account;
|
||||
|
||||
late User _currentUser;
|
||||
|
||||
AuthStatus _status = AuthStatus.uninitialized;
|
||||
|
||||
// Getter methods
|
||||
User get currentUser => _currentUser;
|
||||
AuthStatus get status => _status;
|
||||
String? get username => _currentUser.name;
|
||||
String? get email => _currentUser.email;
|
||||
String? get userid => _currentUser.$id;
|
||||
|
||||
// Constructor
|
||||
AuthAPI() {
|
||||
init();
|
||||
loadUser();
|
||||
}
|
||||
|
||||
// Initialize the Appwrite client
|
||||
init() {
|
||||
client.setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT_ID);
|
||||
account = Account(client);
|
||||
}
|
||||
|
||||
loadUser() async {
|
||||
try {
|
||||
final user = await account.get();
|
||||
_status = AuthStatus.authenticated;
|
||||
_currentUser = user;
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
print(e);
|
||||
_status = AuthStatus.unauthenticated;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<User> createUser(
|
||||
{required String email, required String password}) async {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final user = await account.create(
|
||||
userId: ID.unique(),
|
||||
email: email,
|
||||
password: password,
|
||||
name: 'Simon G');
|
||||
return user;
|
||||
} finally {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Session> createEmailSession(
|
||||
{required String email, required String password}) async {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final session =
|
||||
await account.createEmailSession(email: email, password: password);
|
||||
_currentUser = await account.get();
|
||||
_status = AuthStatus.authenticated;
|
||||
return session;
|
||||
} finally {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
signInWithProvider({required String provider}) async {
|
||||
try {
|
||||
final session = await account.createOAuth2Session(provider: provider);
|
||||
_currentUser = await account.get();
|
||||
_status = AuthStatus.authenticated;
|
||||
return session;
|
||||
} finally {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
signOut() async {
|
||||
try {
|
||||
await account.deleteSession(sessionId: 'current');
|
||||
_status = AuthStatus.unauthenticated;
|
||||
} finally {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Preferences> getUserPreferences() async {
|
||||
return await account.getPrefs();
|
||||
}
|
||||
|
||||
updatePreferences({required String bio}) async {
|
||||
return account.updatePrefs(prefs: {'bio': bio});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import 'package:appwrite/appwrite.dart';
|
||||
import 'package:appwrite/models.dart';
|
||||
import 'package:ltx_flutter/appwrite/appwrite.dart';
|
||||
import 'package:ltx_flutter/constants/constants.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class DatabaseAPI {
|
||||
Client client = Client();
|
||||
late final Account account;
|
||||
late final Databases databases;
|
||||
final AuthAPI auth = AuthAPI();
|
||||
|
||||
final DateFormat formatter = DateFormat('yyyy-MM-dd');
|
||||
|
||||
DatabaseAPI() {
|
||||
init();
|
||||
}
|
||||
|
||||
init() {
|
||||
client
|
||||
.setEndpoint(APPWRITE_URL)
|
||||
.setProject(APPWRITE_PROJECT_ID)
|
||||
.setSelfSigned();
|
||||
account = Account(client);
|
||||
databases = Databases(client);
|
||||
}
|
||||
|
||||
Future<DocumentList> getEntries({int limit = 100, String dateISO = ""}) {
|
||||
if (dateISO == "") {
|
||||
dateISO = formatter.format(DateTime.now());
|
||||
}
|
||||
|
||||
final date = DateTime.parse(dateISO);
|
||||
final offset = date.difference(DateTime.now()).inDays;
|
||||
|
||||
print("Getting ${limit} entries starting from ${offset}");
|
||||
|
||||
return databases.listDocuments(
|
||||
databaseId: APPWRITE_DATABASE_ID,
|
||||
collectionId: COLLECTION,
|
||||
queries: [
|
||||
Query.orderAsc("date"),
|
||||
Query.offset(offset),
|
||||
Query.limit(limit),
|
||||
]);
|
||||
}
|
||||
|
||||
Future<Document> addEntry(
|
||||
{required String date,
|
||||
List hours = const [],
|
||||
int mood = -1,
|
||||
String comments = ""}) {
|
||||
return databases.createDocument(
|
||||
databaseId: APPWRITE_DATABASE_ID,
|
||||
collectionId: COLLECTION,
|
||||
documentId: date,
|
||||
data: {
|
||||
'date': DateTime.parse(date),
|
||||
'hours': hours,
|
||||
'mood': mood,
|
||||
'comments': comments
|
||||
});
|
||||
}
|
||||
|
||||
Future<dynamic> deleteEntry({required String date}) {
|
||||
return databases.deleteDocument(
|
||||
databaseId: APPWRITE_DATABASE_ID,
|
||||
collectionId: COLLECTION,
|
||||
documentId: date);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user