// Path: app/src/main/java/leonardo2204/com/br/flowtests/data/repository/ContactsRepositoryImpl.java
// public class ContactsRepositoryImpl implements ContactsRepository {
// 
//     private final ContentResolver contentResolver;
// 
//     public ContactsRepositoryImpl(ContentResolver contentResolver) {
//         this.contentResolver = contentResolver;
//     }
// 
//     @Override
//     public Observable<List<Contact>> getContactsFromPhone(final boolean mustHaveNumber) {
//         return Observable.create(new Observable.OnSubscribe<List<Contact>>() {
//             @Override
//             public void call(Subscriber<? super List<Contact>> subscriber) {
//                 Cursor queryContacts;
// 
//                 if(mustHaveNumber)
//                     queryContacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
//                             null, ContactsContract.Contacts.HAS_PHONE_NUMBER + " > ?",new String[]{"0"}, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
//                 else
//                     queryContacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
//                             null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
// 
//                 if(queryContacts.getCount() > 0) {
//                     List<Contact> contacts = new ArrayList<>(queryContacts.getColumnCount());
// 
//                     while (queryContacts.moveToNext()) {
//                         Contact contact = new Contact();
// 
//                         String id = queryContacts.getString(
//                                 queryContacts.getColumnIndex(ContactsContract.Contacts._ID));
//                         String name = queryContacts.getString(
//                                 queryContacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// 
//                         contact.setId(id);
//                         contact.setName(name);
//                         contacts.add(contact);
//                     }
//                     subscriber.onNext(contacts);
//                     subscriber.onCompleted();
//                 }else{
//                     subscriber.onError(new Exception("No contacts were found"));
//                     subscriber.onCompleted();
//                 }
//                 queryContacts.close();
//             }
//         });
//     }
// 
//     @Override
//     public Observable<Contact> getContactById(final Contact contact) {
//         return Observable.create(new Observable.OnSubscribe<Contact>() {
//             @Override
//             public void call(Subscriber<? super Contact> subscriber) {
//                 Cursor queryContacts = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
//                         new String[] {ContactsContract.Contacts._ID,
//                                 ContactsContract.CommonDataKinds.Phone.NUMBER},
//                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
//                         new String[]{contact.getId()},
//                         null);
// 
//                 if(queryContacts.getCount() > 0) {
//                     while (queryContacts.moveToNext()) {
//                         Cursor pCur = contentResolver.query(
//                                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
//                                 null,
//                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
//                                 new String[]{contact.getId()}, null);
// 
//                         List<String> phones = new ArrayList<>();
//                         contact.setTelephone(phones);
// 
//                         while (pCur.moveToNext()) {
//                             contact.getTelephone().add(pCur.getString((pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
//                         }
//                         pCur.close();
//                     }
//                     subscriber.onNext(contact);
//                     subscriber.onCompleted();
//                 }else{
//                     subscriber.onError(new Exception("No telephones were found"));
//                     subscriber.onCompleted();
//                 }
// 
//                 queryContacts.close();
//             }
//         });
//     }
// 
// }


import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import dagger.Module;
import dagger.Provides;
import leonardo2204.com.br.flowtests.UIThread;
import leonardo2204.com.br.flowtests.data.executor.JobExecutor;
import leonardo2204.com.br.flowtests.data.repository.ContactsRepositoryImpl;
import leonardo2204.com.br.flowtests.di.scope.ApplicationScope;
import leonardo2204.com.br.flowtests.domain.executor.PostExecutionThread;
import leonardo2204.com.br.flowtests.domain.executor.ThreadExecutor;
import leonardo2204.com.br.flowtests.domain.repository.ContactsRepository;
        return application;
    }

    @Provides
    @ApplicationScope
    public UIThread provideUIThread() {
        return new UIThread();
    }

    @Provides
    @ApplicationScope
    public ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {
        return jobExecutor;
    }

    @Provides
    @ApplicationScope
    public PostExecutionThread providePostExecutionThread(UIThread uiThread) {
        return uiThread;
    }

    @Provides
    @ApplicationScope
    public ContentResolver providesContentResolver() {
        return this.application.getContentResolver();
    }

    @Provides
    @ApplicationScope
    public ContactsRepository providesContactsRepository(ContentResolver contentResolver) {
// Path: app/src/main/java/leonardo2204/com/br/flowtests/data/repository/ContactsRepositoryImpl.java
// public class ContactsRepositoryImpl implements ContactsRepository {
// 
//     private final ContentResolver contentResolver;
// 
//     public ContactsRepositoryImpl(ContentResolver contentResolver) {
//         this.contentResolver = contentResolver;
//     }
// 
//     @Override
//     public Observable<List<Contact>> getContactsFromPhone(final boolean mustHaveNumber) {
//         return Observable.create(new Observable.OnSubscribe<List<Contact>>() {
//             @Override
//             public void call(Subscriber<? super List<Contact>> subscriber) {
//                 Cursor queryContacts;
// 
//                 if(mustHaveNumber)
//                     queryContacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
//                             null, ContactsContract.Contacts.HAS_PHONE_NUMBER + " > ?",new String[]{"0"}, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
//                 else
//                     queryContacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
//                             null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
// 
//                 if(queryContacts.getCount() > 0) {
//                     List<Contact> contacts = new ArrayList<>(queryContacts.getColumnCount());
// 
//                     while (queryContacts.moveToNext()) {
//                         Contact contact = new Contact();
// 
//                         String id = queryContacts.getString(
//                                 queryContacts.getColumnIndex(ContactsContract.Contacts._ID));
//                         String name = queryContacts.getString(
//                                 queryContacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// 
//                         contact.setId(id);
//                         contact.setName(name);
//                         contacts.add(contact);
//                     }
//                     subscriber.onNext(contacts);
//                     subscriber.onCompleted();
//                 }else{
//                     subscriber.onError(new Exception("No contacts were found"));
//                     subscriber.onCompleted();
//                 }
//                 queryContacts.close();
//             }
//         });
//     }
// 
//     @Override
//     public Observable<Contact> getContactById(final Contact contact) {
//         return Observable.create(new Observable.OnSubscribe<Contact>() {
//             @Override
//             public void call(Subscriber<? super Contact> subscriber) {
//                 Cursor queryContacts = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
//                         new String[] {ContactsContract.Contacts._ID,
//                                 ContactsContract.CommonDataKinds.Phone.NUMBER},
//                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
//                         new String[]{contact.getId()},
//                         null);
// 
//                 if(queryContacts.getCount() > 0) {
//                     while (queryContacts.moveToNext()) {
//                         Cursor pCur = contentResolver.query(
//                                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
//                                 null,
//                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
//                                 new String[]{contact.getId()}, null);
// 
//                         List<String> phones = new ArrayList<>();
//                         contact.setTelephone(phones);
// 
//                         while (pCur.moveToNext()) {
//                             contact.getTelephone().add(pCur.getString((pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
//                         }
//                         pCur.close();
//                     }
//                     subscriber.onNext(contact);
//                     subscriber.onCompleted();
//                 }else{
//                     subscriber.onError(new Exception("No telephones were found"));
//                     subscriber.onCompleted();
//                 }
// 
//                 queryContacts.close();
//             }
//         });
//     }
// 
// }


// Path: app/src/main/java/leonardo2204/com/br/flowtests/di/module/AppModule.java
import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import dagger.Module;
import dagger.Provides;
import leonardo2204.com.br.flowtests.UIThread;
import leonardo2204.com.br.flowtests.data.executor.JobExecutor;
import leonardo2204.com.br.flowtests.data.repository.ContactsRepositoryImpl;
import leonardo2204.com.br.flowtests.di.scope.ApplicationScope;
import leonardo2204.com.br.flowtests.domain.executor.PostExecutionThread;
import leonardo2204.com.br.flowtests.domain.executor.ThreadExecutor;
import leonardo2204.com.br.flowtests.domain.repository.ContactsRepository;

        return application;
    }

    @Provides
    @ApplicationScope
    public UIThread provideUIThread() {
        return new UIThread();
    }

    @Provides
    @ApplicationScope
    public ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {
        return jobExecutor;
    }

    @Provides
    @ApplicationScope
    public PostExecutionThread providePostExecutionThread(UIThread uiThread) {
        return uiThread;
    }

    @Provides
    @ApplicationScope
    public ContentResolver providesContentResolver() {
        return this.application.getContentResolver();
    }

    @Provides
    @ApplicationScope
    public ContactsRepository providesContactsRepository(ContentResolver contentResolver) {
        return new ContactsRepositoryImpl(contentResolver);