mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-23 10:21:31 +08:00
Move SyncButton to its own file
This commit is contained in:
lib
@ -1,6 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:git_bindings/git_bindings.dart';
|
||||
import 'package:gitjournal/appstate.dart';
|
||||
|
||||
import 'package:gitjournal/core/note.dart';
|
||||
import 'package:gitjournal/core/notes_folder.dart';
|
||||
@ -11,6 +10,7 @@ import 'package:gitjournal/state_container.dart';
|
||||
import 'package:gitjournal/widgets/app_drawer.dart';
|
||||
import 'package:gitjournal/widgets/app_bar_menu_button.dart';
|
||||
import 'package:gitjournal/widgets/journal_list.dart';
|
||||
import 'package:gitjournal/widgets/sync_button.dart';
|
||||
import 'package:gitjournal/themes.dart';
|
||||
|
||||
class JournalListingScreen extends StatelessWidget {
|
||||
@ -165,95 +165,3 @@ class NoteSearch extends SearchDelegate<Note> {
|
||||
return journalList;
|
||||
}
|
||||
}
|
||||
|
||||
class SyncButton extends StatefulWidget {
|
||||
@override
|
||||
_SyncButtonState createState() => _SyncButtonState();
|
||||
}
|
||||
|
||||
class _SyncButtonState extends State<SyncButton> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final container = StateContainer.of(context);
|
||||
final appState = container.appState;
|
||||
|
||||
if (appState.syncStatus == SyncStatus.Loading) {
|
||||
return RotatingIcon();
|
||||
}
|
||||
return IconButton(
|
||||
icon: Icon(_syncStatusIcon()),
|
||||
onPressed: () async {
|
||||
_syncRepo();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _syncRepo() async {
|
||||
final container = StateContainer.of(context);
|
||||
try {
|
||||
await container.syncNotes();
|
||||
} on GitException catch (exp) {
|
||||
showSnackbar(context, exp.cause);
|
||||
}
|
||||
}
|
||||
|
||||
IconData _syncStatusIcon() {
|
||||
final container = StateContainer.of(context);
|
||||
final appState = container.appState;
|
||||
switch (appState.syncStatus) {
|
||||
case SyncStatus.Error:
|
||||
return Icons.cloud_off;
|
||||
|
||||
case SyncStatus.Unknown:
|
||||
case SyncStatus.Done:
|
||||
default:
|
||||
return Icons.cloud_done;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RotatingIcon extends StatefulWidget {
|
||||
@override
|
||||
_RotatingIconState createState() => _RotatingIconState();
|
||||
}
|
||||
|
||||
class _RotatingIconState extends State<RotatingIcon>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController _controller;
|
||||
Animation<double> _animation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_controller = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 1800),
|
||||
);
|
||||
_animation = CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: Curves.linear,
|
||||
);
|
||||
|
||||
_controller.repeat(reverse: true);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var button = IconButton(
|
||||
icon: const Icon(Icons.loop),
|
||||
onPressed: () {},
|
||||
);
|
||||
|
||||
return RotationTransition(
|
||||
child: button,
|
||||
turns: _animation,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
98
lib/widgets/sync_button.dart
Normal file
98
lib/widgets/sync_button.dart
Normal file
@ -0,0 +1,98 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:git_bindings/git_bindings.dart';
|
||||
import 'package:gitjournal/appstate.dart';
|
||||
|
||||
import 'package:gitjournal/utils.dart';
|
||||
import 'package:gitjournal/state_container.dart';
|
||||
|
||||
class SyncButton extends StatefulWidget {
|
||||
@override
|
||||
_SyncButtonState createState() => _SyncButtonState();
|
||||
}
|
||||
|
||||
class _SyncButtonState extends State<SyncButton> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final container = StateContainer.of(context);
|
||||
final appState = container.appState;
|
||||
|
||||
if (appState.syncStatus == SyncStatus.Loading) {
|
||||
return RotatingIcon();
|
||||
}
|
||||
return IconButton(
|
||||
icon: Icon(_syncStatusIcon()),
|
||||
onPressed: () async {
|
||||
_syncRepo();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _syncRepo() async {
|
||||
final container = StateContainer.of(context);
|
||||
try {
|
||||
await container.syncNotes();
|
||||
} on GitException catch (exp) {
|
||||
showSnackbar(context, exp.cause);
|
||||
}
|
||||
}
|
||||
|
||||
IconData _syncStatusIcon() {
|
||||
final container = StateContainer.of(context);
|
||||
final appState = container.appState;
|
||||
switch (appState.syncStatus) {
|
||||
case SyncStatus.Error:
|
||||
return Icons.cloud_off;
|
||||
|
||||
case SyncStatus.Unknown:
|
||||
case SyncStatus.Done:
|
||||
default:
|
||||
return Icons.cloud_done;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RotatingIcon extends StatefulWidget {
|
||||
@override
|
||||
_RotatingIconState createState() => _RotatingIconState();
|
||||
}
|
||||
|
||||
class _RotatingIconState extends State<RotatingIcon>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController _controller;
|
||||
Animation<double> _animation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_controller = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 1800),
|
||||
);
|
||||
_animation = CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: Curves.linear,
|
||||
);
|
||||
|
||||
_controller.repeat(reverse: true);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var button = IconButton(
|
||||
icon: const Icon(Icons.loop),
|
||||
onPressed: () {},
|
||||
);
|
||||
|
||||
return RotationTransition(
|
||||
child: button,
|
||||
turns: _animation,
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user