mirror of https://github.com/iSoron/uhabits.git
parent
cac62c0278
commit
a2cf78f823
@ -0,0 +1,152 @@
|
||||
package org.isoron.uhabits.activities.habits.show
|
||||
|
||||
import android.content.ContentUris
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import org.isoron.uhabits.AndroidDirFinder
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.AndroidThemeSwitcher
|
||||
import org.isoron.uhabits.activities.HabitGroupsDirFinder
|
||||
import org.isoron.uhabits.activities.common.dialogs.ConfirmDeleteDialog
|
||||
import org.isoron.uhabits.core.commands.Command
|
||||
import org.isoron.uhabits.core.commands.CommandRunner
|
||||
import org.isoron.uhabits.core.models.HabitGroup
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitGroupMenuPresenter
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitGroupPresenter
|
||||
import org.isoron.uhabits.intents.IntentFactory
|
||||
import org.isoron.uhabits.utils.dismissCurrentAndShow
|
||||
import org.isoron.uhabits.utils.dismissCurrentDialog
|
||||
import org.isoron.uhabits.utils.showMessage
|
||||
import org.isoron.uhabits.utils.showSendFileScreen
|
||||
import org.isoron.uhabits.widgets.WidgetUpdater
|
||||
|
||||
class ShowHabitGroupActivity : AppCompatActivity(), CommandRunner.Listener {
|
||||
|
||||
private lateinit var commandRunner: CommandRunner
|
||||
private lateinit var menu: ShowHabitGroupMenu
|
||||
private lateinit var view: ShowHabitGroupView
|
||||
private lateinit var habitGroup: HabitGroup
|
||||
private lateinit var preferences: Preferences
|
||||
private lateinit var themeSwitcher: AndroidThemeSwitcher
|
||||
private lateinit var widgetUpdater: WidgetUpdater
|
||||
|
||||
private val scope = CoroutineScope(Dispatchers.Main)
|
||||
private lateinit var presenter: ShowHabitGroupPresenter
|
||||
private val screen = Screen()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
val appComponent = (applicationContext as HabitsApplication).component
|
||||
val habitGroupList = appComponent.habitGroupList
|
||||
habitGroup = habitGroupList.getById(ContentUris.parseId(intent.data!!))!!
|
||||
preferences = appComponent.preferences
|
||||
commandRunner = appComponent.commandRunner
|
||||
widgetUpdater = appComponent.widgetUpdater
|
||||
|
||||
themeSwitcher = AndroidThemeSwitcher(this, preferences)
|
||||
themeSwitcher.apply()
|
||||
|
||||
presenter = ShowHabitGroupPresenter(
|
||||
commandRunner = commandRunner,
|
||||
habitGroup = habitGroup,
|
||||
preferences = preferences,
|
||||
screen = screen
|
||||
)
|
||||
|
||||
view = ShowHabitGroupView(this)
|
||||
|
||||
val menuPresenter = ShowHabitGroupMenuPresenter(
|
||||
commandRunner = commandRunner,
|
||||
habitGroup = habitGroup,
|
||||
habitGroupList = habitGroupList,
|
||||
screen = screen,
|
||||
system = HabitGroupsDirFinder(AndroidDirFinder(this)),
|
||||
taskRunner = appComponent.taskRunner
|
||||
)
|
||||
|
||||
menu = ShowHabitGroupMenu(
|
||||
activity = this,
|
||||
presenter = menuPresenter,
|
||||
preferences = preferences
|
||||
)
|
||||
|
||||
view.setListener(presenter)
|
||||
setContentView(view)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(m: Menu): Boolean {
|
||||
return menu.onCreateOptionsMenu(m)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return menu.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
commandRunner.addListener(this)
|
||||
screen.refresh()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
dismissCurrentDialog()
|
||||
commandRunner.removeListener(this)
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
override fun onCommandFinished(command: Command) {
|
||||
screen.refresh()
|
||||
}
|
||||
|
||||
inner class Screen : ShowHabitGroupMenuPresenter.Screen, ShowHabitGroupPresenter.Screen {
|
||||
override fun updateWidgets() {
|
||||
widgetUpdater.updateWidgets()
|
||||
}
|
||||
|
||||
override fun refresh() {
|
||||
scope.launch {
|
||||
view.setState(
|
||||
ShowHabitGroupPresenter.buildState(
|
||||
habitGroup = habitGroup,
|
||||
preferences = preferences,
|
||||
theme = themeSwitcher.currentTheme
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun showEditHabitGroupScreen(habitGroup: HabitGroup) {
|
||||
startActivity(IntentFactory().startEditGroupActivity(this@ShowHabitGroupActivity, habitGroup))
|
||||
}
|
||||
|
||||
override fun showMessage(m: ShowHabitGroupMenuPresenter.Message?) {
|
||||
when (m) {
|
||||
ShowHabitGroupMenuPresenter.Message.COULD_NOT_EXPORT -> {
|
||||
showMessage(resources.getString(R.string.could_not_export))
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
override fun showSendFileScreen(filename: String) {
|
||||
this@ShowHabitGroupActivity.showSendFileScreen(filename)
|
||||
}
|
||||
|
||||
override fun showDeleteConfirmationScreen(callback: OnConfirmedCallback) {
|
||||
ConfirmDeleteDialog(this@ShowHabitGroupActivity, callback, 1).dismissCurrentAndShow()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
this@ShowHabitGroupActivity.finish()
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package org.isoron.uhabits.activities.habits.show
|
||||
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitGroupMenuPresenter
|
||||
|
||||
class ShowHabitGroupMenu(
|
||||
val activity: ShowHabitGroupActivity,
|
||||
val presenter: ShowHabitGroupMenuPresenter,
|
||||
val preferences: Preferences
|
||||
) {
|
||||
fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
activity.menuInflater.inflate(R.menu.show_habit_group, menu)
|
||||
return true
|
||||
}
|
||||
|
||||
fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.action_edit_habit_group -> {
|
||||
presenter.onEditHabit()
|
||||
return true
|
||||
}
|
||||
R.id.action_delete -> {
|
||||
presenter.onDeleteHabit()
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package org.isoron.uhabits.activities.habits.show
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.widget.FrameLayout
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitGroupPresenter
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitGroupState
|
||||
import org.isoron.uhabits.databinding.ShowHabitGroupBinding
|
||||
import org.isoron.uhabits.utils.setupToolbar
|
||||
|
||||
class ShowHabitGroupView(context: Context) : FrameLayout(context) {
|
||||
private val binding = ShowHabitGroupBinding.inflate(LayoutInflater.from(context))
|
||||
|
||||
init {
|
||||
addView(binding.root)
|
||||
}
|
||||
|
||||
fun setState(data: ShowHabitGroupState) {
|
||||
setupToolbar(
|
||||
binding.toolbar,
|
||||
title = data.title,
|
||||
color = data.color,
|
||||
theme = data.theme
|
||||
)
|
||||
binding.subtitleCard.setState(data.subtitle)
|
||||
binding.overviewCard.setState(data.overview)
|
||||
binding.notesCard.setState(data.notes)
|
||||
binding.streakCard.setState(data.streaks)
|
||||
binding.scoreCard.setState(data.scores)
|
||||
}
|
||||
|
||||
fun setListener(presenter: ShowHabitGroupPresenter) {
|
||||
binding.scoreCard.setListener(presenter.scoreCardPresenter)
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
~
|
||||
~ 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/>.
|
||||
-->
|
||||
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item
|
||||
android:id="@+id/action_delete"
|
||||
android:title="@string/delete"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_edit_habit_group"
|
||||
android:icon="?iconEdit"
|
||||
android:title="@string/edit"
|
||||
app:showAsAction="ifRoom"/>
|
||||
|
||||
</menu>
|
@ -0,0 +1,78 @@
|
||||
package org.isoron.uhabits.core.ui.screens.habits.show
|
||||
|
||||
import org.isoron.uhabits.core.commands.CommandRunner
|
||||
import org.isoron.uhabits.core.models.HabitGroup
|
||||
import org.isoron.uhabits.core.models.PaletteColor
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.NotesCardPresenter
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.NotesCardState
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.OverviewCardPresenter
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.OverviewCardState
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.ScoreCardPresenter
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.ScoreCardState
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.StreakCardState
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.StreakCartPresenter
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.SubtitleCardPresenter
|
||||
import org.isoron.uhabits.core.ui.screens.habits.show.views.SubtitleCardState
|
||||
import org.isoron.uhabits.core.ui.views.Theme
|
||||
|
||||
data class ShowHabitGroupState(
|
||||
val title: String = "",
|
||||
val color: PaletteColor = PaletteColor(1),
|
||||
val subtitle: SubtitleCardState,
|
||||
val overview: OverviewCardState,
|
||||
val notes: NotesCardState,
|
||||
val streaks: StreakCardState,
|
||||
val scores: ScoreCardState,
|
||||
val theme: Theme
|
||||
)
|
||||
|
||||
class ShowHabitGroupPresenter(
|
||||
val habitGroup: HabitGroup,
|
||||
val preferences: Preferences,
|
||||
val screen: Screen,
|
||||
val commandRunner: CommandRunner
|
||||
) {
|
||||
val scoreCardPresenter = ScoreCardPresenter(
|
||||
preferences = preferences,
|
||||
screen = screen
|
||||
)
|
||||
|
||||
companion object {
|
||||
fun buildState(
|
||||
habitGroup: HabitGroup,
|
||||
preferences: Preferences,
|
||||
theme: Theme
|
||||
): ShowHabitGroupState {
|
||||
return ShowHabitGroupState(
|
||||
title = habitGroup.name,
|
||||
color = habitGroup.color,
|
||||
theme = theme,
|
||||
subtitle = SubtitleCardPresenter.buildState(
|
||||
habitGroup = habitGroup,
|
||||
theme = theme
|
||||
),
|
||||
overview = OverviewCardPresenter.buildState(
|
||||
habitGroup = habitGroup,
|
||||
theme = theme
|
||||
),
|
||||
notes = NotesCardPresenter.buildState(
|
||||
habitGroup = habitGroup
|
||||
),
|
||||
streaks = StreakCartPresenter.buildState(
|
||||
habitGroup = habitGroup,
|
||||
theme = theme
|
||||
),
|
||||
scores = ScoreCardPresenter.buildState(
|
||||
spinnerPosition = preferences.scoreCardSpinnerPosition,
|
||||
habitGroup = habitGroup,
|
||||
firstWeekday = preferences.firstWeekdayInt,
|
||||
theme = theme
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
interface Screen :
|
||||
ScoreCardPresenter.Screen
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package org.isoron.uhabits.core.ui.screens.habits.show
|
||||
|
||||
import org.isoron.uhabits.core.commands.CommandRunner
|
||||
import org.isoron.uhabits.core.commands.DeleteHabitGroupsCommand
|
||||
import org.isoron.uhabits.core.models.HabitGroup
|
||||
import org.isoron.uhabits.core.models.HabitGroupList
|
||||
import org.isoron.uhabits.core.tasks.TaskRunner
|
||||
import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback
|
||||
import java.io.File
|
||||
|
||||
class ShowHabitGroupMenuPresenter(
|
||||
private val commandRunner: CommandRunner,
|
||||
private val habitGroup: HabitGroup,
|
||||
private val habitGroupList: HabitGroupList,
|
||||
private val screen: Screen,
|
||||
private val system: System,
|
||||
private val taskRunner: TaskRunner
|
||||
) {
|
||||
fun onEditHabit() {
|
||||
screen.showEditHabitGroupScreen(habitGroup)
|
||||
}
|
||||
|
||||
fun onDeleteHabit() {
|
||||
screen.showDeleteConfirmationScreen {
|
||||
commandRunner.run(DeleteHabitGroupsCommand(habitGroupList, listOf(habitGroup)))
|
||||
screen.close()
|
||||
}
|
||||
}
|
||||
|
||||
enum class Message {
|
||||
COULD_NOT_EXPORT
|
||||
}
|
||||
|
||||
interface Screen {
|
||||
fun showEditHabitGroupScreen(habitGroup: HabitGroup)
|
||||
fun showMessage(m: Message?)
|
||||
fun showSendFileScreen(filename: String)
|
||||
fun showDeleteConfirmationScreen(callback: OnConfirmedCallback)
|
||||
fun close()
|
||||
fun refresh()
|
||||
}
|
||||
|
||||
interface System {
|
||||
fun getCSVOutputDir(): File
|
||||
}
|
||||
}
|
Loading…
Reference in new issue