Skip to content

Commit

Permalink
fix: 地図描画の改善
Browse files Browse the repository at this point in the history
  • Loading branch information
YumNumm committed May 27, 2023
1 parent cb664f3 commit 93ebda6
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 119 deletions.
102 changes: 38 additions & 64 deletions lib/common/component/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
/// タッチ操作をハンドルするWidget
/// mapViewModelProviderからMapStateを取得すること
class MapTouchHandlerWidget extends HookConsumerWidget {
const MapTouchHandlerWidget({required this.key});
const MapTouchHandlerWidget({super.key, required this.mapKey});

final GlobalKey key;
final Key mapKey;

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand All @@ -27,7 +27,7 @@ class MapTouchHandlerWidget extends HookConsumerWidget {
WidgetsBinding.instance.endOfFrame.then(
(_) => Future(
() {
ref.read(mapViewModelProvider(key).notifier)
ref.read(mapViewModelProvider(mapKey).notifier)
..registerWidgetSize(
context.size!,
)
Expand All @@ -49,66 +49,39 @@ class MapTouchHandlerWidget extends HookConsumerWidget {
[],
);

return Stack(
children: [
Listener(
onPointerSignal:
ref.read(mapViewModelProvider(key).notifier).recievedPointerSignal,
child: GestureDetector(
onScaleUpdate:
ref.read(mapViewModelProvider(key).notifier).handleScaleUpdate,
onScaleStart:
ref.read(mapViewModelProvider(key).notifier).handleScaleStart,
onScaleEnd: ref.read(mapViewModelProvider(key).notifier).handleScaleEnd,
),
),
// 初期値に戻す

Positioned(
bottom: 0,
right: 0,
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
FilledButton.tonalIcon(
onPressed: ref.read(mapViewModelProvider(key).notifier).reset,
icon: const Icon(Icons.refresh),
label: const Text('init'),
),
FilledButton.tonalIcon(
onPressed: () =>
ref.read(mapViewModelProvider(key).notifier).animatedMoveTo(
const LatLng(35, 135),
duration: const Duration(milliseconds: 250),
),
icon: const Icon(Icons.home),
label: const Text('Animated Move To 35,135'),
),
FilledButton.tonalIcon(
onPressed: () => ref
.read(mapViewModelProvider(key).notifier)
.animatedZoomTo(20),
icon: const Icon(Icons.home),
label: const Text('Animated Zoom To Lv.20'),
),
FilledButton.tonalIcon(
onPressed: () =>
ref.read(mapViewModelProvider(key).notifier).animatedBounds(
[
const LatLng(45.6, 145.1),
const LatLng(30, 128.8),
],
),
icon: const Icon(Icons.home),
label: const Text('Animated Fit Japan Map'),
),
],
),
),
),
],
return Listener(
onPointerSignal:
ref.read(mapViewModelProvider(mapKey).notifier).recievedPointerSignal,
child: GestureDetector(
onScaleUpdate:
ref.read(mapViewModelProvider(mapKey).notifier).handleScaleUpdate,
onScaleStart:
ref.read(mapViewModelProvider(mapKey).notifier).handleScaleStart,
onScaleEnd:
ref.read(mapViewModelProvider(mapKey).notifier).handleScaleEnd,
),
);
}
}

class BaseMapWidget extends ConsumerWidget {
const BaseMapWidget({super.key, required this.mapKey});
final Key mapKey;

@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(MapViewModelProvider(mapKey));
final mapData =
ref.watch(mapDataProvider.select((value) => value.projectedData));
if (mapData == null) {
throw Exception('mapData is null');
}
return CustomPaint(
painter: MapPainter(
state: state,
mapData: mapData,
),
size: Size.infinite,
);
}
}
Expand Down Expand Up @@ -244,5 +217,6 @@ class MapPainter extends CustomPainter {
}

@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
bool shouldRepaint(covariant MapPainter oldDelegate) =>
oldDelegate.state != state || oldDelegate.mapData != mapData;
}
15 changes: 0 additions & 15 deletions lib/common/component/map/view_model/map_viemwodel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:developer';
import 'dart:math' as math;

import 'package:eqmonitor/common/component/map/model/map_state.dart';
Expand All @@ -15,9 +14,6 @@ part 'map_viemwodel.g.dart';
class MapViewModel extends _$MapViewModel {
@override
MapState build(Key key) {
ref.listenSelf((previous, next) {
log(next.toString());
});
return const MapState(
offset: Offset.zero,
zoomLevel: 1,
Expand Down Expand Up @@ -123,7 +119,6 @@ class MapViewModel extends _$MapViewModel {
if (details.scale != 1.0) {
return;
}
debugPrint('handleScaleUpdate: ${details.focalPointDelta}');
state = state.move(
Offset(details.focalPointDelta.dx, details.focalPointDelta.dy) /
state.zoomLevel,
Expand Down Expand Up @@ -153,13 +148,10 @@ class MapViewModel extends _$MapViewModel {
_scaleController.reset();
_scaleStart = null;

log(_gestureType.toString(), name: 'GestureType');

if (_gestureType == _GestureType.pan) {
if (details.velocity.pixelsPerSecond.distance < kMinFlingVelocity) {
return;
}
// log('MOVE ANIMATION!');
final translation = state.offset;
final frictionSimulationX = FrictionSimulation(
_interactionEndFrictionCoefficient,
Expand Down Expand Up @@ -192,7 +184,6 @@ class MapViewModel extends _$MapViewModel {
if (details.scaleVelocity.abs() < 0.1) {
return;
}
// log('SCALE ANIMATION!');
final scale = state.zoomLevel;
final frictionSimulation = FrictionSimulation(
_interactionEndFrictionCoefficient * kDefaultMouseScrollToScaleFactor,
Expand Down Expand Up @@ -232,7 +223,6 @@ class MapViewModel extends _$MapViewModel {

void recievedPointerSignal(PointerSignalEvent event) {
final double scaleChange;
log(event.runtimeType.toString());
if (event is PointerScrollEvent) {
if (event.kind == PointerDeviceKind.trackpad) {
// トラックパッドのスクロールなので Pan として扱う
Expand Down Expand Up @@ -415,10 +405,7 @@ class MapViewModel extends _$MapViewModel {
).wait;
}

void _onLatLngAnimation() {}

void registerWidgetSize(Size size) {
log(size.toString());
_widgetSize = size;
}

Expand All @@ -439,9 +426,7 @@ class MapViewModel extends _$MapViewModel {
// setter
set topLeftLatLng(LatLng latLng) {
final topLeftPoint = WebMercatorProjection().project(latLng);
debugPrint('topLeftPoint: $topLeftPoint');
final offset = state.globalPointToOffset(topLeftPoint);
debugPrint('offset: $offset');
state = state.move(offset);
}

Expand Down
7 changes: 4 additions & 3 deletions lib/common/feature/map/model/state/map_data_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ import 'package:freezed_annotation/freezed_annotation.dart';
@immutable
class MapDataState {
const MapDataState({
required this.isReady,
this.data,
this.projectedData,
});

final bool isReady;
final MapDataFromSource? data;
final MapProjectedData? projectedData;

// copywith
MapDataState copyWith({
bool? isReady,
MapDataFromSource? data,
MapProjectedData? projectedData,
}) {
return MapDataState(
isReady: isReady ?? this.isReady,
data: data ?? this.data,
projectedData: projectedData ?? this.projectedData,
);
}
}
Expand Down
38 changes: 17 additions & 21 deletions lib/common/feature/map/provider/map_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ part 'map_data_provider.g.dart';
@Riverpod(keepAlive: true)
class MapData extends _$MapData {
@override
MapDataState build() => const MapDataState(
isReady: false,
);
MapDataState build() => const MapDataState();

MapDataFromSource? _mapDataFromSource;
MapProjectedData? _mapProjectedData;
Future<void> initialize() async {
_mapDataFromSource ??= await _mapData();
_mapProjectedData ??= await _projectMap();
state = state.copyWith(
isReady: true,
);
if (state.data == null) {
final data = await _mapData();
state = state.copyWith(
data: data,
);
}
if (state.projectedData == null) {
final projectedData = await _projectMap();
state = state.copyWith(
projectedData: projectedData,
);
}
}

Future<MapDataFromSource> _mapData() async {
Expand Down Expand Up @@ -74,7 +77,7 @@ class MapData extends _$MapData {
}

Future<MapProjectedData> _projectMap() async {
if (_mapDataFromSource == null) {
if (state.data == null) {
throw Error();
}
final jmaProjectedMap =
Expand All @@ -84,7 +87,7 @@ class MapData extends _$MapData {
if (type == MapDataType.worldMap) {
continue;
}
final map = _mapDataFromSource!.jmaMap[type]!;
final map = state.data!.jmaMap[type]!;
final projectedMap = <MultiPolygonProjectedMapData<JmaMapProperty>>[];
for (final e in map) {
projectedMap.add(
Expand All @@ -99,7 +102,7 @@ class MapData extends _$MapData {

final worldProjectedMap =
<MultiPolygonProjectedMapData<WorldMapProperty>>[];
for (final e in _mapDataFromSource!.worldMap) {
for (final e in state.data!.worldMap) {
worldProjectedMap.add(
MultiPolygonProjectedMapData.fromMapData(
e,
Expand All @@ -109,7 +112,7 @@ class MapData extends _$MapData {
}

final tsunamiProjectedLine = <MultiLineProjectedMapData<JmaMapProperty>>[];
for (final e in _mapDataFromSource!.tsunamiLine) {
for (final e in state.data!.tsunamiLine) {
tsunamiProjectedLine.add(
MultiLineProjectedMapData.fromLineData(
e,
Expand All @@ -124,11 +127,4 @@ class MapData extends _$MapData {
tsunamiLine: tsunamiProjectedLine,
);
}

MapProjectedData get mapProjectedData {
if (_mapProjectedData == null) {
throw Error();
}
return _mapProjectedData!;
}
}
17 changes: 16 additions & 1 deletion lib/common/router/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:eqmonitor/app.dart';
import 'package:eqmonitor/common/provider/shared_preferences.dart';
import 'package:eqmonitor/feature/debug/debug_home_view.dart';
import 'package:eqmonitor/feature/earthquake_history/page/earthquake_history.dart';
import 'package:eqmonitor/feature/earthquake_history_details/screen/earthquake_history_details.dart';
import 'package:eqmonitor/feature/home/view/home_view.dart';
import 'package:eqmonitor/feature/setup/screen/setup_screen.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -50,9 +51,23 @@ class EarthquakeHistoryRoute extends GoRouteData {
const EarthquakeHistoryPage();
}

@TypedGoRoute<EarthquakeHistoryDetailsRoute>(
path: '/earthquake-history/:eventId',
)
class EarthquakeHistoryDetailsRoute extends GoRouteData {
const EarthquakeHistoryDetailsRoute(this.eventId);
final int eventId;
@override
Widget build(BuildContext context, GoRouterState state) {
return EarthquakeHistoryDetailsPage(
eventId: eventId,
);
}
}

@TypedGoRoute<HomeRoute>(path: '/home')
class HomeRoute extends GoRouteData {
const HomeRoute();
@override
Widget build(BuildContext context, GoRouterState state) => const HomeView();
Widget build(BuildContext context, GoRouterState state) => HomeView();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import 'package:intl/intl.dart';
class EarthquakeHistoryTileWidget extends ConsumerWidget {
const EarthquakeHistoryTileWidget({
required this.item,
this.onTap,
super.key,
});

final EarthquakeHistoryItem item;
final void Function(EarthquakeHistoryItem)? onTap;

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand All @@ -35,8 +37,6 @@ class EarthquakeHistoryTileWidget extends ConsumerWidget {
(e) => e.type == TelegramType.vxse51,
) &&
item.telegrams.every((e) => e.type == TelegramType.vxse52);
// 長周期地震動に関する観測情報があるかどうか
final hasVxse62 = item.telegrams.any((e) => e.type == TelegramType.vxse62);

// ! title
final title = StringBuffer();
Expand Down Expand Up @@ -127,10 +127,10 @@ class EarthquakeHistoryTileWidget extends ConsumerWidget {
),
),
),
if (hasVxse62)
if (item.earthquake.lgIntensity != null)
CustomChip(
child: Text(
'長周期地震動 最大階級${item.earthquake.intensity!.maxLgInt}',
'長周期地震動 最大階級${item.earthquake.lgIntensity!.maxLgInt}',
style: const TextStyle(
fontWeight: FontWeight.bold,
),
Expand Down Expand Up @@ -197,6 +197,9 @@ class EarthquakeHistoryTileWidget extends ConsumerWidget {
)
: null,
tileColor: backgroundColor?.withOpacity(0.4),
onTap: () => onTap?.call(
item,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class EarthquakeData with _$EarthquakeData {
const factory EarthquakeData({
required Earthquake? earthquake,
required Intensity? intensity,
required Intensity? lgIntensity,
required CommentsOmitVar? comment,
required bool isVolcano,
}) = _EarthquakeData;
Expand Down
Loading

0 comments on commit 93ebda6

Please sign in to comment.