first bang implementation
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
import 'package:bang_navigator/features/bangs/data/database/database.dart'
|
||||
show BangCompanion;
|
||||
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||
import 'package:drift/drift.dart' show Expression, Insertable, Value;
|
||||
import 'package:fast_equatable/fast_equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'bang.g.dart';
|
||||
|
||||
enum BangGroup {
|
||||
assistant,
|
||||
kagi,
|
||||
}
|
||||
|
||||
enum BangFormat {
|
||||
///When the bang is invoked with no query, opens the base path of the URL (/)
|
||||
///instead of any path given in the template (g., /search)
|
||||
@JsonValue('open_base_path')
|
||||
openBasePath,
|
||||
|
||||
///URL encode the search terms. Some sites do not work with this, so it can
|
||||
///be disabled by omitting this.
|
||||
@JsonValue('url_encode_placeholder')
|
||||
urlEncodePlaceholder,
|
||||
|
||||
///URL encodes spaces as +, instead of %20. Some sites only work correctly
|
||||
///with one or the other.
|
||||
@JsonValue('url_encode_space_to_plus')
|
||||
urlEncodeSpaceToPlus,
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
@CopyWith()
|
||||
class Bang with FastEquatable implements Insertable<Bang> {
|
||||
static const _templateQueryPlaceholder = '{{{s}}}';
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
final BangGroup? group;
|
||||
|
||||
///The name of the website associated with the bang.
|
||||
@JsonKey(name: 's')
|
||||
final String websiteName;
|
||||
|
||||
///The domain name of the websit
|
||||
@JsonKey(name: 'd')
|
||||
final String domain;
|
||||
|
||||
///The specific trigger word or phrase used to invoke the bang.
|
||||
@JsonKey(name: 't')
|
||||
final String trigger;
|
||||
|
||||
///The URL template to use when the bang is invoked, where `{{{s}}}` is replaced by the user's query.
|
||||
@JsonKey(name: 'u')
|
||||
final String urlTemplate;
|
||||
|
||||
///The category of the website, if applicable
|
||||
@JsonKey(name: 'c')
|
||||
final String? category;
|
||||
|
||||
///The subcategory of the website, if applicable
|
||||
@JsonKey(name: 'sc')
|
||||
final String? subCategory;
|
||||
|
||||
///The format flags indicating how the query should be processed.
|
||||
@JsonKey(name: 'fmt')
|
||||
final Set<BangFormat>? format;
|
||||
|
||||
String formatQuery(String input) {
|
||||
return (format == null ||
|
||||
format!.contains(BangFormat.urlEncodePlaceholder) == true)
|
||||
? (format == null ||
|
||||
format?.contains(BangFormat.urlEncodeSpaceToPlus) == true)
|
||||
? Uri.encodeQueryComponent(input)
|
||||
: Uri.encodeComponent(input)
|
||||
: input;
|
||||
}
|
||||
|
||||
Uri getUrl(String? query) {
|
||||
final url = (query != null)
|
||||
? urlTemplate.replaceAll(_templateQueryPlaceholder, formatQuery(query))
|
||||
: urlTemplate;
|
||||
|
||||
var template = Uri.parse(url);
|
||||
if (!template.hasScheme || template.origin.isEmpty) {
|
||||
template = Uri.https(domain).replace(
|
||||
path: template.path,
|
||||
query: template.query,
|
||||
);
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
static Set<BangFormat> decodeFormat(Iterable input) {
|
||||
return input.map((e) => $enumDecode(_$BangFormatEnumMap, e)).toSet();
|
||||
}
|
||||
|
||||
static List<String> encodeFormat(Iterable<BangFormat> format) {
|
||||
return format.map((e) => _$BangFormatEnumMap[e]!).toList();
|
||||
}
|
||||
|
||||
Bang({
|
||||
required this.websiteName,
|
||||
required this.domain,
|
||||
required this.trigger,
|
||||
required this.urlTemplate,
|
||||
this.group,
|
||||
this.category,
|
||||
this.subCategory,
|
||||
this.format,
|
||||
});
|
||||
|
||||
factory Bang.fromJson(Map<String, dynamic> json) => _$BangFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$BangToJson(this);
|
||||
|
||||
@override
|
||||
bool get cacheHash => true;
|
||||
|
||||
@override
|
||||
List<Object?> get hashParameters => [
|
||||
group,
|
||||
websiteName,
|
||||
domain,
|
||||
trigger,
|
||||
urlTemplate,
|
||||
category,
|
||||
subCategory,
|
||||
format,
|
||||
];
|
||||
|
||||
@override
|
||||
Map<String, Expression<Object>> toColumns(bool nullToAbsent) {
|
||||
return BangCompanion(
|
||||
trigger: Value(trigger),
|
||||
websiteName: Value(websiteName),
|
||||
domain: Value(domain),
|
||||
urlTemplate: Value(urlTemplate),
|
||||
group: Value.absentIfNull(group),
|
||||
category: Value.absentIfNull(category),
|
||||
subCategory: Value.absentIfNull(subCategory),
|
||||
format: Value.absentIfNull(format),
|
||||
).toColumns(nullToAbsent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'bang.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// CopyWithGenerator
|
||||
// **************************************************************************
|
||||
|
||||
abstract class _$BangCWProxy {
|
||||
Bang websiteName(String websiteName);
|
||||
|
||||
Bang domain(String domain);
|
||||
|
||||
Bang trigger(String trigger);
|
||||
|
||||
Bang urlTemplate(String urlTemplate);
|
||||
|
||||
Bang group(BangGroup? group);
|
||||
|
||||
Bang category(String? category);
|
||||
|
||||
Bang subCategory(String? subCategory);
|
||||
|
||||
Bang format(Set<BangFormat>? format);
|
||||
|
||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `Bang(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||
///
|
||||
/// Usage
|
||||
/// ```dart
|
||||
/// Bang(...).copyWith(id: 12, name: "My name")
|
||||
/// ````
|
||||
Bang call({
|
||||
String? websiteName,
|
||||
String? domain,
|
||||
String? trigger,
|
||||
String? urlTemplate,
|
||||
BangGroup? group,
|
||||
String? category,
|
||||
String? subCategory,
|
||||
Set<BangFormat>? format,
|
||||
});
|
||||
}
|
||||
|
||||
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfBang.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfBang.copyWith.fieldName(...)`
|
||||
class _$BangCWProxyImpl implements _$BangCWProxy {
|
||||
const _$BangCWProxyImpl(this._value);
|
||||
|
||||
final Bang _value;
|
||||
|
||||
@override
|
||||
Bang websiteName(String websiteName) => this(websiteName: websiteName);
|
||||
|
||||
@override
|
||||
Bang domain(String domain) => this(domain: domain);
|
||||
|
||||
@override
|
||||
Bang trigger(String trigger) => this(trigger: trigger);
|
||||
|
||||
@override
|
||||
Bang urlTemplate(String urlTemplate) => this(urlTemplate: urlTemplate);
|
||||
|
||||
@override
|
||||
Bang group(BangGroup? group) => this(group: group);
|
||||
|
||||
@override
|
||||
Bang category(String? category) => this(category: category);
|
||||
|
||||
@override
|
||||
Bang subCategory(String? subCategory) => this(subCategory: subCategory);
|
||||
|
||||
@override
|
||||
Bang format(Set<BangFormat>? format) => this(format: format);
|
||||
|
||||
@override
|
||||
|
||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `Bang(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||
///
|
||||
/// Usage
|
||||
/// ```dart
|
||||
/// Bang(...).copyWith(id: 12, name: "My name")
|
||||
/// ````
|
||||
Bang call({
|
||||
Object? websiteName = const $CopyWithPlaceholder(),
|
||||
Object? domain = const $CopyWithPlaceholder(),
|
||||
Object? trigger = const $CopyWithPlaceholder(),
|
||||
Object? urlTemplate = const $CopyWithPlaceholder(),
|
||||
Object? group = const $CopyWithPlaceholder(),
|
||||
Object? category = const $CopyWithPlaceholder(),
|
||||
Object? subCategory = const $CopyWithPlaceholder(),
|
||||
Object? format = const $CopyWithPlaceholder(),
|
||||
}) {
|
||||
return Bang(
|
||||
websiteName:
|
||||
websiteName == const $CopyWithPlaceholder() || websiteName == null
|
||||
? _value.websiteName
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: websiteName as String,
|
||||
domain: domain == const $CopyWithPlaceholder() || domain == null
|
||||
? _value.domain
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: domain as String,
|
||||
trigger: trigger == const $CopyWithPlaceholder() || trigger == null
|
||||
? _value.trigger
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: trigger as String,
|
||||
urlTemplate:
|
||||
urlTemplate == const $CopyWithPlaceholder() || urlTemplate == null
|
||||
? _value.urlTemplate
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: urlTemplate as String,
|
||||
group: group == const $CopyWithPlaceholder()
|
||||
? _value.group
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: group as BangGroup?,
|
||||
category: category == const $CopyWithPlaceholder()
|
||||
? _value.category
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: category as String?,
|
||||
subCategory: subCategory == const $CopyWithPlaceholder()
|
||||
? _value.subCategory
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: subCategory as String?,
|
||||
format: format == const $CopyWithPlaceholder()
|
||||
? _value.format
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: format as Set<BangFormat>?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension $BangCopyWith on Bang {
|
||||
/// Returns a callable class that can be used as follows: `instanceOfBang.copyWith(...)` or like so:`instanceOfBang.copyWith.fieldName(...)`.
|
||||
// ignore: library_private_types_in_public_api
|
||||
_$BangCWProxy get copyWith => _$BangCWProxyImpl(this);
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Bang _$BangFromJson(Map<String, dynamic> json) => Bang(
|
||||
websiteName: json['s'] as String,
|
||||
domain: json['d'] as String,
|
||||
trigger: json['t'] as String,
|
||||
urlTemplate: json['u'] as String,
|
||||
category: json['c'] as String?,
|
||||
subCategory: json['sc'] as String?,
|
||||
format: (json['fmt'] as List<dynamic>?)
|
||||
?.map((e) => $enumDecode(_$BangFormatEnumMap, e))
|
||||
.toSet(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$BangToJson(Bang instance) => <String, dynamic>{
|
||||
's': instance.websiteName,
|
||||
'd': instance.domain,
|
||||
't': instance.trigger,
|
||||
'u': instance.urlTemplate,
|
||||
'c': instance.category,
|
||||
'sc': instance.subCategory,
|
||||
'fmt': instance.format?.map((e) => _$BangFormatEnumMap[e]!).toList(),
|
||||
};
|
||||
|
||||
const _$BangFormatEnumMap = {
|
||||
BangFormat.openBasePath: 'open_base_path',
|
||||
BangFormat.urlEncodePlaceholder: 'url_encode_placeholder',
|
||||
BangFormat.urlEncodeSpaceToPlus: 'url_encode_space_to_plus',
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'package:bang_navigator/features/bangs/data/models/bang.dart';
|
||||
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
part 'bang_data.g.dart';
|
||||
|
||||
@CopyWith()
|
||||
class BangData extends Bang {
|
||||
final int frequency;
|
||||
final DateTime? lastUsed;
|
||||
|
||||
final Uint8List? iconData;
|
||||
|
||||
BangData({
|
||||
required super.websiteName,
|
||||
required super.domain,
|
||||
required super.trigger,
|
||||
required super.urlTemplate,
|
||||
super.category,
|
||||
super.subCategory,
|
||||
super.format,
|
||||
int? frequency,
|
||||
this.lastUsed,
|
||||
this.iconData,
|
||||
}) : frequency = frequency ?? 0;
|
||||
|
||||
@override
|
||||
bool get cacheHash => true;
|
||||
|
||||
@override
|
||||
List<Object?> get hashParameters => [
|
||||
...super.hashParameters,
|
||||
frequency,
|
||||
lastUsed,
|
||||
iconData,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'bang_data.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// CopyWithGenerator
|
||||
// **************************************************************************
|
||||
|
||||
abstract class _$BangDataCWProxy {
|
||||
BangData websiteName(String websiteName);
|
||||
|
||||
BangData domain(String domain);
|
||||
|
||||
BangData trigger(String trigger);
|
||||
|
||||
BangData urlTemplate(String urlTemplate);
|
||||
|
||||
BangData category(String? category);
|
||||
|
||||
BangData subCategory(String? subCategory);
|
||||
|
||||
BangData format(Set<BangFormat>? format);
|
||||
|
||||
BangData frequency(int? frequency);
|
||||
|
||||
BangData lastUsed(DateTime? lastUsed);
|
||||
|
||||
BangData iconData(Uint8List? iconData);
|
||||
|
||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `BangData(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||
///
|
||||
/// Usage
|
||||
/// ```dart
|
||||
/// BangData(...).copyWith(id: 12, name: "My name")
|
||||
/// ````
|
||||
BangData call({
|
||||
String? websiteName,
|
||||
String? domain,
|
||||
String? trigger,
|
||||
String? urlTemplate,
|
||||
String? category,
|
||||
String? subCategory,
|
||||
Set<BangFormat>? format,
|
||||
int? frequency,
|
||||
DateTime? lastUsed,
|
||||
Uint8List? iconData,
|
||||
});
|
||||
}
|
||||
|
||||
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfBangData.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfBangData.copyWith.fieldName(...)`
|
||||
class _$BangDataCWProxyImpl implements _$BangDataCWProxy {
|
||||
const _$BangDataCWProxyImpl(this._value);
|
||||
|
||||
final BangData _value;
|
||||
|
||||
@override
|
||||
BangData websiteName(String websiteName) => this(websiteName: websiteName);
|
||||
|
||||
@override
|
||||
BangData domain(String domain) => this(domain: domain);
|
||||
|
||||
@override
|
||||
BangData trigger(String trigger) => this(trigger: trigger);
|
||||
|
||||
@override
|
||||
BangData urlTemplate(String urlTemplate) => this(urlTemplate: urlTemplate);
|
||||
|
||||
@override
|
||||
BangData category(String? category) => this(category: category);
|
||||
|
||||
@override
|
||||
BangData subCategory(String? subCategory) => this(subCategory: subCategory);
|
||||
|
||||
@override
|
||||
BangData format(Set<BangFormat>? format) => this(format: format);
|
||||
|
||||
@override
|
||||
BangData frequency(int? frequency) => this(frequency: frequency);
|
||||
|
||||
@override
|
||||
BangData lastUsed(DateTime? lastUsed) => this(lastUsed: lastUsed);
|
||||
|
||||
@override
|
||||
BangData iconData(Uint8List? iconData) => this(iconData: iconData);
|
||||
|
||||
@override
|
||||
|
||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `BangData(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||
///
|
||||
/// Usage
|
||||
/// ```dart
|
||||
/// BangData(...).copyWith(id: 12, name: "My name")
|
||||
/// ````
|
||||
BangData call({
|
||||
Object? websiteName = const $CopyWithPlaceholder(),
|
||||
Object? domain = const $CopyWithPlaceholder(),
|
||||
Object? trigger = const $CopyWithPlaceholder(),
|
||||
Object? urlTemplate = const $CopyWithPlaceholder(),
|
||||
Object? category = const $CopyWithPlaceholder(),
|
||||
Object? subCategory = const $CopyWithPlaceholder(),
|
||||
Object? format = const $CopyWithPlaceholder(),
|
||||
Object? frequency = const $CopyWithPlaceholder(),
|
||||
Object? lastUsed = const $CopyWithPlaceholder(),
|
||||
Object? iconData = const $CopyWithPlaceholder(),
|
||||
}) {
|
||||
return BangData(
|
||||
websiteName:
|
||||
websiteName == const $CopyWithPlaceholder() || websiteName == null
|
||||
? _value.websiteName
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: websiteName as String,
|
||||
domain: domain == const $CopyWithPlaceholder() || domain == null
|
||||
? _value.domain
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: domain as String,
|
||||
trigger: trigger == const $CopyWithPlaceholder() || trigger == null
|
||||
? _value.trigger
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: trigger as String,
|
||||
urlTemplate:
|
||||
urlTemplate == const $CopyWithPlaceholder() || urlTemplate == null
|
||||
? _value.urlTemplate
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: urlTemplate as String,
|
||||
category: category == const $CopyWithPlaceholder()
|
||||
? _value.category
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: category as String?,
|
||||
subCategory: subCategory == const $CopyWithPlaceholder()
|
||||
? _value.subCategory
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: subCategory as String?,
|
||||
format: format == const $CopyWithPlaceholder()
|
||||
? _value.format
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: format as Set<BangFormat>?,
|
||||
frequency: frequency == const $CopyWithPlaceholder()
|
||||
? _value.frequency
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: frequency as int?,
|
||||
lastUsed: lastUsed == const $CopyWithPlaceholder()
|
||||
? _value.lastUsed
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: lastUsed as DateTime?,
|
||||
iconData: iconData == const $CopyWithPlaceholder()
|
||||
? _value.iconData
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: iconData as Uint8List?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension $BangDataCopyWith on BangData {
|
||||
/// Returns a callable class that can be used as follows: `instanceOfBangData.copyWith(...)` or like so:`instanceOfBangData.copyWith.fieldName(...)`.
|
||||
// ignore: library_private_types_in_public_api
|
||||
_$BangDataCWProxy get copyWith => _$BangDataCWProxyImpl(this);
|
||||
}
|
||||
Reference in New Issue
Block a user