Skip to content

Commit

Permalink
feat: add ability to hide login names
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 committed May 12, 2024
1 parent 4283406 commit e75bc96
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
18 changes: 10 additions & 8 deletions lib/components/chat_history/twitch/follow_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:rtchat/components/chat_history/decorated_event.dart';
import 'package:rtchat/components/image/resilient_network_image.dart';
import 'package:rtchat/models/messages/twitch/event.dart';
import 'package:rtchat/models/style.dart';
import 'package:styled_text/styled_text.dart';

class TwitchFollowEventWidget extends StatelessWidget {
Expand All @@ -18,12 +20,12 @@ class TwitchFollowEventWidget extends StatelessWidget {
avatars: model.followers
.sublist(0, min(3, model.followers.length))
.map((follower) => ResilientNetworkImage(follower.profilePictureUrl)),
child: Builder(builder: ((context) {
child: Consumer<StyleModel>(builder: (context, styleModel, child) {
switch (model.followers.length) {
case 1:
return StyledText(
text: AppLocalizations.of(context)!
.followingEvent(model.followers.first.display),
text: AppLocalizations.of(context)!.followingEvent(
styleModel.getTwitchDisplayName(model.followers.first)),
tags: {
'bold': StyledTextTag(
style: Theme.of(context).textTheme.titleSmall),
Expand All @@ -32,8 +34,8 @@ class TwitchFollowEventWidget extends StatelessWidget {
// return x and y are following you.
return StyledText(
text: AppLocalizations.of(context)!.followingEvent2(
model.followers.first.display,
model.followers.last.display),
styleModel.getTwitchDisplayName(model.followers.first),
styleModel.getTwitchDisplayName(model.followers.last)),
tags: {
'bold': StyledTextTag(
style: Theme.of(context).textTheme.titleSmall),
Expand All @@ -42,15 +44,15 @@ class TwitchFollowEventWidget extends StatelessWidget {
// return x, y, and n others are following you.
return StyledText(
text: AppLocalizations.of(context)!.followingEvent3(
model.followers.first.display,
model.followers.last.display,
styleModel.getTwitchDisplayName(model.followers.first),
styleModel.getTwitchDisplayName(model.followers.last),
model.followers.length - 2),
tags: {
'bold': StyledTextTag(
style: Theme.of(context).textTheme.titleSmall),
});
}
})),
}),
);
}
}
11 changes: 7 additions & 4 deletions lib/components/chat_history/twitch/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ class TwitchMessageWidget extends StatelessWidget {
final orientation = snapshot.data;
if (orientation == null) {
return RichText(
text:
TextSpan(text: model.author.display, style: authorStyle));
text: TextSpan(
text: styleModel.getTwitchDisplayName(model.author),
style: authorStyle));
}
final hslColor = HSLColor.fromColor(
styleModel.applyLightnessBoost(context, color));
Expand All @@ -172,7 +173,7 @@ class TwitchMessageWidget extends StatelessWidget {
hslColor.withHue((hslColor.hue - deg) % 360).toColor();
return RichText(
text: TextSpan(
text: model.author.display,
text: styleModel.getTwitchDisplayName(model.author),
style: authorStyle.copyWith(color: shimmer)));
});
}
Expand All @@ -184,7 +185,9 @@ class TwitchMessageWidget extends StatelessWidget {
if (contributors.contains(model.author.userId)) {
return WidgetSpan(child: authorWidget(author, styleModel, authorStyle));
}
return TextSpan(text: model.author.display, style: authorStyle);
return TextSpan(
text: styleModel.getTwitchDisplayName(model.author),
style: authorStyle);
}

@override
Expand Down
11 changes: 1 addition & 10 deletions lib/models/messages/twitch/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,8 @@ class TwitchUserModel {

bool get isBot => botList.contains(login.toLowerCase());

String get display {
final author = displayName ?? login;
if (author.toLowerCase() != login) {
// this is an internationalized name.
return "$displayName ($login)";
}
return author;
}

Color get color {
final n = display.codeUnits.first + display.codeUnits.last;
final n = login.codeUnits.first + login.codeUnits.last;
return colors[n % colors.length];
}

Expand Down
22 changes: 22 additions & 0 deletions lib/models/style.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:core';

import 'package:flutter/material.dart';
import 'package:rtchat/models/messages/twitch/user.dart';

Color darken(Color color, [double amount = .1]) {
assert(amount >= 0 && amount <= 1);
Expand Down Expand Up @@ -54,6 +55,7 @@ class StyleModel extends ChangeNotifier {
bool _isDeletedMessagesVisible = true;
CompactMessages _compactMessages = CompactMessages.none;
bool _isDiscoModeAvailable = false;
bool _isLoginShown = true;

double get fontSize {
return _fontSize;
Expand Down Expand Up @@ -99,6 +101,22 @@ class StyleModel extends ChangeNotifier {
notifyListeners();
}

bool get isLoginShown => _isLoginShown;

set isLoginShown(bool isLoginShown) {
_isLoginShown = isLoginShown;
notifyListeners();
}

String getTwitchDisplayName(TwitchUserModel user) {
final author = user.displayName ?? user.login;
if (author.toLowerCase() != user.login && isLoginShown) {
// this is an internationalized name.
return "${user.displayName} (${user.login})";
}
return author;
}

bool get isDiscoModeAvailable => _isDiscoModeAvailable;

StyleModel.fromJson(Map<String, dynamic> json) {
Expand All @@ -117,6 +135,9 @@ class StyleModel extends ChangeNotifier {
if (json['isDiscoModeEnabled'] != null) {
_isDiscoModeAvailable = json['isDiscoModeEnabled'];
}
if (json['isLoginShown'] != null) {
_isLoginShown = json['isLoginShown'];
}
}

Map<String, dynamic> toJson() => {
Expand All @@ -125,5 +146,6 @@ class StyleModel extends ChangeNotifier {
"isDeletedMessagesVisible": _isDeletedMessagesVisible,
"compactMessages": _compactMessages.toJson(),
"isDiscoModeEnabled": _isDiscoModeAvailable,
"isLoginShown": _isLoginShown,
};
}
14 changes: 13 additions & 1 deletion lib/screens/settings/chat_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ final message2 = TwitchMessageModel(
channelId: 'placeholder');
final message3 = TwitchMessageModel(
messageId: "placeholder3",
author: const TwitchUserModel(userId: 'muxfd', login: 'muxfd'),
author: const TwitchUserModel(
userId: 'muxfd', login: 'muxfd', displayName: 'マックス'),
tags: {
"color": "#00FF7F",
"badges-raw": "broadcaster/1,moderator/1",
Expand Down Expand Up @@ -141,6 +142,17 @@ class ChatHistoryScreen extends StatelessWidget {
styleModel.isDeletedMessagesVisible = value;
},
),
SwitchListTile.adaptive(
title: const Text('Show login names'),
subtitle: styleModel.isLoginShown
? const Text(
"Login names will be shown if they are different from the display name")
: const Text("Login names will be hidden"),
value: styleModel.isLoginShown,
onChanged: (value) {
styleModel.isLoginShown = value;
},
),
Padding(
padding: const EdgeInsets.all(16),
child: Text("Compact messages",
Expand Down

2 comments on commit e75bc96

@SputNikPlop
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevmo314 Maybe for Chinese, Korean, and Japanese (only three regions supporting localized usernames) users to have this maybe toggled off if they set like the language natively, but in any other region this would be toggled as true. This might not be practical though for uniformity but space wise it would save some screen space.

@kevmo314
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevmo314 Maybe for Chinese, Korean, and Japanese (only three regions supporting localized usernames) users to have this maybe toggled off if they set like the language natively, but in any other region this would be toggled as true. This might not be practical though for uniformity but space wise it would save some screen space.

Yeah, ideally we should actually auto-detect this setting based on the user's phone's language. I'm not really sure how to do that though.

Please sign in to comment.