onInit method
override
Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller.
Implementation
@override
void onInit() {
if (!PlatformUtils.isMobile) {
try {
_devicesSubscription = MediaUtils.onDeviceChange.listen(
(e) => devices.value = e,
);
MediaUtils.enumerateDevices().then((e) => devices.value = e);
} catch (_) {
// No-op, shouldn't break the view.
}
}
listInitIndex = router.profileSection.value?.index ?? 0;
bool ignoreWorker = false;
bool ignorePositions = false;
_profileWorker = ever(router.profileSection, (ProfileTab? tab) async {
if (ignoreWorker) {
ignoreWorker = false;
} else {
ignorePositions = true;
await itemScrollController.scrollTo(
index: tab?.index ?? 0,
duration: 200.milliseconds,
curve: Curves.ease,
);
Future.delayed(Duration.zero, () => ignorePositions = false);
highlight(tab);
}
});
positionsListener.itemPositions.addListener(() {
if (!ignorePositions) {
final ProfileTab tab =
ProfileTab.values[positionsListener
.itemPositions
.value
.first
.index];
if (router.profileSection.value != tab) {
ignoreWorker = true;
router.profileSection.value = tab;
Future.delayed(Duration.zero, () => ignoreWorker = false);
}
}
});
phone = TextFieldState(
approvable: true,
onFocus: (s) {
if (s.text.isNotEmpty) {
try {
final phone = UserPhone(s.text.replaceAll(' ', ''));
if (myUser.value!.phones.confirmed.contains(phone) ||
myUser.value?.phones.unconfirmed == phone) {
s.error.value = 'err_you_already_add_this_phone'.l10n;
}
} on FormatException {
s.error.value = 'err_incorrect_phone'.l10n;
}
}
},
onSubmitted: (s) async {
if (s.text.isEmpty ||
(s.error.value != null && s.resubmitOnError.isFalse)) {
return;
}
final phone = UserPhone(s.text.replaceAll(' ', ''));
s.clear();
bool modalVisible = true;
_myUserService
.addUserPhone(phone, locale: L10n.chosen.value?.toString())
.onError((e, __) {
s.unchecked = phone.val;
if (e is AddUserPhoneException) {
s.error.value = e.toMessage();
s.resubmitOnError.value = e.code == AddUserPhoneErrorCode.busy;
} else {
s.error.value = 'err_data_transfer'.l10n;
s.resubmitOnError.value = true;
}
s.unsubmit();
if (modalVisible) {
Navigator.of(router.context!).pop();
}
});
await AddPhoneView.show(
router.context!,
timeout: true,
phone: phone,
).then((_) => modalVisible = false);
},
);
email = TextFieldState(
approvable: true,
onFocus: (s) {
if (s.text.isNotEmpty) {
try {
final email = UserEmail(s.text);
if (myUser.value!.emails.confirmed.contains(email) ||
myUser.value?.emails.unconfirmed == email) {
s.error.value = 'err_you_already_add_this_email'.l10n;
}
} catch (e) {
s.error.value = 'err_incorrect_email'.l10n;
}
}
},
onSubmitted: (s) async {
if (s.text.isEmpty ||
(s.error.value != null && s.resubmitOnError.isFalse)) {
return;
}
final email = UserEmail(s.text.toLowerCase());
s.clear();
bool modalVisible = true;
_myUserService
.addUserEmail(email, locale: L10n.chosen.value?.toString())
.onError((e, __) {
s.unchecked = email.val;
if (e is AddUserEmailException) {
s.error.value = e.toMessage();
s.resubmitOnError.value = e.code == AddUserEmailErrorCode.busy;
} else {
s.error.value = 'err_data_transfer'.l10n;
s.resubmitOnError.value = true;
}
s.unsubmit();
if (modalVisible) {
Navigator.of(router.context!).pop();
}
});
await AddEmailView.show(
router.context!,
email: email,
timeout: true,
).then((_) => modalVisible = false);
},
);
scrollController.addListener(_ensureNameDisplayed);
welcome = WelcomeFieldController(
_chatService,
onSubmit: () async {
final text = welcome.field.text.trim();
if (text.isNotEmpty || welcome.attachments.isNotEmpty) {
final String previousText = text.toString();
final List<Attachment> previousAttachments =
welcome.attachments.map((e) => e.value).toList();
updateWelcomeMessage(
text: text.isEmpty ? null : ChatMessageText(text),
attachments: welcome.attachments.map((e) => e.value).toList(),
).onError((e, _) {
welcome.field.unchecked = previousText;
welcome.attachments.addAll(
previousAttachments.map((e) => MapEntry(GlobalKey(), e)),
);
if (e is LocalizedExceptionMixin) {
MessagePopup.error(e.toMessage());
} else {
MessagePopup.error('err_data_transfer'.l10n);
}
});
welcome.edited.value = null;
welcome.clear();
}
},
);
super.onInit();
}