Skip to content

Commit

Permalink
Merge pull request #36 from Ishad-M-I-M/implementation/homepage-refresh
Browse files Browse the repository at this point in the history
added feature to reload home screen on pull down
  • Loading branch information
Ishad-M-I-M authored Nov 20, 2022
2 parents e0ff93c + 7489675 commit 36fe3e0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 45 deletions.
22 changes: 22 additions & 0 deletions lib/bloc/initialize/initialize_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:developer';

import 'package:bloc/bloc.dart';
import 'package:iblock/bloc/settings/settings_bloc.dart';
import 'package:local_auth/local_auth.dart';
import 'package:meta/meta.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
Expand Down Expand Up @@ -99,5 +100,26 @@ class InitializeBloc extends Bloc<InitializeEvent, InitializeState> {
emit(NotRegistered());
}
});

on<LoadUserInfoEvent>((event, emit) async{
emit(Loading());
try {
var privateKey = await SecureStorageService.get("private-key") as String;
var contractAddress = await SecureStorageService.get("contract-address") as String;
var userInfo = await UserContractService().getAll(
contractAddress, privateKey).timeout(const Duration(seconds: 10),
onTimeout: (){
throw TimeoutException("Unable to fetch data from blockchain!");
});
emit(Registered(userInfo));
}
catch(e){
emit(Failed(e.toString()));
}
});

on<SetUserInfoEvent>((event, emit){
emit(Registered(event.userInfo));
});
}
}
9 changes: 8 additions & 1 deletion lib/bloc/initialize/initialize_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ class CheckInternetConnection extends InitializeEvent{}

class AuthenticateEvent extends InitializeEvent{}

class CheckStatus extends InitializeEvent{}
class CheckStatus extends InitializeEvent{}

class LoadUserInfoEvent extends InitializeEvent{}

class SetUserInfoEvent extends InitializeEvent{
final Map<String, String> userInfo;
SetUserInfoEvent(this.userInfo);
}
2 changes: 2 additions & 0 deletions lib/bloc/initialize/initialize_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Authenticated extends InitializeState{}

class NoInternetConnection extends InitializeState{}

class Loading extends InitializeState{}

class Registered extends InitializeState {
final Map<String, String> userInfo;
Registered(this.userInfo);
Expand Down
124 changes: 80 additions & 44 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../bloc/initialize/initialize_bloc.dart';

class HomePage extends StatefulWidget {
HomePage({Key? key}) : super(key: key);
Expand All @@ -8,49 +11,88 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
Map<String, String> data = {};
final InitializeBloc _initializeBloc = InitializeBloc();

@override
void initState() {
try {
_initializeBloc.add(SetUserInfoEvent(
ModalRoute.of(context)!.settings.arguments as Map<String, String>));
} catch (e) {
_initializeBloc.add(LoadUserInfoEvent());
}
super.initState();
}

@override
Widget build(BuildContext context) {
data = ModalRoute.of(context)!.settings.arguments as Map<String, String>;
return Scaffold(
appBar: AppBar(
actions: [
IconButton(onPressed: () => Navigator.pushNamed(context, '/history'), icon: const Icon(Icons.history_rounded)),
IconButton(onPressed: () => Navigator.pushNamed(context, '/settings'), icon: const Icon(Icons.settings))
]
),
body: SafeArea(
child: SingleChildScrollView(
child: SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image(
image: Image.asset('assets/images/profile.png').image,
width: 100,
height: 100,
appBar: AppBar(actions: [
IconButton(
onPressed: () => Navigator.pushNamed(context, '/history'),
icon: const Icon(Icons.history_rounded)),
IconButton(
onPressed: () => Navigator.pushNamed(context, '/settings'),
icon: const Icon(Icons.settings))
]),
body: SafeArea(
child: GestureDetector(
onPanStart: (details){
_initializeBloc.add(LoadUserInfoEvent());
},
child: SingleChildScrollView(
child: SizedBox(
width: double.infinity,
child: BlocProvider(
create: (context) => _initializeBloc,
child: BlocBuilder<InitializeBloc, InitializeState>(
builder: (context, state) {
if (state is Registered) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image(
image:
Image.asset('assets/images/profile.png').image,
width: 100,
height: 100,
),
Padding(
padding: const EdgeInsets.all(10),
child: Text(
state.userInfo["Name"]!,
style: const TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
)),
Table(
children: state.userInfo.entries
.map((e) => TableRow(children: [
Padding(
padding: const EdgeInsets.all(10),
child: Text(e.key,
style: const TextStyle(
fontWeight:
FontWeight.bold))),
Padding(
padding: const EdgeInsets.all(10),
child: Text(e.value))
]))
.toList())
],
);
} else if (state is Loading) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
return const Center(
child: Text("Unexpected State"),
);
}
},
),
),
Padding(
padding: const EdgeInsets.all(10),
child: Text(
data["Name"]!,
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
)),
Table(
children:
data.entries.map((e) => TableRow(children: [
Padding(
padding: const EdgeInsets.all(10),
child: Text(e.key,
style: const TextStyle(fontWeight: FontWeight.bold))),
Padding(
padding: const EdgeInsets.all(10), child: Text(e.value))
])).toList()
)
],
),
),
),
),
),
Expand All @@ -63,10 +105,4 @@ class _HomePageState extends State<HomePage> {
)
);
}

void addNewProperty(String property, String value){
setState(() {
data[property] = value;
});
}
}

0 comments on commit 36fe3e0

Please sign in to comment.