Skip to content

Commit

Permalink
Add support for user/community styles (#1201)
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo authored Mar 21, 2024
1 parent e0a3a19 commit f494249
Show file tree
Hide file tree
Showing 47 changed files with 903 additions and 319 deletions.
2 changes: 1 addition & 1 deletion lib/community/pages/create_post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'package:thunder/community/bloc/image_bloc.dart';
import 'package:thunder/community/utils/post_card_action_helpers.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/auth/helpers/fetch_account.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/core/enums/local_settings.dart';
import 'package:thunder/core/enums/view_mode.dart';
import 'package:thunder/core/models/post_view_media.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/community/utils/post_card_action_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:thunder/account/bloc/account_bloc.dart';

import 'package:thunder/community/bloc/community_bloc.dart';
import 'package:thunder/community/enums/community_action.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/core/enums/media_type.dart';
import 'package:thunder/core/models/post_view_media.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/community/widgets/community_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/global_context.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/feed/utils/community.dart';

class CommunityDrawer extends StatefulWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/community/widgets/community_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:lemmy_api_client/v3.dart';

import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/shared/avatars/community_avatar.dart';
import 'package:thunder/shared/icon_text.dart';
import 'package:thunder/utils/instance.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/community/widgets/community_list_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:thunder/account/bloc/account_bloc.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/utils/utils.dart';
import 'package:thunder/feed/view/feed_page.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/community/widgets/community_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:thunder/community/bloc/community_bloc.dart';
import 'package:thunder/community/enums/community_action.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/feed/utils/utils.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/community/widgets/post_card_metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/enums/full_name_separator.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/core/enums/view_mode.dart';
import 'package:thunder/feed/feed.dart';
import 'package:thunder/post/enums/post_card_metadata_item.dart';
Expand Down
245 changes: 245 additions & 0 deletions lib/core/enums/full_name.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:thunder/core/enums/font_scale.dart';
import 'package:thunder/shared/text/scalable_text.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';

enum FullNameSeparator {
dot, // name · instance.tld
at, // [email protected]
lemmy; // '@[email protected] or [email protected]'
}

/// --- SAMPLES ---
String generateSampleUserFullName(FullNameSeparator separator) => generateUserFullName(null, 'name', 'instance.tld', userSeparator: separator);

Widget generateSampleUserFullNameWidget(
FullNameSeparator separator, {
bool? weightUserName,
bool? weightInstanceName,
bool? colorizeUserName,
bool? colorizeInstanceName,
TextStyle? textStyle,
ColorScheme? colorScheme,
}) =>
generateUserFullNameWidget(
null,
'name',
'instance.tld',
userSeparator: separator,
weightUserName: weightUserName,
weightInstanceName: weightInstanceName,
colorizeUserName: colorizeUserName,
colorizeInstanceName: colorizeInstanceName,
textStyle: textStyle,
colorScheme: colorScheme,
);

String generateSampleCommunityFullName(FullNameSeparator separator) => generateCommunityFullName(null, 'name', 'instance.tld', communitySeparator: separator);

Widget generateSampleCommunityFullNameWidget(
FullNameSeparator separator, {
bool? weightCommunityName,
bool? weightInstanceName,
bool? colorizeCommunityName,
bool? colorizeInstanceName,
TextStyle? textStyle,
ColorScheme? colorScheme,
}) =>
generateCommunityFullNameWidget(
null,
'name',
'instance.tld',
communitySeparator: separator,
weightCommunityName: weightCommunityName,
weightInstanceName: weightInstanceName,
colorizeCommunityName: colorizeCommunityName,
colorizeInstanceName: colorizeInstanceName,
textStyle: textStyle,
colorScheme: colorScheme,
);

/// --- USERS ---
String generateUserFullNamePrefix(BuildContext? context, name, {FullNameSeparator? userSeparator}) {
assert(context != null || userSeparator != null);
userSeparator ??= context!.read<ThunderBloc>().state.userSeparator;
return switch (userSeparator) {
FullNameSeparator.dot => '$name',
FullNameSeparator.at => '$name',
FullNameSeparator.lemmy => '@$name',
};
}

String generateUserFullNameSuffix(BuildContext? context, instance, {FullNameSeparator? userSeparator}) {
assert(context != null || userSeparator != null);
userSeparator ??= context!.read<ThunderBloc>().state.userSeparator;
return switch (userSeparator) {
FullNameSeparator.dot => ' · $instance',
FullNameSeparator.at => '@$instance',
FullNameSeparator.lemmy => '@$instance',
};
}

String generateUserFullName(BuildContext? context, name, instance, {FullNameSeparator? userSeparator}) {
assert(context != null || userSeparator != null);
userSeparator ??= context!.read<ThunderBloc>().state.userSeparator;
String prefix = generateUserFullNamePrefix(context, name, userSeparator: userSeparator);
String suffix = generateUserFullNameSuffix(context, instance, userSeparator: userSeparator);
return '$prefix$suffix';
}

Text generateUserFullNameWidget(
BuildContext? context,
name,
instance, {
FullNameSeparator? userSeparator,
bool? weightUserName,
bool? weightInstanceName,
bool? colorizeUserName,
bool? colorizeInstanceName,
TextStyle? textStyle,
ColorScheme? colorScheme,
bool includeInstance = true,
FontScale? fontScale,
}) {
assert(context != null || (userSeparator != null && weightUserName != null && weightInstanceName != null && colorizeUserName != null && colorizeInstanceName != null));
assert(context != null || (textStyle != null && colorScheme != null));
String prefix = generateUserFullNamePrefix(context, name, userSeparator: userSeparator);
String suffix = generateUserFullNameSuffix(context, instance, userSeparator: userSeparator);
weightUserName ??= context!.read<ThunderBloc>().state.userFullNameWeightUserName;
weightInstanceName ??= context!.read<ThunderBloc>().state.userFullNameWeightInstanceName;
colorizeUserName ??= context!.read<ThunderBloc>().state.userFullNameColorizeUserName;
colorizeInstanceName ??= context!.read<ThunderBloc>().state.userFullNameColorizeInstanceName;
textStyle ??= Theme.of(context!).textTheme.bodyMedium;
colorScheme ??= Theme.of(context!).colorScheme;

return Text.rich(
softWrap: false,
overflow: TextOverflow.fade,
style: textStyle,
textScaler: TextScaler.noScaling,
TextSpan(
children: [
TextSpan(
text: prefix,
style: textStyle!.copyWith(
fontWeight: weightUserName
? FontWeight.w500
: weightInstanceName
? FontWeight.w300
: null,
color: colorizeUserName ? colorScheme.primary : null,
fontSize: context == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
if (includeInstance == true)
TextSpan(
text: suffix,
style: textStyle.copyWith(
fontWeight: weightInstanceName
? FontWeight.w500
: weightUserName
? FontWeight.w300
: null,
color: colorizeInstanceName ? colorScheme.primary : null,
fontSize: context == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
],
),
);
}

/// --- COMMUNITIES ---
String generateCommunityFullNamePrefix(BuildContext? context, name, {FullNameSeparator? communitySeparator}) {
assert(context != null || communitySeparator != null);
communitySeparator ??= context!.read<ThunderBloc>().state.communitySeparator;
return switch (communitySeparator) {
FullNameSeparator.dot => '$name',
FullNameSeparator.at => '$name',
FullNameSeparator.lemmy => '!$name',
};
}

String generateCommunityFullNameSuffix(BuildContext? context, instance, {FullNameSeparator? communitySeparator}) {
assert(context != null || communitySeparator != null);
communitySeparator ??= context!.read<ThunderBloc>().state.communitySeparator;
return switch (communitySeparator) {
FullNameSeparator.dot => ' · $instance',
FullNameSeparator.at => '@$instance',
FullNameSeparator.lemmy => '@$instance',
};
}

String generateCommunityFullName(BuildContext? context, name, instance, {FullNameSeparator? communitySeparator}) {
assert(context != null || communitySeparator != null);
communitySeparator ??= context!.read<ThunderBloc>().state.communitySeparator;
String prefix = generateCommunityFullNamePrefix(context, name, communitySeparator: communitySeparator);
String suffix = generateCommunityFullNameSuffix(context, instance, communitySeparator: communitySeparator);
return '$prefix$suffix';
}

Text generateCommunityFullNameWidget(
BuildContext? context,
name,
instance, {
FullNameSeparator? communitySeparator,
bool? weightCommunityName,
bool? weightInstanceName,
bool? colorizeCommunityName,
bool? colorizeInstanceName,
TextStyle? textStyle,
ColorScheme? colorScheme,
bool includeInstance = true,
FontScale? fontScale,
}) {
assert(context != null || (communitySeparator != null && weightCommunityName != null && weightInstanceName != null && colorizeCommunityName != null && colorizeInstanceName != null));
assert(context != null || (textStyle != null && colorScheme != null));
String prefix = generateCommunityFullNamePrefix(context, name, communitySeparator: communitySeparator);
String suffix = generateCommunityFullNameSuffix(context, instance, communitySeparator: communitySeparator);
weightCommunityName ??= context!.read<ThunderBloc>().state.communityFullNameWeightCommunityName;
weightInstanceName ??= context!.read<ThunderBloc>().state.communityFullNameWeightInstanceName;
colorizeCommunityName ??= context!.read<ThunderBloc>().state.communityFullNameColorizeCommunityName;
colorizeInstanceName ??= context!.read<ThunderBloc>().state.communityFullNameColorizeInstanceName;
textStyle ??= Theme.of(context!).textTheme.bodyMedium;
colorScheme ??= Theme.of(context!).colorScheme;

return Text.rich(
softWrap: false,
overflow: TextOverflow.fade,
style: textStyle,
textScaler: TextScaler.noScaling,
TextSpan(
children: [
TextSpan(
text: prefix,
style: textStyle!.copyWith(
fontWeight: weightCommunityName
? FontWeight.w500
: weightInstanceName
? FontWeight.w300
: null,
color: colorizeCommunityName ? colorScheme.primary : null,
fontSize: context == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
if (includeInstance == true)
TextSpan(
text: suffix,
style: textStyle.copyWith(
fontWeight: weightInstanceName
? FontWeight.w500
: weightCommunityName
? FontWeight.w300
: null,
color: colorizeInstanceName ? colorScheme.primary : null,
fontSize: context == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
],
),
);
}
39 changes: 0 additions & 39 deletions lib/core/enums/full_name_separator.dart

This file was deleted.

Loading

0 comments on commit f494249

Please sign in to comment.