Remove BaseRootView

pull/699/head
Alinson S. Xavier 5 years ago
parent b42ad0dd83
commit a58c60b8ce

@ -1,63 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.androidbase.activities
import android.content.Context
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.widget.Toolbar
import org.isoron.androidbase.R
import org.isoron.androidbase.utils.InterfaceUtils.dpToPixels
import org.isoron.androidbase.utils.StyledResources
/**
* Base class for all root views in the application.
*
*
* A root view is an Android view that is directly attached to an activity. This
* view usually includes a toolbar and a progress bar. This abstract class hides
* some of the complexity of setting these things up, for every version of
* Android.
*/
abstract class BaseRootView(context: Context) : FrameLayout(context) {
var displayHomeAsUp = false
var screen: BaseScreen? = null
private set
open fun getToolbar(): Toolbar {
return findViewById(R.id.toolbar)
?: throw RuntimeException("Your BaseRootView should have a toolbar with id R.id.toolbar")
}
open fun getToolbarColor(): Int = StyledResources(context).getColor(R.attr.colorPrimary)
protected open fun initToolbar() {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
getToolbar().elevation = dpToPixels(context, 2f)
findViewById<View>(R.id.toolbarShadow)?.visibility = View.GONE
findViewById<View>(R.id.headerShadow)?.visibility = View.GONE
}
}
fun onAttachedToScreen(screen: BaseScreen?) {
this.screen = screen
}
}

@ -43,31 +43,8 @@ import java.io.*
*/ */
open class BaseScreen(@JvmField protected var activity: BaseActivity) { open class BaseScreen(@JvmField protected var activity: BaseActivity) {
private var rootView: BaseRootView? = null
private var snackbar: Snackbar? = null private var snackbar: Snackbar? = null
/**
* Notifies the screen that its contents should be updated.
*/
fun invalidate() {
rootView?.invalidate()
}
fun invalidateToolbar() {
rootView?.let { root ->
activity.runOnUiThread {
val toolbar = root.getToolbar()
activity.setSupportActionBar(toolbar)
activity.supportActionBar?.let { actionBar ->
actionBar.setDisplayHomeAsUpEnabled(root.displayHomeAsUp)
val color = root.getToolbarColor()
setActionBarColor(actionBar, 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.
* *
@ -85,19 +62,6 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
*/ */
open fun reattachDialogs() {} open fun reattachDialogs() {}
/**
* Sets the root view for this screen.
*
* @param rootView the root view for this screen.
*/
fun setRootView(rootView: BaseRootView?) {
this.rootView = rootView
activity.setContentView(rootView)
rootView?.let {
it.onAttachedToScreen(this)
invalidateToolbar()
}
}
/** /**
* Shows a message on the screen. * Shows a message on the screen.
@ -118,10 +82,6 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
snackbar.show() snackbar.show()
} }
fun showMessage(@StringRes stringId: Int?) {
showMessage(stringId, this.rootView)
}
fun showSendEmailScreen(@StringRes toId: Int, @StringRes subjectId: Int, content: String?) { fun showSendEmailScreen(@StringRes toId: Int, @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)
@ -144,14 +104,4 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) {
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}) })
} }
private fun setActionBarColor(actionBar: ActionBar, color: Int) {
val drawable = ColorDrawable(color)
actionBar.setBackgroundDrawable(drawable)
}
private fun setStatusBarColor(baseColor: Int) {
val darkerColor = mixColors(baseColor, Color.BLACK, 0.75f)
activity.window.statusBarColor = darkerColor
}
} }

@ -60,7 +60,7 @@ class ListHabitsActivity : HabitsActivity() {
setScreen(screen) setScreen(screen)
component.listHabitsBehavior.onStartup() component.listHabitsBehavior.onStartup()
setTitle(R.string.main_activity_title) setContentView(rootView)
} }
override fun onPause() { override fun onPause() {

@ -49,7 +49,7 @@ class ListHabitsRootView @Inject constructor(
runner: TaskRunner, runner: TaskRunner,
private val listAdapter: HabitCardListAdapter, private val listAdapter: HabitCardListAdapter,
habitCardListViewFactory: HabitCardListViewFactory habitCardListViewFactory: HabitCardListViewFactory
) : BaseRootView(context), ModelObservable.Listener { ) : FrameLayout(context), ModelObservable.Listener {
val listView: HabitCardListView = habitCardListViewFactory.create() val listView: HabitCardListView = habitCardListViewFactory.create()
val llEmpty = EmptyListView(context) val llEmpty = EmptyListView(context)
@ -63,7 +63,7 @@ class ListHabitsRootView @Inject constructor(
val hintList = hintListFactory.create(hints) val hintList = hintListFactory.create(hints)
hintView = HintView(context, hintList) hintView = HintView(context, hintList)
addView(RelativeLayout(context).apply { val rootView = RelativeLayout(context).apply {
background = sres.getDrawable(R.attr.windowBackgroundColor) background = sres.getDrawable(R.attr.windowBackgroundColor)
addAtTop(tbar) addAtTop(tbar)
addBelow(header, tbar) addBelow(header, tbar)
@ -73,18 +73,15 @@ class ListHabitsRootView @Inject constructor(
it.topMargin = dp(-6.0f).toInt() it.topMargin = dp(-6.0f).toInt()
} }
addAtBottom(hintView) addAtBottom(hintView)
if (SDK_INT < LOLLIPOP) { }
addBelow(ShadowView(context), tbar) rootView.setupToolbar(
addBelow(ShadowView(context), header) toolbar = tbar,
} title = resources.getString(R.string.main_activity_title),
}, MATCH_PARENT, MATCH_PARENT) color = PaletteColor(17),
displayHomeAsUpEnabled = false,
)
addView(rootView, MATCH_PARENT, MATCH_PARENT)
listAdapter.setListView(listView) listAdapter.setListView(listView)
initToolbar()
}
override fun getToolbar(): Toolbar {
return tbar
} }
override fun onModelChange() { override fun onModelChange() {

@ -39,6 +39,7 @@ import org.isoron.uhabits.core.ui.screens.habits.list.*
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.* import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.*
import org.isoron.uhabits.intents.* import org.isoron.uhabits.intents.*
import org.isoron.uhabits.tasks.* import org.isoron.uhabits.tasks.*
import org.isoron.uhabits.utils.*
import java.io.* import java.io.*
import javax.inject.* import javax.inject.*
@ -73,10 +74,6 @@ class ListHabitsScreen
ListHabitsMenuBehavior.Screen, ListHabitsMenuBehavior.Screen,
ListHabitsSelectionMenuBehavior.Screen { ListHabitsSelectionMenuBehavior.Screen {
init {
setRootView(rootView)
}
fun onAttached() { fun onAttached() {
commandRunner.addListener(this) commandRunner.addListener(this)
if(activity.intent.action == "android.intent.action.VIEW") { if(activity.intent.action == "android.intent.action.VIEW") {
@ -94,8 +91,11 @@ class ListHabitsScreen
} }
override fun onCommandExecuted(command: Command?, refreshKey: Long?) { override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
if (command != null) if (command != null) {
showMessage(getExecuteString(command)) val stringId = getExecuteString(command)
if (stringId != null)
activity.showMessage(stringId)
}
} }
override fun onResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onResult(requestCode: Int, resultCode: Int, data: Intent?) {
@ -115,7 +115,7 @@ class ListHabitsScreen
inStream.copyTo(tempFile) inStream.copyTo(tempFile)
onImportData(tempFile) { tempFile.delete() } onImportData(tempFile) { tempFile.delete() }
} catch (e: IOException) { } catch (e: IOException) {
showMessage(R.string.could_not_import) activity.showMessage(R.string.could_not_import)
e.printStackTrace() e.printStackTrace()
} }
} }
@ -175,7 +175,7 @@ class ListHabitsScreen
} }
override fun showMessage(m: ListHabitsBehavior.Message) { override fun showMessage(m: ListHabitsBehavior.Message) {
showMessage(when (m) { activity.showMessage(when (m) {
COULD_NOT_EXPORT -> R.string.could_not_export COULD_NOT_EXPORT -> R.string.could_not_export
IMPORT_SUCCESSFUL -> R.string.habits_imported IMPORT_SUCCESSFUL -> R.string.habits_imported
IMPORT_FAILED -> R.string.could_not_import IMPORT_FAILED -> R.string.could_not_import
@ -232,11 +232,11 @@ class ListHabitsScreen
taskRunner.execute(importTaskFactory.create(file) { result -> taskRunner.execute(importTaskFactory.create(file) { result ->
if (result == ImportDataTask.SUCCESS) { if (result == ImportDataTask.SUCCESS) {
adapter.refresh() adapter.refresh()
showMessage(R.string.habits_imported) activity.showMessage(R.string.habits_imported)
} else if (result == ImportDataTask.NOT_RECOGNIZED) { } else if (result == ImportDataTask.NOT_RECOGNIZED) {
showMessage(R.string.file_not_recognized) activity.showMessage(R.string.file_not_recognized)
} else { } else {
showMessage(R.string.could_not_import) activity.showMessage(R.string.could_not_import)
} }
onFinished() onFinished()
}) })
@ -245,7 +245,7 @@ class ListHabitsScreen
private fun onExportDB() { private fun onExportDB() {
taskRunner.execute(exportDBFactory.create { filename -> taskRunner.execute(exportDBFactory.create { filename ->
if (filename != null) showSendFileScreen(filename) if (filename != null) showSendFileScreen(filename)
else showMessage(R.string.could_not_export) else activity.showMessage(R.string.could_not_export)
}) })
} }
} }

Loading…
Cancel
Save