Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. public class CurrentUser {
  2. private static final CurrentUser ourInstance = new CurrentUser();
  3.  
  4. private CurrentUserInterface mInterface;
  5.  
  6. private CurrentUser() {
  7. }
  8.  
  9. public static CurrentUser getInstance() {
  10. return ourInstance;
  11. }
  12.  
  13. public void setUserName(String userName) {
  14. if (mInterface != null) {
  15. mInterface.setUserName(userName);
  16. }
  17. }
  18.  
  19. public void setInterface(CurrentUserInterface mInterface) {
  20. this.mInterface = mInterface;
  21. }
  22.  
  23. public interface CurrentUserInterface {
  24. void setUserName(String username);
  25. }
  26. }
  27.  
  28. public class UserViewModel extends AndroidViewModel implements CurrentUser.CurrentUserInterface {
  29.  
  30. private MutableLiveData<String> userName;
  31.  
  32. public UserViewModel(@NonNull Application application) {
  33. super(application);
  34. CurrentUser.getInstance().setInterface(this); // <-- Is this a good practice ??
  35. }
  36.  
  37. public LiveData<String> getUserName() {
  38. if (userName == null) {
  39. userName = new MutableLiveData<>();
  40. loadUser("username","password");
  41. }
  42. return userName;
  43. }
  44.  
  45. @Override
  46. public void setUserName(String username) {
  47. if (this.userName == null) {
  48. return;
  49. }
  50.  
  51. this.userName.postValue(username);
  52. }
  53.  
  54. private void loadUser(String username, String password) {
  55. Intent service = new Intent(context, HttpService.class);
  56.  
  57. service.putExtra(HTTP_SERVICE_LOGIN, true);
  58.  
  59. service.putExtra(HTTP_SERVICE_LOGIN_USERNAME, username);
  60. service.putExtra(HTTP_SERVICE_LOGIN_PASSWORD, password);
  61.  
  62. context.startService(service);
  63. }
  64. }
  65.  
  66. public class HttpService extends IntentService {
  67.  
  68. // Login constants
  69. public static final String HTTP_SERVICE_LOGIN = "HTTP_SERVICE_LOGIN";
  70. public static final String HTTP_SERVICE_LOGIN_USERNAME = "HTTP_SERVICE_LOGIN_USERNAME";
  71. public static final String HTTP_SERVICE_LOGIN_PASSWORD = "HTTP_SERVICE_LOGIN_PASSWORD";
  72.  
  73. @Override
  74. protected void onHandleIntent(@Nullable Intent intent) {
  75. if (intent != null) {
  76. if (intent.getBooleanExtra(HTTP_SERVICE_LOGIN, false)) {
  77.  
  78. String username = intent.getStringExtra(HTTP_SERVICE_LOGIN_USERNAME);
  79. String password = intent.getStringExtra(HTTP_SERVICE_LOGIN_PASSWORD);
  80.  
  81. loginAction(username, password);
  82. }
  83. }
  84. }
  85.  
  86. private void loginAction(String username, String password) {
  87. // Async call to http server - this part is working fine
  88. // Then
  89. CurrentUser.getInstance().setUserName("User connected");
  90. }
  91. }
  92.  
  93. public class MainActivity {
  94.  
  95. @Override
  96. protected void onCreate(Bundle savedInstanceState) {
  97. /// initialization code
  98.  
  99. final TextView tv = navigationView.getHeaderView(0).findViewById(R.id.textViewName);
  100.  
  101. UserViewModel model = ViewModelProviders.of(this).get(UserViewModel.class);
  102. model.getUserName().observe(this, new Observer<String>() {
  103. @Override
  104. public void onChanged(@Nullable String s) {
  105. tv.setText(s);
  106. }
  107. });
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement