6. “As You Like It” / W. Shakespeare
״All the world’s a stage,
And all the men and women merely players;
They have their exits and their entrances,
And one man in his time plays many parts:״
7. His acts being seven ages. At first, the
infant,
Mewling and puking in the nurse’s arms.
Then the whining schoolboy, with his
satchel
And shining morning face, creeping like
snail
Unwillingly to school. And then the lover,
Sighing like furnace, with a woeful ballad
Made to his mistress’ eyebrow. Then a
soldier,
Full of strange oaths and bearded like the
pard,
Jealous in honor, sudden and quick in
quarrel,
Seeking the bubble reputation
Even in the cannon’s mouth. And then the
justice,
In fair round belly with good capon lined,
With eyes severe and beard of formal cut,
Full of wise saws and modern instances;
And so he plays his part. The sixth age
shifts
Into the lean and slippered pantaloon,
With spectacles on nose and pouch on side;
His youthful hose, well saved, a world too
wide
For his shrunk shank, and his big manly
voice,
Turning again toward childish treble, pipes
And whistles in his sound. Last scene of
all,
That ends this strange eventful history,
Is second childishness and mere oblivion,
Sans teeth, sans eyes, sans taste, sans
everything.
21. Data Layer - Chat Module
let‘s talk about the data structure.
22. Firebase DB
- Easy.
- noSQL
- Flat
- Get data by reference
- ref/messages/chat_one_id
23. Don’t - Nest
{
"chats": {
"chatOneId": {
"title": "As You Like It",
"messages": {
"msg1Id": { "sender": "melancholyJaques", "message": "All world’s a stage." },
"msg2Id": { ... },
// a very long list of messages
}
},
"chatTwoId": { ... }
}
}
24. Do - Flat : Chats
{
"chats": {
"chatOneId": {
"title": "As You Like It"
},
"chatTwoId": { ... },
"chatThreeId": { ... }
}
27. Do - Flat : Messages
"messages": {
"chatOneId": {
"msg1Id": {
"sender": "melancholyJaques",
"message": "All world’s a stage.",
"timestamp": 1459361875337
},
"msg2Id": { ... },
// a very long list of messages
},
"chatTwoId": { ... },
}
}
28. On my code
Chat module: individual separate pieces:
- Chats Repository
- Members Repository
- Users Repository
- Messages Repository
46. Get Chat Preview List 2
Too messy. Too many listeners and threads. Too much time.
Chat
Client
Users
Repo
Chat
list
activity
Chats
Repo
userId chatIds
Msgs
Repo
Get
chats
preview
list
47. Get Chat Preview List 3
Add data straight to chat Info node
"chats": {
"chatOneId": {
"title": "As You Like It"
"lastMessage": "All world’s a stage"
},
"chatTwoId": { ... },
"chatThreeId": { ... }
},
48. Get Chat Preview List 3
Too messy. Too many listeners and threads. Too much time.
Chat
Client
Users
Repo
Chat
list
activity
Chats
Repo
userId chatIdsGet
chats
preview
list
55. Personal Chat Info Node
{
"personalInfo": {
"melancholyJaques": {
"lastMessage": All world’s a stage",
"timestamp": 1459361875337
},
...
},
56. Get Chat Preview List 4
Too messy. Too many listeners and threads. Too much time.
Chat
Client
Users
Repo
Chat
list
activity
userId chatIdsGet
chats
preview
list
personal
Info
Repo
general
Info
Repo
57. Send : Update Per User
App
Module
Chat
Client
Msgs
Repo
userId chatIds
personal
Info
Repo
Send
Is visible?
58. But I have many recipients….
Takes long to update each user repo...
62. Sum Up
- Small defined use cases:
- Easy to maintain
- Easy to change
- Easy to test
- Mix and match
- “Play the part” → create POC class
- Encapsulate implementation
- Organize code
- Be mindful to what needs a change
65. FCM Usages
- Notify user
- Update DB
- Update UI
- Fetch new data
- Start service
- ……...
66. How Does It Work?
public class MyFCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//...
}
}
68. FCM Handling
Type per message
RemoteMessage
Data
….
< “type” , “new_chat_message” >
….
69. FCM Handling
public class MyFCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String type = data.get(StaticKeys.KEY_TYPE);
switch (type) {
//....
}
}
}
70. It Worked Great...
We added up more and more events…..
- New chat message
- New member added
- Member removed
- New data available
- ………………..
71. …for some time :-/
- Too many event types
- Too complex code
- Hard to change
- Hard to combine
- Sometimes one usecase represented multiple event types and forced few fcm
messages
- The ugly switch-case...