mirror of
https://github.com/Livinglist/Hacki.git
synced 2025-08-06 18:24:42 +08:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
ef557e7b84 |
@ -133,6 +133,8 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
|
||||
StoriesRefresh event,
|
||||
Emitter<StoriesState> emit,
|
||||
) async {
|
||||
if (state.statusByType[event.type] == StoriesStatus.loading) return;
|
||||
|
||||
emit(
|
||||
state.copyWithStatusUpdated(
|
||||
type: event.type,
|
||||
|
@ -27,7 +27,7 @@ class PinCubit extends Cubit<PinState> {
|
||||
emit(state.copyWith(pinnedStoriesIds: ids));
|
||||
|
||||
_storiesRepository.fetchStoriesStream(ids: ids).listen(_onStoryFetched);
|
||||
});
|
||||
}).whenComplete(() => emit(state.copyWith(status: Status.loaded)));
|
||||
}
|
||||
|
||||
void pinStory(Story story) {
|
||||
@ -52,7 +52,10 @@ class PinCubit extends Cubit<PinState> {
|
||||
_preferenceRepository.updatePinnedStoriesIds(state.pinnedStoriesIds);
|
||||
}
|
||||
|
||||
void refresh() => init();
|
||||
void refresh() {
|
||||
if (state.status == Status.loading) return;
|
||||
init();
|
||||
}
|
||||
|
||||
void _onStoryFetched(Story story) {
|
||||
emit(state.copyWith(pinnedStories: <Story>[...state.pinnedStories, story]));
|
||||
|
@ -4,22 +4,27 @@ class PinState extends Equatable {
|
||||
const PinState({
|
||||
required this.pinnedStoriesIds,
|
||||
required this.pinnedStories,
|
||||
required this.status,
|
||||
});
|
||||
|
||||
PinState.init()
|
||||
: pinnedStoriesIds = <int>[],
|
||||
pinnedStories = <Story>[];
|
||||
pinnedStories = <Story>[],
|
||||
status = Status.idle;
|
||||
|
||||
final List<int> pinnedStoriesIds;
|
||||
final List<Story> pinnedStories;
|
||||
final Status status;
|
||||
|
||||
PinState copyWith({
|
||||
List<int>? pinnedStoriesIds,
|
||||
List<Story>? pinnedStories,
|
||||
Status? status,
|
||||
}) {
|
||||
return PinState(
|
||||
pinnedStoriesIds: pinnedStoriesIds ?? this.pinnedStoriesIds,
|
||||
pinnedStories: pinnedStories ?? this.pinnedStories,
|
||||
status: status ?? this.status,
|
||||
);
|
||||
}
|
||||
|
||||
@ -27,5 +32,6 @@ class PinState extends Equatable {
|
||||
List<Object?> get props => <Object?>[
|
||||
pinnedStoriesIds,
|
||||
pinnedStories,
|
||||
status,
|
||||
];
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ enum FontSize {
|
||||
small('Small', TextDimens.pt15),
|
||||
regular('Regular', TextDimens.pt16),
|
||||
large('Large', TextDimens.pt17),
|
||||
xlarge('XLarge', TextDimens.pt18);
|
||||
xlarge('XLarge', TextDimens.pt18),
|
||||
xxlarge('XXLarge', TextDimens.pt19);
|
||||
|
||||
const FontSize(this.description, this.fontSize);
|
||||
|
||||
|
@ -7,5 +7,6 @@ export 'item/item.dart';
|
||||
export 'post_data.dart';
|
||||
export 'preference.dart';
|
||||
export 'search_params.dart';
|
||||
export 'status.dart';
|
||||
export 'story_type.dart';
|
||||
export 'user.dart';
|
||||
|
6
lib/models/status.dart
Normal file
6
lib/models/status.dart
Normal file
@ -0,0 +1,6 @@
|
||||
enum Status {
|
||||
idle,
|
||||
loading,
|
||||
loaded,
|
||||
error,
|
||||
}
|
@ -43,6 +43,7 @@ class CustomAppBar extends AppBar {
|
||||
fontFamily: FeatherIcons.type.fontFamily,
|
||||
package: FeatherIcons.type.fontPackage,
|
||||
),
|
||||
textScaleFactor: 1,
|
||||
),
|
||||
onPressed: onFontSizeTap,
|
||||
),
|
||||
|
@ -255,6 +255,8 @@ class _ParentItemSection extends StatelessWidget {
|
||||
style: const TextStyle(
|
||||
color: Palette.orange,
|
||||
),
|
||||
textScaleFactor:
|
||||
MediaQuery.of(context).textScaleFactor,
|
||||
),
|
||||
const Spacer(),
|
||||
Text(
|
||||
@ -262,6 +264,8 @@ class _ParentItemSection extends StatelessWidget {
|
||||
style: const TextStyle(
|
||||
color: Palette.grey,
|
||||
),
|
||||
textScaleFactor:
|
||||
MediaQuery.of(context).textScaleFactor,
|
||||
),
|
||||
],
|
||||
),
|
||||
@ -334,9 +338,8 @@ class _ParentItemSection extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
textScaleFactor: MediaQuery.of(
|
||||
context,
|
||||
).textScaleFactor,
|
||||
textScaleFactor:
|
||||
MediaQuery.of(context).textScaleFactor,
|
||||
),
|
||||
),
|
||||
)
|
||||
@ -354,6 +357,8 @@ class _ParentItemSection extends StatelessWidget {
|
||||
),
|
||||
child: ItemText(
|
||||
item: state.item,
|
||||
textScaleFactor:
|
||||
MediaQuery.of(context).textScaleFactor,
|
||||
),
|
||||
),
|
||||
),
|
||||
@ -402,6 +407,7 @@ class _ParentItemSection extends StatelessWidget {
|
||||
style: const TextStyle(
|
||||
fontSize: TextDimens.pt13,
|
||||
),
|
||||
textScaleFactor: 1,
|
||||
),
|
||||
] else ...<Widget>[
|
||||
const SizedBox(
|
||||
@ -444,6 +450,7 @@ class _ParentItemSection extends StatelessWidget {
|
||||
style: TextStyle(
|
||||
fontSize: TextDimens.pt13,
|
||||
),
|
||||
textScaleFactor: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
@ -462,6 +469,7 @@ class _ParentItemSection extends StatelessWidget {
|
||||
style: const TextStyle(
|
||||
fontSize: TextDimens.pt13,
|
||||
),
|
||||
textScaleFactor: 1,
|
||||
),
|
||||
),
|
||||
)
|
||||
@ -483,6 +491,7 @@ class _ParentItemSection extends StatelessWidget {
|
||||
style: const TextStyle(
|
||||
fontSize: TextDimens.pt13,
|
||||
),
|
||||
textScaleFactor: 1,
|
||||
),
|
||||
),
|
||||
)
|
||||
|
@ -345,6 +345,8 @@ class _ReplyBoxState extends State<ReplyBox> {
|
||||
child: SingleChildScrollView(
|
||||
child: ItemText(
|
||||
item: replyingTo,
|
||||
textScaleFactor:
|
||||
MediaQuery.of(context).textScaleFactor,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -143,6 +143,8 @@ class CommentTile extends StatelessWidget {
|
||||
? orange
|
||||
: color,
|
||||
),
|
||||
textScaleFactor:
|
||||
MediaQuery.of(context).textScaleFactor,
|
||||
),
|
||||
if (comment.by == opUsername)
|
||||
const Text(
|
||||
@ -157,6 +159,8 @@ class CommentTile extends StatelessWidget {
|
||||
style: const TextStyle(
|
||||
color: Palette.grey,
|
||||
),
|
||||
textScaleFactor:
|
||||
MediaQuery.of(context).textScaleFactor,
|
||||
),
|
||||
],
|
||||
),
|
||||
@ -196,6 +200,8 @@ class CommentTile extends StatelessWidget {
|
||||
child: ItemText(
|
||||
key: ValueKey<int>(comment.id),
|
||||
item: comment,
|
||||
textScaleFactor: MediaQuery.of(context)
|
||||
.textScaleFactor,
|
||||
onTap: () {
|
||||
if (onTap == null) {
|
||||
_onTextTapped(context);
|
||||
|
@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:hacki/screens/widgets/custom_linkify/linkifiers/linkifiers.dart';
|
||||
import 'package:hacki/styles/palette.dart';
|
||||
import 'package:hacki/utils/utils.dart';
|
||||
import 'package:linkify/linkify.dart';
|
||||
import 'package:linkify/linkify.dart' hide UrlLinkifier;
|
||||
|
||||
export 'package:hacki/screens/widgets/custom_linkify/linkifiers/linkifiers.dart';
|
||||
export 'package:linkify/linkify.dart'
|
||||
@ -14,9 +14,7 @@ export 'package:linkify/linkify.dart'
|
||||
Linkifier,
|
||||
LinkifyElement,
|
||||
LinkifyOptions,
|
||||
TextElement,
|
||||
UrlElement,
|
||||
UrlLinkifier;
|
||||
TextElement;
|
||||
|
||||
/// Callback clicked link
|
||||
typedef LinkCallback = void Function(LinkableElement link);
|
||||
|
@ -1,2 +1,3 @@
|
||||
export 'emphasis_linkifier.dart';
|
||||
export 'quote_linkifier.dart';
|
||||
export 'url_linkifier.dart';
|
||||
|
121
lib/screens/widgets/custom_linkify/linkifiers/url_linkifier.dart
Normal file
121
lib/screens/widgets/custom_linkify/linkifiers/url_linkifier.dart
Normal file
@ -0,0 +1,121 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:linkify/linkify.dart';
|
||||
|
||||
final RegExp _urlRegex = RegExp(
|
||||
r'^(.*?)((?:https?:\/\/|www\.)[^\s/$.?#].[\/\\\%:\?=&#@;A-Za-z0-9_.~-]*)',
|
||||
caseSensitive: false,
|
||||
dotAll: true,
|
||||
);
|
||||
|
||||
final RegExp _looseUrlRegex = RegExp(
|
||||
r'''^(.*?)((https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//="'`]*))''',
|
||||
caseSensitive: false,
|
||||
dotAll: true,
|
||||
);
|
||||
|
||||
final RegExp _protocolIdentifierRegex = RegExp(
|
||||
r'^(https?:\/\/)',
|
||||
caseSensitive: false,
|
||||
);
|
||||
|
||||
class UrlLinkifier extends Linkifier {
|
||||
const UrlLinkifier();
|
||||
|
||||
@override
|
||||
List<LinkifyElement> parse(
|
||||
List<LinkifyElement> elements,
|
||||
LinkifyOptions options,
|
||||
) {
|
||||
final List<LinkifyElement> list = <LinkifyElement>[];
|
||||
|
||||
for (final LinkifyElement element in elements) {
|
||||
if (element is TextElement) {
|
||||
final RegExpMatch? match = options.looseUrl
|
||||
? _looseUrlRegex.firstMatch(element.text)
|
||||
: _urlRegex.firstMatch(element.text);
|
||||
|
||||
if (match == null) {
|
||||
list.add(element);
|
||||
} else {
|
||||
final String text = element.text.replaceFirst(match.group(0)!, '');
|
||||
|
||||
if (match.group(1)?.isNotEmpty ?? false) {
|
||||
list.add(TextElement(match.group(1)!));
|
||||
}
|
||||
|
||||
if (match.group(2)?.isNotEmpty ?? false) {
|
||||
String originalUrl = match.group(2)!;
|
||||
String originText = originalUrl;
|
||||
String? end;
|
||||
|
||||
if ((options.excludeLastPeriod) &&
|
||||
originalUrl[originalUrl.length - 1] == '.') {
|
||||
end = '.';
|
||||
originText = originText.substring(0, originText.length - 1);
|
||||
originalUrl = originalUrl.substring(0, originalUrl.length - 1);
|
||||
}
|
||||
|
||||
String url = originalUrl;
|
||||
|
||||
if (!originalUrl.startsWith(_protocolIdentifierRegex)) {
|
||||
originalUrl = (options.defaultToHttps ? 'https://' : 'http://') +
|
||||
originalUrl;
|
||||
}
|
||||
|
||||
if ((options.humanize) || (options.removeWww)) {
|
||||
if (options.humanize) {
|
||||
url = url.replaceFirst(RegExp('https?://'), '');
|
||||
}
|
||||
if (options.removeWww) {
|
||||
url = url.replaceFirst(RegExp(r'www\.'), '');
|
||||
}
|
||||
|
||||
list.add(
|
||||
UrlElement(
|
||||
originalUrl,
|
||||
url,
|
||||
originText,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
list.add(UrlElement(originalUrl, null, originText));
|
||||
}
|
||||
|
||||
if (end != null) {
|
||||
list.add(TextElement(end));
|
||||
}
|
||||
}
|
||||
|
||||
if (text.isNotEmpty) {
|
||||
list.addAll(parse(<LinkifyElement>[TextElement(text)], options));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
list.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents an element containing a link
|
||||
@immutable
|
||||
class UrlElement extends LinkableElement {
|
||||
UrlElement(String url, [String? text, String? originText])
|
||||
: super(text, url, originText);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "LinkElement: '$url' ($text)";
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => equals(other);
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(text, originText, url);
|
||||
|
||||
@override
|
||||
bool equals(dynamic other) => other is UrlElement && super.equals(other);
|
||||
}
|
@ -10,12 +10,14 @@ import 'package:hacki/utils/utils.dart';
|
||||
class ItemText extends StatelessWidget {
|
||||
const ItemText({
|
||||
required this.item,
|
||||
required this.textScaleFactor,
|
||||
super.key,
|
||||
this.onTap,
|
||||
});
|
||||
|
||||
final Item item;
|
||||
final VoidCallback? onTap;
|
||||
final double textScaleFactor;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -37,7 +39,7 @@ class ItemText extends StatelessWidget {
|
||||
onOpen: (LinkableElement link) => LinkUtil.launch(link.url),
|
||||
),
|
||||
onTap: onTap,
|
||||
textScaleFactor: MediaQuery.of(context).textScaleFactor,
|
||||
textScaleFactor: textScaleFactor,
|
||||
contextMenuBuilder: (
|
||||
BuildContext context,
|
||||
EditableTextState editableTextState,
|
||||
@ -52,7 +54,7 @@ class ItemText extends StatelessWidget {
|
||||
} else {
|
||||
return SelectableLinkify(
|
||||
text: item.text,
|
||||
textScaleFactor: MediaQuery.of(context).textScaleFactor,
|
||||
textScaleFactor: textScaleFactor,
|
||||
style: style,
|
||||
linkStyle: linkStyle,
|
||||
onOpen: (LinkableElement link) => LinkUtil.launch(link.url),
|
||||
|
@ -33,6 +33,7 @@ abstract class TextDimens {
|
||||
static const double pt16 = 16;
|
||||
static const double pt17 = 17;
|
||||
static const double pt18 = 18;
|
||||
static const double pt19 = 19;
|
||||
static const double pt20 = 20;
|
||||
static const double pt24 = 24;
|
||||
static const double pt26 = 26;
|
||||
|
@ -2,7 +2,6 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
import 'package:hacki/config/constants.dart';
|
||||
import 'package:hacki/config/locator.dart';
|
||||
import 'package:hacki/extensions/extensions.dart';
|
||||
import 'package:hacki/main.dart';
|
||||
@ -54,17 +53,7 @@ abstract class LinkUtil {
|
||||
return;
|
||||
}
|
||||
|
||||
Uri rinseLink(String link) {
|
||||
final RegExp regex = RegExp(RegExpConstants.linkSuffix);
|
||||
if (!link.contains('en.wikipedia.org') && link.contains(regex)) {
|
||||
final String match = regex.stringMatch(link) ?? '';
|
||||
return Uri.parse(link.replaceAll(match, ''));
|
||||
}
|
||||
|
||||
return Uri.parse(link);
|
||||
}
|
||||
|
||||
final Uri uri = rinseLink(link);
|
||||
final Uri uri = Uri.parse(link);
|
||||
canLaunchUrl(uri).then((bool val) {
|
||||
if (val) {
|
||||
if (link.contains('http')) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import 'package:hacki/screens/widgets/custom_linkify/linkifiers/linkifiers.dart';
|
||||
import 'package:linkify/linkify.dart';
|
||||
import 'package:linkify/linkify.dart' hide UrlLinkifier;
|
||||
|
||||
abstract class LinkifierUtil {
|
||||
static const LinkifyOptions linkifyOptions = LinkifyOptions(humanize: false);
|
||||
|
@ -1,6 +1,6 @@
|
||||
name: hacki
|
||||
description: A Hacker News reader.
|
||||
version: 1.8.0+116
|
||||
version: 1.8.1+117
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
|
Reference in New Issue
Block a user