Skip to content

Commit 12ff6c0

Browse files
committed
-Implemented dependency injection as PavGameDependencyProvider
-Changed ViewModel to extend AndroidViewModel, so that it could have Application in constructor -Created PavGameViewModelFactory so that we can instantiate the ViewModel with parametrized constructor -Cleaned up binding logic and so that it would use ViewModel methods -Modified all classes in domain to support dependency injection -Cleaned up data packages -Progressed to versionName "2.4"
1 parent 1279836 commit 12ff6c0

23 files changed

+233
-139
lines changed

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ android {
1313
minSdkVersion 23
1414
targetSdkVersion 30
1515
versionCode 2
16-
versionName "2.3"
16+
versionName "2.4"
1717
javaCompileOptions {
1818
annotationProcessorOptions {
1919
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
@@ -75,7 +75,8 @@ dependencies {
7575
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
7676
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
7777
androidTestImplementation "androidx.work:work-testing:2.5.0"
78-
testImplementation 'junit:junit:4.13.1'
78+
testImplementation 'junit:junit:4.13.2'
7979
testImplementation "org.robolectric:robolectric:4.0.2"
8080
testImplementation 'androidx.test:core:1.3.0'
81+
testImplementation "org.mockito:mockito-core:3.3.3"
8182
}
File renamed without changes.

app/src/main/java/ro/tav/pavgame/data/localDB/AppDatabase.java renamed to app/src/main/java/ro/tav/pavgame/data/source/AppDatabase.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ro.tav.pavgame.data.localDB;
1+
package ro.tav.pavgame.data.source;
22

33
import android.content.Context;
44

@@ -14,14 +14,16 @@
1414
@Database( entities = { GameEntity.class }, version = 1 )
1515
public abstract class AppDatabase extends RoomDatabase {
1616
private static volatile AppDatabase INSTANCE;
17-
18-
protected abstract LocalGameDataSource.GameDao gameDao();
19-
2017
private static final int NUMBER_OF_THREADS = 4;
18+
protected static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool( NUMBER_OF_THREADS );
19+
20+
protected AppDatabase() {
21+
//empty constructor for making class protected
22+
}
2123

22-
public static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool( NUMBER_OF_THREADS );
24+
protected abstract LocalGameDataSource.GameDao gameDao();
2325

24-
public static AppDatabase getAppDatabase( final Context context ) {
26+
protected static AppDatabase getAppDatabase( final Context context ) {
2527
if ( INSTANCE == null ) {
2628
synchronized ( AppDatabase.class ) {
2729
INSTANCE = Room.databaseBuilder( context.getApplicationContext(),
@@ -32,8 +34,4 @@ public static AppDatabase getAppDatabase( final Context context ) {
3234
}
3335
return INSTANCE;
3436
}
35-
36-
protected AppDatabase() {
37-
//empty constructor for making class protected
38-
}
3937
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
package ro.tav.pavgame.data.inMemoryDB;
1+
package ro.tav.pavgame.data.source;
22

33
import ro.tav.pavgame.data.GameEntity;
44
import ro.tav.pavgame.domain.GameInMemoryRepository;
55

66
public class InMemoryDataSource extends GameInMemoryRepository {
7+
private final InMemoryTemporaryStorage inMemoryTemporaryStorage;
8+
79
public InMemoryDataSource() {
8-
super( new InMemoryDatabase() );
10+
super();
11+
inMemoryTemporaryStorage = new InMemoryTemporaryStorage();
912
}
1013

1114
@Override
1215
protected void addInMemory( GameEntity gameEntity ) {
13-
inMemoryDatabase.addInMemory( gameEntity );
16+
inMemoryTemporaryStorage.addInMemory( gameEntity );
1417
}
1518

1619
@Override
1720
protected GameEntity removeInMemory() {
18-
return inMemoryDatabase.removeInMemory();
21+
return inMemoryTemporaryStorage.removeInMemory();
1922
}
2023

2124
@Override
2225
protected int getNrOfElements() {
23-
return inMemoryDatabase.getNrOfElements();
26+
return inMemoryTemporaryStorage.getNrOfElements();
2427
}
2528
}

app/src/main/java/ro/tav/pavgame/data/inMemoryDB/InMemoryDatabase.java renamed to app/src/main/java/ro/tav/pavgame/data/source/InMemoryTemporaryStorage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package ro.tav.pavgame.data.inMemoryDB;
1+
package ro.tav.pavgame.data.source;
22

33
import java.util.LinkedList;
44

55
import ro.tav.pavgame.data.GameEntity;
66

7-
public class InMemoryDatabase {
7+
public class InMemoryTemporaryStorage {
88
private static final LinkedList < GameEntity > q = new LinkedList <>();
99
private static int nrOfElements = 0;
1010

11-
protected InMemoryDatabase() {
11+
protected InMemoryTemporaryStorage() {
1212
//empty constructor for making it protected
1313
}
1414

app/src/main/java/ro/tav/pavgame/data/localDB/LocalGameDataSource.java renamed to app/src/main/java/ro/tav/pavgame/data/source/LocalGameDataSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ro.tav.pavgame.data.localDB;
1+
package ro.tav.pavgame.data.source;
22

33
import android.content.Context;
44

@@ -16,7 +16,7 @@
1616

1717

1818
public class LocalGameDataSource extends GameLocalRepository {
19-
protected final LocalGameDataSource.GameDao mGameDao;
19+
protected final GameDao mGameDao;
2020

2121
public LocalGameDataSource( Context context ) {
2222
super();

app/src/main/java/ro/tav/pavgame/data/remoteDB/RemoteDataSource.java renamed to app/src/main/java/ro/tav/pavgame/data/source/RemoteDataSource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ro.tav.pavgame.data.remoteDB;
1+
package ro.tav.pavgame.data.source;
22

33
import com.facebook.stetho.okhttp3.StethoInterceptor;
44
import com.google.gson.Gson;
@@ -62,12 +62,13 @@ public void onResponse( @NotNull Call < GameEntity > call, @NotNull Response < G
6262
@Override
6363
public void onFailure( @NotNull Call < GameEntity > call, @NotNull Throwable t ) {
6464
Timber.d( "fail inserting game in firebase db" );
65-
addInMemoryRepository( gameEntity );
65+
InMemoryDataSource inMemoryDataSource = new InMemoryDataSource();
66+
inMemoryDataSource.addInMemory( gameEntity );
6667
}
6768
} );
6869
}
6970

70-
protected interface RetrofitApi {
71+
private interface RetrofitApi {
7172
String BASE_URL = "https://pav-game-tav.firebaseio.com/";
7273

7374
@GET( "games.json" )

app/src/main/java/ro/tav/pavgame/domain/GameInMemoryRepository.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package ro.tav.pavgame.domain;
22

33
import ro.tav.pavgame.data.GameEntity;
4-
import ro.tav.pavgame.data.inMemoryDB.InMemoryDatabase;
54

65
public abstract class GameInMemoryRepository {
7-
protected final InMemoryDatabase inMemoryDatabase;
8-
9-
protected GameInMemoryRepository( InMemoryDatabase inMemoryDatabase ) {
10-
this.inMemoryDatabase = inMemoryDatabase;
6+
protected GameInMemoryRepository() {
7+
//empty constructor for modifying access
118
}
129

1310
protected abstract void addInMemory( GameEntity gameEntity );

app/src/main/java/ro/tav/pavgame/domain/GameLocalRepository.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import ro.tav.pavgame.data.GameEntity;
88

99
public abstract class GameLocalRepository {
10+
protected GameLocalRepository() {
11+
//empty constructor for modifying access
12+
}
13+
1014
protected abstract LiveData < List < GameEntity > > getAllGames();
1115

1216
protected abstract LiveData < List < GameEntity > > getSpecificGamesbyUserName( String user );
1317

1418
protected abstract void insertGame( GameEntity game );
15-
16-
protected GameLocalRepository(){}
17-
}
19+
}

app/src/main/java/ro/tav/pavgame/domain/GameMediator.java

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ro.tav.pavgame.domain;
22

3-
import android.content.Context;
4-
53
import androidx.lifecycle.LiveData;
64
import androidx.work.Data;
75
import androidx.work.ExistingPeriodicWorkPolicy;
@@ -13,19 +11,20 @@
1311
import java.util.concurrent.TimeUnit;
1412

1513
import ro.tav.pavgame.data.GameEntity;
16-
import ro.tav.pavgame.data.inMemoryDB.InMemoryDataSource;
17-
import ro.tav.pavgame.data.localDB.LocalGameDataSource;
14+
import ro.tav.pavgame.data.source.InMemoryDataSource;
1815
import timber.log.Timber;
1916

2017
public class GameMediator {
21-
private final GameLocalRepository mRepository;
22-
private final Context mContext;
23-
private final Data dataforBuilder = ( new Data.Builder().putString( "mode", "get" ) ).build();
18+
private final GameLocalRepository localRepository;
19+
private final WorkManager workManager;
20+
private final Data getDataforBuilder = ( new Data.Builder().putString( "mode", "get" ) ).build();
2421
private final Data postDataforBuilder = ( new Data.Builder().putString( "mode", "post" ) ).build();
2522

26-
protected GameMediator( Context context ) {
27-
this.mContext = context;
28-
mRepository = new LocalGameDataSource( mContext );
23+
protected GameMediator( GameLocalRepository localRepository,
24+
WorkManager workManager ) {
25+
26+
this.localRepository = localRepository;
27+
this.workManager = workManager;
2928

3029
//Vrem ca primul get sa se faca instant cand se deschide aplicatia
3130
getFromRemoteRepository();
@@ -35,67 +34,64 @@ protected GameMediator( Context context ) {
3534
}
3635

3736
private void setPeriodicRequests() {
38-
try {
39-
PeriodicWorkRequest downloadWorkRequestLoop =
40-
new PeriodicWorkRequest.Builder( GameWorker.class,
41-
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS )
42-
.setInputData( dataforBuilder )
43-
.build();
44-
WorkManager.getInstance( mContext )
45-
.enqueueUniquePeriodicWork( "getFirebaseGames",
46-
ExistingPeriodicWorkPolicy.KEEP, downloadWorkRequestLoop );
47-
} catch ( Exception e ) {
48-
Timber.d( e );
49-
}
5037
//In caz ca vreun joc nu ajunge in Firebase, vrem sa il sincronizam
38+
//fara a fi necesar sa se deschida RecucleViewActivity sau sa se castige alt joc
5139
try {
5240
PeriodicWorkRequest postWorkRequest =
5341
new PeriodicWorkRequest.Builder( GameWorker.class,
5442
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS )
5543
.setInputData( postDataforBuilder )
5644
.build();
57-
WorkManager.getInstance( mContext ).enqueueUniquePeriodicWork( "postInMemoryGames",
45+
workManager.enqueueUniquePeriodicWork( "postInMemoryGames",
5846
ExistingPeriodicWorkPolicy.KEEP, postWorkRequest );
47+
// keep - va fi creat o singura data si va rula la fiecare 15min, alte instantieri fiind ignorate
48+
// ex: cand se acceseaza RecylceViewActivity sau se insereaza un joc
5949
} catch ( Exception e ) {
6050
Timber.d( e );
6151
}
6252
}
6353

64-
protected LiveData < List < GameEntity > > getAllGames() {
65-
return mRepository.getAllGames();
54+
public LiveData < List < GameEntity > > getAllGames() {
55+
return localRepository.getAllGames();
6656
}
6757

68-
protected LiveData < List < GameEntity > > getSpecificGamesbyUserName( String user ) {
69-
return mRepository.getSpecificGamesbyUserName( user );
58+
public LiveData < List < GameEntity > > getSpecificGamesbyUserName( String user ) {
59+
return localRepository.getSpecificGamesbyUserName( user );
7060
}
7161

72-
protected void insertGame( GameEntity game ) {
73-
//inseram jocul in coada repo-ului local
74-
GameInMemoryRepository gameInMemoryRepository = new InMemoryDataSource();
75-
gameInMemoryRepository.addInMemory( game );
62+
public void insertGame( GameEntity game ) {
63+
//adaugam jocul in firebase db
64+
postToRemoteRepository( game );
65+
66+
//Inseram jocul in roomDatabase
67+
localRepository.insertGame( game );
68+
}
7669

77-
//dupa ce am inserat acest joc, il vom trimite (pe el si ce mai e in coada) in repo-ul firebase
70+
private void getFromRemoteRepository() {
71+
//este apelat de cate ori se acceseaza RecycleView activity
7872
try {
79-
OneTimeWorkRequest postWorkRequest =
73+
OneTimeWorkRequest downloadWorkRequest =
8074
new OneTimeWorkRequest.Builder( GameWorker.class )
81-
.setInputData( postDataforBuilder )
75+
.setInputData( getDataforBuilder )
8276
.build();
83-
WorkManager.getInstance( mContext ).enqueue( postWorkRequest );
77+
workManager.enqueue( downloadWorkRequest );
8478
} catch ( Exception e ) {
8579
Timber.d( e );
8680
}
87-
88-
//Inseram jocul in roomDatabase
89-
mRepository.insertGame( game );
9081
}
9182

92-
private void getFromRemoteRepository() {
83+
private void postToRemoteRepository( GameEntity game ) {
84+
//inseram jocul in coada repo-ului local
85+
GameInMemoryRepository gameInMemoryRepository = new InMemoryDataSource();
86+
gameInMemoryRepository.addInMemory( game );
87+
88+
//dupa ce am inserat acest joc, il vom trimite (pe el si ce mai e in coada) in repo-ul firebase, prin worker
9389
try {
94-
OneTimeWorkRequest downloadWorkRequest =
90+
OneTimeWorkRequest postWorkRequest =
9591
new OneTimeWorkRequest.Builder( GameWorker.class )
96-
.setInputData( dataforBuilder )
92+
.setInputData( postDataforBuilder )
9793
.build();
98-
WorkManager.getInstance( mContext ).enqueue( downloadWorkRequest );
94+
workManager.enqueue( postWorkRequest );
9995
} catch ( Exception e ) {
10096
Timber.d( e );
10197
}

0 commit comments

Comments
 (0)