From a02376497a49177e84cf1597cc7859dc154ed07f Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 2 Jul 2017 13:34:23 -0400 Subject: [PATCH] Convert automation and database packages to Kotlin --- .../automation/EditSettingActivity.java | 50 ------- .../uhabits/automation/EditSettingActivity.kt | 44 ++++++ .../automation/EditSettingController.java | 78 ----------- .../automation/EditSettingController.kt | 55 ++++++++ .../automation/EditSettingRootView.java | 105 --------------- .../uhabits/automation/EditSettingRootView.kt | 81 +++++++++++ .../automation/FireSettingReceiver.java | 115 ---------------- .../uhabits/automation/FireSettingReceiver.kt | 73 ++++++++++ .../uhabits/database/AndroidCursor.java | 80 ----------- .../isoron/uhabits/database/AndroidCursor.kt | 48 +++++++ .../uhabits/database/AndroidDatabase.java | 126 ------------------ .../uhabits/database/AndroidDatabase.kt | 74 ++++++++++ .../database/AndroidDatabaseOpener.java | 50 ------- .../uhabits/database/AndroidDatabaseOpener.kt | 32 +++++ 14 files changed, 407 insertions(+), 604 deletions(-) delete mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt delete mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.java create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.kt delete mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.kt delete mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.kt delete mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.java create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.kt delete mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.java create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.kt delete mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.java create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.kt diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java deleted file mode 100644 index c0ec8a2a3..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits.automation; - -import android.os.*; - -import org.isoron.androidbase.activities.*; -import org.isoron.uhabits.*; -import org.isoron.uhabits.core.models.*; - -public class EditSettingActivity extends BaseActivity -{ - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - HabitsApplication app = (HabitsApplication) getApplicationContext(); - - HabitList habits = app.getComponent().getHabitList(); - habits = habits.getFiltered(new HabitMatcherBuilder() - .setArchivedAllowed(false) - .setCompletedAllowed(true) - .build()); - - EditSettingController controller = new EditSettingController(this); - EditSettingRootView rootView = - new EditSettingRootView(this, habits, controller); - - BaseScreen screen = new BaseScreen(this); - screen.setRootView(rootView); - setScreen(screen); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt new file mode 100644 index 000000000..0c7ef8a2d --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingActivity.kt @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015-2017 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.automation + +import android.os.* + +import org.isoron.androidbase.activities.* +import org.isoron.uhabits.* +import org.isoron.uhabits.core.models.* + +class EditSettingActivity : BaseActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val app = applicationContext as HabitsApplication + val habits = app.component.habitList.getFiltered( + HabitMatcherBuilder() + .setArchivedAllowed(false) + .setCompletedAllowed(true) + .build()) + + val controller = EditSettingController(this) + val rootView = EditSettingRootView(this, habits, controller) + val screen = BaseScreen(this) + screen.setRootView(rootView) + setScreen(screen) + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.java b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.java deleted file mode 100644 index e18b2c367..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits.automation; - -import android.app.*; -import android.content.*; -import android.os.*; -import android.support.annotation.*; - -import org.isoron.uhabits.*; -import org.isoron.uhabits.core.models.*; - -import static org.isoron.uhabits.automation.FireSettingReceiver.*; - -public class EditSettingController -{ - @NonNull - private final Activity activity; - - public EditSettingController(@NonNull Activity activity) - { - this.activity = activity; - } - - public void onSave(Habit habit, int action) - { - if (habit.getId() == null) return; - - String actionName = getActionName(action); - String blurb = String.format("%s: %s", actionName, habit.getName()); - - Bundle bundle = new Bundle(); - bundle.putInt("action", action); - bundle.putLong("habit", habit.getId()); - - Intent intent = new Intent(); - intent.putExtra(EXTRA_STRING_BLURB, blurb); - intent.putExtra(EXTRA_BUNDLE, bundle); - - activity.setResult(Activity.RESULT_OK, intent); - activity.finish(); - } - - private String getActionName(int action) - { - switch (action) - { - case ACTION_CHECK: - return activity.getString(R.string.check); - - case ACTION_UNCHECK: - return activity.getString(R.string.uncheck); - - case ACTION_TOGGLE: - return activity.getString(R.string.toggle); - - default: - return "???"; - } - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.kt b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.kt new file mode 100644 index 000000000..2fdd01250 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingController.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2015-2017 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.automation + +import android.app.* +import android.content.* +import android.os.* +import org.isoron.uhabits.* +import org.isoron.uhabits.automation.FireSettingReceiver.* +import org.isoron.uhabits.core.models.* + +class EditSettingController(private val activity: Activity) { + + fun onSave(habit: Habit, action: Int) { + if (habit.getId() == null) return + val actionName = getActionName(action) + val blurb = String.format("%s: %s", actionName, habit.name) + + val bundle = Bundle() + bundle.putInt("action", action) + bundle.putLong("habit", habit.getId()!!) + + activity.setResult(Activity.RESULT_OK, Intent().apply { + putExtra(EXTRA_STRING_BLURB, blurb) + putExtra(EXTRA_BUNDLE, bundle) + }) + activity.finish() + } + + private fun getActionName(action: Int): String { + when (action) { + ACTION_CHECK -> return activity.getString(R.string.check) + ACTION_UNCHECK -> return activity.getString(R.string.uncheck) + ACTION_TOGGLE -> return activity.getString(R.string.toggle) + else -> return "???" + } + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java deleted file mode 100644 index 46eacc94d..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits.automation; - -import android.content.*; -import android.support.annotation.*; -import android.support.v7.widget.*; -import android.support.v7.widget.Toolbar; -import android.widget.*; - -import org.isoron.androidbase.activities.*; -import org.isoron.androidbase.utils.*; -import org.isoron.uhabits.R; -import org.isoron.uhabits.core.models.*; - -import java.util.*; - -import butterknife.*; - -import static android.R.layout.*; - -public class EditSettingRootView extends BaseRootView -{ - @BindView(R.id.toolbar) - Toolbar toolbar; - - @BindView(R.id.habitSpinner) - AppCompatSpinner habitSpinner; - - @BindView(R.id.actionSpinner) - AppCompatSpinner actionSpinner; - - @NonNull - private final HabitList habitList; - - @NonNull - private final EditSettingController controller; - - public EditSettingRootView(@NonNull Context context, - @NonNull HabitList habitList, - @NonNull EditSettingController controller) - { - super(context); - this.habitList = habitList; - this.controller = controller; - - addView(inflate(getContext(), R.layout.automation, null)); - ButterKnife.bind(this); - populateHabitSpinner(); - } - - @NonNull - @Override - public Toolbar getToolbar() - { - return toolbar; - } - - @Override - public int getToolbarColor() - { - StyledResources res = new StyledResources(getContext()); - if (!res.getBoolean(R.attr.useHabitColorAsPrimary)) - return super.getToolbarColor(); - - return res.getColor(R.attr.aboutScreenColor); - } - - @OnClick(R.id.buttonSave) - public void onClickSave() - { - int action = actionSpinner.getSelectedItemPosition(); - int habitPosition = habitSpinner.getSelectedItemPosition(); - Habit habit = habitList.getByPosition(habitPosition); - controller.onSave(habit, action); - } - - private void populateHabitSpinner() - { - List names = new LinkedList<>(); - for (Habit h : habitList) names.add(h.getName()); - - ArrayAdapter adapter = - new ArrayAdapter<>(getContext(), simple_spinner_item, names); - adapter.setDropDownViewResource(simple_spinner_dropdown_item); - habitSpinner.setAdapter(adapter); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.kt b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.kt new file mode 100644 index 000000000..df149d946 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/automation/EditSettingRootView.kt @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.automation + +import android.R.layout.* +import android.content.* +import android.support.v7.widget.* +import android.support.v7.widget.Toolbar +import android.widget.* +import butterknife.* +import org.isoron.androidbase.activities.* +import org.isoron.androidbase.utils.* +import org.isoron.uhabits.R +import org.isoron.uhabits.core.models.* +import java.util.* + +class EditSettingRootView( + context: Context, + private val habitList: HabitList, + private val controller: EditSettingController +) : BaseRootView(context) { + + @BindView(R.id.toolbar) + lateinit var tbar: Toolbar + + @BindView(R.id.habitSpinner) + lateinit var habitSpinner: AppCompatSpinner + + @BindView(R.id.actionSpinner) + lateinit var actionSpinner: AppCompatSpinner + + init { + addView(inflate(getContext(), R.layout.automation, null)) + ButterKnife.bind(this) + populateHabitSpinner() + } + + override fun getToolbar(): Toolbar { + return tbar + } + + override fun getToolbarColor(): Int { + val res = StyledResources(context) + if (!res.getBoolean(R.attr.useHabitColorAsPrimary)) + return super.getToolbarColor() + + return res.getColor(R.attr.aboutScreenColor) + } + + @OnClick(R.id.buttonSave) + fun onClickSave() { + val action = actionSpinner.selectedItemPosition + val habitPosition = habitSpinner.selectedItemPosition + val habit = habitList.getByPosition(habitPosition) + controller.onSave(habit, action) + } + + private fun populateHabitSpinner() { + val names = habitList.mapTo(LinkedList()) { it.name } + val adapter = ArrayAdapter(context, simple_spinner_item, names) + adapter.setDropDownViewResource(simple_spinner_dropdown_item) + habitSpinner.adapter = adapter + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java b/uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java deleted file mode 100644 index dd0bd3d1b..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits.automation; - -import android.content.*; -import android.os.*; - -import org.isoron.uhabits.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.receivers.*; -import org.isoron.uhabits.core.ui.widgets.*; -import org.isoron.uhabits.core.utils.*; - -import dagger.*; - -public class FireSettingReceiver extends BroadcastReceiver -{ - public static final int ACTION_CHECK = 0; - - public static final int ACTION_UNCHECK = 1; - - public static final int ACTION_TOGGLE = 2; - - public static final String EXTRA_BUNDLE = - "com.twofortyfouram.locale.intent.extra.BUNDLE"; - - public static final String EXTRA_STRING_BLURB = - "com.twofortyfouram.locale.intent.extra.BLURB"; - - private HabitList allHabits; - - @Override - public void onReceive(Context context, Intent intent) - { - HabitsApplication app = - (HabitsApplication) context.getApplicationContext(); - - ReceiverComponent component = - DaggerFireSettingReceiver_ReceiverComponent - .builder() - .habitsApplicationComponent(app.getComponent()) - .build(); - - allHabits = app.getComponent().getHabitList(); - - Arguments args = parseIntent(intent); - if (args == null) return; - - long timestamp = DateUtils.getStartOfToday(); - WidgetBehavior controller = component.getWidgetController(); - - switch (args.action) - { - case ACTION_CHECK: - controller.onAddRepetition(args.habit, timestamp); - break; - - case ACTION_UNCHECK: - controller.onRemoveRepetition(args.habit, timestamp); - break; - - case ACTION_TOGGLE: - controller.onToggleRepetition(args.habit, timestamp); - break; - } - } - - private Arguments parseIntent(Intent intent) - { - Arguments args = new Arguments(); - - Bundle bundle = intent.getBundleExtra(EXTRA_BUNDLE); - if (bundle == null) return null; - - args.action = bundle.getInt("action"); - if (args.action < 0 || args.action > 2) return null; - - Habit habit = allHabits.getById(bundle.getLong("habit")); - if (habit == null) return null; - args.habit = habit; - - return args; - } - - @ReceiverScope - @Component(dependencies = HabitsApplicationComponent.class) - interface ReceiverComponent - { - WidgetBehavior getWidgetController(); - } - - private class Arguments - { - int action; - - Habit habit; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.kt b/uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.kt new file mode 100644 index 000000000..19b0467b7 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/automation/FireSettingReceiver.kt @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2015-2017 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.automation + +import android.content.* +import dagger.* +import org.isoron.uhabits.* +import org.isoron.uhabits.core.models.* +import org.isoron.uhabits.core.ui.widgets.* +import org.isoron.uhabits.core.utils.* +import org.isoron.uhabits.receivers.* + +const val ACTION_CHECK = 0 +const val ACTION_UNCHECK = 1 +const val ACTION_TOGGLE = 2 +const val EXTRA_BUNDLE = "com.twofortyfouram.locale.intent.extra.BUNDLE" +const val EXTRA_STRING_BLURB = "com.twofortyfouram.locale.intent.extra.BLURB" + +class FireSettingReceiver : BroadcastReceiver() { + + private lateinit var allHabits: HabitList + + override fun onReceive(context: Context, intent: Intent) { + val app = context.applicationContext as HabitsApplication + val component = DaggerFireSettingReceiver_ReceiverComponent + .builder() + .habitsApplicationComponent(app.component) + .build() + allHabits = app.component.habitList + val args = parseIntent(intent) ?: return + val timestamp = DateUtils.getStartOfToday() + val controller = component.widgetController + + when (args.action) { + ACTION_CHECK -> controller.onAddRepetition(args.habit, timestamp) + ACTION_UNCHECK -> controller.onRemoveRepetition(args.habit, timestamp) + ACTION_TOGGLE -> controller.onToggleRepetition(args.habit, timestamp) + } + } + + private fun parseIntent(intent: Intent): Arguments? { + val bundle = intent.getBundleExtra(EXTRA_BUNDLE) ?: return null + val action = bundle.getInt("action") + if (action < 0 || action > 2) return null + val habit = allHabits.getById(bundle.getLong("habit")) ?: return null + return Arguments(action, habit) + } + + @ReceiverScope + @Component(dependencies = arrayOf(HabitsApplicationComponent::class)) + internal interface ReceiverComponent { + val widgetController: WidgetBehavior + } + + private class Arguments(var action: Int, var habit: Habit) +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.java b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.java deleted file mode 100644 index c3c43fe5f..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2017 Álinson Santos Xavier - * - * 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 . - * - * - */ - -package org.isoron.uhabits.database; - -import android.support.annotation.*; - -import org.isoron.uhabits.core.database.*; - -public class AndroidCursor implements Cursor -{ - private android.database.Cursor cursor; - - public AndroidCursor(android.database.Cursor cursor) - { - this.cursor = cursor; - } - - @Override - public void close() - { - cursor.close(); - } - - @Override - public boolean moveToNext() - { - return cursor.moveToNext(); - } - - @Override - @Nullable - public Integer getInt(int index) - { - if(cursor.isNull(index)) return null; - else return cursor.getInt(index); - } - - @Override - @Nullable - public Long getLong(int index) - { - if(cursor.isNull(index)) return null; - else return cursor.getLong(index); - } - - @Override - @Nullable - public Double getDouble(int index) - { - if(cursor.isNull(index)) return null; - else return cursor.getDouble(index); - } - - @Override - @Nullable - public String getString(int index) - { - if(cursor.isNull(index)) return null; - else return cursor.getString(index); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.kt b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.kt new file mode 100644 index 000000000..703c43847 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidCursor.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2015-2017 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.database + +import org.isoron.uhabits.core.database.* + +class AndroidCursor(private val cursor: android.database.Cursor) : Cursor { + + override fun close() = cursor.close() + override fun moveToNext() = cursor.moveToNext() + + override fun getInt(index: Int): Int? { + if (cursor.isNull(index)) return null + else return cursor.getInt(index) + } + + override fun getLong(index: Int): Long? { + if (cursor.isNull(index)) return null + else return cursor.getLong(index) + } + + override fun getDouble(index: Int): Double? { + if (cursor.isNull(index)) return null + else return cursor.getDouble(index) + } + + override fun getString(index: Int): String? { + if (cursor.isNull(index)) return null + else return cursor.getString(index) + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.java b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.java deleted file mode 100644 index b6bdd1fa4..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2017 Álinson Santos Xavier - * - * 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 . - * - * - */ - -package org.isoron.uhabits.database; - -import android.content.*; -import android.database.sqlite.*; - -import org.isoron.uhabits.core.database.*; - -import java.util.*; - -public class AndroidDatabase implements Database -{ - private final SQLiteDatabase db; - - public AndroidDatabase(SQLiteDatabase db) - { - this.db = db; - } - - @Override - public Cursor query(String query, String... params) - { - return new AndroidCursor(db.rawQuery(query, params)); - } - - @Override - public void execute(String query, Object... params) - { - db.execSQL(query, params); - } - - @Override - public void beginTransaction() - { - db.beginTransaction(); - } - - @Override - public void setTransactionSuccessful() - { - db.setTransactionSuccessful(); - } - - @Override - public void endTransaction() - { - db.endTransaction(); - } - - @Override - public void close() - { - db.close(); - } - - @Override - public int getVersion() - { - return db.getVersion(); - } - - @Override - public int update(String tableName, - Map map, - String where, - String... params) - { - ContentValues values = mapToContentValues(map); - return db.update(tableName, values, where, params); - } - - @Override - public Long insert(String tableName, Map map) - { - ContentValues values = mapToContentValues(map); - return db.insert(tableName, null, values); - } - - @Override - public void delete(String tableName, String where, String... params) - { - db.delete(tableName, where, params); - } - - private ContentValues mapToContentValues(Map map) - { - ContentValues values = new ContentValues(); - for (Map.Entry entry : map.entrySet()) - { - if (entry.getValue() == null) - values.putNull(entry.getKey()); - else if (entry.getValue() instanceof Integer) - values.put(entry.getKey(), (Integer) entry.getValue()); - else if (entry.getValue() instanceof Long) - values.put(entry.getKey(), (Long) entry.getValue()); - else if (entry.getValue() instanceof Double) - values.put(entry.getKey(), (Double) entry.getValue()); - else if (entry.getValue() instanceof String) - values.put(entry.getKey(), (String) entry.getValue()); - else throw new IllegalStateException( - "unsupported type: " + entry.getValue()); - } - - return values; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.kt b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.kt new file mode 100644 index 000000000..684633098 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabase.kt @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2015-2017 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.database + +import android.content.* +import android.database.sqlite.* +import org.isoron.uhabits.core.database.* + +class AndroidDatabase(private val db: SQLiteDatabase) : Database { + + override fun beginTransaction() = db.beginTransaction() + override fun setTransactionSuccessful() = db.setTransactionSuccessful() + override fun endTransaction() = db.endTransaction() + override fun close() = db.close() + override fun getVersion() = db.version + + override fun query(query: String, vararg params: String) + = AndroidCursor(db.rawQuery(query, params)) + + override fun execute(query: String, vararg params: Any) + = db.execSQL(query, params) + + override fun update(tableName: String, + map: Map, + where: String, + vararg params: String): Int { + val values = mapToContentValues(map) + return db.update(tableName, values, where, params) + } + + override fun insert(tableName: String, map: Map): Long? { + val values = mapToContentValues(map) + return db.insert(tableName, null, values) + } + + override fun delete(tableName: String, + where: String, + vararg params: String) { + db.delete(tableName, where, params) + } + + private fun mapToContentValues(map: Map): ContentValues { + val values = ContentValues() + for ((key, value) in map) { + when (value) { + null -> values.putNull(key) + is Int -> values.put(key, value) + is Long -> values.put(key, value) + is Double -> values.put(key, value) + is String -> values.put(key, value) + else -> throw IllegalStateException( + "unsupported type: " + value) + } + } + return values + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.java b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.java deleted file mode 100644 index d2dc1430a..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2017 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits.database; - -import android.content.*; -import android.database.sqlite.*; -import android.support.annotation.*; - -import org.isoron.androidbase.*; -import org.isoron.uhabits.core.database.*; - -import java.io.*; - -import javax.inject.*; - -public class AndroidDatabaseOpener implements DatabaseOpener -{ - private Context context; - - @Inject - public AndroidDatabaseOpener(@NonNull @AppContext Context context) - { - this.context = context; - } - - @Override - public Database open(@NonNull File file) - { - return new AndroidDatabase( - SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, - SQLiteDatabase.OPEN_READWRITE)); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.kt b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.kt new file mode 100644 index 000000000..8aeb612e9 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/database/AndroidDatabaseOpener.kt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2015-2017 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.database + +import android.database.sqlite.* +import org.isoron.uhabits.core.database.* +import java.io.* +import javax.inject.* + +class AndroidDatabaseOpener @Inject constructor() : DatabaseOpener { + override fun open(file: File): AndroidDatabase { + return AndroidDatabase(SQLiteDatabase.openDatabase( + file.absolutePath, null, SQLiteDatabase.OPEN_READWRITE)) + } +}