Created with <3 with dartpad.dev.
Last active
December 20, 2022 17:29
-
-
Save priezz/f9308701309d711af35da05f0fd86c30 to your computer and use it in GitHub Desktop.
Typed data classes
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
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