mirror of
https://github.com/flutter/samples.git
synced 2026-04-06 11:41:26 +00:00
[linting_tool] Implement saving rules to DB (#860)
This commit is contained in:
committed by
GitHub
parent
1818925286
commit
bbb8e342f1
@@ -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 = [];
|
||||
|
||||
77
experimental/linting_tool/lib/repository/hive_service.dart
Normal file
77
experimental/linting_tool/lib/repository/hive_service.dart
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user