diff --git a/lib/app/app.dart b/lib/app/app.dart index dfc9e4c..b38861a 100644 --- a/lib/app/app.dart +++ b/lib/app/app.dart @@ -39,7 +39,9 @@ class App extends StatelessWidget { create: (context) => OpenFoodAPIServiceImpl(), ), RepositoryProvider( - create: (context) => OpenAIServiceImpl()..init(), + create: (context) => OpenAIServiceImpl( + RepositoryProvider.of(context), + )..init(), ), RepositoryProvider( create: (context) => FirebaseAuthServiceImpl(), diff --git a/lib/core/models/profile/profile.dart b/lib/core/models/profile/profile.dart index 0a4f9f7..0453bd3 100644 --- a/lib/core/models/profile/profile.dart +++ b/lib/core/models/profile/profile.dart @@ -14,6 +14,7 @@ part 'profile.g.dart'; class Profile with _$Profile { factory Profile({ Goals? goals, + Keys? keys, }) = _Profile; Profile._(); @@ -46,3 +47,24 @@ class Goals with _$Goals { Map toJson() => _$GoalsToJson(this); } + +@JsonSerializable( + createToJson: true, + explicitToJson: true, +) +@Freezed( + fromJson: false, + toJson: false, +) +class Keys with _$Keys { + factory Keys({ + required String openAiToken, + required String orgId, + }) = _Keys; + + Keys._(); + + factory Keys.fromJson(Map json) => _$KeysFromJson(json); + + Map toJson() => _$KeysToJson(this); +} diff --git a/lib/core/models/profile/profile.freezed.dart b/lib/core/models/profile/profile.freezed.dart index dda1dae..4416e0b 100644 --- a/lib/core/models/profile/profile.freezed.dart +++ b/lib/core/models/profile/profile.freezed.dart @@ -17,6 +17,7 @@ final _privateConstructorUsedError = UnsupportedError( /// @nodoc mixin _$Profile { Goals? get goals => throw _privateConstructorUsedError; + Keys? get keys => throw _privateConstructorUsedError; @JsonKey(ignore: true) $ProfileCopyWith get copyWith => throw _privateConstructorUsedError; @@ -27,9 +28,10 @@ abstract class $ProfileCopyWith<$Res> { factory $ProfileCopyWith(Profile value, $Res Function(Profile) then) = _$ProfileCopyWithImpl<$Res, Profile>; @useResult - $Res call({Goals? goals}); + $Res call({Goals? goals, Keys? keys}); $GoalsCopyWith<$Res>? get goals; + $KeysCopyWith<$Res>? get keys; } /// @nodoc @@ -46,12 +48,17 @@ class _$ProfileCopyWithImpl<$Res, $Val extends Profile> @override $Res call({ Object? goals = freezed, + Object? keys = freezed, }) { return _then(_value.copyWith( goals: freezed == goals ? _value.goals : goals // ignore: cast_nullable_to_non_nullable as Goals?, + keys: freezed == keys + ? _value.keys + : keys // ignore: cast_nullable_to_non_nullable + as Keys?, ) as $Val); } @@ -66,6 +73,18 @@ class _$ProfileCopyWithImpl<$Res, $Val extends Profile> return _then(_value.copyWith(goals: value) as $Val); }); } + + @override + @pragma('vm:prefer-inline') + $KeysCopyWith<$Res>? get keys { + if (_value.keys == null) { + return null; + } + + return $KeysCopyWith<$Res>(_value.keys!, (value) { + return _then(_value.copyWith(keys: value) as $Val); + }); + } } /// @nodoc @@ -75,10 +94,12 @@ abstract class _$$ProfileImplCopyWith<$Res> implements $ProfileCopyWith<$Res> { __$$ProfileImplCopyWithImpl<$Res>; @override @useResult - $Res call({Goals? goals}); + $Res call({Goals? goals, Keys? keys}); @override $GoalsCopyWith<$Res>? get goals; + @override + $KeysCopyWith<$Res>? get keys; } /// @nodoc @@ -93,12 +114,17 @@ class __$$ProfileImplCopyWithImpl<$Res> @override $Res call({ Object? goals = freezed, + Object? keys = freezed, }) { return _then(_$ProfileImpl( goals: freezed == goals ? _value.goals : goals // ignore: cast_nullable_to_non_nullable as Goals?, + keys: freezed == keys + ? _value.keys + : keys // ignore: cast_nullable_to_non_nullable + as Keys?, )); } } @@ -106,14 +132,16 @@ class __$$ProfileImplCopyWithImpl<$Res> /// @nodoc class _$ProfileImpl extends _Profile { - _$ProfileImpl({this.goals}) : super._(); + _$ProfileImpl({this.goals, this.keys}) : super._(); @override final Goals? goals; + @override + final Keys? keys; @override String toString() { - return 'Profile(goals: $goals)'; + return 'Profile(goals: $goals, keys: $keys)'; } @override @@ -121,11 +149,12 @@ class _$ProfileImpl extends _Profile { return identical(this, other) || (other.runtimeType == runtimeType && other is _$ProfileImpl && - (identical(other.goals, goals) || other.goals == goals)); + (identical(other.goals, goals) || other.goals == goals) && + (identical(other.keys, keys) || other.keys == keys)); } @override - int get hashCode => Object.hash(runtimeType, goals); + int get hashCode => Object.hash(runtimeType, goals, keys); @JsonKey(ignore: true) @override @@ -135,12 +164,14 @@ class _$ProfileImpl extends _Profile { } abstract class _Profile extends Profile { - factory _Profile({final Goals? goals}) = _$ProfileImpl; + factory _Profile({final Goals? goals, final Keys? keys}) = _$ProfileImpl; _Profile._() : super._(); @override Goals? get goals; @override + Keys? get keys; + @override @JsonKey(ignore: true) _$$ProfileImplCopyWith<_$ProfileImpl> get copyWith => throw _privateConstructorUsedError; @@ -318,3 +349,136 @@ abstract class _Goals extends Goals { _$$GoalsImplCopyWith<_$GoalsImpl> get copyWith => throw _privateConstructorUsedError; } + +/// @nodoc +mixin _$Keys { + String get openAiToken => throw _privateConstructorUsedError; + String get orgId => throw _privateConstructorUsedError; + + @JsonKey(ignore: true) + $KeysCopyWith get copyWith => throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $KeysCopyWith<$Res> { + factory $KeysCopyWith(Keys value, $Res Function(Keys) then) = + _$KeysCopyWithImpl<$Res, Keys>; + @useResult + $Res call({String openAiToken, String orgId}); +} + +/// @nodoc +class _$KeysCopyWithImpl<$Res, $Val extends Keys> + implements $KeysCopyWith<$Res> { + _$KeysCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? openAiToken = null, + Object? orgId = null, + }) { + return _then(_value.copyWith( + openAiToken: null == openAiToken + ? _value.openAiToken + : openAiToken // ignore: cast_nullable_to_non_nullable + as String, + orgId: null == orgId + ? _value.orgId + : orgId // ignore: cast_nullable_to_non_nullable + as String, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$KeysImplCopyWith<$Res> implements $KeysCopyWith<$Res> { + factory _$$KeysImplCopyWith( + _$KeysImpl value, $Res Function(_$KeysImpl) then) = + __$$KeysImplCopyWithImpl<$Res>; + @override + @useResult + $Res call({String openAiToken, String orgId}); +} + +/// @nodoc +class __$$KeysImplCopyWithImpl<$Res> + extends _$KeysCopyWithImpl<$Res, _$KeysImpl> + implements _$$KeysImplCopyWith<$Res> { + __$$KeysImplCopyWithImpl(_$KeysImpl _value, $Res Function(_$KeysImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? openAiToken = null, + Object? orgId = null, + }) { + return _then(_$KeysImpl( + openAiToken: null == openAiToken + ? _value.openAiToken + : openAiToken // ignore: cast_nullable_to_non_nullable + as String, + orgId: null == orgId + ? _value.orgId + : orgId // ignore: cast_nullable_to_non_nullable + as String, + )); + } +} + +/// @nodoc + +class _$KeysImpl extends _Keys { + _$KeysImpl({required this.openAiToken, required this.orgId}) : super._(); + + @override + final String openAiToken; + @override + final String orgId; + + @override + String toString() { + return 'Keys(openAiToken: $openAiToken, orgId: $orgId)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$KeysImpl && + (identical(other.openAiToken, openAiToken) || + other.openAiToken == openAiToken) && + (identical(other.orgId, orgId) || other.orgId == orgId)); + } + + @override + int get hashCode => Object.hash(runtimeType, openAiToken, orgId); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$KeysImplCopyWith<_$KeysImpl> get copyWith => + __$$KeysImplCopyWithImpl<_$KeysImpl>(this, _$identity); +} + +abstract class _Keys extends Keys { + factory _Keys( + {required final String openAiToken, + required final String orgId}) = _$KeysImpl; + _Keys._() : super._(); + + @override + String get openAiToken; + @override + String get orgId; + @override + @JsonKey(ignore: true) + _$$KeysImplCopyWith<_$KeysImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/core/models/profile/profile.g.dart b/lib/core/models/profile/profile.g.dart index f5f5739..73d8578 100644 --- a/lib/core/models/profile/profile.g.dart +++ b/lib/core/models/profile/profile.g.dart @@ -10,10 +10,14 @@ Profile _$ProfileFromJson(Map json) => Profile( goals: json['goals'] == null ? null : Goals.fromJson(json['goals'] as Map), + keys: json['keys'] == null + ? null + : Keys.fromJson(json['keys'] as Map), ); Map _$ProfileToJson(Profile instance) => { 'goals': instance.goals?.toJson(), + 'keys': instance.keys?.toJson(), }; Goals _$GoalsFromJson(Map json) => Goals( @@ -29,3 +33,13 @@ Map _$GoalsToJson(Goals instance) => { 'fat': instance.fat, 'carbs': instance.carbs, }; + +Keys _$KeysFromJson(Map json) => Keys( + openAiToken: json['openAiToken'] as String, + orgId: json['orgId'] as String, + ); + +Map _$KeysToJson(Keys instance) => { + 'openAiToken': instance.openAiToken, + 'orgId': instance.orgId, + }; diff --git a/lib/core/services/open_ai/open_ai_serice.impl.dart b/lib/core/services/open_ai/open_ai_serice.impl.dart index a7352b2..c939121 100644 --- a/lib/core/services/open_ai/open_ai_serice.impl.dart +++ b/lib/core/services/open_ai/open_ai_serice.impl.dart @@ -2,18 +2,23 @@ import 'dart:convert'; import 'package:calorie_tracker/core/enums/serving_type.enum.dart'; import 'package:calorie_tracker/core/models/food/food.dart'; +import 'package:calorie_tracker/core/services/firebase/firebase_service.dart'; import 'package:calorie_tracker/core/services/open_ai/open_ai_service.dart'; -import 'package:calorie_tracker/keys.dart'; import 'package:dart_openai/dart_openai.dart'; class OpenAIServiceImpl extends OpenAIService { - OpenAIServiceImpl(); + final FirebaseService _firebaseService; + OpenAIServiceImpl(this._firebaseService); init() { + _firebaseService.getProfileData().then((profile) { + if (profile != null && profile.keys != null) { + OpenAI.organization = profile.keys!.orgId; + OpenAI.apiKey = profile.keys!.openAiToken; + } + }); OpenAI.showLogs = true; OpenAI.showResponsesLogs = true; - OpenAI.organization = orogId; - OpenAI.apiKey = openAiToken; } // the system message that will be sent to the request.