Skip to content

Commit

Permalink
* Decouple Context out of Presenters
Browse files Browse the repository at this point in the history
* Some code cleanup
* Clean up Library class
  • Loading branch information
Andr3Carvalh0 committed Mar 21, 2018
1 parent 2f3a4d3 commit cc36ee4
Show file tree
Hide file tree
Showing 24 changed files with 345 additions and 196 deletions.
11 changes: 8 additions & 3 deletions app/src/main/java/io/mgba/DI/Modules/ModelModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
import io.mgba.Model.Interfaces.IFilesManager;
import io.mgba.Model.Interfaces.ILibrary;
import io.mgba.Model.Library;
import io.mgba.Utils.IDeviceManager;
import io.mgba.mgba;

@Module
public class ModelModule {
private final IDeviceManager manager;
private mgba application;
private String path;

public ModelModule(mgba application, String path) {
public ModelModule(mgba application, String path, IDeviceManager manager) {
this.application = application;
this.path = path;
this.manager = manager;
}

@Provides
Expand All @@ -40,7 +43,9 @@ public IFilesManager provideFileManager(){
return new FilesManager(path);
}



@Provides
public IDeviceManager provideDeviceManager(){
return manager;
}

}
34 changes: 16 additions & 18 deletions app/src/main/java/io/mgba/Model/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;

import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.mgba.Data.Remote.DTOs.GameJSON;
Expand All @@ -18,24 +16,27 @@
import io.mgba.Model.Interfaces.IDatabase;
import io.mgba.Model.Interfaces.IFilesManager;
import io.mgba.Model.Interfaces.ILibrary;
import io.mgba.Utils.IDependencyInjector;
import io.mgba.Utils.IDeviceManager;
import io.mgba.mgba;
import io.reactivex.Single;
import io.reactivex.annotations.NonNull;

public class Library implements ILibrary {
private static final String TAG = "ProcService";
private final mgba application;

@Inject IDatabase database;
@Inject IFilesManager filesService;
@Inject IDeviceManager deviceManager;

public Library(mgba application) {
this.application = application;
application.inject(this);
public Library(@NonNull IDependencyInjector dependencyInjector) {
dependencyInjector.inject(this);
}

public Library(mgba application, IDatabase database, IFilesManager filesService) {
this.application = application;
public Library(IDatabase database, IFilesManager filesService, IDeviceManager deviceManager) {
this.database = database;
this.filesService = filesService;
this.deviceManager = deviceManager;
}


Expand Down Expand Up @@ -118,7 +119,7 @@ private List<Game> processNewGames(List<Game> games){
Stream.of(games).filter(g1 -> g1.equals(g)).forEach(games::remove);

if (calculateMD5(g)) {
if(application.isConnectedToWeb())
if(deviceManager.isConnectedToWeb())
searchWeb(g);
storeInDatabase(g);
}
Expand Down Expand Up @@ -148,20 +149,17 @@ private void storeInDatabase(Game game){
database.insert(game);
}

private boolean searchWeb(Game game){
private void searchWeb(Game game){
try {
final GameJSON json = application.getWebService()
.getGameInformation(game.getMD5(), application.getDeviceLanguage())
final GameJSON json = deviceManager.getWebService()
.getGameInformation(game.getMD5(), deviceManager.getDeviceLanguage())
.execute()
.body();

if(json == null)
return false;

copyInformation(game, json);
return true;
if(json != null)
copyInformation(game, json);
} catch (Exception e) {
return false;
mgba.report(e);
}
}

Expand Down
49 changes: 13 additions & 36 deletions app/src/main/java/io/mgba/Presenter/GamesPresenter.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package io.mgba.Presenter;

import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;

import org.lucasr.twowayview.layout.TwoWayView;

import io.mgba.Adapters.GameAdapter;
import io.mgba.Constants;
import io.mgba.Presenter.Interfaces.IGamesPresenter;
import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.mgba.R;
import io.mgba.UI.Fragments.Interfaces.IGamesFragment;
import io.mgba.mgba;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
Expand All @@ -24,9 +15,9 @@ public class GamesPresenter implements IGamesPresenter {
private CompositeDisposable disposable = new CompositeDisposable();
private Platform platform;

public GamesPresenter(Fragment context, IGamesFragment<Game> view) {
public GamesPresenter(Platform platform, IGamesFragment<Game> view) {
this.view = view;
this.platform = (Platform) context.getArguments().getSerializable(Constants.ARG_PLATFORM);
this.platform = platform;
}

@Override
Expand All @@ -43,37 +34,23 @@ public Platform getPlatform() {
public void loadGames(io.mgba.UI.Activities.Interfaces.ILibrary databaseHelper) {
disposable.add(databaseHelper.getLibraryService().prepareGames(platform)
.subscribeOn(Schedulers.io())
.subscribe(games -> disposable.add(
updateView()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(n -> {
view.swapContent(games);
view.showContent(games.size() > 0);
})
)
));
}

// A do nothing kinda of method just so I can update UI.
// On a rx thread that need to be non UI.
private Single<Boolean> updateView(){
Single<Boolean> ret = Single.create(s -> s.onSuccess(true));
return ret.doOnError(mgba::report);
.observeOn(AndroidSchedulers.mainThread())
.subscribe(games -> {
view.swapContent(games);
view.showContent(games.size() > 0);
}));
}

@Override
public void onRefresh(io.mgba.UI.Activities.Interfaces.ILibrary iLibrary) {
disposable.add(iLibrary.getLibraryService().reloadGames(platform)
.subscribeOn(Schedulers.computation())
.subscribe(games -> disposable.add(
updateView()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(n -> {
view.swapContent(games);
view.stopRefreshing();
})
)
));
.observeOn(AndroidSchedulers.mainThread())
.subscribe(games -> {
view.swapContent(games);
view.stopRefreshing();
})
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import permissions.dispatcher.PermissionRequest;

public interface IIntroPresenter {
void setupView(AppIntro2 introActivity);

void onActivityResult(int requestCode, int resultCode, Intent intent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public interface IMainPresenter {
void onDestroy();
ILibrary getILibrary();
void onMenuItemSelected(MenuItem item);
void onSearchTextChanged(String oldQuery, String newQuery, FloatingSearchView mToolbar);
void onSearchTextChanged(String oldQuery, String newQuery);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import permissions.dispatcher.PermissionRequest;

public interface ISettingsPanelPresenter {
void init(Bundle savedInstanceState);

void onSaveInstance(Bundle outState);

Expand Down
74 changes: 33 additions & 41 deletions app/src/main/java/io/mgba/Presenter/IntroPresenter.java
Original file line number Diff line number Diff line change
@@ -1,80 +1,74 @@
package io.mgba.Presenter;


import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.annimon.stream.Stream;
import com.github.paolorotolo.appintro.AppIntro2;
import com.github.paolorotolo.appintro.AppIntro2Fragment;
import com.github.paolorotolo.appintro.AppIntroFragment;
import com.nononsenseapps.filepicker.Controllers.FilePickerUtils;

import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;

import io.mgba.Model.Interfaces.ILibrary;
import io.mgba.Presenter.Interfaces.IIntroPresenter;
import io.mgba.Model.Interfaces.IPermissionManager;
import io.mgba.Model.System.PermissionManager;
import io.mgba.Model.System.PreferencesManager;
import io.mgba.R;
import io.mgba.UI.Activities.Interfaces.IIntroView;
import io.mgba.UI.Activities.MainActivity;
import io.mgba.Utils.IDependencyInjector;
import io.mgba.Utils.IResourcesManager;
import io.mgba.mgba;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import permissions.dispatcher.PermissionRequest;

//S

public class IntroPresenter implements IIntroPresenter {

private final static String TAG = "mgba:IntroCtr";
@Inject ILibrary library;
private final IIntroView view;
private final IPermissionManager permissionService;
private final IResourcesManager resourceManager;
private CompositeDisposable disposable = new CompositeDisposable();
private boolean isVisible = true;
private boolean isDone = false;

@Inject
ILibrary library;
public IntroPresenter(@NonNull IResourcesManager resourcesManager, @NonNull IPermissionManager permissionManager,
@NonNull IDependencyInjector dependencyInjector, @NonNull IIntroView view) {
this.permissionService = permissionManager;
this.resourceManager = resourcesManager;
this.view = view;
dependencyInjector.inject(this);

private final IPermissionManager permissionService;
private AppCompatActivity context;
private CompositeDisposable disposable = new CompositeDisposable();

public IntroPresenter(@NonNull AppCompatActivity context) {
this.permissionService = new PermissionManager(context);
this.context = context;
((mgba)context.getApplication()).inject(this);
view.addSlides(getIntroFragments().toList());
}

private Stream<AppIntroFragment> getIntroFragments(){
List<AppIntroFragment> slides = new LinkedList<>();

//Welcome-screen
slides.add(AppIntro2Fragment.newInstance(context.getResources().getString(R.string.Welcome_Title),
context.getResources().getString(R.string.Welcome_Description),
slides.add(AppIntro2Fragment.newInstance(
resourceManager.getString(R.string.Welcome_Title),
resourceManager.getString(R.string.Welcome_Description),
R.mipmap.ic_launcher,
context.getResources().getColor(R.color.colorPrimary)));
resourceManager.getColor(R.color.colorPrimary)));

//Feature-Library
slides.add(AppIntro2Fragment.newInstance(context.getResources().getString(R.string.Library_Title),
context.getResources().getString(R.string.Library_Description),
slides.add(AppIntro2Fragment.newInstance(
resourceManager.getString(R.string.Library_Title),
resourceManager.getString(R.string.Library_Description),
R.mipmap.ic_launcher,
context.getResources().getColor(R.color.colorPrimary)));
resourceManager.getColor(R.color.colorPrimary)));

return Stream.of(slides);
}

@Override
public void setupView(AppIntro2 introActivity) {
getIntroFragments().forEach(introActivity::addSlide);

introActivity.setProgressButtonEnabled(true);
introActivity.setFadeAnimation();
introActivity.showSkipButton(false);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == PermissionManager.DIR_CODE && resultCode == Activity.RESULT_OK) {
Expand Down Expand Up @@ -123,29 +117,27 @@ public void onPause() {
// * There could also happen that the app gets kill in between the processing...
// In that case the flag setup_done will stay false and on the next launch the setup will show up again.
private void onEnd(String dir){
((mgba)context.getApplication()).savePreference(PreferencesManager.GAMES_DIRECTORY, dir);
((mgba)context.getApplication()).showProgressDialog(context);
view.savePreference(PreferencesManager.GAMES_DIRECTORY, dir);
view.showProgressDialog();

disposable.add(library.reloadGames(dir)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(games -> {
isDone = true;
onEnd();
}));
}

private void onEnd(){
((mgba) context.getApplication()).savePreference(PreferencesManager.SETUP_DONE, true);
((mgba) context.getApplication()).stopProgressDialog();
view.savePreference(PreferencesManager.SETUP_DONE, true);
view.showProgressDialog();

//House cleaning
if(disposable != null && !disposable.isDisposed())
disposable.dispose();

if(isVisible) {
Intent it = new Intent(context.getBaseContext(), MainActivity.class);
context.startActivity(it);
context.finish();
}
if(isVisible)
view.startActivity(MainActivity.class);
}
}
Loading

0 comments on commit cc36ee4

Please sign in to comment.