From 1db2f69f05bc33960012e565d73a1c7a7749dc1a Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Tue, 22 Mar 2016 21:32:37 -0400 Subject: [PATCH] Import data from HabitBull Closes #44 --- .../isoron/uhabits/io/GenericImporter.java | 1 + .../uhabits/io/HabitBullCSVImporter.java | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 app/src/main/java/org/isoron/uhabits/io/HabitBullCSVImporter.java diff --git a/app/src/main/java/org/isoron/uhabits/io/GenericImporter.java b/app/src/main/java/org/isoron/uhabits/io/GenericImporter.java index 38aa6eab8..1ecc4c1fa 100644 --- a/app/src/main/java/org/isoron/uhabits/io/GenericImporter.java +++ b/app/src/main/java/org/isoron/uhabits/io/GenericImporter.java @@ -35,6 +35,7 @@ public class GenericImporter extends AbstractImporter importers = new LinkedList<>(); importers.add(new RewireDBImporter()); importers.add(new TickmateDBImporter()); + importers.add(new HabitBullCSVImporter()); } @Override diff --git a/app/src/main/java/org/isoron/uhabits/io/HabitBullCSVImporter.java b/app/src/main/java/org/isoron/uhabits/io/HabitBullCSVImporter.java new file mode 100644 index 000000000..efa4cd933 --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/io/HabitBullCSVImporter.java @@ -0,0 +1,90 @@ +/* + * 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.io; + +import android.support.annotation.NonNull; +import android.util.Log; + +import com.opencsv.CSVReader; + +import org.isoron.helpers.DateHelper; +import org.isoron.uhabits.models.Habit; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Calendar; +import java.util.HashMap; + +public class HabitBullCSVImporter extends AbstractImporter +{ + @Override + public boolean canHandle(@NonNull File file) throws IOException + { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line = reader.readLine(); + + return line.startsWith("HabitName,HabitDescription,HabitCategory"); + } + + @Override + public void importHabitsFromFile(@NonNull File file) throws IOException + { + CSVReader reader = new CSVReader(new FileReader(file)); + HashMap habits = new HashMap<>(); + + for(String line[] : reader) + { + String name = line[0]; + if(name.equals("HabitName")) continue; + + String description = line[1]; + String dateString[] = line[3].split("-"); + int year = Integer.parseInt(dateString[0]); + int month = Integer.parseInt(dateString[1]); + int day = Integer.parseInt(dateString[2]); + + Calendar date = DateHelper.getStartOfTodayCalendar(); + date.set(year, month - 1, day); + + long timestamp = date.getTimeInMillis(); + + int value = Integer.parseInt(line[4]); + if(value != 1) continue; + + Habit h = habits.get(name); + + if(h == null) + { + h = new Habit(); + h.name = name; + h.description = description; + h.freqNum = h.freqDen = 1; + h.save(); + + habits.put(name, h); + } + + if(!h.repetitions.contains(timestamp)) + h.repetitions.toggle(timestamp); + } + } +}