Last active
August 29, 2015 14:23
-
-
Save miensol/e7fcce936e0acb3499ec to your computer and use it in GitHub Desktop.
Keep current user information in custom application class - don't do it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(MyApplication.getCurrentUser() != null){ | |
userInfoStatus.setText(MyApplication.getCurrentUser().getFirstName()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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