Created
November 16, 2018 16:42
-
-
Save dariopellegrini/2659ae7e112d60808938c2298b610448 to your computer and use it in GitHub Desktop.
Android Conductor base controller
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
abstract class BaseController: Controller(), LayoutContainer { | |
private var _containerView: View? = null | |
override val containerView: View? | |
get() = _containerView | |
protected abstract fun inflateView(inflater: LayoutInflater, container: ViewGroup): View | |
var toolbar: Toolbar? = null | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View { | |
val view = inflateView(inflater, container).also { | |
context = it.context | |
_containerView = it | |
} | |
view.findViewById<Toolbar>(R.id.toolbar)?.let { | |
toolbar = it | |
} | |
onViewBound(view) | |
return view | |
} | |
protected open fun onViewBound(view: View) { | |
} | |
override fun onAttach(view: View) { | |
super.onAttach(view) | |
toolbar?.let { setSupportActionBar(it) } | |
setTitle() | |
if (router.backstackSize > 1) { | |
toolbar?.setNavigationIcon(R.drawable.back_button) | |
toolbar?.setNavigationOnClickListener { | |
router.pop() | |
} | |
} | |
} | |
protected fun getActionBar(): ActionBar? { | |
val actionBarProvider = activity as ActionBarProvider? | |
return actionBarProvider?.supportActionBar | |
} | |
protected fun setTitle() { | |
val title = getTitle() ?: "" | |
toolbar?.let { | |
it.title = title | |
} | |
} | |
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { | |
inflater.inflate(R.menu.menu_empty, menu) | |
} | |
protected open fun getTitle(): String? { | |
return null | |
} | |
fun getString(id: Int): String { | |
return activity?.getString(id) ?: "" | |
} | |
fun getString(id: Int, vararg formatArgs: String): String { | |
return activity?.getString(id, *formatArgs) ?: "" | |
} | |
// Keyboard | |
fun hideKeyboard() { | |
activity?.let { | |
activity -> | |
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager | |
//Find the currently focused view, so we can grab the correct window token from it. | |
var view = activity.currentFocus | |
//If no view currently has focus, create a new one, just so we can grab a window token from it | |
if (view == null) { | |
view = View(activity) | |
} | |
imm.hideSoftInputFromWindow(view.windowToken, 0) | |
} | |
} | |
fun showKeyboard() { | |
activity?.let { | |
activity -> | |
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager | |
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0) | |
} | |
} | |
fun setSupportActionBar(toolbar: Toolbar) { | |
(activity as AppCompatActivity).setSupportActionBar(toolbar) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment