Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dao page implementation #5

Open
wants to merge 8 commits into
base: development_13.6.4
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:tfchain_client/models/dao.dart';
import 'package:tfchain_client/tfchain_client.dart';

Future<Map<String, List<Proposal>?>> getProposals() async {
try {
final client = QueryClient("wss://tfchain.dev.grid.tf/ws");
await client.connect();
final proposals = await client.dao.get();
await client.disconnect();
return proposals;
} catch (e) {
throw Exception("Error occurred: $e");
}
}
15 changes: 15 additions & 0 deletions threefold_connect/lib/features/dao page/data/get_farms_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:gridproxy_client/gridproxy_client.dart';
import 'package:gridproxy_client/models/farms.dart';
import 'package:threefold_connect/main.reflectable.dart';

Future<List<Farm>> getMyFarms(int twinId) async {
try {
initializeReflectable();
GridProxyClient client = GridProxyClient('https://gridproxy.dev.grid.tf/');
final farms =
await client.farms.list(ListFarmsQueryParameters(twin_id: twinId));
return farms;
} catch (e) {
throw Exception("Error occurred: $e");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:tfchain_client/generated/dev/types/pallet_dao/proposal/dao_votes.dart';
import 'package:tfchain_client/tfchain_client.dart';

Future<DaoVotes> getProposalVotes(String hash) async {
try {
final client = QueryClient("wss://tfchain.dev.grid.tf/ws");
await client.connect();
final votes = await client.dao.getProposalVotes(hash: hash);
await client.disconnect();
return votes;
} catch (e) {
throw Exception("Error occurred: $e");
}
}
18 changes: 18 additions & 0 deletions threefold_connect/lib/features/dao page/data/vote.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:tfchain_client/generated/dev/types/pallet_dao/proposal/dao_votes.dart';
import 'package:tfchain_client/tfchain_client.dart';

//TODO: mnemonic need to be changed with user mnemonic
const mnemonic = "";

Future<DaoVotes> vote(bool vote, String hash, int farmId) async {
try {
final client = Client("wss://tfchain.dev.grid.tf/ws", mnemonic, "sr25519");
client.connect();
final daoVotes =
await client.dao.vote(farmId: farmId, hash: hash, approve: vote);
await client.disconnect();
return daoVotes;
} catch (e) {
throw Exception("Error occurred: $e");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'package:flutter/material.dart';
import 'package:tfchain_client/models/dao.dart';
import 'package:threefold_connect/theme/theme.dart';
import 'package:threefold_connect/widgets/app_bar.dart';
import '../../data/get_dao_proposals.dart';
import '../widgets/active_executable_widget.dart';

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

@override
State<DaoPage> createState() => _DaoPageState();
}

class _DaoPageState extends State<DaoPage> {
List<Proposal>? activeList = [];
List<Proposal>? inactiveList = [];

void setActiveList() async {
final proposals = await getProposals();
setState(() {
activeList = proposals['activeProposals'];
inactiveList = proposals['inactiveProposals'];
});
}

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

@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: const MyAppBar(title: 'Dao'),
body: Column(
children: [
PreferredSize(
preferredSize: const Size.fromHeight(50.0),
child: Container(
color: Theme.of(context).scaffoldBackgroundColor,
child: TabBar(
labelColor: Theme.of(context).colorScheme.primary,
indicatorColor: Theme.of(context).colorScheme.primary,
unselectedLabelColor: white,
dividerColor: Theme.of(context).scaffoldBackgroundColor,
tabs: const [
Tab(text: 'Active'),
Tab(text: 'Executable'),
],
),
),
),
Expanded(
child: TabBarView(
children: [
ActiveOrExecutableWidget(proposals: activeList),
ActiveOrExecutableWidget(
proposals: inactiveList,
),
],
),
),
],
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'package:flutter/material.dart';
import 'package:tfchain_client/models/dao.dart';

import '../../../../theme/theme.dart';
import 'dao_card.dart';

class ActiveOrExecutableWidget extends StatefulWidget {
List<Proposal>? proposals;
ActiveOrExecutableWidget({super.key, required this.proposals});

@override
State<ActiveOrExecutableWidget> createState() =>
_ActiveOrExecutableWidgetState();
}

class _ActiveOrExecutableWidgetState extends State<ActiveOrExecutableWidget> {
List<Proposal>? proposals = [];

@override
void initState() {
proposals = widget.proposals;
super.initState();
}

@override
void didUpdateWidget(covariant ActiveOrExecutableWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.proposals != oldWidget.proposals) {
setState(() {
proposals = widget.proposals;
});
}
}

void search(String searchWord) {
setState(() {
final String filterText = searchWord.toLowerCase();
if (searchWord == "") {
setState(() {
proposals = widget.proposals;
});
} else if (widget.proposals != null) {
setState(() {
proposals = widget.proposals
?.where((Proposal entry) =>
entry.action.toLowerCase().contains(filterText))
.toList();
});
}
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(60.0),
child: Padding(
padding: const EdgeInsets.all(10),
child: SizedBox(
height: 40,
child: SearchBar(
onChanged: search,
trailing: const <Widget>[
Icon(
Icons.search,
color: grey,
)
],
backgroundColor: MaterialStateProperty.all<Color>(
Theme.of(context).colorScheme.secondary),
hintText: "Search by proposal description",
hintStyle: MaterialStateProperty.all<TextStyle>(
TextStyle(color: grey, fontFamily: inter, fontSize: 14)),
textStyle: MaterialStateProperty.all<TextStyle>(
TextStyle(color: grey, fontFamily: inter, fontSize: 14)),
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
),
),
),
),
),
body: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: _buildDaoCardList(proposals) ??
[const Text('No active proposal at the moment')],
),
),
);
}
}

List<DaoCard>? _buildDaoCardList(List<Proposal>? list) {
return list?.map((item) {
return DaoCard(
proposal: item,
);
}).toList();
}
Loading