package com.naijab.nextzytimeline.ui.people.addform;

import com.naijab.nextzytimeline.base.BaseMvpPresenter;
import com.naijab.nextzytimeline.ui.people.model.PeopleModel;

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

public class AddPeopleFragmentPresenter extends BaseMvpPresenter<AddPeopleFragmentInterface.View>
        implements AddPeopleFragmentInterface.Presenter {

    private Realm realm;
    private RealmResults<PeopleModel> resultRealm;
    int id = 0;

    public static AddPeopleFragmentInterface.Presenter create() {
        return new AddPeopleFragmentPresenter();
    }

    @Override
    public void onViewStart() {
        super.onViewStart();
        realm = Realm.getDefaultInstance();
    }

    @Override
    public void onViewStop() {
        super.onViewStop();
        realm.close();
    }


    @Override
    public void saveIntoRealm(final PeopleModel peopleModel) {

        resultRealm = realm.where(PeopleModel.class)
                .findAll();
        id = resultRealm.size() + 1;

        Realm realm = Realm.getDefaultInstance();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {

                PeopleModel people = bgRealm.createObject(PeopleModel.class, id);
                people.setName(peopleModel.getName());
                people.setBirthday(peopleModel.getBirthday());
                people.setJobstart(peopleModel.getJobstart());
                people.setJob(peopleModel.getJob());
                people.setGame(peopleModel.getGame());
                people.setSmartphone(peopleModel.getSmartphone());
                people.setPhoto(peopleModel.getPhoto());
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                getView().response("onSuccess");
                getView().saveIsFinish(true);
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                getView().response(error.getMessage());
                getView().saveIsFinish(false);
            }
        });
    }
}

@@ -47,16 +47,49 @@
 dform;%0A%0A
+import android.content.Context;%0A%0A
 import c
@@ -136,16 +136,80 @@
 senter;%0A
+import com.naijab.nextzytimeline.ui.people.model.PeopleManager;%0A
 import c
@@ -289,38 +289,8 @@
 alm;
-%0Aimport io.realm.RealmResults;
 %0A%0Apu
@@ -472,75 +472,8 @@
 alm;
-%0A    private RealmResults%3CPeopleModel%3E resultRealm;%0A    int id = 0;
 %0A%0A  
@@ -719,434 +719,313 @@
     
-%7D%0A%0A
     
-@Override%0A    public void onViewStop() %7B%0A        super.onViewStop();%0A        realm.close();%0A    %7D%0A%0A%0A    @Override%0A    public void saveIntoRealm(final PeopleModel peopleModel) %7B%0A%0A        resultRealm = realm.where(PeopleModel.class)%0A                .findAll();%0A        id = resultRealm.size() + 1;%0A%0A        Realm realm = Realm.getDefaultInstance();%0A        realm.executeTransactionAsync(new Realm.Transaction() %7B%0A    
+String name = PeopleManager.getInstance().getPeople().get(4).getName();%0A        String job = PeopleManager.getInstance().getPeople().get(4).getJob();%0A        String photos = PeopleManager.getInstance().getPeople().get(4).getPhoto();%0A        getView().response(name + %22 %22 + job + %22 %22 + photos);%0A
     
+%7D%0A%0A
     
@@ -1030,32 +1030,24 @@
   @Override%0A
-        
     public v
@@ -1054,585 +1054,268 @@
 oid 
-execute(Realm bgRealm) %7B%0A%0A                PeopleModel people = bgRealm.createObject(PeopleModel.class, id);%0A                people.setName(peopleModel.getNam
+onViewStop() %7B%0A        super.onViewStop();%0A        realm.clos
 e()
-)
 ;%0A    
-            people.setBirthday(peopleModel.getBirthday());%0A                people.setJobstart(peopleModel.getJobstart());%0A                people.setJob(peopleModel.getJob());%0A                people.setGame(peopleModel.getGame());%0A                people.setSmartphone(peopleModel.getSmartphone());%0A                people.setPhoto(peopleModel.getPhoto());%0A            %7D%0A        %7D, new Realm.Transaction.OnSuccess
+%7D%0A%0A%0A    @Override%0A    public void saveIntoRealm(PeopleModel peopleModel, Context context) %7B%0A        PeopleManager.getInstance().saveRealm(peopleModel, context, new PeopleManager.SaveCallback
 () %7B
@@ -1368,15 +1368,33 @@
  onS
+aveS
 uccess(
+String message
 ) %7B%0A
@@ -1432,126 +1432,32 @@
 nse(
-%22onSuccess%22);%0A                getView().saveIsFinish(true);%0A            %7D%0A        %7D, new Realm.Transaction.OnError() %7B
+message);%0A            %7D%0A
 %0A   
@@ -1505,29 +1505,32 @@
 d on
+Save
 Error(
-Throwable error
+String message
 ) %7B%0A
@@ -1568,72 +1568,14 @@
 nse(
-error.getMessage());%0A                getView().saveIsFinish(fals
+messag
 e);%0A

Edit Presenter saveIntoRealm