1
0
mirror of https://github.com/flutter/samples.git synced 2026-04-06 19:52:04 +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

@@ -8,9 +8,12 @@ import 'package:linting_tool/model/rule.dart';
class APIProvider {
final _baseURL = 'https://dart-lang.github.io/linter';
final http.Client httpClient;
APIProvider(this.httpClient);
Future<List<Rule>> getRulesList() async {
http.Response response =
await http.get(Uri.parse('$_baseURL//lints/machine/rules.json'));
await httpClient.get(Uri.parse('$_baseURL//lints/machine/rules.json'));
if (response.statusCode == 200) {
List<Rule> rulesList = [];

View File

@@ -0,0 +1,77 @@
// 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:hive/hive.dart';
class HiveService {
static Future<bool> addBox<T>(T item, String boxName) async {
final openBox = await Hive.openLazyBox<T>(
boxName,
);
List<T> existingProducts = await getBoxes(boxName);
if (!existingProducts.contains(item)) {
openBox.add(item);
return true;
}
return false;
}
static Future addBoxes<T>(List<T> items, String boxName) async {
final openBox = await Hive.openLazyBox<T>(
boxName,
);
List<T> existingProducts = await getBoxes(boxName);
for (var item in items) {
if (!existingProducts.contains(item)) {
openBox.add(item);
}
}
}
static Future deleteBox<T>(T item, String boxName) async {
final openBox = await Hive.openLazyBox<T>(
boxName,
);
List<T> boxes = await getBoxes(boxName);
for (var box in boxes) {
if (box == item) {
openBox.deleteAt(boxes.indexOf(item));
}
}
}
static Future updateBox<T>(T item, T newItem, String boxName) async {
final openBox = await Hive.openLazyBox<T>(
boxName,
);
List<T> boxes = await getBoxes(boxName);
for (var box in boxes) {
if (box == item) {
openBox.putAt(boxes.indexOf(item), newItem);
}
}
}
static Future<List<T>> getBoxes<T>(String boxName, [String? query]) async {
List<T> boxList = [];
final openBox = await Hive.openLazyBox<T>(boxName);
int length = openBox.length;
for (int i = 0; i < length; i++) {
boxList.add(await openBox.getAt(i) as T);
}
return boxList;
}
}

View File

@@ -4,9 +4,14 @@
import 'package:linting_tool/model/rule.dart';
import 'package:linting_tool/repository/api_provider.dart';
import 'package:http/http.dart' as http;
class Repository {
final _apiProvider = APIProvider();
late final APIProvider _apiProvider;
Repository(http.Client httpClient) {
_apiProvider = APIProvider(httpClient);
}
Future<List<Rule>> getRulesList() => _apiProvider.getRulesList();
}