wiring contacts into provider model

This commit is contained in:
erinn 2021-02-09 17:36:12 -08:00
parent 0fea363e4d
commit 387203b9ef
8 changed files with 88 additions and 62 deletions

View File

@ -6,7 +6,6 @@ import 'package:flutter_app/model.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'package:path/path.dart' as path;
import 'package:provider/provider.dart';
import 'cwtch.dart';

View File

@ -1,17 +1,16 @@
import 'dart:convert';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'dart:collection';
import 'cwtch/cwtch.dart';
import 'main.dart';
////////////////////
/// UI State ///
////////////////////
//todo: delete
class ProfileModel {
String onion;
String nickname;
@ -20,6 +19,7 @@ class ProfileModel {
HashMap<String, ContactModel> contacts;
}
//todo: delete
class ContactModel {
String onion;
String nickname;
@ -32,9 +32,13 @@ class ContactModel {
ContactModel({this.onion, this.nickname, this.status, this.isInvitation, this.isBlocked, this.imagePath});
}
//todo: delete
class DanMessageModel {
// ignore: non_constant_identifier_names
String Timestamp;
// ignore: non_constant_identifier_names
bool Acknowledged;
// ignore: non_constant_identifier_names
String Message;
}
@ -81,6 +85,24 @@ class ContactListState extends ChangeNotifier {
List<ContactInfoState> _onions = [];
int get num => _onions.length;
ContactListState(Cwtch cwtch, String profileOnion) {
cwtch.GetContacts(profileOnion).then((jsonStr) {
List<dynamic> contacts = jsonDecode(jsonStr);
contacts.forEach((onion) {
add(ContactInfoState(
profileOnion: profileOnion,
onion: onion,
nickname: "fay quen",
isGroup: false,
isInvitation: false,
isBlocked: false,
status: "",
imagePath: "",
));
});
});
}
void addAll(Iterable<ContactInfoState> newOnions) {
_onions.addAll(newOnions);
notifyListeners();
@ -158,6 +180,18 @@ class ContactInfoState extends ChangeNotifier {
notifyListeners();
}
get isInvitation => this._isInvitation;
set isInvitation(bool newVal) {
this._isInvitation = newVal;
notifyListeners();
}
get status => this._status;
set status(String newVal) {
this._status = newVal;
notifyListeners();
}
get unreadMessages => this._unreadMessages;
set unreadMessages(int newVal) {
this._unreadMessages = newVal;

View File

@ -9,8 +9,7 @@ import 'messageview.dart';
import '../model.dart';
class ContactsView extends StatefulWidget {
const ContactsView({Key key, this.profile}) : super(key: key);
final ProfileInfoState profile;
const ContactsView({Key key}) : super(key: key);
@override
_ContactsViewState createState() => _ContactsViewState();
@ -18,28 +17,31 @@ class ContactsView extends StatefulWidget {
class _ContactsViewState extends State<ContactsView> {
_ContactsViewState();
Map<String, ContactModel> _contacts = new HashMap<String, ContactModel>();
// Map<String, ContactModel> _contacts = new HashMap<String, ContactModel>();
@override
void didChangeDependencies() {
super.didChangeDependencies();
Provider.of<FlwtchState>(context).cwtch.GetContacts(widget.profile.onion).then((jsonContacts) {
print("got contact: $jsonContacts");
setState(() {
List<dynamic> contacts = jsonDecode(jsonContacts);
contacts.forEach((onion) {
_contacts.putIfAbsent(onion['onion'], () => ContactModel(onion: onion['onion'], nickname: onion['name'], status: onion['status']));
});
});
});
}
// @override
// void didChangeDependencies() {
// super.didChangeDependencies();
//
// Provider.of<ContactListState>(context).onions.forEach((contact) {
// _contacts.putIfAbsent(contact.onion, () => ContactModel(contact);
// });
// .cwtch.GetContacts(widget.profile.onion).then((jsonContacts) {
// print("got contact: $jsonContacts");
// setState(() {
// List<dynamic> contacts = jsonDecode(jsonContacts);
// contacts.forEach((onion) {
// _contacts.putIfAbsent(onion['onion'], () => ContactModel(onion: onion['onion'], nickname: onion['name'], status: onion['status']));
// });
// });
// });
// }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.profile.nickname + '\'s contacts'),
title: Text(Provider.of<ProfileInfoState>(context).nickname + '\'s contacts'),
actions: [
IconButton(icon: Icon(Icons.copy), onPressed: _copyOnion,),
],
@ -57,8 +59,8 @@ class _ContactsViewState extends State<ContactsView> {
return StreamBuilder<String>(
stream: Provider.of<FlwtchState>(context).appStatus.contactEvents(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
final tiles = _contacts.values.map(
(ContactModel contact) {
final tiles = Provider.of<ContactListState>(context).onions.map(
(ContactInfoState contact) {
return ListTile(
leading: SizedBox(
width: 60,
@ -103,7 +105,7 @@ class _ContactsViewState extends State<ContactsView> {
builder: (BuildContext context) {
return Provider(
create: (_) => Provider.of<FlwtchState>(context),
child: MessageView(profile: widget.profile, conversationHandle: handle),
child: MessageView(profile: Provider.of<ProfileInfoState>(context), conversationHandle: handle),
);
},
),

View File

@ -1,7 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_app/views/profilemgrview.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import '../main.dart';
import 'contactsview.dart';
@ -21,7 +19,7 @@ class _DoubleColumnViewState extends State<DoubleColumnView> {
children: <Widget>[
Flexible(
flex: flwtch.columns[0],
child: ContactsView(profile: flwtch.selectedProfile),
child: ContactsView(),
),
Flexible(
flex: flwtch.columns[1],

View File

@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_app/main.dart';
import 'package:flutter_app/opaque.dart';
import 'package:provider/provider.dart';

View File

@ -1,11 +1,6 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_app/cwtch/cwtch.dart';
import 'package:provider/provider.dart';
import '../main.dart';
import '../model.dart';
import '../opaque.dart';
import '../widgets/messagelist.dart';
@ -64,27 +59,27 @@ class _MessageViewState extends State<MessageView> {
),
SizedBox(
width: 100,
height: 80,
child: Column(
children: <Widget>[
ElevatedButton(
child: Icon(Icons.send, color: Opaque.current().mainTextColor()),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Opaque.current().defaultButtonColor()),
), onPressed: _sendMessage,
),
Row (
children: <Widget>[
SizedBox(width:45, child:ElevatedButton(
child: Icon(Icons.emoji_emotions_outlined, color: Opaque.current().mainTextColor())
)),
SizedBox(width:45, child:ElevatedButton(
child: Icon(Icons.attach_file, color: Opaque.current().mainTextColor())
)),
]
)
]
),
height: 80,
child: Column(
children: <Widget>[
ElevatedButton(
child: Icon(Icons.send, color: Opaque.current().mainTextColor()),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Opaque.current().defaultButtonColor()),
), onPressed: _sendMessage,
),
Row (
children: <Widget>[
SizedBox(width:45, child:ElevatedButton(
child: Icon(Icons.emoji_emotions_outlined, color: Opaque.current().mainTextColor())
)),
SizedBox(width:45, child:ElevatedButton(
child: Icon(Icons.attach_file, color: Opaque.current().mainTextColor())
)),
]
)
]
),
),
],
),

View File

@ -58,9 +58,12 @@ class _ProfileRowState extends State<ProfileRow> {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return Provider(
create: (_) => Provider.of<FlwtchState>(context),
child: includeDoublePane ? DoubleColumnView() : ContactsView(profile:profile),
return MultiProvider(
providers: [
ChangeNotifierProvider<ProfileInfoState>(create: (_) => profile),
ChangeNotifierProvider<ContactListState>(create: (_) => ContactListState(Provider.of<FlwtchState>(context).cwtch, profile.onion),),
],
builder: (context, widget) => includeDoublePane ? DoubleColumnView() : ContactsView(),
);
},
),

View File

@ -1,10 +1,6 @@
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../main.dart';
import '../model.dart';
class TorStatusLabel extends StatefulWidget {
@override