Skip to content

Instantly share code, notes, and snippets.

@priezz
Last active December 20, 2022 17:29
Show Gist options
  • Save priezz/f9308701309d711af35da05f0fd86c30 to your computer and use it in GitHub Desktop.
Save priezz/f9308701309d711af35da05f0fd86c30 to your computer and use it in GitHub Desktop.
Typed data classes
import 'package:data_classes/data_classes.dart';
import 'package:common/const.dart';
part 'change_log.g.dart';
typedef FieldValueGetter<T> = T Function(dynamic);
enum EntityType {
series,
transaction,
}
enum ChangeField<T> {
/// Transaction
categoryLvl2({EntityType.transaction}, TypeConverters.categoryLvl2),
counterpartyName({EntityType.transaction}, TypeConverters.counterpartyName);
// ...
const ChangeField(this.supportedEntities, this.getValue);
final Set<EntityType> supportedEntities;
final FieldValueGetter<T> getValue;
}
/// {@nodoc}
@DataClass()
class _ChangeModel<T> {
late String id;
late ChangeField<T> field;
late String filterField;
late Object filterFieldValue;
TransactionDetailsChanger initiator = TransactionDetailsChanger.cashcoach;
late T? nextValue;
T? prevValue;
/// Document timestamps
late DateTime createdAt;
late DateTime updatedAt;
}
class TypeConverters {
static String counterpartyName(dynamic v) => v.toString();
static CategoryLvl2 categoryLvl2(v) =>
CategoryLvl2.values.firstWhere((item) => item.name == v.toString());
}
void checkType(Change change) {
if (change is Change<CategoryLvl2>) print('CategoryLvl2');
else print('unknown');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment