Skip to content

Commit

Permalink
Refactor project structure
Browse files Browse the repository at this point in the history
Refactor project into different features, each feature having models,
screens and controllers.
  • Loading branch information
cygnet3 committed Jun 12, 2024
1 parent d05c418 commit dbfd624
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 430 deletions.
65 changes: 65 additions & 0 deletions lib/features/home/home_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:bitcoin_ui/bitcoin_ui.dart';
import 'package:donationwallet/features/wallet/screens/wallet_screen.dart';
import 'package:donationwallet/features/settings/screens/settings_screen.dart';
import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});

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

class HomeScreenState extends State<HomeScreen> {
int _selectedIndex = 0;
final List<Widget> _widgetOptions = [
const WalletScreen(),
const SettingsScreen(),
];

@override
void initState() {
super.initState();
}

void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Silent Payments'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: IndexedStack(
index: _selectedIndex,
children: _widgetOptions,
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Image(
image:
const AssetImage("icons/wallet.png", package: "bitcoin_ui"),
color: Bitcoin.neutral3Dark),
label: 'Wallet',
),
BottomNavigationBarItem(
icon: Image(
image:
const AssetImage("icons/gear.png", package: "bitcoin_ui"),
color: Bitcoin.neutral3Dark),
label: 'Settings',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.green,
onTap: _onItemTapped,
),
);
}
}
77 changes: 9 additions & 68 deletions lib/settings.dart → ...ings/controllers/settings_controller.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import 'package:bitcoin_ui/bitcoin_ui.dart';
import 'package:donationwallet/rust/api/simple.dart';
import 'package:donationwallet/global_functions.dart';
import 'package:donationwallet/main.dart';
import 'package:donationwallet/home.dart';
import 'package:donationwallet/features/wallet/models/wallet_state.dart';
import 'package:donationwallet/utils/global_functions.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
class SettingsController {
const SettingsController();

Future<void> _removeWallet(
Future<void> removeWallet(
WalletState walletState, Function(Exception? e) callback) async {
try {
await walletState.rmWalletFromSecureStorage();
Expand All @@ -22,7 +20,7 @@ class SettingsScreen extends StatelessWidget {
}
}

Future<String?> _getSeedPhrase(WalletState walletState) async {
Future<String?> getSeedPhrase(WalletState walletState) async {
try {
final wallet = await walletState.getWalletFromSecureStorage();
return showMnemonic(encodedWallet: wallet);
Expand All @@ -32,7 +30,7 @@ class SettingsScreen extends StatelessWidget {
}
}

Future<void> _setBirthday(BuildContext context,
Future<void> setBirthday(BuildContext context,
TextEditingController controller, Function(Exception? e) callback) async {
showDialog<int>(
context: context,
Expand Down Expand Up @@ -71,9 +69,8 @@ class SettingsScreen extends StatelessWidget {
final walletState = Provider.of<WalletState>(context, listen: false);
try {
final wallet = await walletState.getWalletFromSecureStorage();
final updatedWallet = changeBirthday(
encodedWallet: wallet,
birthday: value);
final updatedWallet =
changeBirthday(encodedWallet: wallet, birthday: value);
walletState.saveWalletToSecureStorage(updatedWallet);
callback(null);
await walletState.updateWalletStatus();
Expand All @@ -85,60 +82,4 @@ class SettingsScreen extends StatelessWidget {
}
});
}

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
BitcoinButtonOutlined(
title: 'Show seed phrase',
onPressed: () async {
final walletState =
Provider.of<WalletState>(context, listen: false);

const title = 'Backup seed phrase';
final text = await _getSeedPhrase(walletState) ??
'Seed phrase unknown! Did you import from keys?';

showAlertDialog(title, text);
},
),
BitcoinButtonOutlined(
title: 'Set wallet birthday',
onPressed: () async {
final controller = TextEditingController();
await _setBirthday(context, controller, (Exception? e) async {
if (e != null) {
throw e;
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const HomeScreen()));
}
});
},
),
BitcoinButtonOutlined(
title: 'Wipe wallet',
onPressed: () async {
final walletState =
Provider.of<WalletState>(context, listen: false);
await _removeWallet(walletState, (Exception? e) async {
if (e != null) {
throw e;
} else {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
(Route<dynamic> route) => false,
);
}
});
},
),
],
);
}
}
73 changes: 73 additions & 0 deletions lib/features/settings/screens/settings_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:bitcoin_ui/bitcoin_ui.dart';
import 'package:donationwallet/features/settings/controllers/settings_controller.dart';
import 'package:donationwallet/features/setup/screens/setup_wallet_screen.dart';
import 'package:donationwallet/utils/global_functions.dart';
import 'package:donationwallet/features/home/home_screen.dart';
import 'package:donationwallet/features/wallet/models/wallet_state.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SettingsScreen extends StatelessWidget {
final settingsController = const SettingsController();

const SettingsScreen({super.key});

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
BitcoinButtonOutlined(
title: 'Show seed phrase',
onPressed: () async {
final walletState =
Provider.of<WalletState>(context, listen: false);

const title = 'Backup seed phrase';
final text = await settingsController.getSeedPhrase(walletState) ??
'Seed phrase unknown! Did you import from keys?';

showAlertDialog(title, text);
},
),
BitcoinButtonOutlined(
title: 'Set wallet birthday',
onPressed: () async {
final controller = TextEditingController();
await settingsController.setBirthday(context, controller,
(Exception? e) async {
if (e != null) {
throw e;
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const HomeScreen()));
}
});
},
),
BitcoinButtonOutlined(
title: 'Wipe wallet',
onPressed: () async {
final walletState =
Provider.of<WalletState>(context, listen: false);
await settingsController.removeWallet(walletState,
(Exception? e) async {
if (e != null) {
throw e;
} else {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => const SetupWalletScreen()),
(Route<dynamic> route) => false,
);
}
});
},
),
],
);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'dart:async';

import 'package:donationwallet/rust/api/simple.dart';
import 'package:donationwallet/home.dart';
import 'package:donationwallet/main.dart';
import 'package:donationwallet/features/home/home_screen.dart';
import 'package:donationwallet/features/wallet/models/wallet_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class LoadWalletScreen extends StatelessWidget {
const LoadWalletScreen({super.key});
class SetupWalletScreen extends StatelessWidget {
const SetupWalletScreen({super.key});

Future<void> _setup(BuildContext context, String? mnemonic, String? scanKey,
String? spendKey, int birthday) async {
Expand Down Expand Up @@ -230,6 +230,10 @@ class LoadWalletScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Wallet creation/restoration'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
32 changes: 32 additions & 0 deletions lib/features/wallet/controllers/synchronization_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:async';
import 'package:donationwallet/utils/global_functions.dart';
import 'package:donationwallet/rust/api/simple.dart';

class SynchronizationService {
Timer? _timer;
final Duration _interval = const Duration(minutes: 10);

void startSyncTimer() {
_scheduleNextTask();
}

void _scheduleNextTask() async {
_timer?.cancel();
await performSynchronizationTask();
_timer = Timer(_interval, () async {
_scheduleNextTask();
});
}

Future<void> performSynchronizationTask() async {
try {
await syncBlockchain();
} catch (e) {
displayNotification(e.toString());
}
}

void stopSyncTimer() {
_timer?.cancel();
}
}
Loading

0 comments on commit dbfd624

Please sign in to comment.