import 'package:appwrite/models.dart' as models; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:intl/intl.dart'; import 'package:appwrite/appwrite.dart'; import '../../data/models/log.dart'; import '../../data/models/project_info.dart'; /// A repository responsible for handling network interactions with the Appwrite server. /// /// It provides a helper method to ping the server. class AppwriteRepository { static const String pingPath = "/ping"; // static const String appwriteProjectId = String.fromEnvironment('APPWRITE_PROJECT_ID'); // static const String appwriteProjectName = String.fromEnvironment('APPWRITE_PROJECT_NAME'); // static const String appwritePublicEndpoint = String.fromEnvironment('APPWRITE_PUBLIC_ENDPOINT'); final Client _client = Client() .setProject(dotenv.get('APPWRITE_PROJECT_ID')) .setEndpoint(dotenv.get('APPWRITE_PUBLIC_ENDPOINT')); late final Account _account; late final Databases _databases; AppwriteRepository._internal() { _account = Account(_client); _databases = Databases(_client); } static final AppwriteRepository _instance = AppwriteRepository._internal(); /// Singleton instance getter factory AppwriteRepository() => _instance; ProjectInfo getProjectInfo() { return ProjectInfo( endpoint: dotenv.get('APPWRITE_PUBLIC_ENDPOINT'), projectId: dotenv.get('APPWRITE_PROJECT_ID'), projectName: dotenv.get('APPWRITE_PROJECT_NAME'), ); } /// Pings the Appwrite server and captures the response. /// /// @return [Log] containing request and response details. Future ping() async { try { final response = await _client.ping(); return Log( date: _getCurrentDate(), status: 200, method: "GET", path: pingPath, response: response, ); } on AppwriteException catch (error) { return Log( date: _getCurrentDate(), status: error.code ?? 500, method: "GET", path: pingPath, response: error.message ?? "Unknown error", ); } } /// Retrieves the current date in the format "MMM dd, HH:mm". /// /// @return [String] A formatted date. String _getCurrentDate() { return DateFormat("MMM dd, HH:mm").format(DateTime.now()); } Future logout() async => await _account.deleteSession(sessionId: 'current'); Future login(Map map) async => await _account.createEmailPasswordSession( email: map['email'], password: map['password'], ); Future signUpAnonymus() async => await _account.createAnonymousSession(); Future signup(Map map) async => _account.create( userId: ID.unique(), email: map['email'], password: map['password'], name: map['name'], ); Future get getCurrentUser => _account.get(); createTankStop(Map map) {} }