Convert automation and database packages to Kotlin

pull/316/head^2
Alinson S. Xavier 8 years ago
parent 180c18f6bf
commit a02376497a

@ -1,50 +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.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);
}
}

@ -0,0 +1,44 @@
/*
* Copyright (C) 2015-2017 Á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.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)
}
}

@ -1,78 +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.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 "???";
}
}
}

@ -0,0 +1,55 @@
/*
* Copyright (C) 2015-2017 Á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.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 "???"
}
}
}

@ -1,105 +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.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<String> names = new LinkedList<>();
for (Habit h : habitList) names.add(h.getName());
ArrayAdapter<String> adapter =
new ArrayAdapter<>(getContext(), simple_spinner_item, names);
adapter.setDropDownViewResource(simple_spinner_dropdown_item);
habitSpinner.setAdapter(adapter);
}
}

@ -0,0 +1,81 @@
/*
* 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.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<String>()) { it.name }
val adapter = ArrayAdapter(context, simple_spinner_item, names)
adapter.setDropDownViewResource(simple_spinner_dropdown_item)
habitSpinner.adapter = adapter
}
}

@ -1,115 +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.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;
}
}

@ -0,0 +1,73 @@
/*
* Copyright (C) 2015-2017 Á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.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)
}

@ -1,80 +0,0 @@
/*
* Copyright (C) 2017 Á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.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);
}
}

@ -0,0 +1,48 @@
/*
* Copyright (C) 2015-2017 Á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.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)
}
}

@ -1,126 +0,0 @@
/*
* Copyright (C) 2017 Á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.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<String, Object> map,
String where,
String... params)
{
ContentValues values = mapToContentValues(map);
return db.update(tableName, values, where, params);
}
@Override
public Long insert(String tableName, Map<String, Object> 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<String, Object> map)
{
ContentValues values = new ContentValues();
for (Map.Entry<String, Object> 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;
}
}

@ -0,0 +1,74 @@
/*
* Copyright (C) 2015-2017 Á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.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<String, Any?>,
where: String,
vararg params: String): Int {
val values = mapToContentValues(map)
return db.update(tableName, values, where, params)
}
override fun insert(tableName: String, map: Map<String, Any?>): 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<String, Any?>): 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
}
}

@ -1,50 +0,0 @@
/*
* Copyright (C) 2017 Á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.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));
}
}

@ -0,0 +1,32 @@
/*
* Copyright (C) 2015-2017 Á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.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))
}
}
Loading…
Cancel
Save