mirror of
https://github.com/flutter/samples.git
synced 2026-04-04 18:51:05 +00:00
Some linting_tool updates and cleanup (#1279)
This commit is contained in:
@@ -11,7 +11,7 @@ import 'package:linting_tool/model/rule.dart';
|
||||
class EditingController extends ChangeNotifier {
|
||||
bool _isEditing;
|
||||
|
||||
EditingController({bool? isEditing}) : _isEditing = isEditing ?? false;
|
||||
EditingController({bool isEditing = false}) : _isEditing = isEditing;
|
||||
|
||||
bool get isEditing => _isEditing;
|
||||
|
||||
@@ -21,9 +21,9 @@ class EditingController extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
final List<Rule> _selectedRules = [];
|
||||
final Set<Rule> _selectedRules = {};
|
||||
|
||||
List<Rule> get selectedRules => _selectedRules;
|
||||
Set<Rule> get selectedRules => _selectedRules;
|
||||
|
||||
void selectRule(Rule rule) {
|
||||
_selectedRules.add(rule);
|
||||
@@ -37,12 +37,10 @@ class EditingController extends ChangeNotifier {
|
||||
|
||||
Future deleteSelected(
|
||||
RulesProfile profile, ProfilesStore profilesStore) async {
|
||||
var rules = profile.rules;
|
||||
for (var rule in _selectedRules) {
|
||||
rules.remove(rule);
|
||||
}
|
||||
final rules = profile.rules;
|
||||
rules.removeWhere((rule) => _selectedRules.contains(rule));
|
||||
|
||||
RulesProfile newProfile = RulesProfile(name: profile.name, rules: rules);
|
||||
final newProfile = RulesProfile(name: profile.name, rules: rules);
|
||||
|
||||
await profilesStore.updateProfile(profile, newProfile);
|
||||
|
||||
|
||||
@@ -20,11 +20,12 @@ import 'package:yaml/yaml.dart';
|
||||
const _boxName = 'rules_profile';
|
||||
|
||||
class ProfilesStore extends ChangeNotifier {
|
||||
late final Repository repository;
|
||||
ProfilesStore(http.Client httpClient) {
|
||||
repository = Repository(httpClient);
|
||||
final Repository repository;
|
||||
|
||||
ProfilesStore(http.Client httpClient) : repository = Repository(httpClient) {
|
||||
fetchSavedProfiles();
|
||||
}
|
||||
|
||||
bool _isLoading = true;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
@@ -41,8 +42,7 @@ class ProfilesStore extends ChangeNotifier {
|
||||
if (!_isLoading) _isLoading = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
var profiles = await HiveService.getBoxes<RulesProfile>(_boxName);
|
||||
_savedProfiles = profiles;
|
||||
_savedProfiles = await HiveService.getBoxes<RulesProfile>(_boxName);
|
||||
} on Exception catch (e) {
|
||||
log(e.toString());
|
||||
}
|
||||
@@ -64,12 +64,16 @@ class ProfilesStore extends ChangeNotifier {
|
||||
// TODO(abd99): Consider refactoring to LinkedHashSet/SplayTreeSet to avoid
|
||||
// duplication automatically.
|
||||
// ref: https://github.com/flutter/samples/pull/870#discussion_r685666792
|
||||
var rules = profile.rules;
|
||||
if (!rules.contains(rule)) {
|
||||
rules.add(rule);
|
||||
final rules = profile.rules;
|
||||
|
||||
// If rule is already in profile, skip updating profile
|
||||
if (rules.contains(rule)) {
|
||||
return;
|
||||
}
|
||||
|
||||
RulesProfile newProfile = RulesProfile(name: profile.name, rules: rules);
|
||||
rules.add(rule);
|
||||
|
||||
final newProfile = RulesProfile(name: profile.name, rules: rules);
|
||||
|
||||
await HiveService.updateBox<RulesProfile>(profile, newProfile, _boxName);
|
||||
|
||||
@@ -88,7 +92,7 @@ class ProfilesStore extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> removeRuleFromProfile(RulesProfile profile, Rule rule) async {
|
||||
var newProfile =
|
||||
final newProfile =
|
||||
RulesProfile(name: profile.name, rules: profile.rules..remove(rule));
|
||||
await updateProfile(profile, newProfile);
|
||||
}
|
||||
@@ -111,10 +115,10 @@ class ProfilesStore extends ChangeNotifier {
|
||||
var resultSaved = false;
|
||||
|
||||
try {
|
||||
var templateFileData = await repository.getTemplateFile();
|
||||
final templateFileData = await repository.getTemplateFile();
|
||||
|
||||
/// Fetch formatted data to create new YamlFile.
|
||||
String newYamlFile =
|
||||
final newYamlFile =
|
||||
_prepareYamlFile(profile, templateFileData, rulesStyle);
|
||||
|
||||
resultSaved = await _saveFileToDisk(newYamlFile);
|
||||
@@ -136,7 +140,7 @@ class ProfilesStore extends ChangeNotifier {
|
||||
const name = 'analysis_options.yaml';
|
||||
|
||||
/// Get file path using file picker.
|
||||
var savePath = await file_selector.getSavePath(
|
||||
final savePath = await file_selector.getSavePath(
|
||||
suggestedName: name,
|
||||
);
|
||||
|
||||
@@ -149,25 +153,21 @@ class ProfilesStore extends ChangeNotifier {
|
||||
return true;
|
||||
}
|
||||
|
||||
var errorMessage = 'File path not found.';
|
||||
const errorMessage = 'File path not found.';
|
||||
_error = errorMessage;
|
||||
throw Exception(errorMessage);
|
||||
}
|
||||
|
||||
String _prepareYamlFile(
|
||||
RulesProfile profile, YamlMap templateFile, RulesStyle rulesStyle) {
|
||||
var rules = profile.rules.map((e) => e.name).toList();
|
||||
final rules = profile.rules.map((e) => e.name);
|
||||
|
||||
var rulesData =
|
||||
final rulesData =
|
||||
json.decode(json.encode(templateFile)) as Map<String, dynamic>;
|
||||
|
||||
/// Add rules to existing template according to formatting style.
|
||||
if (rulesStyle == RulesStyle.booleanMap) {
|
||||
var rulesMap = Map.fromEntries(
|
||||
rules.map(
|
||||
(e) => MapEntry(e, true),
|
||||
),
|
||||
);
|
||||
final rulesMap = <String, bool>{for (final rule in rules) rule: true};
|
||||
rulesData.update('linter', (dynamic value) => {'rules': rulesMap});
|
||||
} else {
|
||||
rulesData.update('linter', (dynamic value) => {'rules': rules});
|
||||
|
||||
@@ -41,5 +41,5 @@ class Rule extends Equatable {
|
||||
Map<String, dynamic> toJson() => _$RuleToJson(this);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [name];
|
||||
List<Object> get props => [name];
|
||||
}
|
||||
|
||||
@@ -62,19 +62,17 @@ class RuleAdapter extends TypeAdapter<Rule> {
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Rule _$RuleFromJson(Map<String, dynamic> json) {
|
||||
return Rule(
|
||||
name: json['name'] as String,
|
||||
description: json['description'] as String,
|
||||
group: json['group'] as String,
|
||||
maturity: json['maturity'] as String,
|
||||
incompatible: (json['incompatible'] as List<dynamic>)
|
||||
.map((e) => e as String)
|
||||
.toList(),
|
||||
sets: (json['sets'] as List<dynamic>).map((e) => e as String).toList(),
|
||||
details: json['details'] as String,
|
||||
);
|
||||
}
|
||||
Rule _$RuleFromJson(Map<String, dynamic> json) => Rule(
|
||||
name: json['name'] as String,
|
||||
description: json['description'] as String,
|
||||
group: json['group'] as String,
|
||||
maturity: json['maturity'] as String,
|
||||
incompatible: (json['incompatible'] as List<dynamic>)
|
||||
.map((e) => e as String)
|
||||
.toList(),
|
||||
sets: (json['sets'] as List<dynamic>).map((e) => e as String).toList(),
|
||||
details: json['details'] as String,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$RuleToJson(Rule instance) => <String, dynamic>{
|
||||
'name': instance.name,
|
||||
|
||||
@@ -13,12 +13,12 @@ import 'package:linting_tool/repository/repository.dart';
|
||||
|
||||
/// Manages fetching rules from the web.
|
||||
class RuleStore extends ChangeNotifier {
|
||||
late final Repository repository;
|
||||
final Repository repository;
|
||||
|
||||
RuleStore(http.Client httpClient) {
|
||||
repository = Repository(httpClient);
|
||||
RuleStore(http.Client httpClient) : repository = Repository(httpClient) {
|
||||
fetchRules();
|
||||
}
|
||||
|
||||
bool _isLoading = true;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
@@ -32,32 +32,31 @@ class RuleStore extends ChangeNotifier {
|
||||
String? get error => _error;
|
||||
|
||||
List<RulesProfile> get defaultProfiles {
|
||||
List<RulesProfile> defaultProfiles = [];
|
||||
if (isLoading || rules.isEmpty) {
|
||||
return const [];
|
||||
}
|
||||
|
||||
var rulesWithDefaultSets =
|
||||
rules.where((rule) => rule.sets.isNotEmpty).toList();
|
||||
final Map<String, RulesProfile> setsToProfiles = {};
|
||||
|
||||
for (final rule in rulesWithDefaultSets) {
|
||||
for (final rule in rules) {
|
||||
for (final setName in rule.sets) {
|
||||
var profileIndex =
|
||||
defaultProfiles.indexWhere((profile) => profile.name == setName);
|
||||
if (profileIndex >= 0) {
|
||||
defaultProfiles[profileIndex].rules.add(rule);
|
||||
final profile = setsToProfiles[setName];
|
||||
if (profile == null) {
|
||||
setsToProfiles[setName] = RulesProfile(name: setName, rules: [rule]);
|
||||
} else {
|
||||
defaultProfiles.add(RulesProfile(name: setName, rules: [rule]));
|
||||
profile.rules.add(rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return defaultProfiles;
|
||||
return setsToProfiles.values.toList(growable: false);
|
||||
}
|
||||
|
||||
Future<void> fetchRules() async {
|
||||
if (!_isLoading) _isLoading = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
var rules = await repository.getRulesList();
|
||||
_rules = rules;
|
||||
_rules = await repository.getRulesList();
|
||||
} on SocketException catch (e) {
|
||||
log(e.toString());
|
||||
_error = 'Check internet connection.';
|
||||
|
||||
Reference in New Issue
Block a user