mirror of https://github.com/iSoron/uhabits.git
parent
268cb0bc18
commit
8feb07ff1b
@ -1,19 +0,0 @@
|
||||
package org.isoron.uhabits.widgets
|
||||
|
||||
import android.content.Context
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
|
||||
class CheckmarkStackWidgetProvider : BaseWidgetProvider() {
|
||||
|
||||
override fun getWidgetFromId(context: Context, id: Int): CheckmarkStackWidget {
|
||||
val habitIds = getHabitGroupFromWidget(context, id)
|
||||
return CheckmarkStackWidget(context, id, habitIds)
|
||||
}
|
||||
|
||||
private fun getHabitGroupFromWidget(context: Context, widgetId: Int) : List<Long> {
|
||||
val app = context.getApplicationContext() as HabitsApplication
|
||||
val widgetPrefs = app.component.widgetPreferences
|
||||
val habitIds = widgetPrefs.getHabitIdsGroupFromWidgetId(widgetId)
|
||||
return habitIds
|
||||
}
|
||||
}
|
@ -1,133 +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.uhabits.widgets
|
||||
|
||||
import android.app.Activity
|
||||
import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID
|
||||
import android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.*
|
||||
import android.widget.CompoundButton
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.core.models.HabitList
|
||||
import org.isoron.uhabits.core.preferences.WidgetPreferences
|
||||
|
||||
class HabitGroupPickerDialog : Activity(), AdapterView.OnItemClickListener {
|
||||
|
||||
private var widgetId = 0
|
||||
private lateinit var habitList: HabitList
|
||||
private lateinit var preferences: WidgetPreferences
|
||||
private lateinit var habitIds: ArrayList<Long>
|
||||
private lateinit var widgetUpdater: WidgetUpdater
|
||||
private lateinit var habitIdsSelected: ArrayList<Long>
|
||||
private lateinit var habitListView: ListView
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val component = (applicationContext as HabitsApplication).component
|
||||
habitList = component.habitList
|
||||
preferences = component.widgetPreferences
|
||||
widgetUpdater = component.widgetUpdater
|
||||
widgetId = intent.extras?.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID) ?: 0
|
||||
habitIdsSelected = ArrayList<Long>()
|
||||
|
||||
habitIds = ArrayList<Long>()
|
||||
val habitNames = ArrayList<String>()
|
||||
for (h in habitList) {
|
||||
if (h.isArchived) continue
|
||||
habitIds.add(h.getId()!!)
|
||||
habitNames.add(h.name)
|
||||
}
|
||||
|
||||
setContentView(R.layout.stack_widget_configure_activity)
|
||||
habitListView = findViewById(R.id.stackWidgetListView) as ListView
|
||||
with(habitListView) {
|
||||
adapter = ListAdapter(context, R.layout.habit_checkbox_list_item,
|
||||
R.id.listItemHabitName, R.id.listItemHabitCheckbox, habitNames)
|
||||
onItemClickListener = this@HabitGroupPickerDialog
|
||||
}
|
||||
with(findViewById(R.id.doneConfigureButton) as Button) {
|
||||
setOnClickListener {
|
||||
if (habitIdsSelected.size == 1) {
|
||||
preferences.addWidget(widgetId, habitIdsSelected.first())
|
||||
widgetUpdater.updateWidgets()
|
||||
setResult(Activity.RESULT_OK, Intent().apply {
|
||||
putExtra(EXTRA_APPWIDGET_ID, widgetId)
|
||||
})
|
||||
finish()
|
||||
} else if (!habitIdsSelected.isEmpty()) {
|
||||
preferences.addWidget(widgetId, habitIdsSelected.toString())
|
||||
widgetUpdater.updateWidgets()
|
||||
setResult(Activity.RESULT_OK, Intent().apply {
|
||||
putExtra(EXTRA_APPWIDGET_ID, widgetId)
|
||||
})
|
||||
finish()
|
||||
} else {
|
||||
Toast.makeText(context, getString(R.string.select_habit_requirement_prompt),
|
||||
Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onItemClick(parent: AdapterView<*>,
|
||||
view: View,
|
||||
position: Int,
|
||||
id: Long) {
|
||||
val checkbox = view.findViewById(R.id.listItemHabitCheckbox) as CheckBox
|
||||
checkbox.isChecked = !checkbox.isChecked
|
||||
}
|
||||
|
||||
private inner class ListAdapter(context: Context,
|
||||
private var layoutResource: Int,
|
||||
private var textViewResourceId: Int,
|
||||
private var checkBoxResourceId: Int,
|
||||
private var habitNames: List<String>) :
|
||||
ArrayAdapter<String>(context, layoutResource, textViewResourceId, habitNames) {
|
||||
|
||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||
val layoutInflater: LayoutInflater = LayoutInflater.from(context)
|
||||
val view = layoutInflater.inflate(layoutResource, null)
|
||||
|
||||
val item = getItem(position)
|
||||
if (item != null) {
|
||||
val tv = view.findViewById(textViewResourceId) as TextView
|
||||
tv.text = habitNames.get(position)
|
||||
val cb = view.findViewById(checkBoxResourceId) as CheckBox
|
||||
cb.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
|
||||
if (isChecked) {
|
||||
habitIdsSelected.add(habitIds.get(position))
|
||||
} else {
|
||||
habitIdsSelected.remove(habitIds.get(position))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package org.isoron.uhabits.widgets;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
|
||||
/**
|
||||
* Created by victoryu on 11/3/17.
|
||||
*/
|
||||
|
||||
public enum StackWidgetType {
|
||||
|
||||
CHECKMARK(0),
|
||||
FREQUENCY(1),
|
||||
SCORE(2), // habit strength widget
|
||||
HISTORY(3),
|
||||
STREAKS(4);
|
||||
|
||||
private int value;
|
||||
StackWidgetType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static StackWidgetType getWidgetTypeFromValue(int value) {
|
||||
if (CHECKMARK.getValue() == value) {
|
||||
return CHECKMARK;
|
||||
} else if (FREQUENCY.getValue() == value) {
|
||||
return FREQUENCY;
|
||||
} else if (SCORE.getValue() == value) {
|
||||
return SCORE;
|
||||
} else if (HISTORY.getValue() == value) {
|
||||
return HISTORY;
|
||||
} else if (STREAKS.getValue() == value) {
|
||||
return STREAKS;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getStackWidgetLayoutId(StackWidgetType type) {
|
||||
switch (type) {
|
||||
case CHECKMARK:
|
||||
return R.layout.checkmark_stackview_widget;
|
||||
case FREQUENCY:
|
||||
return R.layout.frequency_stackview_widget;
|
||||
case SCORE:
|
||||
return R.layout.score_stackview_widget;
|
||||
case HISTORY:
|
||||
return R.layout.history_stackview_widget;
|
||||
case STREAKS:
|
||||
return R.layout.streak_stackview_widget;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int getStackWidgetAdapterViewId(StackWidgetType type) {
|
||||
switch (type) {
|
||||
case CHECKMARK:
|
||||
return R.id.checkmarkStackWidgetView;
|
||||
case FREQUENCY:
|
||||
return R.id.frequencyStackWidgetView;
|
||||
case SCORE:
|
||||
return R.id.scoreStackWidgetView;
|
||||
case HISTORY:
|
||||
return R.id.historyStackWidgetView;
|
||||
case STREAKS:
|
||||
return R.id.streakStackWidgetView;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int getStackWidgetEmptyViewId(StackWidgetType type) {
|
||||
switch (type) {
|
||||
case CHECKMARK:
|
||||
return R.id.checkmarkStackWidgetEmptyView;
|
||||
case FREQUENCY:
|
||||
return R.id.frequencyStackWidgetEmptyView;
|
||||
case SCORE:
|
||||
return R.id.scoreStackWidgetEmptyView;
|
||||
case HISTORY:
|
||||
return R.id.historyStackWidgetEmptyView;
|
||||
case STREAKS:
|
||||
return R.id.streakStackWidgetEmptyView;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/frequencyStackWidgetView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:loopViews="true" />
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/frequencyStackWidgetEmptyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/frequency_stack_widget"
|
||||
android:gravity="center"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold"
|
||||
android:textSize="16sp" />
|
||||
</FrameLayout>
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/historyStackWidgetView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:loopViews="true" />
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/historyStackWidgetEmptyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/history_stack_widget"
|
||||
android:gravity="center"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold"
|
||||
android:textSize="16sp" />
|
||||
</FrameLayout>
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/scoreStackWidgetView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:loopViews="true" />
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/scoreStackWidgetEmptyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/score_stack_widget"
|
||||
android:gravity="center"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold"
|
||||
android:textSize="16sp" />
|
||||
</FrameLayout>
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/streakStackWidgetView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:loopViews="true" />
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/streakStackWidgetEmptyView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/streaks_stack_widget"
|
||||
android:gravity="center"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold"
|
||||
android:textSize="16sp" />
|
||||
</FrameLayout>
|
Loading…
Reference in new issue