Skip to content

Instantly share code, notes, and snippets.

@alifgiant
Last active July 20, 2021 08:21
Show Gist options
  • Save alifgiant/e7c605d0dacdc8d82798bc4199747a7a to your computer and use it in GitHub Desktop.
Save alifgiant/e7c605d0dacdc8d82798bc4199747a7a to your computer and use it in GitHub Desktop.
MVP example with scheduler
interface RefillProvider {
   fun refill()
}

class PersonPresenter {
   var coffee = EmptyCoffeeCup()
   val shopper = ShopController() // si OB
   val person = PersonController() // otak si Bos
   
   var refillProvider: RefillProvider?
   
   val coffeEmptyCheckScheduler = Scheduler(every = 10_MINUTE) {
      if (coffee.isEmpty()){
         refillProvider.refill() // to run animation on view
      }
   }
   
   fun refill() {
      val money = person.getMoney(15000)
      coffee = shopper.makeCoffee(money)
   }
   
   init {
      coffeEmptyCheckScheduler.run()
   }
}

class PersonView : RefillProvider {   
   val personPresenter = PersonPresenter()
   var coffeRefilloading = false
   
   init {
      personPresenter.provider = this
   }
   
   fun refill() {
      onRefillClick()
   }
   
   fun onRefillClick() {
      coffeRefilloading = true
      personPresenter.refill()
      coffeRefilloading = false
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment