Skip to content

Commit 7b489b5

Browse files
Replacing code blocks with tutorial placeholders
1 parent b7f0be7 commit 7b489b5

File tree

6 files changed

+26
-108
lines changed

6 files changed

+26
-108
lines changed

flutter_sdk_tutorial/lib/providers/friendly_names.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,9 @@ class FriendlyNamesProvider with ChangeNotifier {
3333
// We already have a name for this uuid
3434
return;
3535
}
36-
GetUuidMetadataResult lookupResult;
37-
try {
38-
lookupResult = await pubnub.objects.getUUIDMetadata(uuid: deviceId);
39-
} catch (e) {
40-
// Caught exception when looking up name
41-
return;
42-
}
43-
GetUuidMetadataResult result = lookupResult;
44-
groupMemberDeviceIds[deviceId] = result.metadata?.name;
36+
37+
// TUTORIAL: STEP 2I CODE GOES HERE (1/2)
38+
4539
notifyListeners();
4640
}
4741

flutter_sdk_tutorial/lib/providers/messages.dart

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,17 @@ class MessageProvider with ChangeNotifier {
2323
// When the application is first loaded, it is common to load any recent chat messages so the user
2424
// can get caught up with conversations they missed. Every application will handle this differently
2525
// but here we just load the 8 most recent messages
26-
var result = pubnub.batch.fetchMessages({AppState.channelName}, count: 8);
27-
result.then((batchHistoryResult) {
28-
if (batchHistoryResult.channels[AppState.channelName] != null) {
29-
List<BatchHistoryResultEntry> historyResults = batchHistoryResult
30-
.channels[AppState.channelName] as List<BatchHistoryResultEntry>;
31-
historyResults.forEach((element) async => {
32-
_addMessage(ChatMessage(
33-
timetoken: '${element.timetoken}',
34-
channel: AppState.channelName,
35-
uuid: element.uuid.toString(),
36-
message: element.message)),
37-
38-
// Look up the friendly names for any uuids found in the history
39-
await AppState.friendlyNames
40-
.lookupMemberName(element.uuid.toString())
41-
});
42-
}
43-
});
26+
27+
// TUTORIAL: STEP 2G CODE GOES HERE
4428

4529
// Applications receive various types of information from PubNub through a 'listener'
4630
// This application dynamically registers a listener when it comes to the foreground
47-
subscription.messages.listen((m) {
48-
if (m.messageType == MessageType.normal) {
49-
// A message is received from PubNub. This is the entry point for all messages on all
50-
// channels or channel groups, though this application only uses a single channel.
51-
_addMessage(ChatMessage(
52-
timetoken: '${m.publishedAt}',
53-
channel: m.channel,
54-
uuid: m.uuid.value,
55-
message: m.content));
56-
57-
// Interactive Demo only
58-
if (m.uuid.value != AppState.deviceId) {
59-
DemoInterface.actionCompleted(
60-
"Receive a message (You might need to open a new tab)");
61-
}
62-
} else if (m.messageType == MessageType.objects) {
63-
// Whenever Object meta data is changed, an Object event is received.
64-
// See: https://www.pubnub.com/docs/chat/sdks/users/setup
65-
// Use this to be notified when other users change their friendly names
66-
AppState.friendlyNames.replaceMemberName(
67-
m.payload['data']['id'].toString(),
68-
m.payload['data']['name'].toString());
69-
}
70-
});
31+
32+
// TUTORIAL: STEP 2D CODE GOES HERE (2/2)
33+
34+
// TUTORIAL: STEP 2E CODE GOES WITHIN THE CODE COPIED ABOVE
35+
36+
// TUTORIAL: STEP 2I CODE GOES WITHIN THE CODE COPIED ABOVE (2/2)
7137
}
7238
MessageProvider(PubNubInstance pn) : this._(pn.instance, pn.subscription);
7339

@@ -80,7 +46,7 @@ class MessageProvider with ChangeNotifier {
8046
}
8147

8248
sendMessage(String channel, String message) async {
83-
await pubnub.publish(channel, message);
49+
// TUTORIAL: STEP 2C CODE GOES HERE
8450

8551
// Interactive Demo only
8652
DemoInterface.actionCompleted("Send a message");

flutter_sdk_tutorial/lib/providers/presence.dart

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,10 @@ class PresenceProvider with ChangeNotifier {
4242
// the channel. This is similar to the earlier hereNow call but this API will only be
4343
// invoked when presence information changes, meaning you do NOT have to call hereNow
4444
// periodically. More info: https://www.pubnub.com/docs/sdks/kotlin/api-reference/presence
45-
chatSubscription.presence.listen((presenceEvent) {
46-
switch (presenceEvent.action) {
47-
case PresenceAction.join:
48-
_addOnlineUser(presenceEvent.uuid!.value);
49-
break;
50-
case PresenceAction.leave:
51-
case PresenceAction.timeout:
52-
_removeOnlineUser(presenceEvent.uuid!.value);
53-
break;
54-
case PresenceAction.stateChange:
55-
break;
56-
case PresenceAction.interval:
57-
// 'join' and 'leave' will work up to the ANNOUNCE_MAX setting (defaults to 20 users)
58-
// Over ANNOUNCE_MAX, an 'interval' message is sent. More info: https://www.pubnub.com/docs/presence/presence-events#interval-mode
59-
// The below logic requires that 'Presence Deltas' be defined for the keyset, you can do this from the admin dashboard
60-
if (presenceEvent.join.length > 0) {
61-
_onlineUsers.addAll(presenceEvent.join.map((uuid) => uuid.value));
62-
}
63-
if (presenceEvent.join.length > 0) {
64-
_onlineUsers.removeAll(presenceEvent.leave
65-
.where((id) => id.value != AppState.deviceId)
66-
.map((uuid) => uuid.value));
67-
}
68-
notifyListeners();
69-
break;
70-
default:
71-
break;
72-
}
73-
});
45+
46+
// TUTORIAL: STEP 2D CODE GOES HERE (1/2)
47+
48+
// TUTORIAL: STEP 2F CODE GOES WITHIN THE CODE COPIED ABOVE (1/2)
7449
}
7550
PresenceProvider(PubNubInstance pn) : this._(pn.instance, pn.subscription);
7651

@@ -84,10 +59,8 @@ class PresenceProvider with ChangeNotifier {
8459
// The API will return an array of occupants in the channel, defined by their
8560
// ID. This application will need to look up the friendly name defined for
8661
// each of these IDs (later)
87-
var result = await pubnub.hereNow(channels: {AppState.channelName});
88-
_onlineUsers.addAll(result.channels.values
89-
.expand((c) => c.uuids.values)
90-
.map((occupantInfo) => occupantInfo.uuid));
62+
63+
// TUTORIAL: STEP 2F CODE GOES HERE (2/2)
9164

9265
// Resolve the friendly names of everyone found in the chat
9366
for (var element in _onlineUsers) {

flutter_sdk_tutorial/lib/screens/conversation.dart

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,8 @@ class _ConversationState extends State<Conversation>
9595
// Note that the Dart API uses slightly different terminology / behaviour to unsubscribe / re-subscribe.
9696
// Note: This getting started application is set up to unsubscribe from all channels when the app goes into the background.
9797
// This is good to show the principles of presence but you don't need to do this in a production app if it does not fit your use case.
98-
@override
99-
void didChangeAppLifecycleState(AppLifecycleState state) {
100-
if (state == AppLifecycleState.paused) {
101-
// Mobile only: App has gone to the background
102-
messageProvider.pubnub.announceLeave(channels: {AppState.channelName});
103-
messageProvider.subscription.pause();
104-
} else if (state == AppLifecycleState.resumed) {
105-
// Mobile only: App has returned to the foreground
106-
messageProvider.subscription.resume();
107-
messageProvider.pubnub
108-
.announceHeartbeat(channels: {AppState.channelName});
109-
}
110-
}
98+
99+
// TUTORIAL: STEP 2B CODE GOES HERE (2/2)
111100

112101
@override
113102
void dispose() {

flutter_sdk_tutorial/lib/utils/pubnub_instance.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@ class PubNubInstance {
1111

1212
PubNubInstance() {
1313
// Create PubNub configuration and instantiate the PubNub object, used to communicate with PubNub
14-
_pubnub = PubNub(
15-
defaultKeyset: Keyset(
16-
subscribeKey: AppState.pubnubSubscribeKey,
17-
publishKey: AppState.pubnubPublishKey,
18-
userId: UserId(AppState.deviceId!)));
14+
15+
// TUTORIAL: STEP 2A CODE GOES HERE
1916

2017
// Subscribe to the pre-defined channel representing this chat group. This will allow us to receive messages
2118
// and presence events for the channel (what other users are in the room)
22-
_subscription =
23-
_pubnub.subscribe(channels: {AppState.channelName}, withPresence: true);
19+
20+
// TUTORIAL: STEP 2B CODE GOES HERE (1/2)
2421

2522
// In order to receive object UUID events (in the addListener) it is required to set our
2623
// membership using the Object API.

flutter_sdk_tutorial/lib/widgets/friendly_name.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ class _FriendlyNameWidget extends State<FriendlyNameWidget> {
7272
// Save the contents to PubNub object storage
7373
if (_controller.text.isNotEmpty) {
7474
setState(() => {_saveButtonText = "Edit", _enabled = false});
75-
var uuidMetadataInput = UuidMetadataInput(name: _controller.text);
76-
var result = await AppState.pubnub.instance.objects
77-
.setUUIDMetadata(uuidMetadataInput, uuid: AppState.deviceId);
75+
76+
// TUTORIAL: STEP 2H CODE GOES HERE
7877

7978
// Interactive Demo only
8079
DemoInterface.actionCompleted("Change your friendly name");

0 commit comments

Comments
 (0)