mirror of https://github.com/flutter/samples.git
[linting_tool] Implement saving rules to DB (#860)
parent
1818925286
commit
bbb8e342f1
@ -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];
|
||||
}
|
@ -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;
|
||||
}
|
@ -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();
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
// 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:flutter/material.dart';
|
||||
import 'package:linting_tool/layout/adaptive.dart';
|
||||
import 'package:linting_tool/model/profile.dart';
|
||||
import 'package:linting_tool/widgets/saved_rule_tile.dart';
|
||||
|
||||
class RulesPage extends StatelessWidget {
|
||||
final RulesProfile profile;
|
||||
|
||||
const RulesPage({
|
||||
required this.profile,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDesktop = isDisplayLarge(context);
|
||||
final isTablet = isDisplayMedium(context);
|
||||
var textTheme = Theme.of(context).textTheme;
|
||||
final startPadding = isTablet
|
||||
? 60.0
|
||||
: isDesktop
|
||||
? 120.0
|
||||
: 4.0;
|
||||
final endPadding = isTablet
|
||||
? 60.0
|
||||
: isDesktop
|
||||
? 120.0
|
||||
: 4.0;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
profile.name,
|
||||
style: textTheme.subtitle2!.copyWith(
|
||||
color: textTheme.bodyText1!.color,
|
||||
),
|
||||
),
|
||||
leading: Padding(
|
||||
padding: const EdgeInsets.only(left: 80.0),
|
||||
child: TextButton.icon(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
icon: const Icon(Icons.arrow_back_ios_new),
|
||||
label: const Text('Back'),
|
||||
),
|
||||
),
|
||||
leadingWidth: 160.0,
|
||||
toolbarHeight: 38.0,
|
||||
backgroundColor: Colors.white,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
body: ListView.separated(
|
||||
padding: EdgeInsetsDirectional.only(
|
||||
start: startPadding,
|
||||
end: endPadding,
|
||||
top: isDesktop ? 28 : 0,
|
||||
bottom: isDesktop ? kToolbarHeight : 0,
|
||||
),
|
||||
itemCount: profile.rules.length,
|
||||
cacheExtent: 5,
|
||||
itemBuilder: (context, index) {
|
||||
return SavedRuleTile(
|
||||
rule: profile.rules[index],
|
||||
);
|
||||
},
|
||||
separatorBuilder: (context, index) => const SizedBox(height: 4),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
// 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:flutter/material.dart';
|
||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||
import 'package:linting_tool/model/rule.dart';
|
||||
import 'package:linting_tool/theme/app_theme.dart';
|
||||
import 'package:linting_tool/theme/colors.dart';
|
||||
|
||||
class SavedRuleTile extends StatefulWidget {
|
||||
final Rule rule;
|
||||
const SavedRuleTile({
|
||||
required this.rule,
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SavedRuleTileState createState() => _SavedRuleTileState();
|
||||
}
|
||||
|
||||
class _SavedRuleTileState extends State<SavedRuleTile> {
|
||||
var isExpanded = false;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var theme = Theme.of(context);
|
||||
var textTheme = theme.textTheme;
|
||||
final rule = widget.rule;
|
||||
final incompatibleString =
|
||||
rule.incompatible.isNotEmpty ? rule.incompatible.join(', ') : 'none';
|
||||
final setsString = rule.sets.isNotEmpty ? rule.sets.join(', ') : 'none';
|
||||
|
||||
// TODO(abd99): Add option to remove rule from profile.
|
||||
// TODO(abd99): Add right click functionality.
|
||||
return ExpansionTile(
|
||||
collapsedBackgroundColor: AppColors.white50,
|
||||
title: Text(
|
||||
rule.name,
|
||||
style: textTheme.subtitle1!.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
rule.description,
|
||||
style: textTheme.caption!,
|
||||
),
|
||||
initiallyExpanded: isExpanded,
|
||||
onExpansionChanged: (value) {
|
||||
setState(() {
|
||||
isExpanded = value;
|
||||
});
|
||||
},
|
||||
expandedAlignment: Alignment.centerLeft,
|
||||
childrenPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16.0,
|
||||
vertical: 8.0,
|
||||
),
|
||||
backgroundColor: AppColors.white50,
|
||||
maintainState: true,
|
||||
expandedCrossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Group:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' ${rule.group}',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Maturity:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' ${rule.maturity}',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Incompatible:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' $incompatibleString',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: 'Sets:',
|
||||
style: textTheme.subtitle2,
|
||||
),
|
||||
TextSpan(
|
||||
text: ' $setsString',
|
||||
),
|
||||
],
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
const SizedBox(
|
||||
height: 16.0,
|
||||
),
|
||||
MarkdownBody(
|
||||
data: rule.details,
|
||||
selectable: true,
|
||||
styleSheet: AppTheme.buildMarkDownTheme(theme),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 16.0,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
// Mocks generated by Mockito 5.0.13 from annotations
|
||||
// in linting_tool/test/widget_test.dart.
|
||||
// Do not manually edit this file.
|
||||
|
||||
import 'dart:async' as _i5;
|
||||
import 'dart:convert' as _i6;
|
||||
import 'dart:typed_data' as _i7;
|
||||
|
||||
import 'package:http/src/base_request.dart' as _i8;
|
||||
import 'package:http/src/client.dart' as _i4;
|
||||
import 'package:http/src/response.dart' as _i2;
|
||||
import 'package:http/src/streamed_response.dart' as _i3;
|
||||
import 'package:mockito/mockito.dart' as _i1;
|
||||
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
// ignore_for_file: avoid_setters_without_getters
|
||||
// ignore_for_file: comment_references
|
||||
// ignore_for_file: implementation_imports
|
||||
// ignore_for_file: invalid_use_of_visible_for_testing_member
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
// ignore_for_file: unnecessary_parenthesis
|
||||
|
||||
class _FakeResponse extends _i1.Fake implements _i2.Response {}
|
||||
|
||||
class _FakeStreamedResponse extends _i1.Fake implements _i3.StreamedResponse {}
|
||||
|
||||
/// A class which mocks [Client].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockClient extends _i1.Mock implements _i4.Client {
|
||||
MockClient() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i5.Future<_i2.Response> head(Uri? url, {Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(Invocation.method(#head, [url], {#headers: headers}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> get(Uri? url, {Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(Invocation.method(#get, [url], {#headers: headers}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> post(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#post, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> put(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#put, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> patch(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#patch, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> delete(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#delete, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<String> read(Uri? url, {Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(Invocation.method(#read, [url], {#headers: headers}),
|
||||
returnValue: Future<String>.value('')) as _i5.Future<String>);
|
||||
@override
|
||||
_i5.Future<_i7.Uint8List> readBytes(Uri? url,
|
||||
{Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#readBytes, [url], {#headers: headers}),
|
||||
returnValue: Future<_i7.Uint8List>.value(_i7.Uint8List(0)))
|
||||
as _i5.Future<_i7.Uint8List>);
|
||||
@override
|
||||
_i5.Future<_i3.StreamedResponse> send(_i8.BaseRequest? request) =>
|
||||
(super.noSuchMethod(Invocation.method(#send, [request]),
|
||||
returnValue:
|
||||
Future<_i3.StreamedResponse>.value(_FakeStreamedResponse()))
|
||||
as _i5.Future<_i3.StreamedResponse>);
|
||||
@override
|
||||
void close() => super.noSuchMethod(Invocation.method(#close, []),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
String toString() => super.toString();
|
||||
}
|
Loading…
Reference in new issue