Last active
April 15, 2017 07:39
-
-
Save tonyjs/5bc7aa7914daeb0ab164c8d6d3d3c11f to your computer and use it in GitHub Desktop.
BasePage by Conductor
This file contains hidden or 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
package com.tonyjs.sample | |
import android.animation.ObjectAnimator | |
import android.content.Context | |
import android.content.pm.PackageManager | |
import android.os.Build | |
import android.os.Bundle | |
import android.support.annotation.ColorRes | |
import android.support.annotation.LayoutRes | |
import android.support.annotation.StringRes | |
import android.support.v4.content.ContextCompat | |
import android.support.v7.app.ActionBar | |
import android.support.v7.app.AppCompatActivity | |
import android.support.v7.widget.Toolbar | |
import android.view.LayoutInflater | |
import android.view.MenuItem | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.inputmethod.InputMethodManager | |
import com.bluelinelabs.conductor.Controller | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.disposables.Disposable | |
/** | |
* Created by junseon on 2017. 3. 20.. | |
*/ | |
abstract class BasePage(args: Bundle?) : Controller(args) { | |
constructor() : this(null) | |
init { | |
retainViewMode = RetainViewMode.RETAIN_DETACH | |
} | |
open var statusBarColorResId = -1 | |
@ColorRes get | |
@ColorRes set | |
val disposables = CompositeDisposable() | |
fun addDisposable(disposable: Disposable) = disposables.add(disposable) | |
override fun onAttach(view: View) { | |
super.onAttach(view) | |
if (statusBarColorResId != -1) { | |
setStatusBarColor(statusBarColorResId) | |
} | |
} | |
@get:LayoutRes | |
abstract val pageLayoutResId: Int | |
abstract fun onViewInflated(root: View) | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View { | |
val view = inflater.inflate(pageLayoutResId, container, false) | |
onViewInflated(view) | |
return view | |
} | |
fun isFinished(): Boolean { | |
val activity = activity | |
return activity == null || activity.isFinishing | |
} | |
fun setupToolbar(toolbar: Toolbar) { | |
val activity = activity | |
if (activity is AppCompatActivity) { | |
activity.setSupportActionBar(toolbar) | |
} | |
} | |
fun showSoftInput(view: View) { | |
applicationContext?.let { | |
view.requestFocus() | |
view.post { | |
(it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager) | |
.showSoftInput(view, 0) | |
} | |
} | |
} | |
fun hideSoftInput(view: View) { | |
applicationContext?.let { | |
view.requestFocus() | |
view.post { | |
(it.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager) | |
.hideSoftInputFromWindow(view.windowToken, 0) | |
} | |
} | |
} | |
fun getString(@StringRes resId: Int): String { | |
return activity?.getString(resId) ?: "" | |
} | |
val actionBar: ActionBar? | |
get() { | |
activity?.let { | |
if (it is AppCompatActivity) { | |
return it.supportActionBar | |
} | |
} | |
return null | |
} | |
fun hasPermission(permission: String): Boolean { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
return applicationContext?.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED | |
} | |
return true | |
} | |
fun requestPermission(permission: String, requestCode: Int) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
requestPermissions(arrayOf(permission), requestCode) | |
} | |
} | |
fun setStatusBarColor(@ColorRes colorRes: Int) { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | |
return | |
} | |
if (statusBarColorResId != colorRes) { | |
statusBarColorResId = colorRes | |
} | |
activity?.let { | |
val color = ContextCompat.getColor(it, colorRes) | |
val window = it.window | |
val oldColor = window.statusBarColor | |
if (color != oldColor) { | |
ObjectAnimator.ofArgb(oldColor, color).apply { | |
duration = 300 | |
addUpdateListener { | |
window.statusBarColor = it.animatedValue as Int | |
} | |
}.start() | |
// window.statusBarColor = color | |
} | |
} | |
} | |
fun setWindowBackground(@ColorRes colorRes: Int) { | |
activity?.window?.setBackgroundDrawableResource(colorRes) | |
} | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
if (item.itemId == android.R.id.home) { | |
router.popController(this) | |
return true | |
} | |
return super.onOptionsItemSelected(item) | |
} | |
override fun onDestroyView(view: View) { | |
disposables.dispose() | |
disposables.clear() | |
super.onDestroyView(view) | |
} | |
} |
This file contains hidden or 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
class UserPage : BasePage() { | |
override val pageLayoutResId: Int | |
get() = R.layout.page_user | |
override fun onViewInflated(root: View) { | |
root.list.layoutManager = LinearLayoutManager(activity).apply { | |
isAutoMeasureEnabled = true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment