Skip to content

Commit

Permalink
* Remove platform enum in favor of a int, so its easier on the memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Andr3Carvalh0 committed May 12, 2018
1 parent 4c3d420 commit 7afbc9a
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 117 deletions.
6 changes: 2 additions & 4 deletions app/src/main/java/io/mgba/Adapters/TabViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

import io.mgba.Constants;
import io.mgba.Data.Platform;
import io.mgba.UI.Fragments.Main.FavouritesFragment;
import io.mgba.UI.Fragments.Main.GameFragment;

import static io.mgba.mgba.printLog;

public class TabViewPager extends FragmentStatePagerAdapter {
private final static String TAG = "mgba:TabPager";
private final Platform[] platforms = {Platform.FAVS, Platform.GBA, Platform.GBC};
private final int[] platforms = {Constants.PLATFORM_FAVS, Constants.PLATFORM_GBA, Constants.PLATFORM_GBC};

public TabViewPager(FragmentManager fm) {
super(fm);
Expand All @@ -25,7 +23,7 @@ public TabViewPager(FragmentManager fm) {
@Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putSerializable(Constants.ARG_PLATFORM, platforms[position]);
args.putInt(Constants.ARG_PLATFORM, platforms[position]);

final GameFragment fragment = position == 0 ? new FavouritesFragment() : new GameFragment();
fragment.setArguments(args);
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/io/mgba/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ public class Constants {
public static final String MAIN_VIEWPAGE_STATE = "main_viewpage_state";
public static final String MAIN_RECYCLER_CONTENT = "main_rv_content";
public static final String ARG_SETTINGS_ID = "settings_id_args";
public static final int PLATFORM_GBC = 0;
public static final String PLATFORM_GBA_EXT = "gba";
public static final int PLATFORM_GBA = 1;
public static final int PLATFORM_FAVS = 2;


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import java.io.File;

import io.mgba.Data.Platform;

public class GameConverter {
@TypeConverter
public static File fromStringToFile(String value) {
Expand All @@ -17,14 +15,4 @@ public static String fromFileToString(File value) {
return value == null ? null : value.getAbsolutePath();
}

@TypeConverter
public static Platform fromIntToPlatform(Integer value) {
return value == null ? null : Platform.values()[value];
}

@TypeConverter
public static Integer fromPlatformToInteger(Platform value) {
return value == null ? null : value.ordinal();
}

}
27 changes: 11 additions & 16 deletions app/src/main/java/io/mgba/Data/Database/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;

import java.io.File;

import io.mgba.Data.Platform;
import java.util.Objects;
import io.mgba.Constants;
import io.mgba.Model.IO.FilesManager;

@Entity(tableName = "Games")
Expand All @@ -35,7 +33,7 @@ public Game[] newArray(int size) {
@NonNull
private File file;
@ColumnInfo()
private Platform platform;
private int platform;

@ColumnInfo()
private String name = null;
Expand Down Expand Up @@ -65,7 +63,7 @@ public Game[] newArray(int size) {
public Game() { }

@Ignore
public Game(String path, String name, String description, String released, String developer, String genre, String coverURL, String MD5, boolean favourite, Platform platform) {
public Game(String path, String name, String description, String released, String developer, String genre, String coverURL, String MD5, boolean favourite, int platform) {
this.file = new File(path);
this.name = name;
this.description = description;
Expand All @@ -79,7 +77,7 @@ public Game(String path, String name, String description, String released, Strin
}

@Ignore
public Game(String path, Platform platform) {
public Game(String path, int platform) {
this.file = new File(path);
this.platform = platform;
}
Expand All @@ -95,8 +93,7 @@ protected Game(Parcel in) {
this.coverURL = in.readString();
this.MD5 = in.readString();
this.favourite = in.readByte() != 0;
int tmpPlatform = in.readInt();
this.platform = tmpPlatform == -1 ? null : Platform.values()[tmpPlatform];
this.platform = in.readInt();
}

public String getName() {
Expand Down Expand Up @@ -174,16 +171,16 @@ public void setFavourite(boolean favourite) {
this.favourite = favourite;
}

public void setPlatform(Platform platform){
public void setPlatform(int platform){
this.platform = platform;
}

public Platform getPlatform() {
public int getPlatform() {
return platform;
}

public boolean isAdvanced() {
return platform.getValue() == Platform.GBA.getValue();
return platform == Constants.PLATFORM_GBA;
}

@Override
Expand All @@ -207,7 +204,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.coverURL);
dest.writeString(this.MD5);
dest.writeByte(this.favourite ? (byte) 1 : (byte) 0);
dest.writeInt(this.platform == null ? -1 : this.platform.ordinal());
dest.writeInt(this.platform);
}

@Override
Expand All @@ -222,9 +219,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = getFile().hashCode();
result = 31 * result + getPlatform().hashCode();
return result;
return Objects.hash(file, platform, name, description, released, developer, genre, coverURL, MD5, favourite);
}

public boolean needsUpdate() {
Expand Down
38 changes: 0 additions & 38 deletions app/src/main/java/io/mgba/Data/Platform.java

This file was deleted.

8 changes: 2 additions & 6 deletions app/src/main/java/io/mgba/Model/IO/LocalDB.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package io.mgba.Model.IO;

import android.content.Context;

import java.io.File;
import java.util.List;

import io.mgba.Data.Database.Cheat;
import io.mgba.Data.Database.Database;
import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.mgba.Model.Interfaces.IDatabase;

import static io.mgba.mgba.printLog;
Expand All @@ -26,8 +22,8 @@ public LocalDB(Database database) {
}

@Override
public List<Game> getGamesForPlatform(final Platform platform){
return db.gameDao().getGamesForPlatform(platform.ordinal());
public List<Game> getGamesForPlatform(final int platform){
return db.gameDao().getGamesForPlatform(platform);
}

@Override
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/java/io/mgba/Model/Interfaces/IDatabase.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package io.mgba.Model.Interfaces;

import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Transaction;

import java.io.File;
import java.util.List;

import io.mgba.Data.Database.Cheat;
import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;

public interface IDatabase {
List<Game> getGamesForPlatform(final Platform platform);
List<Game> getGamesForPlatform(final int platform);
List<Game> getFavouritesGames();
void insert(Game... games);
void delete();
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/io/mgba/Model/Interfaces/ILibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import java.util.List;

import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.reactivex.Single;

public interface ILibrary {
Single<List<Game>> prepareGames(Platform platform);
Single<List<Game>> prepareGames(int platform);
Single<List<Game>> query(String query);

Single<List<Game>> reloadGames(Platform... platform);
Single<List<Game>> reloadGames(String path, Platform... platform);
Single<List<Game>> reloadGames(int... platform);
Single<List<Game>> reloadGames(String path, int... platform);
}
47 changes: 28 additions & 19 deletions app/src/main/java/io/mgba/Model/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.annimon.stream.Stream;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.inject.Inject;

import dagger.Lazy;
import io.mgba.Constants;
import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.mgba.Data.Remote.DTOs.GameJSON;
import io.mgba.Data.Remote.Interfaces.IRequest;
import io.mgba.Model.IO.Decoder;
Expand Down Expand Up @@ -43,17 +41,20 @@ public Library(@NonNull IDatabase database, @NonNull IFilesManager filesService,
}

@Override
public Single<List<Game>> prepareGames(Platform platform) {
public Single<List<Game>> prepareGames(int platform) {
Single<List<Game>> ret = Single.create(subscriber -> {

if(platform == null){
subscriber.onSuccess(new LinkedList<>());
return;
}
if(platform == Constants.PLATFORM_GBC
|| platform == Constants.PLATFORM_GBA
|| platform == Constants.PLATFORM_FAVS){

List<Game> games = database.getGamesForPlatform(platform);
List<Game> games = database.getGamesForPlatform(platform);

subscriber.onSuccess(games);
subscriber.onSuccess(games);

}else{
subscriber.onSuccess(new LinkedList<>());
}
});

return ret.doOnError(mgba::report);
Expand All @@ -77,7 +78,7 @@ public Single<List<Game>> query(String query) {
}

@Override
public Single<List<Game>> reloadGames(Platform... platform) {
public Single<List<Game>> reloadGames(int... platform) {
Single<List<Game>> ret = Single.create(subscriber -> {
List<Game> games = database.getGames();
removeGamesFromDatabase(games);
Expand All @@ -87,15 +88,15 @@ public Single<List<Game>> reloadGames(Platform... platform) {
games.addAll(updatedList);

Collections.sort(games, (o1, o2) -> o1.getName().compareTo(o2.getName()));
subscriber.onSuccess(filter(Arrays.asList(platform), games));
subscriber.onSuccess(filter(platform, games));
});

return ret.doOnError(mgba::report);

}

@Override
public Single<List<Game>> reloadGames(String path, Platform... platform) {
public Single<List<Game>> reloadGames(String path, int... platform) {
filesService.setCurrentDirectory(path);
return reloadGames(platform);
}
Expand Down Expand Up @@ -126,18 +127,26 @@ private List<Game> processNewGames(List<Game> games){
}).toList();
}

private List<Game> filter(List<Platform> platform, List<Game> games){
private List<Game> filter(int[] platform, List<Game> games){
return Stream.of(games)
.filter(g -> platform.contains(g.getPlatform()))
.filter(g -> {
for (int i = 0; i < platform.length; i++) {
if(platform[i] == g.getPlatform())
return true;
}

return false;
})
.toList();
}

private Platform getPlatform(File file) {
private int getPlatform(File file) {
final String fileExtension = FilesManager.getFileExtension(file);
if(Platform.GBA.getExtensions().contains(fileExtension))
return Platform.GBA;

return Platform.GBC;
if(Constants.PLATFORM_GBA_EXT.contains(fileExtension))
return Constants.PLATFORM_GBA;

return Constants.PLATFORM_GBC;
}

private void storeInDatabase(Game game){
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/io/mgba/Presenter/GamesPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import io.mgba.Presenter.Interfaces.IGamesPresenter;
import io.mgba.Data.Database.Game;
import io.mgba.Data.Platform;
import io.mgba.UI.Fragments.Interfaces.IGamesFragment;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
Expand All @@ -13,9 +12,9 @@
public class GamesPresenter implements IGamesPresenter {
private final IGamesFragment<Game> view;
private CompositeDisposable disposable = new CompositeDisposable();
private Platform platform;
private int platform;

public GamesPresenter(Platform platform, IGamesFragment<Game> view) {
public GamesPresenter(int platform, IGamesFragment<Game> view) {
this.view = view;
this.platform = platform;
}
Expand All @@ -26,7 +25,7 @@ public void onDestroy() {
}

@Override
public Platform getPlatform() {
public int getPlatform() {
return platform;
}

Expand Down
Loading

0 comments on commit 7afbc9a

Please sign in to comment.