From f7e67309bdf8f8d316bfb30da4e15301e91da49a Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Wed, 16 Jan 2019 22:33:49 +0100 Subject: [PATCH] Move onboarding `git clone` screen to its own file --- lib/screens/onboarding_git_clone.dart | 115 ++++++++++++++++++++++++++ lib/screens/onboarding_screens.dart | 106 +----------------------- 2 files changed, 116 insertions(+), 105 deletions(-) create mode 100644 lib/screens/onboarding_git_clone.dart diff --git a/lib/screens/onboarding_git_clone.dart b/lib/screens/onboarding_git_clone.dart new file mode 100644 index 00000000..1d23edb1 --- /dev/null +++ b/lib/screens/onboarding_git_clone.dart @@ -0,0 +1,115 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'package:path/path.dart' as p; + +import 'package:journal/analytics.dart'; +import 'package:journal/state_container.dart'; +import 'package:journal/storage/git.dart'; + +class OnBoardingGitClone extends StatefulWidget { + final Function doneFunction; + + OnBoardingGitClone({@required this.doneFunction}); + + @override + OnBoardingGitCloneState createState() { + return new OnBoardingGitCloneState(); + } +} + +class OnBoardingGitCloneState extends State { + String errorMessage = ""; + + @override + void initState() { + super.initState(); + + // FIXME: This is throwing an exception! + _initStateAsync(); + } + + void _initStateAsync() async { + var pref = await SharedPreferences.getInstance(); + String sshCloneUrl = pref.getString("sshCloneUrl"); + + // Just in case it was half cloned because of an error + await _removeExistingClone(); + + String error = await gitClone(sshCloneUrl, "journal"); + if (error != null && error.isNotEmpty) { + setState(() { + getAnalytics().logEvent( + name: "onboarding_gitClone_error", + parameters: { + 'error': error, + }, + ); + errorMessage = error; + }); + } else { + this.widget.doneFunction(); + } + } + + Future _removeExistingClone() async { + var baseDir = await getNotesDir(); + var dotGitDir = new Directory(p.join(baseDir.path, ".git")); + bool exists = await dotGitDir.exists(); + if (exists) { + await baseDir.delete(recursive: true); + await baseDir.create(); + } + } + + @override + Widget build(BuildContext context) { + var children = []; + if (this.errorMessage.isEmpty) { + children = [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + 'Cloning ...', + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.display1, + ), + ), + SizedBox(height: 8.0), + Padding( + padding: const EdgeInsets.all(8.0), + child: CircularProgressIndicator( + value: null, + ), + ), + ]; + } else if (this.errorMessage.isNotEmpty) { + children = [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + 'Failed', + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.display1, + ), + ), + Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + this.errorMessage, + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.display1, + ), + ), + ]; + } + + return Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: children, + ); + } +} diff --git a/lib/screens/onboarding_screens.dart b/lib/screens/onboarding_screens.dart index 3b27fd21..ed7f7f52 100644 --- a/lib/screens/onboarding_screens.dart +++ b/lib/screens/onboarding_screens.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -12,6 +10,7 @@ import 'package:journal/state_container.dart'; import 'package:journal/storage/git.dart'; import 'package:journal/screens/onboarding_git_url.dart'; +import 'package:journal/screens/onboarding_git_clone.dart'; class OnBoardingScreen extends StatefulWidget { final Function onBoardingCompletedFunction; @@ -417,106 +416,3 @@ class OnBoardingSshKey extends StatelessWidget { ); } } - -class OnBoardingGitClone extends StatefulWidget { - final Function doneFunction; - - OnBoardingGitClone({@required this.doneFunction}); - - @override - OnBoardingGitCloneState createState() { - return new OnBoardingGitCloneState(); - } -} - -class OnBoardingGitCloneState extends State { - String errorMessage = ""; - - @override - void initState() { - super.initState(); - _initStateAsync(); - } - - void _initStateAsync() async { - var pref = await SharedPreferences.getInstance(); - String sshCloneUrl = pref.getString("sshCloneUrl"); - - // Just in case it was half cloned because of an error - await _removeExistingClone(); - - String error = await gitClone(sshCloneUrl, "journal"); - if (error != null && error.isNotEmpty) { - setState(() { - getAnalytics().logEvent( - name: "onboarding_gitClone_error", - parameters: { - 'error': error, - }, - ); - errorMessage = error; - }); - } else { - this.widget.doneFunction(); - } - } - - Future _removeExistingClone() async { - var baseDir = await getNotesDir(); - var dotGitDir = new Directory(p.join(baseDir.path, ".git")); - bool exists = await dotGitDir.exists(); - if (exists) { - await baseDir.delete(recursive: true); - await baseDir.create(); - } - } - - @override - Widget build(BuildContext context) { - var children = []; - if (this.errorMessage.isEmpty) { - children = [ - Padding( - padding: const EdgeInsets.all(8.0), - child: Text( - 'Cloning ...', - textAlign: TextAlign.center, - style: Theme.of(context).textTheme.display1, - ), - ), - SizedBox(height: 8.0), - Padding( - padding: const EdgeInsets.all(8.0), - child: CircularProgressIndicator( - value: null, - ), - ), - ]; - } else if (this.errorMessage.isNotEmpty) { - children = [ - Padding( - padding: const EdgeInsets.all(8.0), - child: Text( - 'Failed', - textAlign: TextAlign.center, - style: Theme.of(context).textTheme.display1, - ), - ), - Padding( - padding: const EdgeInsets.all(8.0), - child: Text( - this.errorMessage, - textAlign: TextAlign.center, - style: Theme.of(context).textTheme.display1, - ), - ), - ]; - } - - return Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: children, - ); - } -}