SHARE
TWEET

Untitled

a guest Jun 27th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PacksFragment : BaseCompatFragment() {
  2.  
  3.     @Inject
  4.     lateinit var presenter: PacksFragmentPresenter
  5.  
  6.     override fun onCreateView(
  7.        ...
  8.     }
  9.  
  10.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  11.         ...
  12.         LibApp.get().injector.inject(this)
  13.         ...
  14.  
  15.         //Working good
  16.         val userData = ViewModelProviders.of(this).get(UserViewModel::class.java).getData()
  17.         val user = userData.value
  18.  
  19.         //Correct value logged
  20.         Timber.e(user?.coins.toString())
  21.     }
  22.  
  23.     fun updateToolbar() {
  24.         Timber.e("Loaded?")
  25.  
  26.         //"Loaded?" logged, so method called. Next line is reason of Error
  27.         val userData = ViewModelProviders.of(this).get(UserViewModel::class.java).getData()
  28.         presenter.openNewPack(packId)
  29.     }
  30. }
  31.      
  32. class PacksFragmentPresenter @Inject constructor(
  33.     private val packsFragment: PacksFragment,
  34.     private val getCoinsFromUserCase: GetCoinsFromUserCase
  35. ) {
  36.  
  37.     fun openNewPack(packId: Int) {
  38.         if (getCoinsFromUserCase.getCoinsFromUser()){
  39.             packsFragment.updateToolbar()
  40.         }
  41.     }
  42. }
  43.      
  44. class GetCoinsFromUserCase {
  45.  
  46.     fun getCoinsFromUser(): Boolean {
  47.         val userViewModel = UserViewModel()
  48.         userViewModel.takeCoins(10)
  49.         return true
  50.     }
  51. }
  52.      
  53. class UserViewModel : ViewModel(), UserApi {
  54.     private val data = MutableLiveData<User>()
  55.  
  56.     fun setData(user: User) {
  57.         //Logged fine
  58.         Timber.e("User setted")
  59.         data.value = user
  60.     }
  61.  
  62.     fun getData(): LiveData<User> {
  63.         if (data.value == null) {
  64.             val user = User()
  65.             user.coins = 200
  66.             data.value = user
  67.         }
  68.         //Logged fine, "false"
  69.         Timber.e("User getted")
  70.         Timber.e("Is user == null? %s", (data.value == null).toString())
  71.         return data
  72.     }
  73.  
  74.     override fun takeCoins(value: Int) {
  75.           //Specially commented it
  76. //        getCoins(value)
  77.     }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top