From 89164952ab8943359953b86071e0d7b636b6fe81 Mon Sep 17 00:00:00 2001 From: zhengxiaopeng Date: Thu, 5 Nov 2015 10:04:06 +0800 Subject: [PATCH] remove dagger(di) --- build.gradle | 1 - buildsystem/dependencies.gradle | 13 +- data/build.gradle | 4 - .../sample/data/cache/FileManager.java | 218 ++++++----- .../sample/data/cache/UserCacheImpl.java | 341 +++++++++--------- .../data/cache/serializer/JsonSerializer.java | 4 - .../entity/mapper/UserEntityDataMapper.java | 86 +++-- .../entity/mapper/UserEntityJsonMapper.java | 2 - .../sample/data/executor/JobExecutor.java | 71 ++-- .../data/repository/UserDataRepository.java | 7 +- .../datasource/UserDataStoreFactory.java | 4 - .../sample/data/ApplicationTestCase.java | 2 +- domain/build.gradle | 4 - .../domain/interactor/GetUserDetails.java | 34 +- .../sample/domain/interactor/GetUserList.java | 30 +- presentation/build.gradle | 4 - .../presentation/AndroidApplication.java | 30 +- .../sample/presentation/UIThread.java | 23 +- .../internal/di/HasComponent.java | 23 -- .../presentation/internal/di/PerActivity.java | 30 -- .../di/components/ActivityComponent.java | 35 -- .../di/components/ApplicationComponent.java | 40 -- .../internal/di/components/UserComponent.java | 34 -- .../internal/di/modules/ActivityModule.java | 40 -- .../di/modules/ApplicationModule.java | 67 ---- .../internal/di/modules/UserModule.java | 53 --- .../mapper/UserModelDataMapper.java | 4 - .../presentation/navigation/Navigator.java | 4 - .../presenter/UserDetailsPresenter.java | 7 +- .../presenter/UserListPresenter.java | 203 ++++++----- .../view/activity/BaseActivity.java | 25 +- .../view/activity/UserDetailsActivity.java | 101 +++--- .../view/activity/UserListActivity.java | 46 +-- .../view/fragment/BaseFragment.java | 8 - .../view/fragment/UserDetailsFragment.java | 290 ++++++++------- .../view/fragment/UserListFragment.java | 323 ++++++++++------- 36 files changed, 931 insertions(+), 1280 deletions(-) delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/HasComponent.java delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/PerActivity.java delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ActivityComponent.java delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ApplicationComponent.java delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/UserComponent.java delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ActivityModule.java delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ApplicationModule.java delete mode 100644 presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/UserModule.java diff --git a/build.gradle b/build.gradle index ae9f07d4..cc5e5fff 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,6 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' - classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } } diff --git a/buildsystem/dependencies.gradle b/buildsystem/dependencies.gradle index 0d7ee638..8c081d8f 100644 --- a/buildsystem/dependencies.gradle +++ b/buildsystem/dependencies.gradle @@ -12,12 +12,10 @@ ext { androidCompileSdkVersion = 23 //Libraries - daggerVersion = '2.0' butterKnifeVersion = '7.0.1' recyclerViewVersion = '21.0.3' rxJavaVersion = '1.0.14' - rxAndroidVersion = '0.25.0' - javaxAnnotationVersion = '1.0' + rxAndroidVersion = '1.0.1' gsonVersion = '2.3' okHttpVersion = '2.5.0' androidAnnotationsVersion = '20.0.0' @@ -33,13 +31,10 @@ ext { presentationDependencies = [ - daggerCompiler: "com.google.dagger:dagger-compiler:${daggerVersion}", - dagger: "com.google.dagger:dagger:${daggerVersion}", butterKnife: "com.jakewharton:butterknife:${butterKnifeVersion}", recyclerView: "com.android.support:recyclerview-v7:${recyclerViewVersion}", rxJava: "io.reactivex:rxjava:${rxJavaVersion}", rxAndroid: "io.reactivex:rxandroid:${rxAndroidVersion}", - javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}", ] presentationTestDependencies = [ @@ -51,9 +46,6 @@ ext { ] domainDependencies = [ - daggerCompiler: "com.google.dagger:dagger-compiler:${daggerVersion}", - dagger: "com.google.dagger:dagger:${daggerVersion}", - javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}", rxJava: "io.reactivex:rxjava:${rxJavaVersion}", ] @@ -63,13 +55,10 @@ ext { ] dataDependencies = [ - daggerCompiler: "com.google.dagger:dagger-compiler:${daggerVersion}", - dagger: "com.google.dagger:dagger:${daggerVersion}", okHttp: "com.squareup.okhttp:okhttp:${okHttpVersion}", gson: "com.google.code.gson:gson:${gsonVersion}", rxJava: "io.reactivex:rxjava:${rxJavaVersion}", rxAndroid: "io.reactivex:rxandroid:${rxAndroidVersion}", - javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}", androidAnnotations: "com.android.support:support-annotations:${androidAnnotationsVersion}" ] diff --git a/data/build.gradle b/data/build.gradle index ee9e2952..df723b8e 100644 --- a/data/build.gradle +++ b/data/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'com.android.library' -apply plugin: 'com.neenbedankt.android-apt' android { def globalConfiguration = rootProject.extensions.getByName("ext") @@ -40,9 +39,6 @@ dependencies { def testDependencies = rootProject.ext.dataTestDependencies compile project(':domain') - apt dataDependencies.daggerCompiler - provided dataDependencies.javaxAnnotation - compile dataDependencies.dagger compile dataDependencies.okHttp compile dataDependencies.gson compile dataDependencies.rxJava diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/cache/FileManager.java b/data/src/main/java/com/fernandocejas/android10/sample/data/cache/FileManager.java index 670ee93f..671b2d29 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/cache/FileManager.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/cache/FileManager.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,131 +17,129 @@ import android.content.Context; import android.content.SharedPreferences; + import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import javax.inject.Inject; -import javax.inject.Singleton; /** * Helper class to do operations on regular files/directories. */ -@Singleton public class FileManager { - @Inject - public FileManager() {} + public FileManager() { + } - /** - * Writes a file to Disk. - * This is an I/O operation and this method executes in the main thread, so it is recommended to - * perform this operation using another thread. - * - * @param file The file to write to Disk. - */ - public void writeToFile(File file, String fileContent) { - if (!file.exists()) { - try { - FileWriter writer = new FileWriter(file); - writer.write(fileContent); - writer.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { + /** + * Writes a file to Disk. + * This is an I/O operation and this method executes in the main thread, so it is recommended to + * perform this operation using another thread. + * + * @param file The file to write to Disk. + */ + public void writeToFile(File file, String fileContent) { + if (!file.exists()) { + try { + FileWriter writer = new FileWriter(file); + writer.write(fileContent); + writer.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { - } - } - } + } + } + } - /** - * Reads a content from a file. - * This is an I/O operation and this method executes in the main thread, so it is recommended to - * perform the operation using another thread. - * - * @param file The file to read from. - * @return A string with the content of the file. - */ - public String readFileContent(File file) { - StringBuilder fileContentBuilder = new StringBuilder(); - if (file.exists()) { - String stringLine; - try { - FileReader fileReader = new FileReader(file); - BufferedReader bufferedReader = new BufferedReader(fileReader); - while ((stringLine = bufferedReader.readLine()) != null) { - fileContentBuilder.append(stringLine + "\n"); - } - bufferedReader.close(); - fileReader.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } + /** + * Reads a content from a file. + * This is an I/O operation and this method executes in the main thread, so it is recommended to + * perform the operation using another thread. + * + * @param file The file to read from. + * @return A string with the content of the file. + */ + public String readFileContent(File file) { + StringBuilder fileContentBuilder = new StringBuilder(); + if (file.exists()) { + String stringLine; + try { + FileReader fileReader = new FileReader(file); + BufferedReader bufferedReader = new BufferedReader(fileReader); + while ((stringLine = bufferedReader.readLine()) != null) { + fileContentBuilder.append(stringLine + "\n"); + } + bufferedReader.close(); + fileReader.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } - return fileContentBuilder.toString(); - } + return fileContentBuilder.toString(); + } - /** - * Returns a boolean indicating whether this file can be found on the underlying file system. - * - * @param file The file to check existence. - * @return true if this file exists, false otherwise. - */ - public boolean exists(File file) { - return file.exists(); - } + /** + * Returns a boolean indicating whether this file can be found on the underlying file system. + * + * @param file The file to check existence. + * @return true if this file exists, false otherwise. + */ + public boolean exists(File file) { + return file.exists(); + } - /** - * Warning: Deletes the content of a directory. - * This is an I/O operation and this method executes in the main thread, so it is recommended to - * perform the operation using another thread. - * - * @param directory The directory which its content will be deleted. - */ - public void clearDirectory(File directory) { - if (directory.exists()) { - for (File file : directory.listFiles()) { - file.delete(); - } - } - } + /** + * Warning: Deletes the content of a directory. + * This is an I/O operation and this method executes in the main thread, so it is recommended to + * perform the operation using another thread. + * + * @param directory The directory which its content will be deleted. + */ + public void clearDirectory(File directory) { + if (directory.exists()) { + for (File file : directory.listFiles()) { + file.delete(); + } + } + } - /** - * Write a value to a user preferences file. - * - * @param context {@link android.content.Context} to retrieve android user preferences. - * @param preferenceFileName A file name reprensenting where data will be written to. - * @param key A string for the key that will be used to retrieve the value in the future. - * @param value A long representing the value to be inserted. - */ - public void writeToPreferences(Context context, String preferenceFileName, String key, - long value) { + /** + * Write a value to a user preferences file. + * + * @param context {@link android.content.Context} to retrieve android user preferences. + * @param preferenceFileName A file name reprensenting where data will be written to. + * @param key A string for the key that will be used to retrieve the value in the future. + * @param value A long representing the value to be inserted. + */ + public void writeToPreferences(Context context, String preferenceFileName, String key, + long value) { - SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceFileName, - Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putLong(key, value); - editor.apply(); - } + SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceFileName, + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putLong(key, value); + editor.apply(); + } - /** - * Get a value from a user preferences file. - * - * @param context {@link android.content.Context} to retrieve android user preferences. - * @param preferenceFileName A file name representing where data will be get from. - * @param key A key that will be used to retrieve the value from the preference file. - * @return A long representing the value retrieved from the preferences file. - */ - public long getFromPreferences(Context context, String preferenceFileName, String key) { - SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceFileName, - Context.MODE_PRIVATE); - return sharedPreferences.getLong(key, 0); - } + /** + * Get a value from a user preferences file. + * + * @param context {@link android.content.Context} to retrieve android user preferences. + * @param preferenceFileName A file name representing where data will be get from. + * @param key A key that will be used to retrieve the value from the preference file. + * @return A long representing the value retrieved from the preferences file. + */ + public long getFromPreferences(Context context, String preferenceFileName, String key) { + SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceFileName, + Context.MODE_PRIVATE); + return sharedPreferences.getLong(key, 0); + } } diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/cache/UserCacheImpl.java b/data/src/main/java/com/fernandocejas/android10/sample/data/cache/UserCacheImpl.java index 1b723fb3..6e3454a5 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/cache/UserCacheImpl.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/cache/UserCacheImpl.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,180 +16,187 @@ package com.fernandocejas.android10.sample.data.cache; import android.content.Context; + import com.fernandocejas.android10.sample.data.cache.serializer.JsonSerializer; import com.fernandocejas.android10.sample.data.entity.UserEntity; import com.fernandocejas.android10.sample.data.exception.UserNotFoundException; import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor; + import java.io.File; -import javax.inject.Inject; -import javax.inject.Singleton; + import rx.Observable; import rx.Subscriber; /** * {@link UserCache} implementation. */ -@Singleton public class UserCacheImpl implements UserCache { - private static final String SETTINGS_FILE_NAME = "com.fernandocejas.android10.SETTINGS"; - private static final String SETTINGS_KEY_LAST_CACHE_UPDATE = "last_cache_update"; - - private static final String DEFAULT_FILE_NAME = "user_"; - private static final long EXPIRATION_TIME = 60 * 10 * 1000; - - private final Context context; - private final File cacheDir; - private final JsonSerializer serializer; - private final FileManager fileManager; - private final ThreadExecutor threadExecutor; - - /** - * Constructor of the class {@link UserCacheImpl}. - * - * @param context A - * @param userCacheSerializer {@link JsonSerializer} for object serialization. - * @param fileManager {@link FileManager} for saving serialized objects to the file system. - */ - @Inject - public UserCacheImpl(Context context, JsonSerializer userCacheSerializer, - FileManager fileManager, ThreadExecutor executor) { - if (context == null || userCacheSerializer == null || fileManager == null || executor == null) { - throw new IllegalArgumentException("Invalid null parameter"); - } - this.context = context.getApplicationContext(); - this.cacheDir = this.context.getCacheDir(); - this.serializer = userCacheSerializer; - this.fileManager = fileManager; - this.threadExecutor = executor; - } - - @Override public synchronized Observable get(final int userId) { - return Observable.create(new Observable.OnSubscribe() { - @Override public void call(Subscriber subscriber) { - File userEntityFile = UserCacheImpl.this.buildFile(userId); - String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile); - UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent); - - if (userEntity != null) { - subscriber.onNext(userEntity); - subscriber.onCompleted(); - } else { - subscriber.onError(new UserNotFoundException()); - } - } - }); - } - - @Override public synchronized void put(UserEntity userEntity) { - if (userEntity != null) { - File userEntitiyFile = this.buildFile(userEntity.getUserId()); - if (!isCached(userEntity.getUserId())) { - String jsonString = this.serializer.serialize(userEntity); - this.executeAsynchronously(new CacheWriter(this.fileManager, userEntitiyFile, - jsonString)); - setLastCacheUpdateTimeMillis(); - } - } - } - - @Override public boolean isCached(int userId) { - File userEntitiyFile = this.buildFile(userId); - return this.fileManager.exists(userEntitiyFile); - } - - @Override public boolean isExpired() { - long currentTime = System.currentTimeMillis(); - long lastUpdateTime = this.getLastCacheUpdateTimeMillis(); - - boolean expired = ((currentTime - lastUpdateTime) > EXPIRATION_TIME); - - if (expired) { - this.evictAll(); - } - - return expired; - } - - @Override public synchronized void evictAll() { - this.executeAsynchronously(new CacheEvictor(this.fileManager, this.cacheDir)); - } - - /** - * Build a file, used to be inserted in the disk cache. - * - * @param userId The id user to build the file. - * @return A valid file. - */ - private File buildFile(int userId) { - StringBuilder fileNameBuilder = new StringBuilder(); - fileNameBuilder.append(this.cacheDir.getPath()); - fileNameBuilder.append(File.separator); - fileNameBuilder.append(DEFAULT_FILE_NAME); - fileNameBuilder.append(userId); - - return new File(fileNameBuilder.toString()); - } - - /** - * Set in millis, the last time the cache was accessed. - */ - private void setLastCacheUpdateTimeMillis() { - long currentMillis = System.currentTimeMillis(); - this.fileManager.writeToPreferences(this.context, SETTINGS_FILE_NAME, - SETTINGS_KEY_LAST_CACHE_UPDATE, currentMillis); - } - - /** - * Get in millis, the last time the cache was accessed. - */ - private long getLastCacheUpdateTimeMillis() { - return this.fileManager.getFromPreferences(this.context, SETTINGS_FILE_NAME, - SETTINGS_KEY_LAST_CACHE_UPDATE); - } - - /** - * Executes a {@link Runnable} in another Thread. - * - * @param runnable {@link Runnable} to execute - */ - private void executeAsynchronously(Runnable runnable) { - this.threadExecutor.execute(runnable); - } - - /** - * {@link Runnable} class for writing to disk. - */ - private static class CacheWriter implements Runnable { - private final FileManager fileManager; - private final File fileToWrite; - private final String fileContent; - - CacheWriter(FileManager fileManager, File fileToWrite, String fileContent) { - this.fileManager = fileManager; - this.fileToWrite = fileToWrite; - this.fileContent = fileContent; - } - - @Override public void run() { - this.fileManager.writeToFile(fileToWrite, fileContent); - } - } - - /** - * {@link Runnable} class for evicting all the cached files - */ - private static class CacheEvictor implements Runnable { - private final FileManager fileManager; - private final File cacheDir; - - CacheEvictor(FileManager fileManager, File cacheDir) { - this.fileManager = fileManager; - this.cacheDir = cacheDir; - } - - @Override public void run() { - this.fileManager.clearDirectory(this.cacheDir); - } - } + private static final String SETTINGS_FILE_NAME = "com.fernandocejas.android10.SETTINGS"; + private static final String SETTINGS_KEY_LAST_CACHE_UPDATE = "last_cache_update"; + + private static final String DEFAULT_FILE_NAME = "user_"; + private static final long EXPIRATION_TIME = 60 * 10 * 1000; + + private final Context context; + private final File cacheDir; + private final JsonSerializer serializer; + private final FileManager fileManager; + private final ThreadExecutor threadExecutor; + + /** + * Constructor of the class {@link UserCacheImpl}. + * + * @param context A + * @param userCacheSerializer {@link JsonSerializer} for object serialization. + * @param fileManager {@link FileManager} for saving serialized objects to the file system. + */ + public UserCacheImpl(Context context, JsonSerializer userCacheSerializer, + FileManager fileManager, ThreadExecutor executor) { + if (context == null || userCacheSerializer == null || fileManager == null || executor == null) { + throw new IllegalArgumentException("Invalid null parameter"); + } + this.context = context.getApplicationContext(); + this.cacheDir = this.context.getCacheDir(); + this.serializer = userCacheSerializer; + this.fileManager = fileManager; + this.threadExecutor = executor; + } + + @Override + public synchronized Observable get(final int userId) { + return Observable.create(new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + File userEntityFile = UserCacheImpl.this.buildFile(userId); + String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile); + UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent); + + if (userEntity != null) { + subscriber.onNext(userEntity); + subscriber.onCompleted(); + } else { + subscriber.onError(new UserNotFoundException()); + } + } + }); + } + + @Override + public synchronized void put(UserEntity userEntity) { + if (userEntity != null) { + File userEntitiyFile = this.buildFile(userEntity.getUserId()); + if (!isCached(userEntity.getUserId())) { + String jsonString = this.serializer.serialize(userEntity); + this.executeAsynchronously(new CacheWriter(this.fileManager, userEntitiyFile, + jsonString)); + setLastCacheUpdateTimeMillis(); + } + } + } + + @Override + public boolean isCached(int userId) { + File userEntitiyFile = this.buildFile(userId); + return this.fileManager.exists(userEntitiyFile); + } + + @Override + public boolean isExpired() { + long currentTime = System.currentTimeMillis(); + long lastUpdateTime = this.getLastCacheUpdateTimeMillis(); + + boolean expired = ((currentTime - lastUpdateTime) > EXPIRATION_TIME); + + if (expired) { + this.evictAll(); + } + + return expired; + } + + @Override + public synchronized void evictAll() { + this.executeAsynchronously(new CacheEvictor(this.fileManager, this.cacheDir)); + } + + /** + * Build a file, used to be inserted in the disk cache. + * + * @param userId The id user to build the file. + * @return A valid file. + */ + private File buildFile(int userId) { + StringBuilder fileNameBuilder = new StringBuilder(); + fileNameBuilder.append(this.cacheDir.getPath()); + fileNameBuilder.append(File.separator); + fileNameBuilder.append(DEFAULT_FILE_NAME); + fileNameBuilder.append(userId); + + return new File(fileNameBuilder.toString()); + } + + /** + * Set in millis, the last time the cache was accessed. + */ + private void setLastCacheUpdateTimeMillis() { + long currentMillis = System.currentTimeMillis(); + this.fileManager.writeToPreferences(this.context, SETTINGS_FILE_NAME, + SETTINGS_KEY_LAST_CACHE_UPDATE, currentMillis); + } + + /** + * Get in millis, the last time the cache was accessed. + */ + private long getLastCacheUpdateTimeMillis() { + return this.fileManager.getFromPreferences(this.context, SETTINGS_FILE_NAME, + SETTINGS_KEY_LAST_CACHE_UPDATE); + } + + /** + * Executes a {@link Runnable} in another Thread. + * + * @param runnable {@link Runnable} to execute + */ + private void executeAsynchronously(Runnable runnable) { + this.threadExecutor.execute(runnable); + } + + /** + * {@link Runnable} class for writing to disk. + */ + private static class CacheWriter implements Runnable { + private final FileManager fileManager; + private final File fileToWrite; + private final String fileContent; + + CacheWriter(FileManager fileManager, File fileToWrite, String fileContent) { + this.fileManager = fileManager; + this.fileToWrite = fileToWrite; + this.fileContent = fileContent; + } + + @Override + public void run() { + this.fileManager.writeToFile(fileToWrite, fileContent); + } + } + + /** + * {@link Runnable} class for evicting all the cached files + */ + private static class CacheEvictor implements Runnable { + private final FileManager fileManager; + private final File cacheDir; + + CacheEvictor(FileManager fileManager, File cacheDir) { + this.fileManager = fileManager; + this.cacheDir = cacheDir; + } + + @Override + public void run() { + this.fileManager.clearDirectory(this.cacheDir); + } + } } diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializer.java b/data/src/main/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializer.java index 6e053187..89b815fd 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializer.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializer.java @@ -17,18 +17,14 @@ import com.fernandocejas.android10.sample.data.entity.UserEntity; import com.google.gson.Gson; -import javax.inject.Inject; -import javax.inject.Singleton; /** * Class user as Serializer/Deserializer for user entities. */ -@Singleton public class JsonSerializer { private final Gson gson = new Gson(); - @Inject public JsonSerializer() {} /** diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapper.java b/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapper.java index 1ecbdcdc..08cdacf4 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapper.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapper.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,58 +17,56 @@ import com.fernandocejas.android10.sample.data.entity.UserEntity; import com.fernandocejas.android10.sample.domain.User; + import java.util.ArrayList; import java.util.Collection; import java.util.List; -import javax.inject.Inject; -import javax.inject.Singleton; /** * Mapper class used to transform {@link UserEntity} (in the data layer) to {@link User} in the * domain layer. */ -@Singleton public class UserEntityDataMapper { - @Inject - public UserEntityDataMapper() {} + public UserEntityDataMapper() { + } - /** - * Transform a {@link UserEntity} into an {@link User}. - * - * @param userEntity Object to be transformed. - * @return {@link User} if valid {@link UserEntity} otherwise null. - */ - public User transform(UserEntity userEntity) { - User user = null; - if (userEntity != null) { - user = new User(userEntity.getUserId()); - user.setCoverUrl(userEntity.getCoverUrl()); - user.setFullName(userEntity.getFullname()); - user.setDescription(userEntity.getDescription()); - user.setFollowers(userEntity.getFollowers()); - user.setEmail(userEntity.getEmail()); - } + /** + * Transform a {@link UserEntity} into an {@link User}. + * + * @param userEntity Object to be transformed. + * @return {@link User} if valid {@link UserEntity} otherwise null. + */ + public User transform(UserEntity userEntity) { + User user = null; + if (userEntity != null) { + user = new User(userEntity.getUserId()); + user.setCoverUrl(userEntity.getCoverUrl()); + user.setFullName(userEntity.getFullname()); + user.setDescription(userEntity.getDescription()); + user.setFollowers(userEntity.getFollowers()); + user.setEmail(userEntity.getEmail()); + } - return user; - } + return user; + } - /** - * Transform a List of {@link UserEntity} into a Collection of {@link User}. - * - * @param userEntityCollection Object Collection to be transformed. - * @return {@link User} if valid {@link UserEntity} otherwise null. - */ - public List transform(Collection userEntityCollection) { - List userList = new ArrayList<>(20); - User user; - for (UserEntity userEntity : userEntityCollection) { - user = transform(userEntity); - if (user != null) { - userList.add(user); - } - } + /** + * Transform a List of {@link UserEntity} into a Collection of {@link User}. + * + * @param userEntityCollection Object Collection to be transformed. + * @return {@link User} if valid {@link UserEntity} otherwise null. + */ + public List transform(Collection userEntityCollection) { + List userList = new ArrayList<>(20); + User user; + for (UserEntity userEntity : userEntityCollection) { + user = transform(userEntity); + if (user != null) { + userList.add(user); + } + } - return userList; - } + return userList; + } } diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapper.java b/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapper.java index 8e8dca90..64ae6854 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapper.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapper.java @@ -21,7 +21,6 @@ import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.List; -import javax.inject.Inject; /** * Class used to transform from Strings representing json to valid objects. @@ -30,7 +29,6 @@ public class UserEntityJsonMapper { private final Gson gson; - @Inject public UserEntityJsonMapper() { this.gson = new Gson(); } diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/executor/JobExecutor.java b/data/src/main/java/com/fernandocejas/android10/sample/data/executor/JobExecutor.java index 46d83253..4c40e0de 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/executor/JobExecutor.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/executor/JobExecutor.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,56 +16,55 @@ package com.fernandocejas.android10.sample.data.executor; import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor; + import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import javax.inject.Inject; -import javax.inject.Singleton; /** * Decorated {@link java.util.concurrent.ThreadPoolExecutor} */ -@Singleton public class JobExecutor implements ThreadExecutor { - private static final int INITIAL_POOL_SIZE = 3; - private static final int MAX_POOL_SIZE = 5; + private static final int INITIAL_POOL_SIZE = 3; + private static final int MAX_POOL_SIZE = 5; - // Sets the amount of time an idle thread waits before terminating - private static final int KEEP_ALIVE_TIME = 10; + // Sets the amount of time an idle thread waits before terminating + private static final int KEEP_ALIVE_TIME = 10; - // Sets the Time Unit to seconds - private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS; + // Sets the Time Unit to seconds + private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS; - private final BlockingQueue workQueue; + private final BlockingQueue workQueue; - private final ThreadPoolExecutor threadPoolExecutor; + private final ThreadPoolExecutor threadPoolExecutor; - private final ThreadFactory threadFactory; + private final ThreadFactory threadFactory; - @Inject - public JobExecutor() { - this.workQueue = new LinkedBlockingQueue<>(); - this.threadFactory = new JobThreadFactory(); - this.threadPoolExecutor = new ThreadPoolExecutor(INITIAL_POOL_SIZE, MAX_POOL_SIZE, - KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, this.workQueue, this.threadFactory); - } + public JobExecutor() { + this.workQueue = new LinkedBlockingQueue<>(); + this.threadFactory = new JobThreadFactory(); + this.threadPoolExecutor = new ThreadPoolExecutor(INITIAL_POOL_SIZE, MAX_POOL_SIZE, + KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, this.workQueue, this.threadFactory); + } - @Override public void execute(Runnable runnable) { - if (runnable == null) { - throw new IllegalArgumentException("Runnable to execute cannot be null"); - } - this.threadPoolExecutor.execute(runnable); - } + @Override + public void execute(Runnable runnable) { + if (runnable == null) { + throw new IllegalArgumentException("Runnable to execute cannot be null"); + } + this.threadPoolExecutor.execute(runnable); + } - private static class JobThreadFactory implements ThreadFactory { - private static final String THREAD_NAME = "android_"; - private int counter = 0; + private static class JobThreadFactory implements ThreadFactory { + private static final String THREAD_NAME = "android_"; + private int counter = 0; - @Override public Thread newThread(Runnable runnable) { - return new Thread(runnable, THREAD_NAME + counter); - } - } + @Override + public Thread newThread(Runnable runnable) { + return new Thread(runnable, THREAD_NAME + counter); + } + } } \ No newline at end of file diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/repository/UserDataRepository.java b/data/src/main/java/com/fernandocejas/android10/sample/data/repository/UserDataRepository.java index 095c80a6..99e0449e 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/repository/UserDataRepository.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/repository/UserDataRepository.java @@ -24,16 +24,12 @@ import java.util.List; -import javax.inject.Inject; -import javax.inject.Singleton; - import rx.Observable; import rx.functions.Func1; /** * {@link UserRepository} for retrieving user data. */ -@Singleton public class UserDataRepository implements UserRepository { private final UserDataStoreFactory userDataStoreFactory; @@ -42,10 +38,9 @@ public class UserDataRepository implements UserRepository { /** * Constructs a {@link UserRepository}. * - * @param dataStoreFactory A factory to construct different data source implementations. + * @param dataStoreFactory A factory to construct different data source implementations. * @param userEntityDataMapper {@link UserEntityDataMapper}. */ - @Inject public UserDataRepository(UserDataStoreFactory dataStoreFactory, UserEntityDataMapper userEntityDataMapper) { this.userDataStoreFactory = dataStoreFactory; diff --git a/data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactory.java b/data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactory.java index 59e397e1..c3da952a 100644 --- a/data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactory.java +++ b/data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactory.java @@ -20,19 +20,15 @@ import com.fernandocejas.android10.sample.data.entity.mapper.UserEntityJsonMapper; import com.fernandocejas.android10.sample.data.net.RestApi; import com.fernandocejas.android10.sample.data.net.RestApiImpl; -import javax.inject.Inject; -import javax.inject.Singleton; /** * Factory that creates different implementations of {@link UserDataStore}. */ -@Singleton public class UserDataStoreFactory { private final Context context; private final UserCache userCache; - @Inject public UserDataStoreFactory(Context context, UserCache userCache) { if (context == null || userCache == null) { throw new IllegalArgumentException("Constructor parameters cannot be null!!!"); diff --git a/data/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestCase.java b/data/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestCase.java index 72c06075..fc17086e 100644 --- a/data/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestCase.java +++ b/data/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestCase.java @@ -24,5 +24,5 @@ * Inherit from this class to create a test. */ @RunWith(RobolectricGradleTestRunner.class) -@Config(constants = BuildConfig.class, application = ApplicationStub.class) +@Config(constants = BuildConfig.class, application = ApplicationStub.class, sdk = 21) public abstract class ApplicationTestCase {} diff --git a/domain/build.gradle b/domain/build.gradle index e9c9e8c6..82a28e01 100644 --- a/domain/build.gradle +++ b/domain/build.gradle @@ -19,10 +19,6 @@ dependencies { def domainDependencies = rootProject.ext.domainDependencies def domainTestDependencies = rootProject.ext.domainTestDependencies - provided domainDependencies.daggerCompiler - provided domainDependencies.javaxAnnotation - - compile domainDependencies.dagger compile domainDependencies.rxJava testCompile domainTestDependencies.junit diff --git a/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserDetails.java b/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserDetails.java index 134fc4d0..d16c942c 100644 --- a/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserDetails.java +++ b/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserDetails.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,7 +19,7 @@ import com.fernandocejas.android10.sample.domain.executor.PostExecutionThread; import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor; import com.fernandocejas.android10.sample.domain.repository.UserRepository; -import javax.inject.Inject; + import rx.Observable; /** @@ -28,18 +28,18 @@ */ public class GetUserDetails extends UseCase { - private final int userId; - private final UserRepository userRepository; + private final int userId; + private final UserRepository userRepository; - @Inject - public GetUserDetails(int userId, UserRepository userRepository, - ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread) { - super(threadExecutor, postExecutionThread); - this.userId = userId; - this.userRepository = userRepository; - } + public GetUserDetails(int userId, UserRepository userRepository, + ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread) { + super(threadExecutor, postExecutionThread); + this.userId = userId; + this.userRepository = userRepository; + } - @Override protected Observable buildUseCaseObservable() { - return this.userRepository.user(this.userId); - } + @Override + protected Observable buildUseCaseObservable() { + return this.userRepository.user(this.userId); + } } diff --git a/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserList.java b/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserList.java index 0780ff3d..321f0d8b 100644 --- a/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserList.java +++ b/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserList.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,7 +19,7 @@ import com.fernandocejas.android10.sample.domain.executor.PostExecutionThread; import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor; import com.fernandocejas.android10.sample.domain.repository.UserRepository; -import javax.inject.Inject; + import rx.Observable; /** @@ -28,16 +28,16 @@ */ public class GetUserList extends UseCase { - private final UserRepository userRepository; + private final UserRepository userRepository; - @Inject - public GetUserList(UserRepository userRepository, ThreadExecutor threadExecutor, - PostExecutionThread postExecutionThread) { - super(threadExecutor, postExecutionThread); - this.userRepository = userRepository; - } + public GetUserList(UserRepository userRepository, ThreadExecutor threadExecutor, + PostExecutionThread postExecutionThread) { + super(threadExecutor, postExecutionThread); + this.userRepository = userRepository; + } - @Override public Observable buildUseCaseObservable() { - return this.userRepository.users(); - } + @Override + public Observable buildUseCaseObservable() { + return this.userRepository.users(); + } } diff --git a/presentation/build.gradle b/presentation/build.gradle index a98a17ba..0893d4ba 100644 --- a/presentation/build.gradle +++ b/presentation/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'com.android.application' -apply plugin: 'com.neenbedankt.android-apt' android { def globalConfiguration = rootProject.extensions.getByName("ext") @@ -64,13 +63,10 @@ dependencies { compile project(':domain') compile project(':data') - apt presentationDependencies.daggerCompiler - compile presentationDependencies.dagger compile presentationDependencies.butterKnife compile presentationDependencies.recyclerView compile presentationDependencies.rxJava compile presentationDependencies.rxAndroid - provided presentationDependencies.javaxAnnotation androidTestCompile presentationTestDependencies.mockito androidTestCompile presentationTestDependencies.dexmaker diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/AndroidApplication.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/AndroidApplication.java index d8cdb4eb..1a67601e 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/AndroidApplication.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/AndroidApplication.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,29 +16,15 @@ package com.fernandocejas.android10.sample.presentation; import android.app.Application; -import com.fernandocejas.android10.sample.presentation.internal.di.components.ApplicationComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.components.DaggerApplicationComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.modules.ApplicationModule; /** * Android Main Application */ public class AndroidApplication extends Application { - private ApplicationComponent applicationComponent; + @Override + public void onCreate() { + super.onCreate(); + } - @Override public void onCreate() { - super.onCreate(); - this.initializeInjector(); - } - - private void initializeInjector() { - this.applicationComponent = DaggerApplicationComponent.builder() - .applicationModule(new ApplicationModule(this)) - .build(); - } - - public ApplicationComponent getApplicationComponent() { - return this.applicationComponent; - } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/UIThread.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/UIThread.java index bd4ceca4..adb06805 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/UIThread.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/UIThread.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,8 +16,7 @@ package com.fernandocejas.android10.sample.presentation; import com.fernandocejas.android10.sample.domain.executor.PostExecutionThread; -import javax.inject.Inject; -import javax.inject.Singleton; + import rx.Scheduler; import rx.android.schedulers.AndroidSchedulers; @@ -25,13 +24,13 @@ * MainThread (UI Thread) implementation based on a {@link rx.Scheduler} * which will execute actions on the Android UI thread */ -@Singleton public class UIThread implements PostExecutionThread { - @Inject - public UIThread() {} + public UIThread() { + } - @Override public Scheduler getScheduler() { - return AndroidSchedulers.mainThread(); - } + @Override + public Scheduler getScheduler() { + return AndroidSchedulers.mainThread(); + } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/HasComponent.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/HasComponent.java deleted file mode 100644 index 1e744459..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/HasComponent.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di; - -/** - * Interface representing a contract for clients that contains a component for dependency injection. - */ -public interface HasComponent { - C getComponent(); -} diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/PerActivity.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/PerActivity.java deleted file mode 100644 index ba04d9c6..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/PerActivity.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di; - -import java.lang.annotation.Retention; -import javax.inject.Scope; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -/** - * A scoping annotation to permit objects whose lifetime should - * conform to the life of the activity to be memorized in the - * correct component. - */ -@Scope -@Retention(RUNTIME) -public @interface PerActivity {} diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ActivityComponent.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ActivityComponent.java deleted file mode 100644 index a7ffc0d9..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ActivityComponent.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di.components; - -import android.app.Activity; -import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity; -import com.fernandocejas.android10.sample.presentation.internal.di.modules.ActivityModule; -import dagger.Component; - -/** - * A base component upon which fragment's components may depend. - * Activity-level components should extend this component. - * - * Subtypes of ActivityComponent should be decorated with annotation: - * {@link com.fernandocejas.android10.sample.presentation.internal.di.PerActivity} - */ -@PerActivity -@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class) -public interface ActivityComponent { - //Exposed to sub-graphs. - Activity activity(); -} diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ApplicationComponent.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ApplicationComponent.java deleted file mode 100644 index 23e08b1d..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/ApplicationComponent.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di.components; - -import android.content.Context; -import com.fernandocejas.android10.sample.domain.executor.PostExecutionThread; -import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor; -import com.fernandocejas.android10.sample.domain.repository.UserRepository; -import com.fernandocejas.android10.sample.presentation.internal.di.modules.ApplicationModule; -import com.fernandocejas.android10.sample.presentation.view.activity.BaseActivity; -import dagger.Component; -import javax.inject.Singleton; - -/** - * A component whose lifetime is the life of the application. - */ -@Singleton // Constraints this component to one-per-application or unscoped bindings. -@Component(modules = ApplicationModule.class) -public interface ApplicationComponent { - void inject(BaseActivity baseActivity); - - //Exposed to sub-graphs. - Context context(); - ThreadExecutor threadExecutor(); - PostExecutionThread postExecutionThread(); - UserRepository userRepository(); -} diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/UserComponent.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/UserComponent.java deleted file mode 100644 index ef4872f5..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/components/UserComponent.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di.components; - -import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity; -import com.fernandocejas.android10.sample.presentation.internal.di.modules.ActivityModule; -import com.fernandocejas.android10.sample.presentation.internal.di.modules.UserModule; -import com.fernandocejas.android10.sample.presentation.view.fragment.UserDetailsFragment; -import com.fernandocejas.android10.sample.presentation.view.fragment.UserListFragment; -import dagger.Component; - -/** - * A scope {@link com.fernandocejas.android10.sample.presentation.internal.di.PerActivity} component. - * Injects user specific Fragments. - */ -@PerActivity -@Component(dependencies = ApplicationComponent.class, modules = {ActivityModule.class, UserModule.class}) -public interface UserComponent extends ActivityComponent { - void inject(UserListFragment userListFragment); - void inject(UserDetailsFragment userDetailsFragment); -} diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ActivityModule.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ActivityModule.java deleted file mode 100644 index 3f7f97c1..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ActivityModule.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di.modules; - -import android.app.Activity; -import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity; -import dagger.Module; -import dagger.Provides; - -/** - * A module to wrap the Activity state and expose it to the graph. - */ -@Module -public class ActivityModule { - private final Activity activity; - - public ActivityModule(Activity activity) { - this.activity = activity; - } - - /** - * Expose the activity to dependents in the graph. - */ - @Provides @PerActivity Activity activity() { - return this.activity; - } -} diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ApplicationModule.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ApplicationModule.java deleted file mode 100644 index 6d016eaa..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/ApplicationModule.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di.modules; - -import android.content.Context; -import com.fernandocejas.android10.sample.data.cache.UserCache; -import com.fernandocejas.android10.sample.data.cache.UserCacheImpl; -import com.fernandocejas.android10.sample.data.executor.JobExecutor; -import com.fernandocejas.android10.sample.data.repository.UserDataRepository; -import com.fernandocejas.android10.sample.domain.executor.PostExecutionThread; -import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor; -import com.fernandocejas.android10.sample.domain.repository.UserRepository; -import com.fernandocejas.android10.sample.presentation.AndroidApplication; -import com.fernandocejas.android10.sample.presentation.UIThread; -import com.fernandocejas.android10.sample.presentation.navigation.Navigator; -import dagger.Module; -import dagger.Provides; -import javax.inject.Singleton; - -/** - * Dagger module that provides objects which will live during the application lifecycle. - */ -@Module -public class ApplicationModule { - private final AndroidApplication application; - - public ApplicationModule(AndroidApplication application) { - this.application = application; - } - - @Provides @Singleton Context provideApplicationContext() { - return this.application; - } - - @Provides @Singleton Navigator provideNavigator() { - return new Navigator(); - } - - @Provides @Singleton ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) { - return jobExecutor; - } - - @Provides @Singleton PostExecutionThread providePostExecutionThread(UIThread uiThread) { - return uiThread; - } - - @Provides @Singleton UserCache provideUserCache(UserCacheImpl userCache) { - return userCache; - } - - @Provides @Singleton UserRepository provideUserRepository(UserDataRepository userDataRepository) { - return userDataRepository; - } -} diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/UserModule.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/UserModule.java deleted file mode 100644 index 3f12d332..00000000 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di/modules/UserModule.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.presentation.internal.di.modules; - -import com.fernandocejas.android10.sample.domain.executor.PostExecutionThread; -import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor; -import com.fernandocejas.android10.sample.domain.interactor.GetUserDetails; -import com.fernandocejas.android10.sample.domain.interactor.GetUserList; -import com.fernandocejas.android10.sample.domain.interactor.UseCase; -import com.fernandocejas.android10.sample.domain.repository.UserRepository; -import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity; -import dagger.Module; -import dagger.Provides; -import javax.inject.Named; - -/** - * Dagger module that provides user related collaborators. - */ -@Module -public class UserModule { - - private int userId = -1; - - public UserModule() {} - - public UserModule(int userId) { - this.userId = userId; - } - - @Provides @PerActivity @Named("userList") UseCase provideGetUserListUseCase( - GetUserList getUserList) { - return getUserList; - } - - @Provides @PerActivity @Named("userDetails") UseCase provideGetUserDetailsUseCase( - UserRepository userRepository, ThreadExecutor threadExecutor, - PostExecutionThread postExecutionThread) { - return new GetUserDetails(userId, userRepository, threadExecutor, postExecutionThread); - } -} \ No newline at end of file diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/mapper/UserModelDataMapper.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/mapper/UserModelDataMapper.java index 3c64bac3..ea6f5521 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/mapper/UserModelDataMapper.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/mapper/UserModelDataMapper.java @@ -16,21 +16,17 @@ package com.fernandocejas.android10.sample.presentation.mapper; import com.fernandocejas.android10.sample.domain.User; -import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity; import com.fernandocejas.android10.sample.presentation.model.UserModel; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import javax.inject.Inject; /** * Mapper class used to transform {@link User} (in the domain layer) to {@link UserModel} in the * presentation layer. */ -@PerActivity public class UserModelDataMapper { - @Inject public UserModelDataMapper() {} /** diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/navigation/Navigator.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/navigation/Navigator.java index 959bd451..7fc1b511 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/navigation/Navigator.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/navigation/Navigator.java @@ -19,16 +19,12 @@ import android.content.Intent; import com.fernandocejas.android10.sample.presentation.view.activity.UserDetailsActivity; import com.fernandocejas.android10.sample.presentation.view.activity.UserListActivity; -import javax.inject.Inject; -import javax.inject.Singleton; /** * Class used to navigate through the application. */ -@Singleton public class Navigator { - @Inject public void Navigator() { //empty } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserDetailsPresenter.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserDetailsPresenter.java index cd39165d..dc92dfe5 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserDetailsPresenter.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserDetailsPresenter.java @@ -22,18 +22,14 @@ import com.fernandocejas.android10.sample.domain.interactor.DefaultSubscriber; import com.fernandocejas.android10.sample.domain.interactor.UseCase; import com.fernandocejas.android10.sample.presentation.exception.ErrorMessageFactory; -import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity; import com.fernandocejas.android10.sample.presentation.mapper.UserModelDataMapper; import com.fernandocejas.android10.sample.presentation.model.UserModel; import com.fernandocejas.android10.sample.presentation.view.UserDetailsView; -import javax.inject.Inject; -import javax.inject.Named; /** * {@link Presenter} that controls communication between views and models of the presentation * layer. */ -@PerActivity public class UserDetailsPresenter implements Presenter { /** id used to retrieve user details */ @@ -44,8 +40,7 @@ public class UserDetailsPresenter implements Presenter { private final UseCase getUserDetailsUseCase; private final UserModelDataMapper userModelDataMapper; - @Inject - public UserDetailsPresenter(@Named("userDetails") UseCase getUserDetailsUseCase, + public UserDetailsPresenter(UseCase getUserDetailsUseCase, UserModelDataMapper userModelDataMapper) { this.getUserDetailsUseCase = getUserDetailsUseCase; this.userModelDataMapper = userModelDataMapper; diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserListPresenter.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserListPresenter.java index 75ab5866..a843ebfc 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserListPresenter.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter/UserListPresenter.java @@ -1,12 +1,12 @@ /** * Copyright (C) 2015 Fernando Cejas Open Source Project - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,117 +16,122 @@ package com.fernandocejas.android10.sample.presentation.presenter; import android.support.annotation.NonNull; + import com.fernandocejas.android10.sample.domain.User; import com.fernandocejas.android10.sample.domain.exception.DefaultErrorBundle; import com.fernandocejas.android10.sample.domain.exception.ErrorBundle; import com.fernandocejas.android10.sample.domain.interactor.DefaultSubscriber; import com.fernandocejas.android10.sample.domain.interactor.UseCase; import com.fernandocejas.android10.sample.presentation.exception.ErrorMessageFactory; -import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity; import com.fernandocejas.android10.sample.presentation.mapper.UserModelDataMapper; import com.fernandocejas.android10.sample.presentation.model.UserModel; import com.fernandocejas.android10.sample.presentation.view.UserListView; + import java.util.Collection; import java.util.List; -import javax.inject.Inject; -import javax.inject.Named; /** * {@link Presenter} that controls communication between views and models of the presentation * layer. */ -@PerActivity public class UserListPresenter implements Presenter { - private UserListView viewListView; - - private final UseCase getUserListUseCase; - private final UserModelDataMapper userModelDataMapper; - - @Inject - public UserListPresenter(@Named("userList") UseCase getUserListUserCase, UserModelDataMapper userModelDataMapper) { - this.getUserListUseCase = getUserListUserCase; - this.userModelDataMapper = userModelDataMapper; - } - - public void setView(@NonNull UserListView view) { - this.viewListView = view; - } - - @Override public void resume() {} - - @Override public void pause() {} - - @Override public void destroy() { - this.getUserListUseCase.unsubscribe(); - } - - /** - * Initializes the presenter by start retrieving the user list. - */ - public void initialize() { - this.loadUserList(); - } - - /** - * Loads all users. - */ - private void loadUserList() { - this.hideViewRetry(); - this.showViewLoading(); - this.getUserList(); - } - - public void onUserClicked(UserModel userModel) { - this.viewListView.viewUser(userModel); - } - - private void showViewLoading() { - this.viewListView.showLoading(); - } - - private void hideViewLoading() { - this.viewListView.hideLoading(); - } - - private void showViewRetry() { - this.viewListView.showRetry(); - } - - private void hideViewRetry() { - this.viewListView.hideRetry(); - } - - private void showErrorMessage(ErrorBundle errorBundle) { - String errorMessage = ErrorMessageFactory.create(this.viewListView.getContext(), - errorBundle.getException()); - this.viewListView.showError(errorMessage); - } - - private void showUsersCollectionInView(Collection usersCollection) { - final Collection userModelsCollection = - this.userModelDataMapper.transform(usersCollection); - this.viewListView.renderUserList(userModelsCollection); - } - - private void getUserList() { - this.getUserListUseCase.execute(new UserListSubscriber()); - } - - private final class UserListSubscriber extends DefaultSubscriber> { - - @Override public void onCompleted() { - UserListPresenter.this.hideViewLoading(); - } - - @Override public void onError(Throwable e) { - UserListPresenter.this.hideViewLoading(); - UserListPresenter.this.showErrorMessage(new DefaultErrorBundle((Exception) e)); - UserListPresenter.this.showViewRetry(); - } - - @Override public void onNext(List users) { - UserListPresenter.this.showUsersCollectionInView(users); - } - } + private UserListView viewListView; + + private final UseCase getUserListUseCase; + private final UserModelDataMapper userModelDataMapper; + + public UserListPresenter(UseCase getUserListUserCase, UserModelDataMapper userModelDataMapper) { + this.getUserListUseCase = getUserListUserCase; + this.userModelDataMapper = userModelDataMapper; + } + + public void setView(@NonNull UserListView view) { + this.viewListView = view; + } + + @Override + public void resume() { + } + + @Override + public void pause() { + } + + @Override + public void destroy() { + this.getUserListUseCase.unsubscribe(); + } + + /** + * Initializes the presenter by start retrieving the user list. + */ + public void initialize() { + this.loadUserList(); + } + + /** + * Loads all users. + */ + private void loadUserList() { + this.hideViewRetry(); + this.showViewLoading(); + this.getUserList(); + } + + public void onUserClicked(UserModel userModel) { + this.viewListView.viewUser(userModel); + } + + private void showViewLoading() { + this.viewListView.showLoading(); + } + + private void hideViewLoading() { + this.viewListView.hideLoading(); + } + + private void showViewRetry() { + this.viewListView.showRetry(); + } + + private void hideViewRetry() { + this.viewListView.hideRetry(); + } + + private void showErrorMessage(ErrorBundle errorBundle) { + String errorMessage = ErrorMessageFactory.create(this.viewListView.getContext(), + errorBundle.getException()); + this.viewListView.showError(errorMessage); + } + + private void showUsersCollectionInView(Collection usersCollection) { + final Collection userModelsCollection = + this.userModelDataMapper.transform(usersCollection); + this.viewListView.renderUserList(userModelsCollection); + } + + private void getUserList() { + this.getUserListUseCase.execute(new UserListSubscriber()); + } + + private final class UserListSubscriber extends DefaultSubscriber> { + + @Override + public void onCompleted() { + UserListPresenter.this.hideViewLoading(); + } + + @Override + public void onError(Throwable e) { + UserListPresenter.this.hideViewLoading(); + UserListPresenter.this.showErrorMessage(new DefaultErrorBundle((Exception) e)); + UserListPresenter.this.showViewRetry(); + } + + @Override + public void onNext(List users) { + UserListPresenter.this.showUsersCollectionInView(users); + } + } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/BaseActivity.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/BaseActivity.java index 716e2915..d4b5762f 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/BaseActivity.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/BaseActivity.java @@ -4,23 +4,19 @@ import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; -import com.fernandocejas.android10.sample.presentation.AndroidApplication; -import com.fernandocejas.android10.sample.presentation.internal.di.components.ApplicationComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.modules.ActivityModule; + import com.fernandocejas.android10.sample.presentation.navigation.Navigator; -import javax.inject.Inject; /** * Base {@link android.app.Activity} class for every Activity in this application. */ public abstract class BaseActivity extends Activity { - @Inject Navigator navigator; + Navigator navigator = new Navigator(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - this.getApplicationComponent().inject(this); } /** @@ -35,21 +31,4 @@ protected void addFragment(int containerViewId, Fragment fragment) { fragmentTransaction.commit(); } - /** - * Get the Main Application component for dependency injection. - * - * @return {@link com.fernandocejas.android10.sample.presentation.internal.di.components.ApplicationComponent} - */ - protected ApplicationComponent getApplicationComponent() { - return ((AndroidApplication)getApplication()).getApplicationComponent(); - } - - /** - * Get an Activity module for dependency injection. - * - * @return {@link com.fernandocejas.android10.sample.presentation.internal.di.modules.ActivityModule} - */ - protected ActivityModule getActivityModule() { - return new ActivityModule(this); - } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserDetailsActivity.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserDetailsActivity.java index b446a352..c9ef885e 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserDetailsActivity.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserDetailsActivity.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2014 android10.org. All rights reserved. + * * @author Fernando Cejas (the android10 coder) */ package com.fernandocejas.android10.sample.presentation.view.activity; @@ -8,68 +9,54 @@ import android.content.Intent; import android.os.Bundle; import android.view.Window; + import com.fernandocejas.android10.sample.presentation.R; -import com.fernandocejas.android10.sample.presentation.internal.di.HasComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.components.DaggerUserComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.components.UserComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.modules.UserModule; import com.fernandocejas.android10.sample.presentation.view.fragment.UserDetailsFragment; /** * Activity that shows details of a certain user. */ -public class UserDetailsActivity extends BaseActivity implements HasComponent { - - private static final String INTENT_EXTRA_PARAM_USER_ID = "org.android10.INTENT_PARAM_USER_ID"; - private static final String INSTANCE_STATE_PARAM_USER_ID = "org.android10.STATE_PARAM_USER_ID"; - - private int userId; - private UserComponent userComponent; - - public static Intent getCallingIntent(Context context, int userId) { - Intent callingIntent = new Intent(context, UserDetailsActivity.class); - callingIntent.putExtra(INTENT_EXTRA_PARAM_USER_ID, userId); - - return callingIntent; - } - - @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - setContentView(R.layout.activity_user_details); - - this.initializeActivity(savedInstanceState); - this.initializeInjector(); - } - - @Override protected void onSaveInstanceState(Bundle outState) { - if (outState != null) { - outState.putInt(INSTANCE_STATE_PARAM_USER_ID, this.userId); - } - super.onSaveInstanceState(outState); - } - - /** - * Initializes this activity. - */ - private void initializeActivity(Bundle savedInstanceState) { - if (savedInstanceState == null) { - this.userId = getIntent().getIntExtra(INTENT_EXTRA_PARAM_USER_ID, -1); - addFragment(R.id.fl_fragment, UserDetailsFragment.newInstance(this.userId)); - } else { - this.userId = savedInstanceState.getInt(INSTANCE_STATE_PARAM_USER_ID); - } - } - - private void initializeInjector() { - this.userComponent = DaggerUserComponent.builder() - .applicationComponent(getApplicationComponent()) - .activityModule(getActivityModule()) - .userModule(new UserModule(this.userId)) - .build(); - } +public class UserDetailsActivity extends BaseActivity { + + private static final String INTENT_EXTRA_PARAM_USER_ID = "org.android10.INTENT_PARAM_USER_ID"; + private static final String INSTANCE_STATE_PARAM_USER_ID = "org.android10.STATE_PARAM_USER_ID"; + + private int userId; + + public static Intent getCallingIntent(Context context, int userId) { + Intent callingIntent = new Intent(context, UserDetailsActivity.class); + callingIntent.putExtra(INTENT_EXTRA_PARAM_USER_ID, userId); + + return callingIntent; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + setContentView(R.layout.activity_user_details); + + this.initializeActivity(savedInstanceState); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + if (outState != null) { + outState.putInt(INSTANCE_STATE_PARAM_USER_ID, this.userId); + } + super.onSaveInstanceState(outState); + } + + /** + * Initializes this activity. + */ + private void initializeActivity(Bundle savedInstanceState) { + if (savedInstanceState == null) { + this.userId = getIntent().getIntExtra(INTENT_EXTRA_PARAM_USER_ID, -1); + addFragment(R.id.fl_fragment, UserDetailsFragment.newInstance(this.userId)); + } else { + this.userId = savedInstanceState.getInt(INSTANCE_STATE_PARAM_USER_ID); + } + } - @Override public UserComponent getComponent() { - return userComponent; - } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserListActivity.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserListActivity.java index 6ed1c5ec..53b21dbb 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserListActivity.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity/UserListActivity.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2014 android10.org. All rights reserved. + * * @author Fernando Cejas (the android10 coder) */ package com.fernandocejas.android10.sample.presentation.view.activity; @@ -8,45 +9,30 @@ import android.content.Intent; import android.os.Bundle; import android.view.Window; + import com.fernandocejas.android10.sample.presentation.R; -import com.fernandocejas.android10.sample.presentation.internal.di.HasComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.components.DaggerUserComponent; -import com.fernandocejas.android10.sample.presentation.internal.di.components.UserComponent; import com.fernandocejas.android10.sample.presentation.model.UserModel; import com.fernandocejas.android10.sample.presentation.view.fragment.UserListFragment; /** * Activity that shows a list of Users. */ -public class UserListActivity extends BaseActivity implements HasComponent, - UserListFragment.UserListListener { - - public static Intent getCallingIntent(Context context) { - return new Intent(context, UserListActivity.class); - } - - private UserComponent userComponent; - - @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - setContentView(R.layout.activity_user_list); +public class UserListActivity extends BaseActivity implements UserListFragment.UserListListener { - this.initializeInjector(); - } + public static Intent getCallingIntent(Context context) { + return new Intent(context, UserListActivity.class); + } - private void initializeInjector() { - this.userComponent = DaggerUserComponent.builder() - .applicationComponent(getApplicationComponent()) - .activityModule(getActivityModule()) - .build(); - } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + setContentView(R.layout.activity_user_list); - @Override public UserComponent getComponent() { - return userComponent; - } + } - @Override public void onUserClicked(UserModel userModel) { - this.navigator.navigateToUserDetails(this, userModel.getUserId()); - } + @Override + public void onUserClicked(UserModel userModel) { + this.navigator.navigateToUserDetails(this, userModel.getUserId()); + } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/BaseFragment.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/BaseFragment.java index 56e5aa05..79ddcee0 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/BaseFragment.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/BaseFragment.java @@ -7,7 +7,6 @@ import android.app.Fragment; import android.os.Bundle; import android.widget.Toast; -import com.fernandocejas.android10.sample.presentation.internal.di.HasComponent; /** * Base {@link android.app.Fragment} class for every fragment in this application. @@ -28,11 +27,4 @@ protected void showToastMessage(String message) { Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show(); } - /** - * Gets a component for dependency injection by its type. - */ - @SuppressWarnings("unchecked") - protected C getComponent(Class componentType) { - return componentType.cast(((HasComponent)getActivity()).getComponent()); - } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserDetailsFragment.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserDetailsFragment.java index cfbfa6e0..b3e98805 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserDetailsFragment.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserDetailsFragment.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2014 android10.org. All rights reserved. + * * @author Fernando Cejas (the android10 coder) */ package com.fernandocejas.android10.sample.presentation.view.fragment; @@ -12,137 +13,182 @@ import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; -import butterknife.Bind; -import butterknife.ButterKnife; -import butterknife.OnClick; + +import com.fernandocejas.android10.sample.data.cache.FileManager; +import com.fernandocejas.android10.sample.data.cache.UserCache; +import com.fernandocejas.android10.sample.data.cache.UserCacheImpl; +import com.fernandocejas.android10.sample.data.cache.serializer.JsonSerializer; +import com.fernandocejas.android10.sample.data.entity.mapper.UserEntityDataMapper; +import com.fernandocejas.android10.sample.data.executor.JobExecutor; +import com.fernandocejas.android10.sample.data.repository.UserDataRepository; +import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStoreFactory; +import com.fernandocejas.android10.sample.domain.interactor.GetUserDetails; +import com.fernandocejas.android10.sample.domain.interactor.UseCase; +import com.fernandocejas.android10.sample.domain.repository.UserRepository; import com.fernandocejas.android10.sample.presentation.R; -import com.fernandocejas.android10.sample.presentation.internal.di.components.UserComponent; +import com.fernandocejas.android10.sample.presentation.UIThread; +import com.fernandocejas.android10.sample.presentation.mapper.UserModelDataMapper; import com.fernandocejas.android10.sample.presentation.model.UserModel; import com.fernandocejas.android10.sample.presentation.presenter.UserDetailsPresenter; import com.fernandocejas.android10.sample.presentation.view.UserDetailsView; import com.fernandocejas.android10.sample.presentation.view.component.AutoLoadImageView; -import javax.inject.Inject; + +import butterknife.Bind; +import butterknife.ButterKnife; +import butterknife.OnClick; /** * Fragment that shows details of a certain user. */ public class UserDetailsFragment extends BaseFragment implements UserDetailsView { - private static final String ARGUMENT_KEY_USER_ID = "org.android10.ARGUMENT_USER_ID"; - - private int userId; - - @Inject UserDetailsPresenter userDetailsPresenter; - - @Bind(R.id.iv_cover) AutoLoadImageView iv_cover; - @Bind(R.id.tv_fullname) TextView tv_fullname; - @Bind(R.id.tv_email) TextView tv_email; - @Bind(R.id.tv_followers) TextView tv_followers; - @Bind(R.id.tv_description) TextView tv_description; - @Bind(R.id.rl_progress) RelativeLayout rl_progress; - @Bind(R.id.rl_retry) RelativeLayout rl_retry; - @Bind(R.id.bt_retry) Button bt_retry; - - public UserDetailsFragment() { super(); } - - public static UserDetailsFragment newInstance(int userId) { - UserDetailsFragment userDetailsFragment = new UserDetailsFragment(); - - Bundle argumentsBundle = new Bundle(); - argumentsBundle.putInt(ARGUMENT_KEY_USER_ID, userId); - userDetailsFragment.setArguments(argumentsBundle); - - return userDetailsFragment; - } - - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - - View fragmentView = inflater.inflate(R.layout.fragment_user_details, container, false); - ButterKnife.bind(this, fragmentView); - - return fragmentView; - } - - @Override public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - this.initialize(); - } - - @Override public void onResume() { - super.onResume(); - this.userDetailsPresenter.resume(); - } - - @Override public void onPause() { - super.onPause(); - this.userDetailsPresenter.pause(); - } - - @Override public void onDestroyView() { - super.onDestroyView(); - ButterKnife.unbind(this); - } - - @Override public void onDestroy() { - super.onDestroy(); - this.userDetailsPresenter.destroy(); - } - - private void initialize() { - this.getComponent(UserComponent.class).inject(this); - this.userDetailsPresenter.setView(this); - this.userId = getArguments().getInt(ARGUMENT_KEY_USER_ID); - this.userDetailsPresenter.initialize(this.userId); - } - - @Override public void renderUser(UserModel user) { - if (user != null) { - this.iv_cover.setImageUrl(user.getCoverUrl()); - this.tv_fullname.setText(user.getFullName()); - this.tv_email.setText(user.getEmail()); - this.tv_followers.setText(String.valueOf(user.getFollowers())); - this.tv_description.setText(user.getDescription()); - } - } - - @Override public void showLoading() { - this.rl_progress.setVisibility(View.VISIBLE); - this.getActivity().setProgressBarIndeterminateVisibility(true); - } - - @Override public void hideLoading() { - this.rl_progress.setVisibility(View.GONE); - this.getActivity().setProgressBarIndeterminateVisibility(false); - } - - @Override public void showRetry() { - this.rl_retry.setVisibility(View.VISIBLE); - } - - @Override public void hideRetry() { - this.rl_retry.setVisibility(View.GONE); - } - - @Override public void showError(String message) { - this.showToastMessage(message); - } - - @Override public Context getContext() { - return getActivity().getApplicationContext(); - } - - /** - * Loads all users. - */ - private void loadUserDetails() { - if (this.userDetailsPresenter != null) { - this.userDetailsPresenter.initialize(this.userId); - } - } - - @OnClick(R.id.bt_retry) - void onButtonRetryClick() { - UserDetailsFragment.this.loadUserDetails(); - } + private static final String ARGUMENT_KEY_USER_ID = "org.android10.ARGUMENT_USER_ID"; + + private int userId; + + UserDetailsPresenter userDetailsPresenter; + + @Bind(R.id.iv_cover) + AutoLoadImageView iv_cover; + @Bind(R.id.tv_fullname) + TextView tv_fullname; + @Bind(R.id.tv_email) + TextView tv_email; + @Bind(R.id.tv_followers) + TextView tv_followers; + @Bind(R.id.tv_description) + TextView tv_description; + @Bind(R.id.rl_progress) + RelativeLayout rl_progress; + @Bind(R.id.rl_retry) + RelativeLayout rl_retry; + @Bind(R.id.bt_retry) + Button bt_retry; + + public UserDetailsFragment() { + super(); + + } + + public static UserDetailsFragment newInstance(int userId) { + UserDetailsFragment userDetailsFragment = new UserDetailsFragment(); + + Bundle argumentsBundle = new Bundle(); + argumentsBundle.putInt(ARGUMENT_KEY_USER_ID, userId); + userDetailsFragment.setArguments(argumentsBundle); + + return userDetailsFragment; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + View fragmentView = inflater.inflate(R.layout.fragment_user_details, container, false); + ButterKnife.bind(this, fragmentView); + + return fragmentView; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + this.initialize(); + } + + @Override + public void onResume() { + super.onResume(); + this.userDetailsPresenter.resume(); + } + + @Override + public void onPause() { + super.onPause(); + this.userDetailsPresenter.pause(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + ButterKnife.unbind(this); + } + + @Override + public void onDestroy() { + super.onDestroy(); + this.userDetailsPresenter.destroy(); + } + + private void initialize() { + this.userId = getArguments().getInt(ARGUMENT_KEY_USER_ID); + + UserCache userCache = new UserCacheImpl(getContext(), new JsonSerializer(), new FileManager(), new JobExecutor()); + UserDataStoreFactory userDataStoreFactory = new UserDataStoreFactory(getContext(), userCache); + UserRepository userRepository = new UserDataRepository(userDataStoreFactory, new UserEntityDataMapper()); + UseCase useCase = new GetUserDetails(userId, userRepository, new JobExecutor(), new UIThread()); + + userDetailsPresenter = new UserDetailsPresenter(useCase, new UserModelDataMapper()); + + this.userDetailsPresenter.setView(this); + this.userDetailsPresenter.initialize(this.userId); + + } + + @Override + public void renderUser(UserModel user) { + if (user != null) { + this.iv_cover.setImageUrl(user.getCoverUrl()); + this.tv_fullname.setText(user.getFullName()); + this.tv_email.setText(user.getEmail()); + this.tv_followers.setText(String.valueOf(user.getFollowers())); + this.tv_description.setText(user.getDescription()); + } + } + + @Override + public void showLoading() { + this.rl_progress.setVisibility(View.VISIBLE); + this.getActivity().setProgressBarIndeterminateVisibility(true); + } + + @Override + public void hideLoading() { + this.rl_progress.setVisibility(View.GONE); + this.getActivity().setProgressBarIndeterminateVisibility(false); + } + + @Override + public void showRetry() { + this.rl_retry.setVisibility(View.VISIBLE); + } + + @Override + public void hideRetry() { + this.rl_retry.setVisibility(View.GONE); + } + + @Override + public void showError(String message) { + this.showToastMessage(message); + } + + @Override + public Context getContext() { + return getActivity().getApplicationContext(); + } + + /** + * Loads all users. + */ + private void loadUserDetails() { + if (this.userDetailsPresenter != null) { + this.userDetailsPresenter.initialize(this.userId); + } + } + + @OnClick(R.id.bt_retry) + void onButtonRetryClick() { + UserDetailsFragment.this.loadUserDetails(); + } } diff --git a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserListFragment.java b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserListFragment.java index 1a3ffada..d764071d 100644 --- a/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserListFragment.java +++ b/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment/UserListFragment.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2014 android10.org. All rights reserved. + * * @author Fernando Cejas (the android10 coder) */ package com.fernandocejas.android10.sample.presentation.view.fragment; @@ -13,158 +14,200 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.RelativeLayout; -import butterknife.Bind; -import butterknife.ButterKnife; -import butterknife.OnClick; + +import com.fernandocejas.android10.sample.data.cache.FileManager; +import com.fernandocejas.android10.sample.data.cache.UserCache; +import com.fernandocejas.android10.sample.data.cache.UserCacheImpl; +import com.fernandocejas.android10.sample.data.cache.serializer.JsonSerializer; +import com.fernandocejas.android10.sample.data.entity.mapper.UserEntityDataMapper; +import com.fernandocejas.android10.sample.data.executor.JobExecutor; +import com.fernandocejas.android10.sample.data.repository.UserDataRepository; +import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStoreFactory; +import com.fernandocejas.android10.sample.domain.interactor.GetUserList; +import com.fernandocejas.android10.sample.domain.interactor.UseCase; +import com.fernandocejas.android10.sample.domain.repository.UserRepository; import com.fernandocejas.android10.sample.presentation.R; -import com.fernandocejas.android10.sample.presentation.internal.di.components.UserComponent; +import com.fernandocejas.android10.sample.presentation.UIThread; +import com.fernandocejas.android10.sample.presentation.mapper.UserModelDataMapper; import com.fernandocejas.android10.sample.presentation.model.UserModel; import com.fernandocejas.android10.sample.presentation.presenter.UserListPresenter; import com.fernandocejas.android10.sample.presentation.view.UserListView; import com.fernandocejas.android10.sample.presentation.view.adapter.UsersAdapter; import com.fernandocejas.android10.sample.presentation.view.adapter.UsersLayoutManager; + import java.util.ArrayList; import java.util.Collection; -import javax.inject.Inject; + +import butterknife.Bind; +import butterknife.ButterKnife; +import butterknife.OnClick; /** * Fragment that shows a list of Users. */ public class UserListFragment extends BaseFragment implements UserListView { - /** - * Interface for listening user list events. - */ - public interface UserListListener { - void onUserClicked(final UserModel userModel); - } - - @Inject UserListPresenter userListPresenter; - - @Bind(R.id.rv_users) RecyclerView rv_users; - @Bind(R.id.rl_progress) RelativeLayout rl_progress; - @Bind(R.id.rl_retry) RelativeLayout rl_retry; - @Bind(R.id.bt_retry) Button bt_retry; - - private UsersAdapter usersAdapter; - private UsersLayoutManager usersLayoutManager; - - private UserListListener userListListener; - - public UserListFragment() { super(); } - - @Override public void onAttach(Activity activity) { - super.onAttach(activity); - if (activity instanceof UserListListener) { - this.userListListener = (UserListListener) activity; - } - } - - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - - View fragmentView = inflater.inflate(R.layout.fragment_user_list, container, true); - ButterKnife.bind(this, fragmentView); - setupUI(); - - return fragmentView; - } - - @Override public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - this.initialize(); - this.loadUserList(); - } - - @Override public void onResume() { - super.onResume(); - this.userListPresenter.resume(); - } - - @Override public void onPause() { - super.onPause(); - this.userListPresenter.pause(); - } - - @Override public void onDestroy() { - super.onDestroy(); - this.userListPresenter.destroy(); - } - - @Override public void onDestroyView() { - super.onDestroyView(); - ButterKnife.unbind(this); - } - - private void initialize() { - this.getComponent(UserComponent.class).inject(this); - this.userListPresenter.setView(this); - } - - private void setupUI() { - this.usersLayoutManager = new UsersLayoutManager(getActivity()); - this.rv_users.setLayoutManager(usersLayoutManager); - - this.usersAdapter = new UsersAdapter(getActivity(), new ArrayList()); - this.usersAdapter.setOnItemClickListener(onItemClickListener); - this.rv_users.setAdapter(usersAdapter); - } - - @Override public void showLoading() { - this.rl_progress.setVisibility(View.VISIBLE); - this.getActivity().setProgressBarIndeterminateVisibility(true); - } - - @Override public void hideLoading() { - this.rl_progress.setVisibility(View.GONE); - this.getActivity().setProgressBarIndeterminateVisibility(false); - } - - @Override public void showRetry() { - this.rl_retry.setVisibility(View.VISIBLE); - } - - @Override public void hideRetry() { - this.rl_retry.setVisibility(View.GONE); - } - - @Override public void renderUserList(Collection userModelCollection) { - if (userModelCollection != null) { - this.usersAdapter.setUsersCollection(userModelCollection); - } - } - - @Override public void viewUser(UserModel userModel) { - if (this.userListListener != null) { - this.userListListener.onUserClicked(userModel); - } - } - - @Override public void showError(String message) { - this.showToastMessage(message); - } - - @Override public Context getContext() { - return this.getActivity().getApplicationContext(); - } - - /** - * Loads all users. - */ - private void loadUserList() { - this.userListPresenter.initialize(); - } - - @OnClick(R.id.bt_retry) void onButtonRetryClick() { - UserListFragment.this.loadUserList(); - } - - private UsersAdapter.OnItemClickListener onItemClickListener = - new UsersAdapter.OnItemClickListener() { - @Override public void onUserItemClicked(UserModel userModel) { - if (UserListFragment.this.userListPresenter != null && userModel != null) { - UserListFragment.this.userListPresenter.onUserClicked(userModel); - } - } - }; + /** + * Interface for listening user list events. + */ + public interface UserListListener { + void onUserClicked(final UserModel userModel); + } + + UserListPresenter userListPresenter; + + @Bind(R.id.rv_users) + RecyclerView rv_users; + @Bind(R.id.rl_progress) + RelativeLayout rl_progress; + @Bind(R.id.rl_retry) + RelativeLayout rl_retry; + @Bind(R.id.bt_retry) + Button bt_retry; + + private UsersAdapter usersAdapter; + private UsersLayoutManager usersLayoutManager; + + private UserListListener userListListener; + + public UserListFragment() { + super(); + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + if (activity instanceof UserListListener) { + this.userListListener = (UserListListener) activity; + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + View fragmentView = inflater.inflate(R.layout.fragment_user_list, container, true); + ButterKnife.bind(this, fragmentView); + setupUI(); + + return fragmentView; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + this.initialize(); + this.loadUserList(); + } + + @Override + public void onResume() { + super.onResume(); + this.userListPresenter.resume(); + } + + @Override + public void onPause() { + super.onPause(); + this.userListPresenter.pause(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + this.userListPresenter.destroy(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + ButterKnife.unbind(this); + } + + private void initialize() { + UserCache userCache = new UserCacheImpl(getContext(), new JsonSerializer(), new FileManager(), new JobExecutor()); + UserDataStoreFactory userDataStoreFactory = new UserDataStoreFactory(getContext(), userCache); + UserRepository userRepository = new UserDataRepository(userDataStoreFactory, new UserEntityDataMapper()); + UseCase useCase = new GetUserList(userRepository, new JobExecutor(), new UIThread()); + userListPresenter = new UserListPresenter(useCase, new UserModelDataMapper()); + + this.userListPresenter.setView(this); + } + + private void setupUI() { + this.usersLayoutManager = new UsersLayoutManager(getActivity()); + this.rv_users.setLayoutManager(usersLayoutManager); + + this.usersAdapter = new UsersAdapter(getActivity(), new ArrayList()); + this.usersAdapter.setOnItemClickListener(onItemClickListener); + this.rv_users.setAdapter(usersAdapter); + } + + @Override + public void showLoading() { + this.rl_progress.setVisibility(View.VISIBLE); + this.getActivity().setProgressBarIndeterminateVisibility(true); + } + + @Override + public void hideLoading() { + this.rl_progress.setVisibility(View.GONE); + this.getActivity().setProgressBarIndeterminateVisibility(false); + } + + @Override + public void showRetry() { + this.rl_retry.setVisibility(View.VISIBLE); + } + + @Override + public void hideRetry() { + this.rl_retry.setVisibility(View.GONE); + } + + @Override + public void renderUserList(Collection userModelCollection) { + if (userModelCollection != null) { + this.usersAdapter.setUsersCollection(userModelCollection); + } + } + + @Override + public void viewUser(UserModel userModel) { + if (this.userListListener != null) { + this.userListListener.onUserClicked(userModel); + } + } + + @Override + public void showError(String message) { + this.showToastMessage(message); + } + + @Override + public Context getContext() { + return this.getActivity().getApplicationContext(); + } + + /** + * Loads all users. + */ + private void loadUserList() { + this.userListPresenter.initialize(); + } + + @OnClick(R.id.bt_retry) + void onButtonRetryClick() { + UserListFragment.this.loadUserList(); + } + + private UsersAdapter.OnItemClickListener onItemClickListener = + new UsersAdapter.OnItemClickListener() { + @Override + public void onUserItemClicked(UserModel userModel) { + if (UserListFragment.this.userListPresenter != null && userModel != null) { + UserListFragment.this.userListPresenter.onUserClicked(userModel); + } + } + }; }