Skip to content

Instantly share code, notes, and snippets.

@derekcsm
Last active August 29, 2015 14:15
Show Gist options
  • Save derekcsm/119c2e9c4459f78d057f to your computer and use it in GitHub Desktop.
Save derekcsm/119c2e9c4459f78d057f to your computer and use it in GitHub Desktop.
Psuedo code main activity & fragment core lifecycle and state design
MAIN ACTIVITY —
String sockToken;
String sockUrl;
ChannelFragment mChannelFragment;
onCreate(Bundle savedState) {
super.onCreate(savedState);
if (savedState != null) {
sockToken = savedState.getString(“sockToken”, “”);
sockUrl = savedState.getString(“sockUrl”, “”);
} else {
sockToken = null;
sockUrl = null;
// During initial setup, plug in the channel fragment.
mChannelFragment = new ChannelFragment();
mChannelFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
onResume() {
super.onResume();
if (AuthUtils.userIsAuthenticated()) {
instantiateWebSocket();
getEnvironment();
/*
will need more API research on this getEnvironment() idea
basically what this would do is get a list of all channel
objects and members, at the end there could be a statement
to loadInitialChatConversation in channel fragment on null
check
*/
} else {
// go to welcome screen, clear any saved data
}
}
onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(“sockToken”, sockToken);
outState.putString(“sockUrl”, sockUrl);
}
private void instantiateWebSocket() {
/*
If the sock variables are null we should POST to /sessions to retrieve new ones.
Afterwards make connection to socket server, and register listeners
*/
}
CHANNEL FRAGMENT —
String channelType;
int channelId;
String enteredMsgText;
onCreateView(Bundle savedState) {
super.onCreateView(savedState);
if (savedState != null) {
et_message.setText(savedState.getString(“curMsg”, “”);
channelType = savedState.getString(“curChannelType”, “”);
channelId = savedState.getInt(“curChannelId”, 0);
loadInitialChannelConversation(50);
}
/*
display loading animation in view, the main activity will
set channel variables and call loadInitialChannelConversation
once auth has been checked and channel data is grabbed
*/
}
onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(“curMsg”, enteredMsgText);
outState.putInt(“curChannelId”, channelId);
outState.putString(“curChannelType”, channelType);
}
public static void loadInitialChannelConversation(int msgLimit) {
/*
Would load initial channel convo from REST for the last msgLimit amount of messages.
Will also differentiate adapters later on based on channelType, adapters will then clear previous
messages and add all new ones, this should keep the socket in sync as well, may add some
methods to onDataAvailable to check if a received message is missed in the time since this call
is made.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment