Skip to content

Commit a7555ee

Browse files
committed
refactor: pubnub 4
1 parent efa7d2c commit a7555ee

18 files changed

+224
-132
lines changed

ios/Podfile.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- emoji_picker (0.0.3):
2+
- emoji_picker_flutter (0.0.1):
33
- Flutter
44
- Flutter (1.0.0)
55
- shared_preferences (0.0.1):
@@ -8,14 +8,14 @@ PODS:
88
- Flutter
99

1010
DEPENDENCIES:
11-
- emoji_picker (from `.symlinks/plugins/emoji_picker/ios`)
11+
- emoji_picker_flutter (from `.symlinks/plugins/emoji_picker_flutter/ios`)
1212
- Flutter (from `Flutter`)
1313
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)
1414
- url_launcher (from `.symlinks/plugins/url_launcher/ios`)
1515

1616
EXTERNAL SOURCES:
17-
emoji_picker:
18-
:path: ".symlinks/plugins/emoji_picker/ios"
17+
emoji_picker_flutter:
18+
:path: ".symlinks/plugins/emoji_picker_flutter/ios"
1919
Flutter:
2020
:path: Flutter
2121
shared_preferences:
@@ -24,7 +24,7 @@ EXTERNAL SOURCES:
2424
:path: ".symlinks/plugins/url_launcher/ios"
2525

2626
SPEC CHECKSUMS:
27-
emoji_picker: 0e868059aa18f9473d234f3d0701fbd4d5fd310c
27+
emoji_picker_flutter: 4f9a1ac31d355e1bf9622a1e821a809149130127
2828
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
2929
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d
3030
url_launcher: 6fef411d543ceb26efce54b05a0a40bfd74cbbef

lib/main.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
2-
import 'package:flutter_dotenv/flutter_dotenv.dart' as DotEnv;
32
import 'package:provider/provider.dart';
3+
import 'package:flutter_dotenv/flutter_dotenv.dart' as dotenv;
44

55
import 'providers/pubnub_instance.dart';
66
import 'providers/app_data.dart';
@@ -12,7 +12,7 @@ import 'screens/conversation.dart';
1212
void main() async {
1313
WidgetsFlutterBinding.ensureInitialized();
1414
await AppData.init();
15-
await DotEnv.load(fileName: '.env');
15+
await dotenv.load();
1616

1717
runApp(MyApp());
1818
}

lib/providers/app_data.dart

+18-18
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ class AppData {
1616
'space_efc5b3ef96ea4bffbde733e249c8cbb3'
1717
};
1818
static const INDICATORTIMEOUT = 10;
19-
static List<UserProfile> _users;
20-
static UserProfile _currentUser;
21-
static List<Space> _spaces;
22-
static List<Space> _directChats;
23-
static List<ChatMessage> _conversations;
24-
static UserProfile get currentUser => _currentUser;
25-
static Space get defaultSpace => _spaces.first;
26-
static List<Space> get spaces => _spaces;
27-
static List<Space> get directChats => _directChats;
28-
static List<Space> get allSpaces => [..._spaces, ..._directChats];
29-
static List<UserProfile> get users => [..._users];
30-
static List<ChatMessage> get conversations => _conversations;
19+
static List<UserProfile>? _users;
20+
static UserProfile? _currentUser;
21+
static List<Space>? _spaces;
22+
static List<Space>? _directChats;
23+
static List<ChatMessage>? _conversations;
24+
static UserProfile? get currentUser => _currentUser;
25+
static Space? get defaultSpace => _spaces!.first;
26+
static List<Space>? get spaces => _spaces;
27+
static List<Space>? get directChats => _directChats;
28+
static List<Space>? get allSpaces => [..._spaces!, ..._directChats!];
29+
static List<UserProfile>? get users => [..._users!];
30+
static List<ChatMessage>? get conversations => _conversations;
3131

3232
static init() async {
3333
const String DIRECTORY = 'assets/setup';
@@ -50,7 +50,7 @@ class AppData {
5050

5151
_directChats = directs.map((space) => Space.fromJson(space)).toList();
5252

53-
_currentUser = _users[Random().nextInt(_users.length - 4)];
53+
_currentUser = _users![Random().nextInt(_users!.length - 4)];
5454

5555
_conversations = chatMessages
5656
.map((conversation) => ChatMessage.fromJson(conversation))
@@ -59,15 +59,15 @@ class AppData {
5959

6060
static UserProfile getUserById(String uuid) {
6161
if (uuid == 'current_user') {
62-
return currentUser;
62+
return currentUser!;
6363
}
64-
return users.firstWhere((user) => (user.uuid == uuid));
64+
return users!.firstWhere((user) => (user.uuid == uuid));
6565
}
6666

67-
static Space getSpaceById(String id) {
67+
static Space getSpaceById(String? id) {
6868
if (id == null) {
69-
return defaultSpace;
69+
return defaultSpace!;
7070
}
71-
return allSpaces.firstWhere((space) => space.id == id);
71+
return allSpaces!.firstWhere((space) => space.id == id);
7272
}
7373
}

lib/providers/messages.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ export 'models.dart';
1010
class MessageProvider with ChangeNotifier {
1111
final PubNub pubnub;
1212
final Subscription subscription;
13-
List<ChatMessage> _messages;
13+
late List<ChatMessage> _messages;
1414

1515
List<ChatMessage> get messages =>
1616
([..._messages]..sort((m1, m2) => m2.timetoken.compareTo(m1.timetoken)))
1717
.toList();
1818

1919
MessageProvider._(this.pubnub, this.subscription) {
20-
_messages = [...AppData.conversations];
20+
_messages = [...AppData.conversations!];
2121

2222
subscription.messages.listen((m) {
2323
if (m.messageType == MessageType.normal) {
2424
_addMessage(ChatMessage(
25-
timetoken: m.originalMessage['p']['t'],
25+
timetoken: '${m.publishedAt}',
2626
channel: m.channel,
2727
uuid: m.uuid.value,
2828
message: m.content));

lib/providers/models.dart

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import 'package:flutter/material.dart' show required;
2-
31
class ChatMessage {
42
final String timetoken;
53
final String channel;
64
final String uuid;
75
final Map message;
86

97
ChatMessage(
10-
{@required this.timetoken,
11-
@required this.channel,
12-
@required this.uuid,
13-
@required this.message});
8+
{required this.timetoken,
9+
required this.channel,
10+
required this.uuid,
11+
required this.message});
1412

1513
factory ChatMessage.fromJson(Map json) => ChatMessage(
1614
timetoken: json['timetoken'],
@@ -21,12 +19,11 @@ class ChatMessage {
2119

2220
class Space {
2321
final String name;
24-
final Map custom;
25-
final String description;
22+
final Map? custom;
23+
final String? description;
2624
final String id;
2725

28-
Space(
29-
{@required this.name, @required this.id, this.description, this.custom});
26+
Space({required this.name, required this.id, this.description, this.custom});
3027

3128
factory Space.fromJson(Map json) => Space(
3229
name: json['name'],
@@ -36,12 +33,12 @@ class Space {
3633
}
3734

3835
class UserProfile {
39-
final String name;
36+
final String? name;
4037
final String uuid;
41-
final String profileUrl;
42-
final Map custom;
38+
final String? profileUrl;
39+
final Map? custom;
4340

44-
UserProfile({@required this.uuid, this.profileUrl, this.name, this.custom});
41+
UserProfile({required this.uuid, this.profileUrl, this.name, this.custom});
4542

4643
factory UserProfile.fromJson(Map json) => UserProfile(
4744
uuid: json['id'],
@@ -54,5 +51,5 @@ class Signal {
5451
final String message;
5552
final String sender;
5653

57-
Signal({@required this.message, @required this.sender});
54+
Signal({required this.message, required this.sender});
5855
}

lib/providers/presence.dart

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class PresenceProvider with ChangeNotifier {
2020
chatSubscription.presence.listen((presenceEvent) {
2121
switch (presenceEvent.action) {
2222
case PresenceAction.join:
23-
_addOnlineUser(presenceEvent.uuid.value);
23+
_addOnlineUser(presenceEvent.uuid!.value);
2424
break;
2525
case PresenceAction.leave:
2626
case PresenceAction.timeout:
27-
_removeOnlineUser(presenceEvent.uuid.value);
27+
_removeOnlineUser(presenceEvent.uuid!.value);
2828
break;
2929
case PresenceAction.stateChange:
3030
break;
@@ -34,11 +34,13 @@ class PresenceProvider with ChangeNotifier {
3434
}
3535
if (presenceEvent.join.length > 0) {
3636
_onlineUsers.removeAll(presenceEvent.leave
37-
.where((id) => id.value != AppData.currentUser.uuid)
37+
.where((id) => id.value != AppData.currentUser!.uuid)
3838
.map((uuid) => uuid.value));
3939
}
4040
notifyListeners();
4141
break;
42+
default:
43+
break;
4244
}
4345
});
4446
}
@@ -48,8 +50,7 @@ class PresenceProvider with ChangeNotifier {
4850
var result = await pubnub.hereNow(channelGroups: {AppData.CHANNELGROUP});
4951
_onlineUsers.addAll(result.channels.values
5052
.expand((c) => c.uuids.values)
51-
.map((uuid) => uuid.value)
52-
.toSet());
53+
.map((occupantInfo) => occupantInfo.uuid));
5354
}
5455

5556
void _addOnlineUser(String uuid) {
@@ -58,7 +59,7 @@ class PresenceProvider with ChangeNotifier {
5859
}
5960

6061
void _removeOnlineUser(String uuid) {
61-
if (uuid != AppData.currentUser.uuid) {
62+
if (uuid != AppData.currentUser!.uuid) {
6263
_onlineUsers.remove((uuid));
6364
notifyListeners();
6465
}

lib/providers/pubnub_instance.dart

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import 'dart:async';
2+
import 'package:flutter_dotenv/flutter_dotenv.dart' as dotenv;
3+
4+
import 'package:pubnub/networking.dart';
25
import 'package:pubnub/pubnub.dart';
3-
import 'package:flutter_dotenv/flutter_dotenv.dart' as DotEnv;
46
import 'app_data.dart';
57

68
class PubNubInstance {
7-
PubNub _pubnub;
8-
Subscription _subscription;
9+
late PubNub _pubnub;
10+
late Subscription _subscription;
911

1012
PubNub get instance => _pubnub;
1113

1214
Subscription get subscription => _subscription;
1315

14-
static Timer _heartbeatTimer;
16+
static Timer? _heartbeatTimer;
1517

1618
PubNubInstance() {
1719
_pubnub = PubNub(
20+
networking: NetworkingModule(retryPolicy: RetryPolicy.exponential()),
1821
defaultKeyset: Keyset(
19-
subscribeKey: DotEnv.env['PUBNUB_SUBSCRIBE_KEY'],
20-
publishKey: DotEnv.env['PUBNUB_PUBLISH_KEY'],
21-
uuid: UUID(AppData.currentUser.uuid)));
22+
subscribeKey: dotenv.env['PUBNUB_SUBSCRIBE_KEY']!,
23+
publishKey: dotenv.env['PUBNUB_PUBLISH_KEY'],
24+
uuid: UUID(AppData.currentUser!.uuid)));
2225
_pubnub.channelGroups
2326
.addChannels(AppData.CHANNELGROUP, AppData.CHANNELS)
2427
.then((result) {});
@@ -42,8 +45,8 @@ class PubNubInstance {
4245
}
4346

4447
stopHeartbeats() {
45-
if (_heartbeatTimer != null && _heartbeatTimer.isActive) {
46-
_heartbeatTimer.cancel();
48+
if (_heartbeatTimer != null && _heartbeatTimer!.isActive) {
49+
_heartbeatTimer!.cancel();
4750
_heartbeatTimer = null;
4851
}
4952
}

lib/screens/conversation.dart

+12-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ class _ConversationState extends State<Conversation> {
2222
@override
2323
Widget build(BuildContext context) {
2424
messageProvider = Provider.of<MessageProvider>(context, listen: false);
25-
final space = AppData.getSpaceById(
26-
ModalRoute.of(context).settings.arguments as String);
25+
var spaceId = ModalRoute.of(context)!.settings.arguments != null
26+
? ModalRoute.of(context)!.settings.arguments as String
27+
: null;
28+
final space = AppData.getSpaceById(spaceId);
2729

2830
return Scaffold(
2931
key: _scaffoldKey,
@@ -44,15 +46,15 @@ class _ConversationState extends State<Conversation> {
4446
IconButton(
4547
icon: Icon(Icons.people_outline),
4648
onPressed: () {
47-
_scaffoldKey.currentState.openEndDrawer();
49+
_scaffoldKey.currentState!.openEndDrawer();
4850
})
4951
],
5052
leading: Row(
5153
children: [
5254
IconButton(
5355
icon: Icon(Icons.arrow_back_ios_rounded),
5456
onPressed: () {
55-
_scaffoldKey.currentState.openDrawer();
57+
_scaffoldKey.currentState!.openDrawer();
5658
}),
5759
],
5860
),
@@ -71,4 +73,10 @@ class _ConversationState extends State<Conversation> {
7173
},
7274
child: ChatBody(space)));
7375
}
76+
77+
@override
78+
void dispose() {
79+
Provider.of<MessageProvider>(context, listen: false).dispose();
80+
super.dispose();
81+
}
7482
}

lib/widgets/chat_title.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ class ChatTitle extends StatelessWidget {
1313
children: [
1414
Column(
1515
crossAxisAlignment: CrossAxisAlignment.start,
16-
mainAxisAlignment: space.description.isNotEmpty
16+
mainAxisAlignment: space.description!.isNotEmpty
1717
? MainAxisAlignment.center
1818
: MainAxisAlignment.start,
1919
children: [
2020
Text(
2121
space.name,
2222
style: TextStyle(color: Colors.black),
2323
),
24-
space.description.isNotEmpty
25-
? Text(space.description,
24+
space.description!.isNotEmpty
25+
? Text(space.description!,
2626
style: Theme.of(context)
2727
.textTheme
28-
.caption
28+
.caption!
2929
.apply(color: Colors.grey))
3030
: Container()
3131
],

lib/widgets/drawer.dart

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class AppDrawer extends StatelessWidget {
1212
Container(
1313
height: 100,
1414
child: AppDrawerHeader(
15-
userName: AppData.currentUser.name,
16-
description: AppData.currentUser.custom['title'],
17-
profileUrl: AppData.currentUser.profileUrl,
15+
userName: AppData.currentUser!.name!,
16+
description: AppData.currentUser!.custom!['title'],
17+
profileUrl: AppData.currentUser!.profileUrl!,
1818
),
1919
),
2020
const SizedBox(height: 20),
@@ -24,8 +24,8 @@ class AppDrawer extends StatelessWidget {
2424
style: TextStyle(color: Colors.grey, fontWeight: FontWeight.bold)),
2525
),
2626
...List.generate(
27-
AppData.spaces.length,
28-
(index) => DrawerListItem(AppData.spaces[index]),
27+
AppData.spaces!.length,
28+
(index) => DrawerListItem(AppData.spaces![index]),
2929
),
3030
const Divider(),
3131
Container(
@@ -34,8 +34,8 @@ class AppDrawer extends StatelessWidget {
3434
style: const TextStyle(
3535
color: Colors.grey, fontWeight: FontWeight.bold)),
3636
),
37-
...List.generate(AppData.directChats.length,
38-
(index) => DrawerListItem(AppData.directChats[index]))
37+
...List.generate(AppData.directChats!.length,
38+
(index) => DrawerListItem(AppData.directChats![index]))
3939
]);
4040
}
4141
}

0 commit comments

Comments
 (0)