82 lines
1.8 KiB
Dart
82 lines
1.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:odoo_rpc/odoo_rpc.dart';
|
|
import '../utils/constants.dart';
|
|
|
|
class OdooService with ChangeNotifier {
|
|
OdooClient? _client;
|
|
OdooSession? _session;
|
|
String _baseUrl = AppConstants.defaultOdooUrl;
|
|
|
|
OdooClient? get client => _client;
|
|
OdooSession? get session => _session;
|
|
String get baseUrl => _baseUrl;
|
|
|
|
bool get isConnected => _client != null;
|
|
bool get isLoggedIn => _session != null;
|
|
|
|
OdooService() {
|
|
_initClient();
|
|
}
|
|
|
|
void _initClient() {
|
|
_client = OdooClient(_baseUrl);
|
|
// Subscribe to session changes if needed
|
|
_client!.sessionStream.listen((session) {
|
|
_session = session;
|
|
notifyListeners();
|
|
});
|
|
_client!.loginStream.listen((event) {
|
|
if (event == OdooLoginEvent.loggedIn) {
|
|
if (kDebugMode) {
|
|
print('Logged in');
|
|
}
|
|
}
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
void updateUrl(String url) {
|
|
if (url != _baseUrl) {
|
|
_baseUrl = url;
|
|
_client?.close();
|
|
_initClient();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> authenticate(String db, String login, String password) async {
|
|
if (_client == null) _initClient();
|
|
try {
|
|
await _client!.authenticate(db, login, password);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
if (_client != null) {
|
|
await _client!.destroySession();
|
|
}
|
|
}
|
|
|
|
// Example of calling a method (search_read)
|
|
Future<dynamic> callKw({
|
|
required String model,
|
|
required String method,
|
|
required List args,
|
|
required Map<String, dynamic> kwargs,
|
|
}) async {
|
|
if (_client == null) throw Exception("Client not initialized");
|
|
|
|
|
|
|
|
// In odoo_rpc 0.4.x, callKw expects a Map of params
|
|
return _client!.callKw({
|
|
'model': model,
|
|
'method': method,
|
|
'args': args,
|
|
'kwargs': kwargs,
|
|
});
|
|
}
|
|
}
|