1
0
mirror of https://github.com/flutter/samples.git synced 2026-04-04 18:51:05 +00:00

[linting_tool] Implement saving rules to DB (#860)

This commit is contained in:
Abdullah Deshmukh
2021-08-05 03:48:22 +05:30
committed by GitHub
parent 1818925286
commit bbb8e342f1
22 changed files with 1045 additions and 24 deletions

View File

@@ -0,0 +1,26 @@
// Copyright 2021 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:equatable/equatable.dart';
import 'package:hive/hive.dart';
import 'package:linting_tool/model/rule.dart';
part 'profile.g.dart';
@HiveType(typeId: 1)
class RulesProfile extends Equatable {
@HiveField(0)
final String name;
@HiveField(1)
final List<Rule> rules;
const RulesProfile({
required this.name,
required this.rules,
});
@override
List<Object?> get props => [name];
}

View File

@@ -0,0 +1,44 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'profile.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class RulesProfileAdapter extends TypeAdapter<RulesProfile> {
@override
final int typeId = 1;
@override
RulesProfile read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return RulesProfile(
name: fields[0] as String,
rules: (fields[1] as List).cast<Rule>(),
);
}
@override
void write(BinaryWriter writer, RulesProfile obj) {
writer
..writeByte(2)
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.rules);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is RulesProfileAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

View File

@@ -0,0 +1,69 @@
// Copyright 2021 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:linting_tool/model/profile.dart';
import 'package:linting_tool/model/rule.dart';
import 'package:linting_tool/repository/hive_service.dart';
const _boxName = 'rules_profile';
class ProfilesStore extends ChangeNotifier {
ProfilesStore() {
fetchSavedProfiles();
}
bool _isLoading = true;
bool get isLoading => _isLoading;
List<RulesProfile> _savedProfiles = [];
List<RulesProfile> get savedProfiles => _savedProfiles;
String? _error;
String? get error => _error;
Future<void> fetchSavedProfiles() async {
if (!_isLoading) _isLoading = true;
notifyListeners();
try {
var profiles = await HiveService.getBoxes<RulesProfile>(_boxName);
_savedProfiles = profiles;
} on Exception catch (e) {
log(e.toString());
}
_isLoading = false;
notifyListeners();
}
Future<void> addToNewProfile(RulesProfile profile) async {
await HiveService.addBox<RulesProfile>(profile, _boxName);
await Future.delayed(const Duration(milliseconds: 100), () async {
await fetchSavedProfiles();
});
}
Future<void> addToExistingProfile(RulesProfile profile, Rule rule) async {
RulesProfile newProfile =
RulesProfile(name: profile.name, rules: profile.rules..add(rule));
await HiveService.updateBox<RulesProfile>(profile, newProfile, _boxName);
await Future.delayed(const Duration(milliseconds: 100), () async {
await fetchSavedProfiles();
});
}
Future<void> deleteProfile(RulesProfile profile) async {
await HiveService.deleteBox<RulesProfile>(profile, _boxName);
await Future.delayed(const Duration(milliseconds: 100), () async {
await fetchSavedProfiles();
});
}
}

View File

@@ -3,18 +3,27 @@
// found in the LICENSE file.
import 'package:equatable/equatable.dart';
import 'package:hive/hive.dart';
import 'package:json_annotation/json_annotation.dart';
part 'rule.g.dart';
@JsonSerializable()
@HiveType(typeId: 0)
class Rule extends Equatable {
@HiveField(0)
final String name;
@HiveField(1)
final String description;
@HiveField(2)
final String group;
@HiveField(3)
final String maturity;
@HiveField(4)
final List<String> incompatible;
@HiveField(5)
final List<String> sets;
@HiveField(6)
final String details;
const Rule({

View File

@@ -2,6 +2,62 @@
part of 'rule.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class RuleAdapter extends TypeAdapter<Rule> {
@override
final int typeId = 0;
@override
Rule read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Rule(
name: fields[0] as String,
description: fields[1] as String,
group: fields[2] as String,
maturity: fields[3] as String,
incompatible: (fields[4] as List).cast<String>(),
sets: (fields[5] as List).cast<String>(),
details: fields[6] as String,
);
}
@override
void write(BinaryWriter writer, Rule obj) {
writer
..writeByte(7)
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.description)
..writeByte(2)
..write(obj.group)
..writeByte(3)
..write(obj.maturity)
..writeByte(4)
..write(obj.incompatible)
..writeByte(5)
..write(obj.sets)
..writeByte(6)
..write(obj.details);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is RuleAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

View File

@@ -8,9 +8,13 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:linting_tool/model/rule.dart';
import 'package:linting_tool/repository/repository.dart';
import 'package:http/http.dart' as http;
class RuleStore extends ChangeNotifier {
RuleStore() {
late final Repository repository;
RuleStore(http.Client httpClient) {
repository = Repository(httpClient);
fetchRules();
}
bool _isLoading = true;
@@ -29,7 +33,7 @@ class RuleStore extends ChangeNotifier {
if (!_isLoading) _isLoading = true;
notifyListeners();
try {
var rules = await Repository().getRulesList();
var rules = await repository.getRulesList();
_rules = rules;
} on SocketException catch (e) {
log(e.toString());