BaseScreen kotlinerization

pull/595/head
olegivo 5 years ago
parent cc86eb54b8
commit f31a033082

@ -59,22 +59,23 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
* Notifies the screen that its contents should be updated. * Notifies the screen that its contents should be updated.
*/ */
fun invalidate() { fun invalidate() {
if (rootView == null) return rootView?.invalidate()
rootView!!.invalidate()
} }
fun invalidateToolbar() { fun invalidateToolbar() {
if (rootView == null) return rootView?.let { root ->
activity.runOnUiThread { activity.runOnUiThread {
val toolbar = rootView!!.getToolbar() val toolbar = root.getToolbar()
activity.setSupportActionBar(toolbar) activity.setSupportActionBar(toolbar)
val actionBar = activity.supportActionBar ?: return@runOnUiThread activity.supportActionBar?.let { actionBar ->
actionBar.setDisplayHomeAsUpEnabled(rootView!!.displayHomeAsUp) actionBar.setDisplayHomeAsUpEnabled(root.displayHomeAsUp)
val color = rootView!!.getToolbarColor() val color = root.getToolbarColor()
setActionBarColor(actionBar, color) setActionBarColor(actionBar, color)
setStatusBarColor(color) setStatusBarColor(color)
} }
} }
}
}
/** /**
* Called when another Activity has finished, and has returned some result. * Called when another Activity has finished, and has returned some result.
@ -114,10 +115,11 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
fun setRootView(rootView: BaseRootView?) { fun setRootView(rootView: BaseRootView?) {
this.rootView = rootView this.rootView = rootView
activity.setContentView(rootView) activity.setContentView(rootView)
if (rootView == null) return rootView?.let {
rootView.onAttachedToScreen(this) it.onAttachedToScreen(this)
invalidateToolbar() invalidateToolbar()
} }
}
/** /**
* Sets the menu to be shown when a selection is active on the screen. * Sets the menu to be shown when a selection is active on the screen.
@ -135,26 +137,28 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
*/ */
fun showMessage(@StringRes stringId: Int?) { fun showMessage(@StringRes stringId: Int?) {
if (stringId == null || rootView == null) return if (stringId == null || rootView == null) return
if (snackbar == null) { (snackbar?.setText(stringId)
snackbar = Snackbar.make(rootView!!, stringId, Snackbar.LENGTH_SHORT) ?: run {
val snack = Snackbar.make(rootView!!, stringId, Snackbar.LENGTH_SHORT)
val tvId = R.id.snackbar_text val tvId = R.id.snackbar_text
val tv = snackbar!!.view.findViewById<View>(tvId) as TextView val tv = snack.view.findViewById<View>(tvId) as TextView
tv.setTextColor(Color.WHITE) tv.setTextColor(Color.WHITE)
} else snackbar!!.setText(stringId) this.snackbar = snack
snackbar!!.show() snack
}).show()
} }
fun showSendEmailScreen(@StringRes toId: Int, fun showSendEmailScreen(@StringRes toId: Int, @StringRes subjectId: Int, content: String?) {
@StringRes subjectId: Int,
content: String?) {
val to = activity.getString(toId) val to = activity.getString(toId)
val subject = activity.getString(subjectId) val subject = activity.getString(subjectId)
val intent = Intent() val intent = Intent()
intent.action = Intent.ACTION_SEND .apply {
intent.type = "message/rfc822" action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(to)) type = "message/rfc822"
intent.putExtra(Intent.EXTRA_SUBJECT, subject) putExtra(Intent.EXTRA_EMAIL, arrayOf(to))
intent.putExtra(Intent.EXTRA_TEXT, content) putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, content)
}
activity.startActivity(intent) activity.startActivity(intent)
} }
@ -162,10 +166,12 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
val file = File(archiveFilename) val file = File(archiveFilename)
val fileUri = FileProvider.getUriForFile(activity, "org.isoron.uhabits", file) val fileUri = FileProvider.getUriForFile(activity, "org.isoron.uhabits", file)
val intent = Intent() val intent = Intent()
intent.action = Intent.ACTION_SEND .apply {
intent.type = "application/zip" action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_STREAM, fileUri) type = "application/zip"
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION putExtra(Intent.EXTRA_STREAM, fileUri)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
activity.startActivity(intent) activity.startActivity(intent)
} }
@ -192,13 +198,14 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
} }
private inner class ActionModeWrapper : ActionMode.Callback { private inner class ActionModeWrapper : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean =
item: MenuItem?): Boolean { if (item == null || selectionMenu == null) {
return if (item == null || selectionMenu == null) false else selectionMenu!!.onItemClicked(item) false
} else {
selectionMenu!!.onItemClicked(item)
} }
override fun onCreateActionMode(mode: ActionMode?, override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
menu: Menu?): Boolean {
if (selectionMenu == null) return false if (selectionMenu == null) return false
if (mode == null || menu == null) return false if (mode == null || menu == null) return false
selectionMenu!!.onCreate(activity.menuInflater, mode, menu) selectionMenu!!.onCreate(activity.menuInflater, mode, menu)
@ -206,13 +213,14 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
} }
override fun onDestroyActionMode(mode: ActionMode?) { override fun onDestroyActionMode(mode: ActionMode?) {
if (selectionMenu == null) return selectionMenu?.onFinish()
selectionMenu!!.onFinish()
} }
override fun onPrepareActionMode(mode: ActionMode?, override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean =
menu: Menu?): Boolean { if (selectionMenu == null || menu == null) {
return if (selectionMenu == null || menu == null) false else selectionMenu!!.onPrepare(menu) false
} else {
selectionMenu!!.onPrepare(menu)
} }
} }
@ -221,21 +229,18 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
@Deprecated("") @Deprecated("")
fun getDefaultActionBarColor(context: Context): Int { fun getDefaultActionBarColor(context: Context): Int {
return if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) { return if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
ResourcesCompat.getColor(context.resources, ResourcesCompat.getColor(context.resources, R.color.grey_900, context.theme)
R.color.grey_900, context.theme)
} else { } else {
val res = StyledResources(context) StyledResources(context).getColor(R.attr.colorPrimary)
res.getColor(R.attr.colorPrimary)
} }
} }
@JvmStatic @JvmStatic
@Deprecated("") @Deprecated("")
fun setupActionBarColor(activity: AppCompatActivity, fun setupActionBarColor(activity: AppCompatActivity, color: Int) {
color: Int) { activity.findViewById<Toolbar>(R.id.toolbar)?.let { toolbar ->
val toolbar = activity.findViewById<View>(R.id.toolbar) as Toolbar ?: return
activity.setSupportActionBar(toolbar) activity.setSupportActionBar(toolbar)
val actionBar = activity.supportActionBar ?: return activity.supportActionBar?.let { actionBar ->
actionBar.setDisplayHomeAsUpEnabled(true) actionBar.setDisplayHomeAsUpEnabled(true)
val drawable = ColorDrawable(color) val drawable = ColorDrawable(color)
actionBar.setBackgroundDrawable(drawable) actionBar.setBackgroundDrawable(drawable)
@ -243,10 +248,10 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
val darkerColor = mixColors(color, Color.BLACK, 0.75f) val darkerColor = mixColors(color, Color.BLACK, 0.75f)
activity.window.statusBarColor = darkerColor activity.window.statusBarColor = darkerColor
toolbar.elevation = dpToPixels(activity, 2f) toolbar.elevation = dpToPixels(activity, 2f)
var view = activity.findViewById<View>(R.id.toolbarShadow) activity.findViewById<View>(R.id.toolbarShadow)?.visibility = View.GONE
if (view != null) view.visibility = View.GONE activity.findViewById<View>(R.id.headerShadow)?.visibility = View.GONE
view = activity.findViewById(R.id.headerShadow) }
if (view != null) view.visibility = View.GONE }
} }
} }
} }

Loading…
Cancel
Save