daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Feb 19th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.jetredsbytes.togglclient.presentation.switcher
  2.  
  3. import android.view.View
  4. import android.view.ViewGroup
  5. import com.jetredsbytes.togglclient.R
  6. import dagger.android.DaggerActivity
  7. import kotlinx.android.synthetic.main.error_screen.view.*
  8.  
  9.  
  10. class StateViewSwitcher(activity: DaggerActivity) {
  11.     private lateinit var mainView: View
  12.     private val states = HashMap<StateView, ViewInfo>()
  13.     private val layoutInflater = activity.layoutInflater
  14.     private lateinit var container: ViewGroup
  15.  
  16.     fun initMainView(view: View) = apply {
  17.         this.mainView = view
  18.         this.container = mainView.parent as ViewGroup
  19.         states[StateView.STATE_MAIN] = ViewInfo(0, mainView)
  20.     }
  21.  
  22.     fun switchToError(errorMessage: String, listener: StateSwitcherEvent.OnErrorEvent) {
  23.         val errorView = layoutInflater.inflate(R.layout.error_screen, container, false)
  24.         errorView.tvError.text = errorMessage
  25.         errorView.btnRetry.setOnClickListener {
  26.             listener.onRetryButtonClick()
  27.         }
  28.         addViewState(StateView.STATE_ERROR, errorView)
  29.         switchToError()
  30.     }
  31.  
  32.     fun switchToLoading() {
  33.         val loadingView = layoutInflater.inflate(R.layout.loading_screen, container, false)
  34.         addViewState(StateView.STATE_LOADING, loadingView)
  35.         switchToState(StateView.STATE_LOADING)
  36.         show(loadingView)
  37.     }
  38.  
  39.     fun switchToMain() = switchToState(StateView.STATE_MAIN)
  40.  
  41.     private fun switchToEmpty() = switchToState(StateView.STATE_EMPTY)
  42.  
  43.     private fun switchToSearchResultsEmpty() = switchToState(StateView.STATE_NOT_FOUND)
  44.  
  45.     private fun switchToError() = switchToState(StateView.STATE_ERROR)
  46.  
  47.     private fun inflateStateView(layout: Int) = layoutInflater.inflate(layout, container, false)
  48.  
  49.     private fun addViewState(state: StateView, stateView: View) {
  50.         stateView.visibility = View.GONE
  51.         states[state] = ViewInfo(0, stateView)
  52.     }
  53.  
  54.     private fun switchToState(state: StateView) {
  55.         val viewInfo = states[state] ?: return
  56.         if (viewInfo.view == null) {
  57.             viewInfo.view = layoutInflater.inflate(viewInfo.layoutId, container, false)
  58.         }
  59.         val nextView = viewInfo.view
  60.         if (nextView == null || nextView == mainView) return
  61.         if (nextView.parent != container) container.addView(nextView)
  62.         show(nextView)
  63.     }
  64.  
  65.     private fun show(nextView: View) {
  66.         mainView.visibility = View.GONE
  67.         nextView.visibility = View.VISIBLE
  68.         mainView = nextView
  69.     }
  70.  
  71.     private class ViewInfo(var layoutId: Int, var view: View?)
  72.  
  73. }
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