-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
2,987 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:learning_about_b4a_flutter/app/routes.dart'; | ||
|
||
class App extends StatelessWidget { | ||
const App({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return GetMaterialApp( | ||
debugShowCheckedModeBanner: false, | ||
title: 'Learning About B4A', | ||
theme: ThemeData.dark(), | ||
// localizationsDelegates: const [ | ||
// GlobalMaterialLocalizations.delegate, | ||
// GlobalWidgetsLocalizations.delegate, | ||
// GlobalCupertinoLocalizations.delegate, | ||
// ], | ||
supportedLocales: const [ | ||
Locale('pt', 'BR'), | ||
], | ||
initialBinding: SplashDependencies(), | ||
getPages: Routes.pageList, | ||
initialRoute: Routes.splash, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import 'dart:convert'; | ||
|
||
class ProfileModel { | ||
final String? objectId; | ||
final String? typeString; | ||
final String? typeFile; | ||
final bool? isActive; | ||
ProfileModel({ | ||
this.objectId, | ||
this.typeString, | ||
this.typeFile, | ||
this.isActive, | ||
}); | ||
|
||
ProfileModel copyWith({ | ||
String? objectId, | ||
String? typeString, | ||
String? typeFile, | ||
bool? isActive, | ||
}) { | ||
return ProfileModel( | ||
objectId: objectId ?? this.objectId, | ||
typeString: typeString ?? this.typeString, | ||
typeFile: typeFile ?? this.typeFile, | ||
isActive: isActive ?? this.isActive, | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
final result = <String, dynamic>{}; | ||
|
||
if (objectId != null) { | ||
result.addAll({'objectId': objectId}); | ||
} | ||
if (typeString != null) { | ||
result.addAll({'typeString': typeString}); | ||
} | ||
if (typeFile != null) { | ||
result.addAll({'typeFile': typeFile}); | ||
} | ||
if (isActive != null) { | ||
result.addAll({'isActive': isActive}); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
factory ProfileModel.fromMap(Map<String, dynamic> map) { | ||
return ProfileModel( | ||
objectId: map['objectId'], | ||
typeString: map['typeString'], | ||
typeFile: map['typeFile'], | ||
isActive: map['isActive'], | ||
); | ||
} | ||
|
||
String toJson() => json.encode(toMap()); | ||
|
||
factory ProfileModel.fromJson(String source) => | ||
ProfileModel.fromMap(json.decode(source)); | ||
|
||
@override | ||
String toString() { | ||
return 'ProfileModel(objectId: $objectId, typeString: $typeString, typeFile: $typeFile, isActive: $isActive)'; | ||
} | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
|
||
return other is ProfileModel && | ||
other.objectId == objectId && | ||
other.typeString == typeString && | ||
other.typeFile == typeFile && | ||
other.isActive == isActive; | ||
} | ||
|
||
@override | ||
int get hashCode { | ||
return objectId.hashCode ^ | ||
typeString.hashCode ^ | ||
typeFile.hashCode ^ | ||
isActive.hashCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:learning_about_b4a_flutter/app/core/models/profile_model.dart'; | ||
|
||
class UserModel { | ||
final String objectId; | ||
final String email; | ||
ProfileModel? profile; | ||
UserModel({ | ||
required this.objectId, | ||
required this.email, | ||
this.profile, | ||
}); | ||
|
||
UserModel copyWith({ | ||
String? objectId, | ||
String? email, | ||
ProfileModel? profile, | ||
}) { | ||
return UserModel( | ||
objectId: objectId ?? this.objectId, | ||
email: email ?? this.email, | ||
profile: profile ?? this.profile, | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
final result = <String, dynamic>{}; | ||
|
||
result.addAll({'objectId': objectId}); | ||
result.addAll({'email': email}); | ||
if (profile != null) { | ||
result.addAll({'profile': profile!.toMap()}); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
factory UserModel.fromMap(Map<String, dynamic> map) { | ||
return UserModel( | ||
objectId: map['objectId'] ?? '', | ||
email: map['email'] ?? '', | ||
profile: | ||
map['profile'] != null ? ProfileModel.fromMap(map['profile']) : null, | ||
); | ||
} | ||
|
||
String toJson() => json.encode(toMap()); | ||
|
||
factory UserModel.fromJson(String source) => | ||
UserModel.fromMap(json.decode(source)); | ||
|
||
@override | ||
String toString() => | ||
'UserModel(objectId: $objectId, email: $email, profile: $profile)'; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
|
||
return other is UserModel && | ||
other.objectId == objectId && | ||
other.email == email && | ||
other.profile == profile; | ||
} | ||
|
||
@override | ||
int get hashCode => objectId.hashCode ^ email.hashCode ^ profile.hashCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class AppErrorCode { | ||
int codeNumber; | ||
AppErrorCode( | ||
this.codeNumber, | ||
) { | ||
decode(); | ||
} | ||
String code = ''; | ||
String message = ''; | ||
decode() { | ||
code = 'Fluxus Erro: $codeNumber'; | ||
message = _appCodes[code]!; | ||
} | ||
|
||
final Map<int, String> _appCodes = { | ||
0: 'Não consegui iniciar banco de dados.', | ||
1: 'Estamos analisando seu cadastro.', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:dotenv/dotenv.dart'; | ||
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart'; | ||
|
||
class ConnectB4A { | ||
String _appId = '1'; | ||
String _clientKey = '1'; | ||
|
||
getCredentials() { | ||
var env = DotEnv()..load(); | ||
_appId = env['appId'] ?? _appId; | ||
_clientKey = env['clientKey'] ?? _clientKey; | ||
} | ||
|
||
Future<bool> initialize({bool debug = false}) async { | ||
getCredentials(); | ||
String serverUrl = 'https://parseapi.back4app.com'; | ||
await Parse().initialize( | ||
_appId, | ||
serverUrl, | ||
clientKey: _clientKey, | ||
debug: debug, | ||
// hideCompletAppIdAndClientKey: true, | ||
); | ||
return await healthCheck(); | ||
} | ||
|
||
/// No healthCheck se o valor de appId ou clientKey estiver errado | ||
/// ele gera uma exceção. | ||
/// Mas ele nao deveria gerar uma exceção. Tinha que retornar um | ||
/// (await Parse().healthCheck()).success = false | ||
/// Então relatei isto nesta issue | ||
/// https://github.com/parse-community/Parse-SDK-Flutter/issues/799 | ||
/// Pra resolver isto envolvi com um try...catch | ||
Future<bool> healthCheck() async { | ||
try { | ||
if ((await Parse().healthCheck()).success) { | ||
log('Back4app Connected.'); | ||
return true; | ||
} else { | ||
log('Back4app NOT Connected.'); | ||
log('Exit app.'); | ||
return false; | ||
} | ||
} catch (e) { | ||
log('Parse().healthCheck() with erros.'); | ||
log('Back4app NOT Connected.'); | ||
log('Exit app.'); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:learning_about_b4a_flutter/app/core/models/profile_model.dart'; | ||
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart'; | ||
|
||
class ProfileEntity { | ||
static const String className = 'Profile'; | ||
|
||
ProfileModel fromParse(ParseObject parseObject) { | ||
ProfileModel profileModel = ProfileModel( | ||
objectId: parseObject.objectId!, | ||
typeString: parseObject.get('typeString'), | ||
typeFile: parseObject.get('typeFile')?.get('url'), | ||
isActive: parseObject.get('isActive'), | ||
); | ||
return profileModel; | ||
} | ||
|
||
Future<ParseObject> toParse(ProfileModel profileModel) async { | ||
final profileParseObject = ParseObject(ProfileEntity.className); | ||
if (profileModel.objectId != null) { | ||
profileParseObject.objectId = profileModel.objectId; | ||
} | ||
if (profileModel.typeString != null) { | ||
profileParseObject.set('typeString', profileModel.typeString); | ||
} | ||
if (profileModel.isActive != null) { | ||
profileParseObject.set('isActive', profileModel.isActive); | ||
} | ||
return profileParseObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:learning_about_b4a_flutter/app/core/models/user_model.dart'; | ||
import 'package:learning_about_b4a_flutter/app/data/b4a/entity/profile_entity.dart'; | ||
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart'; | ||
|
||
class UserEntity { | ||
static const String className = '_User'; | ||
|
||
Future<UserModel> fromParse(ParseObject parseUser) async { | ||
return UserModel( | ||
objectId: parseUser.objectId!, | ||
email: parseUser.get('username'), | ||
profile: parseUser.get('profile') != null | ||
? ProfileEntity().fromParse(parseUser.get('profile')) | ||
: null, | ||
); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
lib/app/data/b4a/table/profile/profile_repository_b4a.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:learning_about_b4a_flutter/app/core/models/profile_model.dart'; | ||
import 'package:learning_about_b4a_flutter/app/data/b4a/entity/profile_entity.dart'; | ||
import 'package:learning_about_b4a_flutter/app/data/b4a/table/profile/profile_repository_exception.dart'; | ||
import 'package:learning_about_b4a_flutter/app/data/b4a/utils/parse_error_code.dart'; | ||
import 'package:learning_about_b4a_flutter/app/data/repositories/profile_repository.dart'; | ||
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart'; | ||
|
||
class ProfileRepositoryB4a implements ProfileRepository { | ||
@override | ||
Future<ProfileModel?> readById(String id) async { | ||
log('+++', name: 'ProfileRepositoryB4a.readById'); | ||
QueryBuilder<ParseObject> query = | ||
QueryBuilder<ParseObject>(ParseObject(ProfileEntity.className)); | ||
query.whereEqualTo('objectId', id); | ||
query.first(); | ||
ParseResponse? response; | ||
try { | ||
response = await query.query(); | ||
|
||
if (response.success && response.results != null) { | ||
return ProfileEntity().fromParse(response.results!.first); | ||
} else { | ||
throw Exception(); | ||
} | ||
} on Exception { | ||
var errorCodes = ParseErrorCode(response!.error!); | ||
throw ProfileRepositoryException( | ||
code: errorCodes.code, | ||
message: errorCodes.message, | ||
); | ||
} | ||
} | ||
|
||
@override | ||
Future<String> update(ProfileModel profileModel) async { | ||
final userProfileParse = await ProfileEntity().toParse(profileModel); | ||
ParseResponse? response; | ||
try { | ||
response = await userProfileParse.save(); | ||
|
||
if (response.success && response.results != null) { | ||
ParseObject userProfile = response.results!.first as ParseObject; | ||
return userProfile.objectId!; | ||
} else { | ||
throw Exception(); | ||
} | ||
} on Exception { | ||
var errorCodes = ParseErrorCode(response!.error!); | ||
throw ProfileRepositoryException( | ||
code: errorCodes.code, | ||
message: errorCodes.message, | ||
); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
lib/app/data/b4a/table/profile/profile_repository_exception.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class ProfileRepositoryException implements Exception { | ||
final String code; | ||
final String message; | ||
ProfileRepositoryException({ | ||
required this.code, | ||
required this.message, | ||
}); | ||
} |
Oops, something went wrong.