Skip to content

Instantly share code, notes, and snippets.

@miensol
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miensol/e7fcce936e0acb3499ec to your computer and use it in GitHub Desktop.
Save miensol/e7fcce936e0acb3499ec to your computer and use it in GitHub Desktop.
Keep current user information in custom application class - don't do it
if(MyApplication.getCurrentUser() != null){
userInfoStatus.setText(MyApplication.getCurrentUser().getFirstName());
}
package pl.brightinventions.bezpiecznyautobus;
import android.app.Application;
class User {}
public class MyApplication extends Application {
private static User currentUser;
public static User getCurrentUser(){
return currentUser;
}
public static void setCurrentUser(User user){
currentUser = user;
}
@Override
public void onCreate() {
super.onCreate();
}
}
package pl.application.login;
import pl.application.user.CurrentUserInfo;
import pl.application.user.User;
public interface CurrentUserInfo {
boolean isLoggedIn();
String getName();
}
@Singleton
class InMemorySession implements CurrentUserInfo {
private User user;
void setCurrentUser(User user){
user = user;
}
void reset(){
setCurrentUser(null);
}
@Override
public boolean isLoggedIn() {
return user != null;
}
@Override
public String getName() {
return user.getName();
}
}
// Component requesting information current user
public class AppBeahvior {
CurrentUserInfo _currentUserInfo;
@Inject
public AppBeahvior(CurrentUserInfo currentUserInfo){
_currentUserInfo = currentUserInfo;
}
public void doSomeWork(){
Log.d("AppBehavior", "Current user name is " + _currentUserInfo.getName());
}
}
public class User {
private static User currentUser;
public static User getCurrentUser() {
return currentUser;
}
public static boolean isLoggedIn() {
return currentUser != null;
}
static void setCurrentUser(User user) {
currentUser = user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment