package com.bignerdranch.android.criminalintent;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.bignerdranch.android.criminalintent.database.CrimeBaseHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CrimeLab {

    private static CrimeLab sCrimeLab;

    private List<Crime> mCrimes;
    private Context mContext;
    private SQLiteDatabase mDatabase;

    public static CrimeLab get(Context context) {
        if (sCrimeLab == null) {
            sCrimeLab = new CrimeLab(context);
        }
        return sCrimeLab;
    }

    public void removeCrime(Crime crime) {
        mCrimes.remove(crime);
    }

    public void addCrime(Crime crime) {
        mCrimes.add(crime);
    }

    private CrimeLab(Context context) {
        mCrimes = new ArrayList<>();
        mContext = context;
        mDatabase = new CrimeBaseHelper(mContext).getWritableDatabase();
    }

    public List<Crime> getCrimes() {
        return mCrimes;
    }

    public Crime getCrime(UUID id) {
        for (Crime crime : mCrimes) {
            if (crime.getId().equals(id)) {
                return crime;
            }
        }
        return null;
    }
}

@@ -43,16 +43,54 @@
 ntent;%0A%0A
+import android.content.ContentValues;%0A
 import a
@@ -233,16 +233,169 @@
 eHelper;
+%0Aimport com.bignerdranch.android.criminalintent.database.CrimeDbSchema;%0Aimport com.bignerdranch.android.criminalintent.database.CrimeDbSchema.CrimeTable;
 %0A%0Aimport
@@ -532,41 +532,8 @@
 b;%0A%0A
-    private List%3CCrime%3E mCrimes;%0A
     
@@ -825,103 +825,681 @@
     
-mCrimes.remove(crime);%0A    %7D%0A%0A    public void addCrime(Crime crime) %7B%0A        mCrimes.add(crime
+ContentValues contentValues = getContentValues(crime);%0A        mDatabase.delete(CrimeTable.NAME,%0A                            CrimeTable.Cols.UUID + %22 = ?%22, new String%5B%5D %7B crime.getId().toString() %7D);%0A    %7D%0A%0A    public void addCrime(Crime crime) %7B%0A        ContentValues contentValues = getContentValues(crime);%0A        mDatabase.insert(CrimeTable.NAME, null, contentValues);%0A    %7D%0A%0A    public void updateCrime(Crime crime) %7B%0A        ContentValues contentValues = getContentValues(crime);%0A%0A        mDatabase.update(CrimeTable.NAME,%0A                            contentValues,%0A                            CrimeTable.Cols.UUID + %22 = ?%22, new String%5B%5D %7B crime.getId().toString() %7D
 );%0A 
@@ -1548,45 +1548,8 @@
 ) %7B%0A
-        mCrimes = new ArrayList%3C%3E();%0A
     
@@ -1704,23 +1704,33 @@
  return 
-mCrimes
+new ArrayList%3C%3E()
 ;%0A    %7D%0A
@@ -1767,24 +1767,25 @@
 D id) %7B%0A
+%0A
         
 for (Cri
@@ -1780,34 +1780,89 @@
     
-for (Crime c
+return null;%0A    %7D%0A%0A    private static ContentValues getContentValues(C
 rime 
-: mC
+c
 rime
-s
 ) %7B%0A
@@ -1873,97 +1873,356 @@
     
-    if (crime.getId().equals(id)) %7B%0A                return crime;%0A            %7D%0A        %7D
+ContentValues contentValues = new ContentValues();%0A%0A        contentValues.put(CrimeTable.Cols.UUID, crime.getId().toString());%0A        contentValues.put(CrimeTable.Cols.TITLE, crime.getTitle());%0A        contentValues.put(CrimeTable.Cols.DATE, crime.getDate().getTime());%0A        contentValues.put(CrimeTable.Cols.SOLVED, crime.isSolved() ? 1 : 0);%0A
 %0A   
@@ -2225,30 +2225,39 @@
 %0A        return 
-null
+contentValues
 ;%0A    %7D%0A%7D%0A

Update CrimeLab to keep data in the database instead of in memory