Skip to content

Commit b546a6a

Browse files
committed
Got rid of useless static variables
1 parent 12ff6c0 commit b546a6a

File tree

13 files changed

+45
-46
lines changed

13 files changed

+45
-46
lines changed

app/src/main/java/ro/tav/pavgame/PavGameApplication.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class PavGameApplication extends Application {
2424
private static PavGameApplication pavGameApplication;
2525
private final List < Activity > activities;
26-
private static NotificationManager notificationManager = null;
26+
private NotificationManager notificationManager = null;
2727

2828
public PavGameApplication() {
2929
super();
@@ -76,19 +76,19 @@ public void onTerminate() {
7676
Timber.d( "Aplicatia -> onTerminate() nu mai are activitati" );
7777
}
7878

79-
public static void addActivity( Activity a ) {
79+
public void addActivity( Activity a ) {
8080
pavGameApplication.activities.add( a );
8181
}
8282

83-
public static void removeActivity( Activity a ) {
83+
public void removeActivity( Activity a ) {
8484
pavGameApplication.activities.remove( a );
8585
}
8686

8787
public static PavGameApplication getApplication() {
8888
return pavGameApplication;
8989
}
9090

91-
public static NotificationManager getNotificationManager() {
91+
public NotificationManager getNotificationManager() {
9292
return notificationManager;
9393
}
9494
}

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
@@ -46,14 +46,14 @@ protected void insertGame( GameEntity game ) {
4646

4747
@Dao
4848
protected interface GameDao {
49-
@Query( "SELECT DISTINCT numeJucator, gameType, result, cast (count(*) as text) as gameId from gameEntity group by numeJucator, gameType, result order by count(*) desc, numeJucator, gameType desc, result desc" )
49+
@Query( "SELECT DISTINCT numeJucator, gameType, result, cast (count(*) as text) as gameId from gameEntity group by numeJucator, gameType, result order by count(*) desc, gameType desc, result desc, numeJucator" )
5050
LiveData < List < GameEntity > > getAllGames();
5151

5252
@Query( "SELECT DISTINCT numeJucator, gameType, result, cast ( count(*) as text) as gameId from gameEntity group by numeJucator, gameType, result having numeJucator=:user order by gameType desc, result desc" )
5353
LiveData < List < GameEntity > > getSpecificGamesbyUserName( String user );
5454

5555
@Query( "SELECT DISTINCT numeJucator, gameType, result, cast ( count(*) as text) as gameId from gameEntity group by numeJucator, gameType, result having numeJucator=:user order by gameType desc, result desc" )
56-
List < GameEntity > getSpecificGamesbyUserNameStatic( String user );
56+
List < GameEntity > getSpecificGamesbyUserNameAsList( String user );
5757

5858
@Insert( onConflict = OnConflictStrategy.IGNORE )
5959
void insertGame( GameEntity game );

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import androidx.lifecycle.AndroidViewModel;
66
import androidx.lifecycle.LiveData;
77

8+
import com.google.firebase.auth.FirebaseAuth;
9+
810
import java.sql.Timestamp;
911
import java.util.List;
1012

@@ -13,8 +15,7 @@
1315
import ro.tav.pavgame.domain.PavGameDependencyProvider;
1416

1517
public class PavGameViewModel extends AndroidViewModel {
16-
private static final String defaultUserName = "Current User";
17-
private static String userName = defaultUserName;
18+
private static FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
1819
private final GameUseCase gameUseCase;
1920

2021
public PavGameViewModel( Application application ) {//AndroidViewModel extinde ViewModel si ne permite sa avem acest prototip in constructor
@@ -23,14 +24,16 @@ public PavGameViewModel( Application application ) {//AndroidViewModel extinde V
2324
this.gameUseCase = pavGameDependencyProvider.provideUseCase(); //comunicarea cu domain si data
2425
}
2526

26-
public static void setUserName( String name ) { //apelat din LoginActivity
27-
if ( name == null )
28-
name = defaultUserName;
29-
userName = name;
27+
public static void setFirebaseAuth( FirebaseAuth firebaseAuth ) {
28+
mFirebaseAuth = firebaseAuth;
29+
}
30+
31+
public static FirebaseAuth getFirebaseAuth() {
32+
return mFirebaseAuth;
3033
}
3134

3235
public static String getUserName() {
33-
return userName;
36+
return mFirebaseAuth.getCurrentUser().getEmail();
3437
}
3538

3639
public void addResult( String userName, boolean result, int gametype ) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ else if ( tipDala == 1 ) {//mutarile
300300
//adaugam jocul in baza de date folosindu-ne de viewModel
301301
PavGameViewModel pavGameViewModel = new ViewModelProvider( getActivity(), new PavGameViewModelFactory( PavGameApplication.getApplication() ) ).get( PavGameViewModel.class );
302302
pavGameViewModel.addResult( PavGameViewModel.getUserName(), result, lat );
303-
PavGameApplication.getNotificationManager().notify( PavGameNotificationFactory.getHelloNotificationId(),
303+
PavGameApplication.getApplication().getNotificationManager().notify( PavGameNotificationFactory.getHelloNotificationId(),
304304
PavGameNotificationFactory.createCustomHelloNotification( getContext(),
305305
s1, s2 ) );
306306
Toast.makeText( getContext(), messege, Toast.LENGTH_LONG ).show();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
import androidx.annotation.Nullable;
1111
import androidx.fragment.app.Fragment;
1212

13-
import java.util.Objects;
14-
1513
import ro.tav.pavgame.R;
16-
import ro.tav.pavgame.presentation.view.LoginActivity;
14+
import ro.tav.pavgame.presentation.PavGameViewModel;
1715

1816
public class HomeFragment extends Fragment {
1917
@Nullable
@@ -22,7 +20,7 @@ public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup
2220
View root = super.onCreateView( inflater, container, savedInstanceState );
2321
if ( root != null ) {
2422
TextView textView = root.findViewById( R.id.text_home );
25-
textView.setText( String.format( getString( R.string.welcome ), Objects.requireNonNull( LoginActivity.getFireBaseCurrentInstance().getCurrentUser() ).getEmail() ) );
23+
textView.setText( String.format( getString( R.string.welcome ), PavGameViewModel.getUserName() ) );
2624
}
2725
return root;
2826
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CreditsActivity extends Activity {
2020
protected void onCreate( Bundle savedInstanceState ) {
2121
super.onCreate( savedInstanceState );
2222
setContentView( R.layout.activity_credits );
23-
PavGameApplication.addActivity( this );
23+
PavGameApplication.getApplication().addActivity( this );
2424

2525
CardView view = findViewById( R.id.cardViewProgrammer );
2626
view.setOnClickListener( new View.OnClickListener() {
@@ -46,6 +46,6 @@ public void onClick( View v ) {
4646
@Override
4747
protected void onDestroy() {
4848
super.onDestroy();
49-
PavGameApplication.removeActivity( this );
49+
PavGameApplication.getApplication().removeActivity( this );
5050
}
5151
}

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,20 @@
1818
import com.google.firebase.auth.FirebaseAuth;
1919
import com.google.firebase.auth.FirebaseUser;
2020

21-
import java.util.Objects;
22-
2321
import ro.tav.pavgame.PavGameApplication;
2422
import ro.tav.pavgame.R;
2523
import ro.tav.pavgame.presentation.PavGameViewModel;
2624

2725
public class LoginActivity extends AppCompatActivity {
2826
private EditText email, password;
29-
static FirebaseAuth mFirebaseAuth;
27+
private FirebaseAuth mFirebaseAuth;
3028
private FirebaseAuth.AuthStateListener mAuthStateListener;
3129

3230
@Override
3331
protected void onCreate( Bundle savedInstanceState ) {
3432
super.onCreate( savedInstanceState );
3533
setContentView( R.layout.activity_login );
36-
PavGameApplication.addActivity( this );
34+
PavGameApplication.getApplication().addActivity( this );
3735

3836
mFirebaseAuth = FirebaseAuth.getInstance();
3937
email = findViewById( R.id.email );
@@ -44,13 +42,12 @@ protected void onCreate( Bundle savedInstanceState ) {
4442
public void onAuthStateChanged( @NonNull FirebaseAuth firebaseAuth ) {
4543
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
4644
if ( mFirebaseUser != null ) {
47-
Toast.makeText( LoginActivity.this, "You are logged in", Toast.LENGTH_SHORT ).show();
48-
PavGameViewModel.setUserName( mFirebaseUser.getEmail() );
45+
Toast.makeText( LoginActivity.this, getString( R.string.loggedIn ), Toast.LENGTH_SHORT ).show();
46+
PavGameViewModel.setFirebaseAuth( mFirebaseAuth );
4947
Intent i = new Intent( LoginActivity.this, MainActivity.class );
5048
startActivity( i );
5149
} else
52-
Toast.makeText( LoginActivity.this, "Please Login", Toast.LENGTH_SHORT ).show();
53-
50+
Toast.makeText( LoginActivity.this, getString( R.string.loginRequired ), Toast.LENGTH_SHORT ).show();
5451
}
5552
};
5653

@@ -91,7 +88,7 @@ public void onComplete( @NonNull Task < AuthResult > task ) {
9188
if ( !task.isSuccessful() ) {
9289
Toast.makeText( LoginActivity.this, "Login Error. Please try again", Toast.LENGTH_SHORT ).show();
9390
} else {
94-
PavGameViewModel.setUserName( Objects.requireNonNull( mFirebaseAuth.getCurrentUser() ).getEmail() );
91+
PavGameViewModel.setFirebaseAuth( mFirebaseAuth );
9592
Intent intent = new Intent( LoginActivity.this, MainActivity.class );
9693
startActivity( intent );
9794
}
@@ -105,6 +102,7 @@ public void onComplete( @NonNull Task < AuthResult > task ) {
105102
login_notRegistered.setOnClickListener( new View.OnClickListener() {
106103
@Override
107104
public void onClick( View view ) {
105+
PavGameViewModel.setFirebaseAuth( mFirebaseAuth );
108106
Intent intent = new Intent( LoginActivity.this, RegisterActivity.class );
109107
startActivity( intent );
110108
}
@@ -131,10 +129,6 @@ protected void onStop() {
131129
@Override
132130
protected void onDestroy() {
133131
super.onDestroy();
134-
PavGameApplication.removeActivity( this );
135-
}
136-
137-
public static FirebaseAuth getFireBaseCurrentInstance() {
138-
return mFirebaseAuth;
132+
PavGameApplication.getApplication().removeActivity( this );
139133
}
140134
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
3939
protected void onCreate( Bundle savedInstanceState ) {
4040
super.onCreate( savedInstanceState );
4141
///adaugam instanta
42-
PavGameApplication.addActivity( this );
42+
PavGameApplication.getApplication().addActivity( this );
4343

4444
//ne afisam un mesaj cu timber
4545
Timber.i( "MainActivity created" );
@@ -81,7 +81,7 @@ public void onClick( View view ) {
8181

8282

8383
//afisam o notificare de bun venit
84-
PavGameApplication.getNotificationManager().notify( PavGameNotificationFactory.getHelloNotificationId(),
84+
PavGameApplication.getApplication().getNotificationManager().notify( PavGameNotificationFactory.getHelloNotificationId(),
8585
PavGameNotificationFactory.createHelloNotification( this ) );
8686

8787
}
@@ -107,8 +107,8 @@ public boolean onKeyLongPress( int keyCode, KeyEvent event ) {
107107
if ( keyCode == KeyEvent.KEYCODE_BACK ) {
108108
popFragment();
109109
if ( fragmentStack.empty() ) {
110-
while ( LoginActivity.getFireBaseCurrentInstance().getCurrentUser() != null )
111-
LoginActivity.getFireBaseCurrentInstance().signOut();
110+
while ( PavGameViewModel.getFirebaseAuth().getCurrentUser() != null )
111+
PavGameViewModel.getFirebaseAuth().signOut();
112112
finish();
113113
}
114114
}
@@ -203,7 +203,7 @@ public boolean onCreateOptionsMenu( Menu menu ) {
203203
@Override
204204
public void onDestroy() {
205205
super.onDestroy();
206-
PavGameApplication.removeActivity( this );
206+
PavGameApplication.getApplication().removeActivity( this );
207207
}
208208

209209

@@ -212,8 +212,8 @@ public void openCreditActivity( MenuItem item ) {
212212
}
213213

214214
public void makeLogout( View view ) {
215-
while ( LoginActivity.getFireBaseCurrentInstance().getCurrentUser() != null )
216-
LoginActivity.getFireBaseCurrentInstance().signOut();
215+
while ( PavGameViewModel.getFirebaseAuth().getCurrentUser() != null )
216+
PavGameViewModel.getFirebaseAuth().signOut();
217217
finish();
218218
}
219219
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected void onCreate( Bundle savedInstanceState ) {
3030
actionBar.setDisplayHomeAsUpEnabled( true );
3131

3232
//adaugam activitatea in lista
33-
PavGameApplication.addActivity( this );
33+
PavGameApplication.getApplication().addActivity( this );
3434

3535
// get recycler view from xml layout
3636
RecyclerView mRecyclerViewGames = findViewById( R.id.recycler_view_contacts_1 );
@@ -63,7 +63,7 @@ protected void onCreate( Bundle savedInstanceState ) {
6363
@Override
6464
protected void onDestroy() {
6565
super.onDestroy();
66-
PavGameApplication.removeActivity( this );
66+
PavGameApplication.getApplication().removeActivity( this );
6767
}
6868

6969

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class RegisterActivity extends AppCompatActivity {
2828
protected void onCreate( Bundle savedInstanceState ) {
2929
super.onCreate( savedInstanceState );
3030
setContentView( R.layout.activity_register );
31-
PavGameApplication.addActivity( this );
31+
PavGameApplication.getApplication().addActivity( this );
3232

3333
TextView register_notLogedIn = findViewById( R.id.logInFromRegister );
3434
register_notLogedIn.setOnClickListener( new View.OnClickListener() {
@@ -102,6 +102,6 @@ protected void onStop() {
102102
@Override
103103
protected void onDestroy() {
104104
super.onDestroy();
105-
PavGameApplication.removeActivity( this );
105+
PavGameApplication.getApplication().removeActivity( this );
106106
}
107107
}

0 commit comments

Comments
 (0)