Skip to content

Commit

Permalink
Merge pull request #49 from peaqnetwork/fix/1202883586978410_bug-ther…
Browse files Browse the repository at this point in the history
…e-is-no-login-screen-after-installing-the-app

Fix/1202883586978410 bug there is no login screen after installing the app
  • Loading branch information
irediaes authored Aug 30, 2022
2 parents d4d2273 + c03da63 commit 6bc4a1c
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 95 deletions.
101 changes: 49 additions & 52 deletions lib/common/providers/account_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import 'dart:async';
import 'package:charmev/theme.dart';

class CEVAccountProvider with ChangeNotifier {
CEVAccountProvider({
required this.cevSharedPref,
});

final CEVSharedPref cevSharedPref;
CEVAccountProvider();

late CEVApplicationProvider appProvider;

Expand All @@ -25,33 +21,29 @@ class CEVAccountProvider with ChangeNotifier {
}

LoadingStatus _status = LoadingStatus.idle;
String _error = '';
String _statusMessage = '';
bool _isLoggedIn = false;
bool _showNodeDropdown = false;
bool _logoutInitiated = false;
CEVAccount? _account;
List<Detail> _details = [];
// set the SS58 registry prefix
// currently set to Substrate which is 42
// ignore: todo
// TODO:: change to PEAQ SS58 registry prefix when it's set
final int _ss58 = 42;

List<String> _events = [];
List<String> _nodes = [Env.peaqTestnet];
String _selectedNode = Env.peaqTestnet;

String get error => _error;
String get statusMessage => _statusMessage;
LoadingStatus get status => _status;
bool get isLoggedIn => _isLoggedIn;
CEVAccount get account => _account!;
List<Detail> get details => _details;
List<String> get events => _events;
List<String> get nodes => _nodes;
String get selectedNode => _selectedNode;
bool get showNodeDropdown => _showNodeDropdown;

Future<bool> get isLoggedIn async {
var str =
appProvider.cevSharedPrefs.prefs.getString(Env.accountPrefKey) ?? "";

return str.isNotEmpty;
}

set showNodeDropdown(bool show) {
_showNodeDropdown = show;
notifyListeners();
Expand All @@ -62,29 +54,32 @@ class CEVAccountProvider with ChangeNotifier {
notifyListeners();
}

reset() {
_status = LoadingStatus.idle;
_statusMessage = "";
notifyListeners();
}

addNode(String node) async {
var exist = _nodes.contains(node);
if (!exist) {
_nodes.add(node);
await cevSharedPref.init();
cevSharedPref.prefs.setStringList(Env.nodePrefKey, _nodes);
appProvider.cevSharedPrefs.prefs.setStringList(Env.nodePrefKey, _nodes);
}
notifyListeners();
}

_deleteNodes() async {
await cevSharedPref.init();
cevSharedPref.prefs.remove(Env.nodePrefKey);
appProvider.cevSharedPrefs.prefs.remove(Env.nodePrefKey);
}

_fetchNode() async {
await cevSharedPref.init();
List<String>? _savedNodes =
cevSharedPref.prefs.getStringList(Env.nodePrefKey);
List<String>? savedNodes =
appProvider.cevSharedPrefs.prefs.getStringList(Env.nodePrefKey);

if (_savedNodes != null) {
if (_savedNodes.isNotEmpty) {
_nodes = _savedNodes;
if (savedNodes != null) {
if (savedNodes.isNotEmpty) {
_nodes = savedNodes;
notifyListeners();
}
}
Expand All @@ -93,17 +88,17 @@ class CEVAccountProvider with ChangeNotifier {

// generate consumer account details
generateDetails({bool notify = false}) {
List<Detail> _newDetails = [];
List<Detail> newDetails = [];

if (_account != null) {
_newDetails.add(Detail("Identity", _account!.did ?? ""));
newDetails.add(Detail("Identity", _account!.did ?? ""));
}

_newDetails.add(
newDetails.add(
Detail("Balance", "${_account!.balance} ${_account!.tokenSymbol}",
color: CEVTheme.accentColor),
);
_details = _newDetails;
_details = newDetails;
if (notify) {
notifyListeners();
}
Expand All @@ -113,68 +108,70 @@ class CEVAccountProvider with ChangeNotifier {
/// Generate wallet address
/// Save the account details in shared preference for further retrival
generateAccount(String secretPhrase) async {
_status = LoadingStatus.loading;
_statusMessage = Env.generatingAccount;
notifyListeners();
CEVAccount account =
await appProvider.peerProvider.generateAccount(secretPhrase);
print("account: ${accountToJson(account)}");
// print("account: ${accountToJson(account)}");

await cevSharedPref.prefs
await appProvider.cevSharedPrefs.prefs
.setString(Env.accountPrefKey, accountToJson(account));

_account = account;

generateDetails();

await Future.wait([
initBeforeOnboardingPage(),
initBeforeHomePage(),
]);

notifyListeners();
reset();

return;
}

getAccountBalance() async {
String balance = await appProvider.peerProvider
.getAccountBalance(_account!.tokenDecimals.toString(), _account!.seed!);
print("balance: $balance");
// print("balance: $balance");

_account?.balance = double.parse(balance);
print("new account: ${accountToJson(_account!)}");
// print("new account: ${accountToJson(_account!)}");

await cevSharedPref.prefs
.setString(Env.accountPrefKey, accountToJson(account));
if (!_logoutInitiated) {
await appProvider.cevSharedPrefs.prefs
.setString(Env.accountPrefKey, accountToJson(account));

_account = account;
_account = account;

generateDetails();
generateDetails();

notifyListeners();
notifyListeners();
}

return;
}

/// Fetch account saved in the shared pref
Future<void> getAccount() async {
await cevSharedPref.init();
String? _accountString = cevSharedPref.prefs.getString(Env.accountPrefKey);
String? accountString =
appProvider.cevSharedPrefs.prefs.getString(Env.accountPrefKey);

if (_accountString != null) {
_account = accountFromJson(_accountString);
if (accountString != null) {
_account = accountFromJson(accountString);
}
}

/// Delete account saved in the shared pref
Future<void> _deleteAccount() async {
await cevSharedPref.init();
cevSharedPref.prefs.remove(Env.accountPrefKey);
appProvider.cevSharedPrefs.prefs.remove(Env.accountPrefKey);
_logoutInitiated = true;
}

/// Initializes the authenticated [CEVAccount].
Future<bool> initBeforeOnboardingPage() async {
Future<bool> initBeforeHomePage() async {
await Future.wait([getAccount()]);

if (_account != null) {
_isLoggedIn = true;
generateDetails(notify: true);
_fetchNode();
}
Expand Down
13 changes: 9 additions & 4 deletions lib/common/providers/application_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class CEVApplicationProvider extends ChangeNotifier {
}

Future<void> _initialize(context) async {
await cevSharedPrefs.init();

initLogger();
_log.fine("initializing");
// set application model references
Expand All @@ -64,11 +66,10 @@ class CEVApplicationProvider extends ChangeNotifier {
// charmev shared preferences
// Initialize the shared preference
cevSharedPrefs.init(),
accountProvider.initBeforeOnboardingPage(),
]);
chargeProvider.generateDetails(notify: true);

if (accountProvider.isLoggedIn) {
if (await accountProvider.isLoggedIn) {
_authenticated = true;
notifyListeners();
initAuthenticated();
Expand All @@ -84,7 +85,9 @@ class CEVApplicationProvider extends ChangeNotifier {
_log.fine("on initialized");
// use to initiate rust log in lib
peerProvider.initLog().then((v) {
chargeProvider.processPendingTransactionsFromDB();
if (authenticated) {
chargeProvider.processPendingTransactionsFromDB();
}
});

bool hasTransaction = await chargeProvider.hasPendingTransaction();
Expand All @@ -106,5 +109,7 @@ class CEVApplicationProvider extends ChangeNotifier {

// / Called when initializing and already authenticated or
// / in the [CEVAuthProvider] after authenticating for the first time.
void initAuthenticated() {}
void initAuthenticated() {
accountProvider.initBeforeHomePage();
}
}
5 changes: 2 additions & 3 deletions lib/common/providers/charge_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import 'package:peaq_network_ev_charging_message_format/p2p_message_format.pb.da
as msg;

class CEVChargeProvider with ChangeNotifier {
CEVChargeProvider({required this.cevSharedPref, required this.db});
CEVChargeProvider({required this.db});

final CEVSharedPref cevSharedPref;
final CEVTransactionDB db;

late CEVApplicationProvider appProvider;
Expand Down Expand Up @@ -227,7 +226,7 @@ class CEVChargeProvider with ChangeNotifier {
setStatus(LoadingStatus.error, message: Env.invalidProviderDid);
}

setStatus(LoadingStatus.error, message: Env.fetchingData);
setStatus(LoadingStatus.loading, message: Env.fetchingData);

var address = did.split(":")[2];

Expand Down
2 changes: 0 additions & 2 deletions lib/common/providers/peer_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ Timer runPeriodically(void Function() callback) =>

class CEVPeerProvider with ChangeNotifier {
CEVPeerProvider({
required this.cevSharedPref,
required this.db,
});

final CEVSharedPref cevSharedPref;
final CEVTransactionDB db;

late CEVApplicationProvider appProvider;
Expand Down
12 changes: 3 additions & 9 deletions lib/common/providers/providers_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ class CEVProvidersWrapperState extends State<CEVProvidersWrapper> {
Widget build(BuildContext context) {
final serviceProvider = CEVServiceProvider.of(context);

accountProvider = CEVAccountProvider(
cevSharedPref: serviceProvider!.data.cevSharedPref,
);
accountProvider = CEVAccountProvider();

chargeProvider = CEVChargeProvider(
cevSharedPref: serviceProvider.data.cevSharedPref,
db: serviceProvider.data.transactionDB);
chargeProvider = CEVChargeProvider(db: serviceProvider!.data.transactionDB);

peerProvider = CEVPeerProvider(
cevSharedPref: serviceProvider.data.cevSharedPref,
db: serviceProvider.data.transactionDB);
peerProvider = CEVPeerProvider(db: serviceProvider.data.transactionDB);

applicationProvider = CEVApplicationProvider(context,
cevSharedPrefs: serviceProvider.data.cevSharedPref,
Expand Down
2 changes: 1 addition & 1 deletion lib/common/providers/service_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CEVServiceProvider extends InheritedWidget {
}

@override
bool updateShouldNotify(CEVServiceProvider old) {
bool updateShouldNotify(CEVServiceProvider oldWidget) {
// service provider shouldn't rebuild
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/common/utils/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void initLogger({String? prefix}) {
"${rec.loggerName}$separator"
"${_colored(rec.message, color)}";

print(logString);
// print(logString);
});
}

Expand Down
13 changes: 10 additions & 3 deletions lib/common/widgets/buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CEVRaisedButton extends StatelessWidget {
this.bgColor,
this.iconSize,
this.isIconRight = false,
this.clipText = false,
this.spacing,
this.padding,
this.textSize,
Expand All @@ -37,6 +38,7 @@ class CEVRaisedButton extends StatelessWidget {
final MaterialStateProperty<double>? elevation;
final void Function()? onPressed;
final double? radius;
final bool clipText;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -86,15 +88,20 @@ class CEVRaisedButton extends StatelessWidget {
Widget buildText() {
return (text.isNotEmpty)
? Flexible(
flex: 6,
child: Text(
text,
clipText
? text.characters
.replaceAll(Characters(''), Characters('\u{200B}'))
.toString()
: text,
overflow: clipText ? TextOverflow.ellipsis : TextOverflow.visible,
style: TextStyle(
fontSize: textSize ?? 18.0,
color: textColor ?? Colors.black,
fontWeight: isTextBold ? FontWeight.bold : FontWeight.normal,
letterSpacing: 0),
),
flex: 6,
)
: const SizedBox();
}
Expand Down Expand Up @@ -183,6 +190,7 @@ class CEVFlatButton extends StatelessWidget {
Widget buildText() {
return (text.isEmpty)
? Flexible(
flex: 6,
child: Text(
text,
style: TextStyle(
Expand All @@ -191,7 +199,6 @@ class CEVFlatButton extends StatelessWidget {
fontWeight: isTextBold ? FontWeight.bold : FontWeight.normal,
letterSpacing: 0),
),
flex: 6,
)
: const SizedBox();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/common/widgets/status_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CEVStatusCard extends StatelessWidget {

@override
Widget build(BuildContext context) {
print("CEVStatusCard progress:: $progress");
// print("CEVStatusCard progress:: $progress");
// String flareUrl = MDImageAssets.loader;
return GestureDetector(
onTap: status == LoadingStatus.loading ? null : onTap!,
Expand Down
Loading

0 comments on commit 6bc4a1c

Please sign in to comment.