Skip to content

Commit

Permalink
✨ improve structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
iota9star committed May 30, 2021
1 parent 9f8c6cd commit 3342527
Show file tree
Hide file tree
Showing 25 changed files with 721 additions and 584 deletions.
77 changes: 46 additions & 31 deletions lib/internal/delegate.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:extended_list_library/extended_list_library.dart'
show ViewportBuilder;
import 'package:flutter/material.dart' hide ViewportBuilder;
import 'package:flutter/rendering.dart';
import 'package:waterfall_flow/waterfall_flow.dart';

class SliverGridDelegateWithMinCrossAxisExtent extends SliverGridDelegate {
/// Creates a delegate that makes grid layouts with tiles that have a maximum
/// cross-axis extent.
///
/// All of the arguments except [mainAxisExtent] must not be null.
/// The [minCrossAxisExtent], [mainAxisExtent], [mainAxisSpacing],
/// and [crossAxisSpacing] arguments must not be negative.
/// The [childAspectRatio] argument must be greater than zero.
const SliverGridDelegateWithMinCrossAxisExtent({
required this.minCrossAxisExtent,
this.mainAxisSpacing = 0.0,
Expand All @@ -21,33 +17,10 @@ class SliverGridDelegateWithMinCrossAxisExtent extends SliverGridDelegate {
assert(mainAxisSpacing >= 0),
assert(crossAxisSpacing >= 0),
assert(childAspectRatio > 0);

/// The maximum extent of tiles in the cross axis.
///
/// This delegate will select a cross-axis extent for the tiles that is as
/// large as possible subject to the following conditions:
///
/// - The extent evenly divides the cross-axis extent of the grid.
/// - The extent is at least [minCrossAxisExtent].
///
/// For example, if the grid is vertical, the grid is 600.0 pixels wide, and
/// [minCrossAxisExtent] is 190.0, this delegate will create a grid with 3
/// columns that are 200.0 pixels wide.
final double minCrossAxisExtent;

/// The number of logical pixels between each child along the main axis.
final double mainAxisSpacing;

/// The number of logical pixels between each child along the cross axis.
final double crossAxisSpacing;

/// The ratio of the cross-axis to the main-axis extent of each child.
final double childAspectRatio;

/// The extent of each tile in the main axis. If provided it would define the
/// logical pixels taken by each tile in the main-axis.
///
/// If null, [childAspectRatio] is used instead.
final double? mainAxisExtent;

bool _debugAssertIsValid(double crossAxisExtent) {
Expand Down Expand Up @@ -92,3 +65,45 @@ class SliverGridDelegateWithMinCrossAxisExtent extends SliverGridDelegate {
oldDelegate.mainAxisExtent != mainAxisExtent;
}
}

class SliverWaterfallFlowDelegateWithMinCrossAxisExtent
extends SliverWaterfallFlowDelegate {
const SliverWaterfallFlowDelegateWithMinCrossAxisExtent({
required this.minCrossAxisExtent,
double mainAxisSpacing = 0.0,
double crossAxisSpacing = 0.0,
LastChildLayoutTypeBuilder? lastChildLayoutTypeBuilder,
CollectGarbage? collectGarbage,
ViewportBuilder? viewportBuilder,
bool closeToTrailing = false,
}) : assert(minCrossAxisExtent >= 0),
super(
mainAxisSpacing: mainAxisSpacing,
crossAxisSpacing: crossAxisSpacing,
lastChildLayoutTypeBuilder: lastChildLayoutTypeBuilder,
collectGarbage: collectGarbage,
viewportBuilder: viewportBuilder,
closeToTrailing: closeToTrailing,
);

final double minCrossAxisExtent;

@override
int getCrossAxisCount(SliverConstraints constraints) {
final count =
(constraints.crossAxisExtent / (minCrossAxisExtent + crossAxisSpacing))
.floor();
return count == 0 ? 1 : count;
}

@override
bool shouldRelayout(SliverWaterfallFlowDelegate oldDelegate) {
if (oldDelegate.runtimeType != runtimeType) {
return true;
}

return oldDelegate is SliverWaterfallFlowDelegateWithMaxCrossAxisExtent &&
(oldDelegate.maxCrossAxisExtent != minCrossAxisExtent ||
super.shouldRelayout(oldDelegate));
}
}
6 changes: 3 additions & 3 deletions lib/internal/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class Resolver {
if (_intro.isNotBlank) {
_intro = "\u3000\u3000" + _intro!.replaceAll("\n", "\n\u3000\u3000");
}
detail.intro = _intro;
detail.intro = _intro ?? "";
detail.subscribed = document
.querySelector(".subscribed-badge")
?.attributes["style"]
Expand All @@ -551,7 +551,7 @@ class Resolver {
final List<Element> tables = document
.querySelectorAll("#sk-container > div.central-container > table");
final List<Element> subs = document.querySelectorAll(".subgroup-text");
detail.subgroupBangumis = [];
detail.subgroupBangumis = {};
SubgroupBangumi subgroupBangumi;
Element? element;
List<Element> elements;
Expand Down Expand Up @@ -641,7 +641,7 @@ class Resolver {
records.add(record);
}
subgroupBangumi.records = records;
detail.subgroupBangumis.add(subgroupBangumi);
detail.subgroupBangumis[subgroupBangumi.dataId] = subgroupBangumi;
}
}
return detail;
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class MikanApp extends StatelessWidget {
],
child: Consumer<ThemeModel>(
builder: (context, themeModel, child) {
final FirebaseModel firebaseModel = Provider.of<FirebaseModel>(
final firebaseModel = Provider.of<FirebaseModel>(
context,
listen: false,
);
Expand Down
4 changes: 2 additions & 2 deletions lib/model/bangumi_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class BangumiDetail {
late String name;
late bool subscribed;
late Map<String, String> more;
String? intro;
late List<SubgroupBangumi> subgroupBangumis;
late String intro;
late Map<String, SubgroupBangumi> subgroupBangumis;

@override
bool operator ==(Object other) =>
Expand Down
31 changes: 7 additions & 24 deletions lib/providers/bangumi_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:mikan_flutter/internal/extension.dart';
import 'package:mikan_flutter/internal/http.dart';
import 'package:mikan_flutter/internal/repo.dart';
import 'package:mikan_flutter/model/bangumi_details.dart';
import 'package:mikan_flutter/model/subgroup_bangumi.dart';
import 'package:mikan_flutter/providers/base_model.dart';
import 'package:palette_generator/palette_generator.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
Expand Down Expand Up @@ -46,23 +45,6 @@ class BangumiModel extends CancelableBaseModel {
.whenComplete(() => this._loadCoverMainColor());
}

SubgroupBangumi? _subgroupBangumi;

SubgroupBangumi? get subgroupBangumi => _subgroupBangumi;

set selectedSubgroupId(String value) {
if (value == _subgroupBangumi?.dataId) {
return;
}
_subgroupBangumi = _bangumiDetail?.subgroupBangumis
.firstWhere((element) => element.dataId == value);
_hasScrolled = false;
if (_refreshController.headerStatus != RefreshStatus.completed) {
_refreshController.loadComplete();
}
notifyListeners();
}

Color? _coverMainColor;

Color? get coverMainColor => _coverMainColor;
Expand All @@ -85,23 +67,24 @@ class BangumiModel extends CancelableBaseModel {
});
}

loadSubgroupList() async {
if ((this._subgroupBangumi?.records.length ?? 0) < 10) {
loadSubgroupList(final String dataId) async {
final sb = _bangumiDetail?.subgroupBangumis[dataId];
if ((sb?.records.length ?? 0) < 10) {
return _refreshController.loadNoData();
}
final Resp resp = await (this +
Repo.bangumiMore(
this.id,
this._subgroupBangumi?.dataId ?? "",
this._subgroupBangumi?.records.length ?? 0 + 20,
sb?.dataId ?? "",
sb?.records.length ?? 0 + 20,
));
if (resp.success) {
if (this._subgroupBangumi?.records.length == resp.data.length) {
if (sb?.records.length == resp.data.length) {
_refreshController.loadNoData();
} else {
_refreshController.loadComplete();
}
this._subgroupBangumi?.records = resp.data;
sb?.records = resp.data;
notifyListeners();
} else {
_refreshController.loadFailed();
Expand Down
18 changes: 16 additions & 2 deletions lib/topvars.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ const edgeH16T4 = const EdgeInsets.only(left: 16.0, right: 16.0, top: 4.0);
const edgeH16T8 = const EdgeInsets.only(left: 16.0, right: 16.0, top: 8.0);
const edgeHB16T8 =
const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16.0, top: 8.0);
const edgeHB24 = const EdgeInsets.only(left: 24.0, right: 24.0, bottom: 24.0);
const edgeH16T24B8 =
const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 8.0, top: 24.0);
const edge16 = const EdgeInsets.all(16.0);
const edge8 = const EdgeInsets.all(8.0);
const edge10 = const EdgeInsets.all(10.0);
const edge12 = const EdgeInsets.all(12.0);
const edge4 = const EdgeInsets.all(4.0);
const edgeV4 = const EdgeInsets.symmetric(vertical: 4.0);
const edgeV8 = const EdgeInsets.symmetric(vertical: 8.0);
const edge24 = const EdgeInsets.all(24.0);
const edge28 = const EdgeInsets.all(28.0);
const edgeH12V8 = const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0);
const edgeH16 = const EdgeInsets.symmetric(horizontal: 16.0);
const edgeH24 = const EdgeInsets.symmetric(horizontal: 24.0);
const edgeH8 = const EdgeInsets.symmetric(horizontal: 8.0);
const edgeH16V12 = const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0);
const edgeH8V6 = const EdgeInsets.symmetric(horizontal: 8.0, vertical: 6.0);
Expand Down Expand Up @@ -132,7 +136,10 @@ const textStyle13 = const TextStyle(
fontSize: 13.0,
height: 1.25,
);

const textStyle12 = const TextStyle(
fontSize: 12.0,
height: 1.25,
);
const textStyle13B500 = const TextStyle(
fontSize: 13.0,
height: 1.25,
Expand Down Expand Up @@ -163,6 +170,7 @@ const sizedBoxW16 = const SizedBox(width: 16.0);
const sizedBoxH16 = const SizedBox(height: 16.0);
const sizedBoxW12 = const SizedBox(width: 12.0);
const sizedBoxH12 = const SizedBox(height: 12.0);
const sizedBoxH24 = const SizedBox(height: 24.0);
const sizedBoxH10 = const SizedBox(height: 10.0);
const sizedBoxH8 = const SizedBox(height: 8.0);
const sizedBoxW8 = const SizedBox(width: 8.0);
Expand All @@ -171,12 +179,16 @@ const sizedBoxH4 = const SizedBox(height: 4.0);
const sizedBoxH56 = const SizedBox(height: 56.0);
const sizedBoxH42 = const SizedBox(height: 42.0);

const sliverSizedBoxH64 = const SliverToBoxAdapter(
child: const SizedBox(height: 64.0),
);

const spacer = const Spacer();

const dur240 = const Duration(milliseconds: 240);
const dur3000 = const Duration(milliseconds: 3000);

const sliverToBoxAdapter = const SliverToBoxAdapter();
const emptySliverToBoxAdapter = const SliverToBoxAdapter();

const circleShape = const CircleBorder();

Expand Down Expand Up @@ -207,3 +219,5 @@ final normalFormHeader = Row(
)
],
);

const centerLoading = const Center(child: CupertinoActivityIndicator());
Loading

0 comments on commit 3342527

Please sign in to comment.