package ca.antonious.habittracker;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ca.antonious.habittracker.models.Habit;
import ca.antonious.habittracker.observable.IObservable;
import ca.antonious.habittracker.observable.IObserver;

/**
 * Created by George on 2016-09-02.
 */
public class HabitRepository implements IObservable<List<Habit>> {
    private IHabitService habitService;
    private List<Habit> habits;

    private Set<IObserver<List<Habit>>> observers = new HashSet<>();

    public HabitRepository(IHabitService habitService) {
        this.habitService = habitService;
        this.habits = new ArrayList<>();
    }

    public List<Habit> getHabits() {
        if (habits.isEmpty()) {
            habits.addAll(habitService.getHabits());
        }
        return habits;
    }

    public void addHabit(Habit habit) {
        // get habits to make sure state is good
        getHabits();

        habitService.addHabit(habit);
        habits.add(habit);
        notifyChange();
    }

    public void updateHabit(Habit habit) {
        // get habits to make sure state is good

        habitService.updateHabit(habit);
        notifyChange();
    }

    public void removeHabit(String id) {
        // get habits to make sure state is good
        habitService.removeHabit(id);
        notifyChange();
    }

    private void notifyChange() {
        for (IObserver<List<Habit>> observer: observers) {
            observer.onNext(getHabits());
        }
    }

    @Override
    public void addObserver(IObserver<List<Habit>> observer) {
        observers.add(observer);
        observer.onNext(getHabits());
    }

    @Override
    public void removeObserver(IObserver<List<Habit>> observer) {
        observers.remove(observer);
    }

    @Override
    public void removeAllObservers() {
        observers.clear();
    }
}

@@ -53,24 +53,50 @@
 .ArrayList;%0A
+import java.util.HashMap;%0A
 import java.
@@ -128,24 +128,46 @@
 .util.List;%0A
+import java.util.Map;%0A
 import java.
@@ -500,21 +500,28 @@
 private 
-List%3C
+Map%3CString, 
 Habit%3E h
@@ -723,25 +723,23 @@
 s = new 
-ArrayList
+HashMap
 %3C%3E();%0A  
@@ -828,22 +828,26 @@
     
-habits.addAll(
+for (Habit habit: 
 habi
@@ -871,138 +871,180 @@
 s())
-;%0A        %7D%0A        return habits;%0A    %7D%0A%0A    public void addHabit(Habit habit) %7B%0A        // get habits to make sure state is good
+ %7B%0A                habits.put(habit.getId(), habit);%0A            %7D%0A        %7D%0A        return new ArrayList%3C%3E(habits.values());%0A    %7D%0A%0A    public void addHabit(Habit habit) %7B
 %0A   
@@ -1115,20 +1115,35 @@
  habits.
-add(
+put(habit.getId(), 
 habit);%0A
@@ -1227,49 +1227,21 @@
     
- //
  get
- h
+H
 abits
- to make sure state is good
+();
 %0A%0A  
@@ -1271,32 +1271,74 @@
 teHabit(habit);%0A
+        habits.put(habit.getId(), habit);%0A
         notifyCh
@@ -1404,49 +1404,22 @@
     
- //
  get
- h
+H
 abits
- to make sure state is good
+();%0A
 %0A   
@@ -1453,16 +1453,43 @@
 it(id);%0A
+        habits.remove(id);%0A
         

Use map in habit repository to make it easier to update/remove