mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Create number picker dialog
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
package org.isoron.uhabits.activities.habits.list;
|
||||
|
||||
import android.support.annotation.*;
|
||||
import android.util.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.*;
|
||||
@@ -166,7 +167,12 @@ public class ListHabitsController
|
||||
@Override
|
||||
public void onEdit(@NonNull Habit habit, long timestamp)
|
||||
{
|
||||
|
||||
int oldValue = habit.getCheckmarks().getTodayValue();
|
||||
screen.showNumberPicker(oldValue, newValue -> {
|
||||
Log.d("ListHabitsController",
|
||||
String.format("%s %d %d", habit.getName(), timestamp,
|
||||
newValue));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -23,6 +23,9 @@ import android.app.*;
|
||||
import android.content.*;
|
||||
import android.net.*;
|
||||
import android.support.annotation.*;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.*;
|
||||
@@ -41,25 +44,26 @@ import javax.inject.*;
|
||||
|
||||
import static android.os.Build.VERSION.*;
|
||||
import static android.os.Build.VERSION_CODES.*;
|
||||
import static org.isoron.uhabits.utils.InterfaceUtils.*;
|
||||
|
||||
@ActivityScope
|
||||
public class ListHabitsScreen extends BaseScreen
|
||||
implements CommandRunner.Listener
|
||||
{
|
||||
public static final int RESULT_IMPORT_DATA = 1;
|
||||
public static final int REQUEST_OPEN_DOCUMENT = 6;
|
||||
|
||||
public static final int REQUEST_SETTINGS = 7;
|
||||
|
||||
public static final int RESULT_BUG_REPORT = 4;
|
||||
|
||||
public static final int RESULT_EXPORT_CSV = 2;
|
||||
|
||||
public static final int RESULT_EXPORT_DB = 3;
|
||||
|
||||
public static final int RESULT_BUG_REPORT = 4;
|
||||
public static final int RESULT_IMPORT_DATA = 1;
|
||||
|
||||
public static final int RESULT_REPAIR_DB = 5;
|
||||
|
||||
public static final int REQUEST_OPEN_DOCUMENT = 6;
|
||||
|
||||
public static final int REQUEST_SETTINGS = 7;
|
||||
|
||||
@Nullable
|
||||
private ListHabitsController controller;
|
||||
|
||||
@@ -97,11 +101,16 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@NonNull ListHabitsRootView rootView,
|
||||
@NonNull IntentFactory intentFactory,
|
||||
@NonNull ThemeSwitcher themeSwitcher,
|
||||
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
|
||||
@NonNull CreateHabitDialogFactory createHabitDialogFactory,
|
||||
@NonNull FilePickerDialogFactory filePickerDialogFactory,
|
||||
@NonNull ColorPickerDialogFactory colorPickerFactory,
|
||||
@NonNull EditHabitDialogFactory editHabitDialogFactory)
|
||||
@NonNull
|
||||
ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
|
||||
@NonNull
|
||||
CreateHabitDialogFactory createHabitDialogFactory,
|
||||
@NonNull
|
||||
FilePickerDialogFactory filePickerDialogFactory,
|
||||
@NonNull
|
||||
ColorPickerDialogFactory colorPickerFactory,
|
||||
@NonNull
|
||||
EditHabitDialogFactory editHabitDialogFactory)
|
||||
{
|
||||
super(activity);
|
||||
setRootView(rootView);
|
||||
@@ -139,60 +148,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
if (requestCode == REQUEST_OPEN_DOCUMENT)
|
||||
onOpenDocumentResult(resultCode, data);
|
||||
|
||||
if (requestCode == REQUEST_SETTINGS)
|
||||
onSettingsResult(resultCode);
|
||||
}
|
||||
|
||||
private void onSettingsResult(int resultCode)
|
||||
{
|
||||
if (controller == null) return;
|
||||
|
||||
switch (resultCode)
|
||||
{
|
||||
case RESULT_IMPORT_DATA:
|
||||
showImportScreen();
|
||||
break;
|
||||
|
||||
case RESULT_EXPORT_CSV:
|
||||
controller.onExportCSV();
|
||||
break;
|
||||
|
||||
case RESULT_EXPORT_DB:
|
||||
controller.onExportDB();
|
||||
break;
|
||||
|
||||
case RESULT_BUG_REPORT:
|
||||
controller.onSendBugReport();
|
||||
break;
|
||||
|
||||
case RESULT_REPAIR_DB:
|
||||
controller.onRepairDB();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void onOpenDocumentResult(int resultCode, Intent data)
|
||||
{
|
||||
if (controller == null) return;
|
||||
if (resultCode != Activity.RESULT_OK) return;
|
||||
|
||||
try
|
||||
{
|
||||
Uri uri = data.getData();
|
||||
ContentResolver cr = activity.getContentResolver();
|
||||
InputStream is = cr.openInputStream(uri);
|
||||
|
||||
File cacheDir = activity.getExternalCacheDir();
|
||||
File tempFile = File.createTempFile("import", "", cacheDir);
|
||||
|
||||
FileUtils.copy(is, tempFile);
|
||||
controller.onImportData(tempFile, () -> tempFile.delete());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
showMessage(R.string.could_not_import);
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (requestCode == REQUEST_SETTINGS) onSettingsResult(resultCode);
|
||||
}
|
||||
|
||||
public void setController(@Nullable ListHabitsController controller)
|
||||
@@ -238,6 +194,36 @@ public class ListHabitsScreen extends BaseScreen
|
||||
activity.showDialog(dialog, "editHabit");
|
||||
}
|
||||
|
||||
public void showNumberPicker(int initialValue,
|
||||
@NonNull NumberPickerCallback callback)
|
||||
{
|
||||
LayoutInflater inflater = activity.getLayoutInflater();
|
||||
View view = inflater.inflate(R.layout.number_picker_dialog, null);
|
||||
|
||||
final NumberPicker picker =
|
||||
(NumberPicker) view.findViewById(R.id.picker);
|
||||
|
||||
picker.setMinValue(0);
|
||||
picker.setMaxValue(Integer.MAX_VALUE);
|
||||
picker.setValue(initialValue);
|
||||
picker.setWrapSelectorWheel(false);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder
|
||||
.setView(view)
|
||||
.setTitle(R.string.change_value)
|
||||
.setPositiveButton(android.R.string.ok, (dialog, which) ->
|
||||
{
|
||||
callback.onNumberPicked(picker.getValue());
|
||||
});
|
||||
|
||||
AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
int width = (int) dpToPixels(activity, 200);
|
||||
int height = (int) dpToPixels(activity, 275);
|
||||
dialog.getWindow().setLayout(width, height);
|
||||
}
|
||||
|
||||
public void showFAQScreen()
|
||||
{
|
||||
Intent intent = intentFactory.viewFAQ(activity);
|
||||
@@ -278,7 +264,9 @@ public class ListHabitsScreen extends BaseScreen
|
||||
FilePickerDialog picker = filePickerDialogFactory.create(dir);
|
||||
|
||||
if (controller != null)
|
||||
picker.setListener(file -> controller.onImportData(file, () -> {}));
|
||||
picker.setListener(file -> controller.onImportData(file, () ->
|
||||
{
|
||||
}));
|
||||
|
||||
activity.showDialog(picker.getDialog());
|
||||
}
|
||||
@@ -300,4 +288,61 @@ public class ListHabitsScreen extends BaseScreen
|
||||
themeSwitcher.toggleNightMode();
|
||||
activity.restartWithFade();
|
||||
}
|
||||
|
||||
private void onOpenDocumentResult(int resultCode, Intent data)
|
||||
{
|
||||
if (controller == null) return;
|
||||
if (resultCode != Activity.RESULT_OK) return;
|
||||
|
||||
try
|
||||
{
|
||||
Uri uri = data.getData();
|
||||
ContentResolver cr = activity.getContentResolver();
|
||||
InputStream is = cr.openInputStream(uri);
|
||||
|
||||
File cacheDir = activity.getExternalCacheDir();
|
||||
File tempFile = File.createTempFile("import", "", cacheDir);
|
||||
|
||||
FileUtils.copy(is, tempFile);
|
||||
controller.onImportData(tempFile, () -> tempFile.delete());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
showMessage(R.string.could_not_import);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void onSettingsResult(int resultCode)
|
||||
{
|
||||
if (controller == null) return;
|
||||
|
||||
switch (resultCode)
|
||||
{
|
||||
case RESULT_IMPORT_DATA:
|
||||
showImportScreen();
|
||||
break;
|
||||
|
||||
case RESULT_EXPORT_CSV:
|
||||
controller.onExportCSV();
|
||||
break;
|
||||
|
||||
case RESULT_EXPORT_DB:
|
||||
controller.onExportDB();
|
||||
break;
|
||||
|
||||
case RESULT_BUG_REPORT:
|
||||
controller.onSendBugReport();
|
||||
break;
|
||||
|
||||
case RESULT_REPAIR_DB:
|
||||
controller.onRepairDB();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public interface NumberPickerCallback
|
||||
{
|
||||
void onNumberPicked(int newValue);
|
||||
}
|
||||
}
|
||||
|
||||
33
app/src/main/res/layout/number_picker_dialog.xml
Normal file
33
app/src/main/res/layout/number_picker_dialog.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ 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/>.
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<NumberPicker
|
||||
android:id="@+id/picker"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -206,4 +206,5 @@
|
||||
<string name="export">Export</string>
|
||||
<string name="long_press_to_edit">Press-and-hold to change the
|
||||
value</string>
|
||||
<string name="change_value">Change value</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user