Skip to content

Commit

Permalink
timer toggle on tap
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Zhdanov committed Dec 26, 2024
1 parent 1f9adf9 commit ae46dad
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 196 deletions.
2 changes: 1 addition & 1 deletion lib/category/model/selected_file_catetory.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions lib/file/selected/model/selected_file.dart

This file was deleted.

25 changes: 0 additions & 25 deletions lib/file/selected/model/selected_file.g.dart

This file was deleted.

30 changes: 30 additions & 0 deletions lib/file/state/files_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:chrono_sheet/file/model/google_file.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../logging/logging.dart';

part 'files_state.g.dart';

final _logger = getNamedLogger();

class FilesInfo {

final GoogleFile? selected;
final List<GoogleFile> recent;

const FilesInfo(this.selected, [this.recent = const []]);
}

@riverpod
class FilesInfoHolder extends _$FilesInfoHolder {
@override
FilesInfo build() {
return FilesInfo(null);
}

void select(GoogleFile file) {
_logger.info("file '${file.name}' is now active");
List<GoogleFile> recent = List.from(state.recent);
recent.removeWhere((recentFile) => recentFile.id == file.id);
state = FilesInfo(file, recent);
}
}
26 changes: 26 additions & 0 deletions lib/file/state/files_state.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:chrono_sheet/router/router.dart';
import 'package:chrono_sheet/file/selected/model/selected_file.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../state/files_state.dart';

class SelectedFileWidget extends ConsumerWidget {
const SelectedFileWidget({super.key});

Expand All @@ -13,7 +14,7 @@ class SelectedFileWidget extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final selectedFile = ref.watch(selectedFileProvider);
final files = ref.watch(filesInfoHolderProvider);
return GestureDetector(
onTap: () => _selectFile(context),
child: Container(
Expand All @@ -25,11 +26,13 @@ class SelectedFileWidget extends ConsumerWidget {
padding: EdgeInsets.all(16.0),
child: Center(
child: Text(
selectedFile?.name ?? 'tap to select a file', // TODO implement i18n
files.selected?.name ?? 'tap to select a file', // TODO implement i18n
style: TextStyle(
color: selectedFile?.name == null
? Colors.grey
: Colors.black),
// TODO implement use theme
color: files.selected == null
? Colors.grey
: Colors.black
),
),
)
)
Expand Down
12 changes: 8 additions & 4 deletions lib/measurement/model/measurement_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ part 'measurement_state.g.dart';
@riverpod
class MeasurementState extends _$MeasurementState {

final Duration duration = Duration.zero;

@override
MeasurementState build() {
return this;
Duration build() {
return Duration.zero;
}

void increment(Duration diff) {
state = state + diff;
}

void reset() => state = Duration.zero;
}
6 changes: 3 additions & 3 deletions lib/measurement/model/measurement_state.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 54 additions & 8 deletions lib/measurement/widget/stop_watch_widget.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class StopWatch extends ConsumerWidget {
import '../model/measurement_state.dart';

class StopWatchWidget extends ConsumerStatefulWidget {

const StopWatchWidget({super.key});

@override
StopWatchState createState() => StopWatchState();
}

class StopWatchState extends ConsumerState<StopWatchWidget> {

const StopWatch({super.key});
late Timer _timer;
DateTime _lastMeasurementTime = DateTime.now();
bool _running = false;

void _toggle() {
if (_running) {
_timer.cancel();
} else {
_lastMeasurementTime = DateTime.now();
_timer = Timer.periodic(Duration(milliseconds: 100), (timer) {
setState(() {
final now = DateTime.now();
ref.read(measurementStateProvider.notifier).increment(
now.difference(_lastMeasurementTime)
);
_lastMeasurementTime = now;
});
});
}
setState(() {
_running = !_running;
});
}

void _reset() {
setState(() {
_timer.cancel();
_running = false;
ref.read(measurementStateProvider.notifier).reset();
});
}

String _formatTime(Duration duration) {
String twoDigits(int n) => n.toString().padLeft(2, '0');
Expand All @@ -14,17 +56,21 @@ class StopWatch extends ConsumerWidget {
}

@override
Widget build(BuildContext context, WidgetRef ref) {
Widget build(BuildContext context) {
final measurement = ref.watch(measurementStateProvider);
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text(
_formatTime(Duration.zero),
style: TextStyle(
fontSize: 64,
fontWeight: FontWeight.bold,
child: GestureDetector(
onTap: _toggle,
child: Text(
_formatTime(measurement),
style: TextStyle(
fontSize: 64,
fontWeight: FontWeight.bold,
),
),
),
),
Expand Down
6 changes: 3 additions & 3 deletions lib/screen/choose_sheet_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:chrono_sheet/file/selected/model/selected_file.dart';
import 'package:chrono_sheet/file/state/files_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
Expand All @@ -10,7 +10,7 @@ class ChooseSheetScreen extends ConsumerStatefulWidget {
const ChooseSheetScreen({super.key});

@override
ConsumerState createState() => ChooseSheetState();
ChooseSheetState createState() => ChooseSheetState();
}

class ChooseSheetState extends ConsumerState<ChooseSheetScreen> {
Expand Down Expand Up @@ -59,7 +59,7 @@ class ChooseSheetState extends ConsumerState<ChooseSheetScreen> {
style: theme.textTheme.headlineSmall,
),
onTap: () {
ref.read(selectedFileProvider.notifier).select(
ref.read(filesInfoHolderProvider.notifier).select(
state.files[index]
);
context.pop();
Expand Down
4 changes: 2 additions & 2 deletions lib/screen/main_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:chrono_sheet/file/widget/selected_file_widget.dart';
import 'package:chrono_sheet/measurement/widget/stop_watch_widget.dart';
import 'package:chrono_sheet/file/selected/widget/selected_file_widget.dart';
import 'package:flutter/material.dart';

class MainScreen extends StatelessWidget {
Expand All @@ -18,7 +18,7 @@ class MainScreen extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
StopWatch(),
StopWatchWidget(),
SizedBox(height: 24),
SelectedFileWidget(),
],
Expand Down
Loading

0 comments on commit ae46dad

Please sign in to comment.