package com.home.young.myPassword.database;

import android.content.Context;

import com.home.young.myPassword.model.PasswordRealm;

import java.util.UUID;

import io.realm.Realm;
import io.realm.RealmResults;

//TODO: 添加版本的代码（RealmMigration must be provided）
public class PasswordDBRealm {

    private Realm getRealm(Context context) {
        Realm.init(context);
        return Realm.getDefaultInstance();
    }

    public String insertPasswordRealm(Context context, PasswordRealm passwordRealm) {
        passwordRealm.setId(UUID.randomUUID().toString());
        Realm realm = getRealm(context);
        realm.beginTransaction();
        realm.copyToRealm(passwordRealm);
        realm.commitTransaction();
        return passwordRealm.getId();
    }

    public RealmResults<PasswordRealm> getAllPasswordRealm(Context context) {
        Realm realm = getRealm(context);
        final RealmResults<PasswordRealm> passwordRealms = realm.where(PasswordRealm.class).findAll();
        return passwordRealms;
    }
}

@@ -69,16 +69,47 @@
 Context;
+%0Aimport android.text.TextUtils;
 %0A%0Aimport
@@ -316,16 +316,200 @@
 ealm %7B%0A%0A
+    private Realm realm;%0A    private String encryptKey;%0A%0A    public PasswordDBRealm(Context context) %7B%0A        Realm.init(context);%0A        realm = Realm.getDefaultInstance();%0A    %7D%0A%0A%0A
     priv
@@ -649,16 +649,24 @@
 g insert
+OrUpdate
 Password
@@ -675,153 +675,244 @@
 alm(
-Context context, PasswordRealm passwordRealm) %7B%0A        passwordRealm.setId(UUID.randomUUID().toString());%0A        Realm realm = getRealm(context
+PasswordRealm passwordRealm) %7B%0A        if (TextUtils.isEmpty(passwordRealm.getId())) %7B%0A            passwordRealm.setId(UUID.randomUUID().toString());%0A        %7D%0A        realm.beginTransaction();%0A        realm.insertOrUpdate(passwordRealm
 );%0A 
@@ -916,37 +916,38 @@
 ;%0A        realm.
-begin
+commit
 Transaction();%0A 
@@ -959,24 +959,93 @@
   re
-alm.copyToRealm(
+turn passwordRealm.getId();%0A    %7D%0A%0A    public void deletePasswordRealm(PasswordRealm 
 pass
@@ -1046,33 +1046,34 @@
 m passwordRealm)
-;
+ %7B
 %0A        realm.c
@@ -1063,38 +1063,37 @@
 %7B%0A        realm.
-commit
+begin
 Transaction();%0A 
@@ -1103,43 +1103,258 @@
     
-return passwordRealm.getId();%0A    %7D
+RealmResults%3CPasswordRealm%3E realmResults = realm.where(PasswordRealm.class).equalTo(%22id%22, passwordRealm.getId()).findAll();%0A        if (realmResults.size() %3E 0) %7B%0A            realmResults.remove(1);%0A        %7D%0A        realm.commitTransaction();%0A    %7D%0A
 %0A%0A  
@@ -1609,11 +1609,67 @@
 s;%0A    %7D
+%0A%0A    public void Close() %7B%0A        realm.close();%0A    %7D
 %0A%7D%0A

Add Realm