calcDisplayname method

String calcDisplayname({
  1. bool? formatLocalpart,
  2. bool? mxidLocalPartFallback,
  3. SDNLocalizations i18n = const SDNDefaultLocalizations(),
})

Returns the displayname or the local part of the SDN ID if the user has no displayname. If formatLocalpart is true, then the localpart will be formatted in the way, that all "_" characters are becomming white spaces and the first character of each word becomes uppercase. If mxidLocalPartFallback is true, then the local part of the mxid will be shown if there is no other displayname available. If not then this will return "Unknown user".

Implementation

String calcDisplayname(
    {bool? formatLocalpart,
    bool? mxidLocalPartFallback,
    SDNLocalizations i18n = const SDNDefaultLocalizations()}) {
  formatLocalpart ??= room.client.formatLocalpart;
  mxidLocalPartFallback ??= room.client.mxidLocalPartFallback;
  final displayName = this.displayName;
  if (displayName != null && displayName.isNotEmpty) {
    return displayName;
  }
  final stateKey = this.stateKey;
  if (stateKey != null && mxidLocalPartFallback) {
    if (!formatLocalpart) {
      return stateKey.localpart ?? '';
    }
    final words = stateKey.localpart?.replaceAll('_', ' ').split(' ') ?? [];
    for (var i = 0; i < words.length; i++) {
      if (words[i].isNotEmpty) {
        words[i] = words[i][0].toUpperCase() + words[i].substring(1);
      }
    }
    return words.join(' ').trim();
  }
  return i18n.unknownUser;
}