Skip to content

Commit

Permalink
Nauta Accounts init
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Ciber committed Sep 28, 2021
1 parent 8446be1 commit 27ed6ff
Show file tree
Hide file tree
Showing 28 changed files with 325 additions and 46 deletions.
3 changes: 3 additions & 0 deletions lib/app/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import 'package:todo/app/dependencies/dependencies.dart';
import 'package:todo/app/theme/theme.dart';
import 'package:todo/app/widgets/widgets.dart';

export 'package:todo/gen/assets.gen.dart';
export 'package:todo/l10n/l10n.dart';

export 'app_environment.dart';
export 'bloc/bloc.dart';
export 'data/data.dart';
Expand Down
3 changes: 2 additions & 1 deletion lib/app/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:todo/app/app.dart';
import 'package:todo/app/bloc/bloc.dart';
import 'package:todo/app/theme/theme.dart';
import 'package:todo/home/home.dart';
import 'package:todo/l10n/l10n.dart';
import 'package:todo/nauta/nauta.dart';
import 'package:todo/settings/router/router.dart';
import 'package:todo/ussd_codes/ussd_codes.dart';

Expand Down Expand Up @@ -51,6 +51,7 @@ class AppRouter extends StatelessWidget {
HomeLocation(),
UssdCodesLocation(),
SettingsLocation(),
NautaLocation(),
],
),
);
Expand Down
1 change: 1 addition & 0 deletions lib/app/data/datasources/datasources.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'nauta/nauta_account_datasource.dart';
export 'ussd/ussd.dart';
85 changes: 85 additions & 0 deletions lib/app/data/datasources/nauta/nauta_account_datasource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'package:injectable/injectable.dart';
import 'package:sqflite/sqflite.dart';
import 'package:todo/app/app.dart';

abstract class NautaAccountDatasource {
Future<int> save(NautaAccount account);
Future<List<NautaAccount>> findAll();
Future<NautaAccount?> findById(int id);
Future<NautaAccount?> findByUsername(String username);
Future<int> update(int id, NautaAccount account);
Future<int> delete(int id);
}

@Injectable(as: NautaAccountDatasource)
class NautaAccountSqliteDatasource extends NautaAccountDatasource {
NautaAccountSqliteDatasource(this._db);

final Database _db;

static const table = 'users';

@override
Future<int> delete(int id) => _db.delete(
table,
where: 'id = ?',
whereArgs: [id],
);

@override
Future<List<NautaAccount>> findAll() async {
final map = await _db.query(table);

return List.generate(
map.length,
(index) => NautaAccount.fromJson(map[index]),
);
}

@override
Future<NautaAccount?> findById(int id) async {
final map = await _db.query(
table,
where: 'id = ?',
whereArgs: [id],
);

final result = List.generate(
map.length,
(index) => NautaAccount.fromJson(map[index]),
);

return result.isNotEmpty ? result.first : null;
}

@override
Future<NautaAccount?> findByUsername(String username) async {
final map = await _db.query(
table,
where: 'username = ?',
whereArgs: [username],
);

final result = List.generate(
map.length,
(index) => NautaAccount.fromJson(map[index]),
);

return result.isNotEmpty ? result.first : null;
}

@override
Future<int> save(NautaAccount account) => _db.insert(
table,
account.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);

@override
Future<int> update(int id, NautaAccount account) => _db.update(
table,
account.toJson(),
where: 'id = ?',
whereArgs: [id],
);
}
1 change: 0 additions & 1 deletion lib/app/data/datasources/ussd/ussd_assets_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:developer';
import 'package:flutter/services.dart';
import 'package:injectable/injectable.dart';
import 'package:todo/app/app.dart';
import 'package:todo/gen/assets.gen.dart';

@injectable
class UssdAssetsDatasource {
Expand Down
1 change: 1 addition & 0 deletions lib/app/data/models/models.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'nauta/nauta_account.dart';
export 'ussd/ussd.dart';
29 changes: 29 additions & 0 deletions lib/app/data/models/nauta/nauta_account.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'nauta_account.g.dart';

@JsonSerializable()
class NautaAccount extends Equatable {
const NautaAccount({
this.id = 0,
required this.username,
required this.password,
});

factory NautaAccount.fromJson(Map<String, dynamic> json) =>
_$NautaAccountFromJson(json);

Map<String, dynamic> toJson() => _$NautaAccountToJson(this);

final int id;
final String username;
final String password;

@override
List<Object?> get props => [
id,
username,
password,
];
}
22 changes: 22 additions & 0 deletions lib/app/data/models/nauta/nauta_account.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions lib/app/data/repositories/nauta_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:injectable/injectable.dart';
import 'package:todo/app/app.dart';

@injectable
class NautaRepository {
NautaRepository(this._accountDatasource);

final NautaAccountDatasource _accountDatasource;

Future<int> saveAccount(NautaAccount account) =>
_accountDatasource.save(account);

Future<List<NautaAccount>> findAllAccounts() => _accountDatasource.findAll();

Future<NautaAccount?> findByIdAccount(int id) =>
_accountDatasource.findById(id);

Future<NautaAccount?> findByUsernameAccount(String username) =>
_accountDatasource.findByUsername(username);

Future<int> updateAccount(int id, NautaAccount account) =>
_accountDatasource.update(id, account);

Future<int> deleteAccount(int id) => _accountDatasource.delete(id);
}
1 change: 1 addition & 0 deletions lib/app/data/repositories/repositories.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'nauta_repository.dart';
export 'ussd_repository.dart';
80 changes: 45 additions & 35 deletions lib/app/dependencies/dependencies.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions lib/app/dependencies/dependencies.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'package:path/path.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sqflite/sqflite.dart';
import 'package:todo/app/dependencies/dependencies.config.dart';

final getIt = GetIt.instance;
Expand All @@ -9,6 +11,22 @@ final getIt = GetIt.instance;
abstract class RegisterModule {
@preResolve
Future<SharedPreferences> get prefs => SharedPreferences.getInstance();

@preResolve
Future<Database> get database async {
return openDatabase(
// Establece la ruta de la base de datos
join(await getDatabasesPath(), 'todoapp.db'),
// Cuando la base de datos se crea por primera vez, crea las tablas
onCreate: (db, version) {
db.execute(
// ignore: lines_longer_than_80_chars
'CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)',
);
},
version: 1,
);
}
}

@InjectableInit()
Expand Down
7 changes: 4 additions & 3 deletions lib/app/widgets/app_drawer.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:todo/app/app.dart';
import 'package:todo/gen/assets.gen.dart';
import 'package:todo/l10n/l10n.dart';
import 'package:todo/nauta/nauta.dart';
import 'package:todo/settings/settings.dart';

class AppDrawer extends StatelessWidget {
Expand Down Expand Up @@ -49,7 +48,9 @@ class AppDrawer extends StatelessWidget {
AppDrawerTile(
title: l10n.accounts,
icon: Icons.account_circle_outlined,
onTap: () {},
onTap: () {
AccountsPage.open(context);
},
),
AppDrawerTile(
title: l10n.settings,
Expand Down
1 change: 0 additions & 1 deletion lib/home/view/home_view.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:todo/app/app.dart';
import 'package:todo/l10n/l10n.dart';
import 'package:todo/ussd_codes/ussd_codes.dart';

class HomeView extends StatelessWidget {
Expand Down
Empty file added lib/nauta/bloc/bloc.dart
Empty file.
4 changes: 4 additions & 0 deletions lib/nauta/nauta.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'bloc/bloc.dart';
export 'router/router.dart';
export 'view/view.dart';
export 'widgets/widgets.dart';
21 changes: 21 additions & 0 deletions lib/nauta/router/accounts_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:todo/nauta/nauta.dart';

class AccountsPage extends BeamPage {
AccountsPage()
: super(
title: 'Cuentas',
type: BeamPageType.cupertino,
child: const AccountsView(),
);

static String get pathBlueprint => '/nauta/accounts';

static String route() => '/nauta/accounts';

static void open(BuildContext context) => context.beamToNamed(route());

static bool checkBeamState(BeamState state) =>
state.pathBlueprintSegments.contains('accounts');
}
Loading

0 comments on commit 27ed6ff

Please sign in to comment.