famedlysdk/lib/src/utils/states_map.dart

41 lines
1.4 KiB
Dart
Raw Normal View History

2019-11-20 13:02:23 +00:00
import 'package:famedlysdk/famedlysdk.dart';
/// Matrix room states are addressed by a tuple of the [type] and an
/// optional [stateKey].
class StatesMap {
2020-01-02 14:09:49 +00:00
Map<String, Map<String, Event>> states = {};
2019-11-20 13:02:23 +00:00
2020-01-02 14:09:49 +00:00
/// Returns either the [Event] or a map of state_keys to [Event] objects.
2019-11-20 13:02:23 +00:00
/// If you just enter a MatrixID, it will try to return the corresponding m.room.member event.
dynamic operator [](String key) {
//print("[Warning] This method will be depracated in the future!");
2020-02-14 13:16:26 +00:00
if (key == null) return null;
if (key.startsWith('@') && key.contains(':')) {
if (!states.containsKey('m.room.member')) states['m.room.member'] = {};
return states['m.room.member'][key];
2019-11-20 13:02:23 +00:00
}
if (!states.containsKey(key)) states[key] = {};
if (states[key][''] is Event) {
return states[key][''];
2020-01-02 14:33:26 +00:00
} else if (states[key].isEmpty) {
2019-11-20 13:02:23 +00:00
return null;
2020-01-02 14:33:26 +00:00
} else {
2019-11-20 13:02:23 +00:00
return states[key];
2020-01-02 14:33:26 +00:00
}
2019-11-20 13:02:23 +00:00
}
2020-01-02 14:09:49 +00:00
void operator []=(String key, Event val) {
//print("[Warning] This method will be depracated in the future!");
if (key.startsWith('@') && key.contains(':')) {
if (!states.containsKey('m.room.member')) states['m.room.member'] = {};
states['m.room.member'][key] = val;
2019-11-20 13:02:23 +00:00
}
if (!states.containsKey(key)) states[key] = {};
states[key][val.stateKey ?? ''] = val;
2019-11-20 13:02:23 +00:00
}
bool containsKey(String key) => states.containsKey(key);
void forEach(f) => states.forEach(f);
}