Skip to content

Commit f3562f0

Browse files
committed
Deleted PavGameViewModelI and returned to PavGameViewModel extends Viewmodel +
Am mutat usernameul din MainActivity in PavGameViewModel
1 parent bbdf350 commit f3562f0

File tree

6 files changed

+49
-44
lines changed

6 files changed

+49
-44
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ro.tav.pavgame.presentation;
2+
3+
import androidx.lifecycle.ViewModel;
4+
5+
import ro.tav.pavgame.PavGameApplication;
6+
import ro.tav.pavgame.data.GameEntity;
7+
import ro.tav.pavgame.domain.GameUseCase;
8+
9+
public class PavGameViewModel extends ViewModel {
10+
private static final String defaultUserName = "Current User";
11+
private static final GameUseCase gameUseCase = new GameUseCase( PavGameApplication.getApplication() );///gameUseCase face legatura cu repo-ul din domain
12+
private static String userName = defaultUserName;
13+
14+
public static void setUserName( String name ) {
15+
if ( name == null )
16+
name = defaultUserName;
17+
userName = name;
18+
}
19+
20+
public static String getUserName() {
21+
return userName;
22+
}
23+
24+
public static GameUseCase getGameUseCase() {
25+
return gameUseCase;
26+
}
27+
28+
public static void addResult( String userName, String result, String gametype ) {
29+
//construim jocul si apoi il trimitem in bindingAdapter pt a fi adaugat
30+
GameEntity mGame = new GameEntity();
31+
mGame.setNumeJucator( userName );
32+
mGame.setResult( result );
33+
mGame.setGameType( gametype );
34+
mGame.setGameId( "" );//va fi setat in mediator pt a evita ca ce se descarca de pe firebase sa fie uploadat inca o data
35+
gameUseCase.insertGame( mGame );
36+
}
37+
}

app/src/main/java/ro/tav/pavgame/presentation/PavGameViewModelI.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/src/main/java/ro/tav/pavgame/presentation/fragments/GameFragment.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
import ro.tav.pavgame.PavGameApplication;
2626
import ro.tav.pavgame.R;
2727
import ro.tav.pavgame.presentation.PavGameService;
28-
import ro.tav.pavgame.presentation.PavGameViewModelI;
28+
import ro.tav.pavgame.presentation.PavGameViewModel;
2929
import ro.tav.pavgame.presentation.notification.PavGameNotificationFactory;
30-
import ro.tav.pavgame.presentation.view.MainActivity;
3130
import timber.log.Timber;
3231

3332
public class GameFragment extends Fragment {
@@ -60,7 +59,7 @@ public void onActivityCreated( @Nullable Bundle savedInstanceState ) {
6059
long timeSpent; //timpul petrecut(cu precizie de minute) in joc
6160
sharedPreferences = super.requireContext().getSharedPreferences( pavGameSharedPreference, Context.MODE_PRIVATE );
6261
try {
63-
String s = sharedPreferences.getString( MainActivity.getUserName(), "0" );//preluam timpul utilizatorului
62+
String s = sharedPreferences.getString( PavGameViewModel.getUserName(), "0" );//preluam timpul utilizatorului
6463
timeSpent = Long.parseLong( s );
6564
} catch ( Exception e ) {
6665
Timber.d( e );
@@ -97,7 +96,7 @@ public void onDestroyView() {
9796
//si ii fac update in shared preferances
9897
Gson gson = new Gson();
9998
String json = gson.toJson( newTime ); ///convertim acest timp la un json string
100-
sharedPreferences.edit().putString( MainActivity.getUserName() + "", json ).apply(); ///updatam valoarea in shared preferances
99+
sharedPreferences.edit().putString( PavGameViewModel.getUserName() + "", json ).apply(); ///updatam valoarea in shared preferances
101100

102101
stopGameService();
103102
}
@@ -278,17 +277,17 @@ else if ( tipDala == 1 ) {//mutarile
278277
button.setText( messege );
279278
result = "Win";
280279
s1 = getString( R.string.Win );
281-
s2 = getString( R.string.victorie ) + ", " + MainActivity.getUserName() + "!";
280+
s2 = getString( R.string.victorie ) + ", " + PavGameViewModel.getUserName() + "!";
282281
} else {
283282
messege = getString( R.string.esec ) + " :(";
284283
button.setText( messege );
285284
result = "Lose";
286285
s1 = getString( R.string.Lose );
287-
s2 = getString( R.string.esec ) + ", " + MainActivity.getUserName() + "!";
286+
s2 = getString( R.string.esec ) + ", " + PavGameViewModel.getUserName() + "!";
288287
}
289288

290289
//adaugam jocul folosindu-ne de viewModel
291-
PavGameViewModelI.addResult( MainActivity.getUserName(), result, "Game Type: " + lat + "x" + lat );
290+
PavGameViewModel.addResult( PavGameViewModel.getUserName(), result, "Game Type: " + lat + "x" + lat );
292291
PavGameApplication.getNotificationManager().notify( PavGameNotificationFactory.getHelloNotificationId(),
293292
PavGameNotificationFactory.createCustomHelloNotification( getContext(),
294293
s1, s2 ) );

app/src/main/java/ro/tav/pavgame/presentation/view/LoginActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import ro.tav.pavgame.PavGameApplication;
2424
import ro.tav.pavgame.R;
25+
import ro.tav.pavgame.presentation.PavGameViewModel;
2526

2627
public class LoginActivity extends AppCompatActivity {
2728
private EditText email, password;
@@ -44,7 +45,7 @@ public void onAuthStateChanged( @NonNull FirebaseAuth firebaseAuth ) {
4445
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
4546
if ( mFirebaseUser != null ) {
4647
Toast.makeText( LoginActivity.this, "You are logged in", Toast.LENGTH_SHORT ).show();
47-
MainActivity.setUserName( mFirebaseUser.getEmail() );
48+
PavGameViewModel.setUserName( mFirebaseUser.getEmail() );
4849
Intent i = new Intent( LoginActivity.this, MainActivity.class );
4950
startActivity( i );
5051
} else
@@ -90,7 +91,7 @@ public void onComplete( @NonNull Task < AuthResult > task ) {
9091
if ( !task.isSuccessful() ) {
9192
Toast.makeText( LoginActivity.this, "Login Error. Please try again", Toast.LENGTH_SHORT ).show();
9293
} else {
93-
MainActivity.setUserName( Objects.requireNonNull( mFirebaseAuth.getCurrentUser() ).getEmail() );
94+
PavGameViewModel.setUserName( Objects.requireNonNull( mFirebaseAuth.getCurrentUser() ).getEmail() );
9495
Intent intent = new Intent( LoginActivity.this, MainActivity.class );
9596
startActivity( intent );
9697
}

app/src/main/java/ro/tav/pavgame/presentation/view/MainActivity.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535

3636
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
37-
private static String userName = "Current User";
3837
private final PavGameFragmentStack fragmentStack = new PavGameFragmentStack();
3938

4039
@Override
@@ -218,16 +217,4 @@ public void makeLogout( View view ) {
218217
LoginActivity.getFireBaseCurrentInstance().signOut();
219218
finish();
220219
}
221-
222-
223-
protected static void setUserName( String name ) {
224-
if ( name == null )
225-
name = "Current User";
226-
userName = name;
227-
}
228-
229-
public static String getUserName() {
230-
return userName;
231-
}
232-
233220
}

app/src/main/java/ro/tav/pavgame/presentation/view/recycleViewAux/PavGameBindingAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010

1111
import ro.tav.pavgame.data.GameEntity;
12-
import ro.tav.pavgame.presentation.PavGameViewModelI;
12+
import ro.tav.pavgame.presentation.PavGameViewModel;
1313

1414
public class PavGameBindingAdapter {
1515
@BindingAdapter( { "game_adapter", "user" } )
@@ -23,14 +23,14 @@ public static void recycleViewGamesBinding( RecyclerView mRecyclerViewGames, Gam
2323
}
2424
//pornim binduirea permanenta intre recyleview si baza de date
2525
if ( user == null ) {
26-
PavGameViewModelI.gameUseCase.getAllGames().observeForever( new Observer < List < GameEntity > >() {
26+
PavGameViewModel.getGameUseCase().getAllGames().observeForever( new Observer < List < GameEntity > >() {
2727
@Override
2828
public void onChanged( @Nullable final List < GameEntity > games ) {
2929
gamesAdapter.setGames( games );
3030
}
3131
} );
3232
} else {
33-
PavGameViewModelI.gameUseCase.getSpecificGamesbyUserName( user ).observeForever( new Observer < List < GameEntity > >() {
33+
PavGameViewModel.getGameUseCase().getSpecificGamesbyUserName( user ).observeForever( new Observer < List < GameEntity > >() {
3434
@Override
3535
public void onChanged( @Nullable final List < GameEntity > games ) {
3636
gamesAdapter.setGames( games );

0 commit comments

Comments
 (0)