Skip to content

Instantly share code, notes, and snippets.

@fabiojansenbr
Created July 29, 2020 11:39
Show Gist options
  • Save fabiojansenbr/c92cfc3828f7d0c92ed0668f68661db0 to your computer and use it in GitHub Desktop.
Save fabiojansenbr/c92cfc3828f7d0c92ed0668f68661db0 to your computer and use it in GitHub Desktop.
import 'package:companies_modular_flutter/app/core/models/company_model.dart';
import 'package:companies_modular_flutter/app/repositories/company_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:mobx/mobx.dart';
part 'companies_controller.g.dart';
class CompaniesController = _CompaniesControllerBase with _$CompaniesController;
abstract class _CompaniesControllerBase with Store {
final CompanyRepository companyRepository;
_CompaniesControllerBase(this.companyRepository) {
fetchCompanies();
}
GlobalKey<FormState> formKey = GlobalKey<FormState>();
@observable
String name;
@observable
String email;
@action
setName(String value) => name = value;
@action
setEmail(String value) => email = value;
@observable
ObservableFuture<List<CompanyModel>> companies;
@action
Future<void> fetchCompanies() {
companies = companyRepository.getCompanies().asObservable();
return companies;
}
@action
Future createCompany() async {
if (formKey.currentState.validate()) {
formKey.currentState.save();
print(name);
print(email);
await companyRepository.createCompany(name, email);
Modular.to.pop('true');
}
}
@action
Future updateCompany(String id) async {
if (formKey.currentState.validate()) {
formKey.currentState.save();
print(name);
print(email);
await companyRepository.updateCompany(id, name, email);
Modular.to.pop('true');
}
}
@action
Future deleteCompany(String id) async {
await companyRepository.deleteCompany(id);
fetchCompanies();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment