Skip to content

Commit

Permalink
feat: Starport template - quality improvements (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejchm authored Jan 24, 2022
1 parent 96964da commit 88828e6
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ class CosmosBottomSheetContainer extends StatelessWidget {
final screenHeight = MediaQuery.of(context).size.height;
final maxHeight = screenHeight - (screenHeight / 10);
final theme = CosmosTheme.of(context);
return ConstrainedBox(
constraints: BoxConstraints(
maxHeight: maxHeight,
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(
top: Radius.elliptical(
theme.radiusXL,
theme.radiusXL * 1.1,
return MediaQuery.removePadding(
context: context,
removeTop: true,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: maxHeight,
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(
top: Radius.elliptical(
theme.radiusXL,
theme.radiusXL * 1.1,
),
),
color: theme.colors.background,
),
color: theme.colors.background,
child: child,
),
child: child,
),
);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/cosmos_utils/lib/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ extension IterableExtensions<T> on Iterable<T> {
.map((key, value) => MapEntry(key, mapper(key, value)))
.values;
}

T? firstOrNull({required bool Function(T) where}) {
try {
return firstWhere(
(it) => where(it),
);
// ignore: avoid_catching_errors
} on StateError {
return null;
}
}
}

/// Extensions for the Either, Future and MobX combinations
Expand Down
2 changes: 1 addition & 1 deletion starport_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
- If you're running the app on real device instead of emulator/simulator or want to taget a specific blockchain run on a remote machine, make sure to specify proper urls and ports when running the app. Here is an example for running the app on cosmos hub testnet:

```
flutter run --dart-define=BASE_LCD_URL=api.testnet.cosmos.network --dart-define=PORT=443
flutter run --dart-define=LCD_URL=https://api.testnet.cosmos.network --dart-define=LCD_PORT=443 --dart-define=GRPC_URL=https://grpc.testnet.cosmos.network --dart-define=GRPC_PORT=443
```

7 changes: 0 additions & 7 deletions starport_template/android/app/src/debug/AndroidManifest.xml

This file was deleted.

19 changes: 10 additions & 9 deletions starport_template/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.starport.template">
<application
package="com.starport.template">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="Starport"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -15,18 +16,18 @@
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand All @@ -36,6 +37,6 @@
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
android:value="2"/>
</application>
</manifest>
7 changes: 0 additions & 7 deletions starport_template/android/app/src/profile/AndroidManifest.xml

This file was deleted.

36 changes: 27 additions & 9 deletions starport_template/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:alan/alan.dart';
import 'package:cosmos_auth/cosmos_auth.dart';
import 'package:cosmos_utils/cosmos_utils.dart';
import 'package:flutter/material.dart';
import 'package:starport_template/starport_app.dart';
import 'package:starport_template/stores/settings_store.dart';
Expand All @@ -16,11 +16,9 @@ void main() {
}

void _buildDependencies() {
StarportApp.networkInfo = NetworkInfo(
bech32Hrp: 'cosmos',
lcdInfo: LCDInfo(host: Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'),
grpcInfo: GRPCInfo(host: Platform.isAndroid ? 'http://10.0.2.2' : 'http://localhost'),
);
StarportApp.baseEnv = BaseEnv();
StarportApp.networkInfo = StarportApp.baseEnv.networkInfo;
_logBackendInfo(StarportApp.networkInfo);
StarportApp.secureDataStore = FlutterSecureStorageDataStore();

StarportApp.signingGateway = TransactionSigningGateway(
Expand All @@ -37,8 +35,28 @@ void _buildDependencies() {
plainDataStore: SharedPrefsPlainDataStore(),
),
);
StarportApp.baseEnv = BaseEnv();

StarportApp.cosmosAuth = CosmosAuth();
StarportApp.walletsStore = WalletsStore(StarportApp.signingGateway, StarportApp.baseEnv);
StarportApp.settingsStore = SettingsStore();
StarportApp.settingsStore = SettingsStore(StarportApp.cosmosAuth, StarportApp.secureDataStore);
StarportApp.transactionsStore = TransactionsStore(StarportApp.baseEnv);
}

void _logBackendInfo(NetworkInfo networkInfo) => debugLog(
'''
Starting app with following info:
bech32Hrp:\t${networkInfo.bech32Hrp}
LCD : {
host:\t\t${networkInfo.lcdInfo.host}
port:\t\t${networkInfo.lcdInfo.port}
fullUrl:\t${networkInfo.lcdInfo.fullUrl}
}
GRPC: {
host:\t\t${networkInfo.grpcInfo.host}
port:\t\t${networkInfo.grpcInfo.port}
}
''',
);
2 changes: 1 addition & 1 deletion starport_template/lib/pages/assets_portfolio_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:starport_template/entities/balance.dart';
import 'package:starport_template/pages/receive_money_sheet.dart';
import 'package:starport_template/pages/select_asset_page.dart';
import 'package:starport_template/pages/transaction_history_page.dart';
import 'package:starport_template/pages/wallets_list_sheet.dart';
import 'package:starport_template/starport_app.dart';
import 'package:starport_template/widgets/asset_portfolio_heading.dart';
import 'package:starport_template/widgets/balance_card_list.dart';
import 'package:starport_template/widgets/receive_money_sheet.dart';
import 'package:starport_template/widgets/starport_button_bar.dart';
import 'package:transaction_signing_gateway/model/wallet_public_info.dart';

Expand Down
2 changes: 1 addition & 1 deletion starport_template/lib/pages/create_wallet_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class _CreateWalletPageState extends State<CreateWalletPage> {
}

Future<void> _authenticateUser() async {
final result = await CosmosAuth().biometricAuthenticate();
final result = await StarportApp.cosmosAuth.biometricAuthenticate();
setState(
() => _isAuthenticated = result.fold(
(fail) => fail.type == LocalAuthFailureType.noBiometrics,
Expand Down
4 changes: 1 addition & 3 deletions starport_template/lib/pages/onboarding_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ class _OnboardingPageState extends State<OnboardingPage> {
right: theme.spacingL,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
height: MediaQuery.of(context).size.width + 24,
),
Text(
'Welcome to your first blockchain-powered mobile app.',
style: Theme.of(context).textTheme.headline2,
Expand Down
36 changes: 19 additions & 17 deletions starport_template/lib/pages/passcode_prompt_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:cosmos_auth/auth/cosmos_auth.dart';
import 'package:cosmos_ui_components/cosmos_ui_components.dart';
import 'package:cosmos_utils/extensions.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -17,17 +16,12 @@ class PasswordPromptPage extends StatefulWidget {
final String? message;

static Future<String?> promptPassword(BuildContext context, {String? message}) async {
return (await CosmosAuth().hasPassword(
secureDataStore: StarportApp.secureDataStore,
id: PasswordPromptPage.starportPassId,
))
.fold(
(l) => null,
(hasPassword) => _showPage(
context,
setUpPasscode: !hasPassword,
message: hasPassword ? message : 'Provide passcode that we will use to secure your wallet data',
),
final hasPassword = await StarportApp.settingsStore.hasPasswordSet();
// ignore: use_build_context_synchronously
return _showPage(
context,
setUpPasscode: !hasPassword,
message: hasPassword ? message : 'Provide passcode that we will use to secure your wallet data',
);
}

Expand Down Expand Up @@ -64,6 +58,10 @@ class _PasswordPromptPageState extends State<PasswordPromptPage> {

bool get _arePasscodesValid => _passcodes.length == 2 && _passcodes[0] == _passcodes[1];

int _attempts = 0;

ValueKey get _promptKey => ValueKey(_attempts);

@override
void initState() {
super.initState();
Expand All @@ -89,7 +87,7 @@ class _PasswordPromptPageState extends State<PasswordPromptPage> {
return Scaffold(
body: SafeArea(
child: CosmosPasscodePrompt(
key: ValueKey(_title),
key: _promptKey,
title: _title,
message: widget.message,
onSubmit: widget.setUpPasscode ? _onSetUpPasscodeSubmit : _onPasscodeSubmit,
Expand All @@ -99,6 +97,7 @@ class _PasswordPromptPageState extends State<PasswordPromptPage> {
}

void _onSetUpPasscodeSubmit(String value) {
_attempts++;
setState(() => _passcodes.add(value));
if (_passcodes.length == 2) {
if (_arePasscodesValid) {
Expand All @@ -110,17 +109,20 @@ class _PasswordPromptPageState extends State<PasswordPromptPage> {
}

Future<void> _onPasscodeSubmit(String value) async {
final password = await CosmosAuth().readPassword(
final password = await StarportApp.cosmosAuth.readPassword(
secureDataStore: StarportApp.secureDataStore,
id: PasswordPromptPage.starportPassId,
useBiometrics: false,
);
if (password.getOrElse(() => '') == value) {
_attempts++;
if (password.getOrElse(() => value) == value) {
if (mounted) {
Navigator.of(context).pop(value);
}
} else {
_passcodes.clear();
_showPasscodeError();
setState(() {});
}
}

Expand All @@ -134,7 +136,7 @@ class _PasswordPromptPageState extends State<PasswordPromptPage> {
}

void _savePasscode() {
CosmosAuth().savePassword(
StarportApp.cosmosAuth.savePassword(
secureDataStore: StarportApp.secureDataStore,
id: PasswordPromptPage.starportPassId,
password: _passcodes[0],
Expand All @@ -143,7 +145,7 @@ class _PasswordPromptPageState extends State<PasswordPromptPage> {
}

Future<void> _runBiometricsCheck() async {
final cosmosAuth = CosmosAuth();
final cosmosAuth = StarportApp.cosmosAuth;
final availableBiometrics = await cosmosAuth.getAvailableBiometrics().asyncFold(
(fail) => [],
(types) => types,
Expand Down
Loading

0 comments on commit 88828e6

Please sign in to comment.