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;

    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 String name,
                              final String birthday,
                              final String startJob,
                              final String job,
                              final String game,
                              final String smartphone,
                              final String photos) {


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

                RealmResults<PeopleModel> resultRealm = realm.where(PeopleModel.class)
                        .findAll();
                int id = resultRealm.size() + 1;

                PeopleModel people = realm.createObject(PeopleModel.class, id);
                people.setName(name);
                people.setBirthday(birthday);
                people.setJobstart(startJob);
                people.setJob(job);
                people.setGame(game);
                people.setSmartphone(smartphone);
                people.setPhoto(photos);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                getView().response("onSuccess");
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                getView().response(error.getMessage());
            }
        });



    }
}

@@ -401,16 +401,83 @@
 m realm;
+%0A    private RealmResults%3CPeopleModel%3E resultRealm;%0A    int id = 0;
 %0A%0A    pu
@@ -813,32 +813,33 @@
 close();%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -876,641 +876,339 @@
 nal 
-String name,%0A                              final String birthday,%0A                              final String startJob,%0A                              final String job,%0A                              final String game,%0A                              final String smartphone,%0A                              final String photos) %7B%0A%0A%0A        realm.executeTransactionAsync(new Realm.Transaction() %7B%0A            @Override%0A            public void execute(Realm bgRealm) %7B%0A%0A                RealmResults%3CPeopleModel%3E resultRealm = realm.where(PeopleModel.class)%0A                        .findAll();%0A                int id = resultRealm.size() + 1;
+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            @Override%0A            public void execute(Realm bgRealm) %7B
 %0A%0A  
@@ -1242,17 +1242,19 @@
 eople = 
-r
+bgR
 ealm.cre
@@ -1322,12 +1322,29 @@
 ame(
-n
+peopleModel.getN
 ame
+()
 );%0A 
@@ -1381,16 +1381,33 @@
 day(
-b
+peopleModel.getB
 irthday
+()
 );%0A 
@@ -1444,16 +1444,33 @@
 art(
+peopleModel.getJob
 start
-Job
+()
 );%0A 
@@ -1502,11 +1502,28 @@
 Job(
-job
+peopleModel.getJob()
 );%0A 
@@ -1556,12 +1556,29 @@
 ame(
-g
+peopleModel.getG
 ame
+()
 );%0A 
@@ -1613,17 +1613,32 @@
 rtphone(
-s
+peopleModel.getS
 martphon
@@ -1638,16 +1638,18 @@
 artphone
+()
 );%0A     
@@ -1680,13 +1680,29 @@
 to(p
+eopleModel.getP
 hoto
-s
+()
 );%0A 
@@ -2084,19 +2084,16 @@
     %7D);%0A
-%0A%0A%0A
     %7D%0A%7D%0A

Edit insert to Realm , Presenter