AmidamaruZXC

Untitled

May 19th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.85 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.     private DatabaseReference mDatabase;
  4.     private String sessionId;
  5.     private AlertDialog waitDialog;
  6.     private boolean newSession;
  7.  
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.  
  13.         findViewById(R.id.btnCreate).setEnabled(false);
  14.         findViewById(R.id.btnJoin).setEnabled(false);
  15.  
  16.         FirebaseAuth mAuth = FirebaseAuth.getInstance();
  17.         mDatabase = FirebaseDatabase.getInstance().getReference();
  18.  
  19.         View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_wait_users, null);
  20.         AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  21.         ((TextView)dialogView.findViewById(R.id.txtTitle)).setText(R.string.loading);
  22.         dialog.setView(dialogView);
  23.         dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
  24.             @Override
  25.             public void onCancel(DialogInterface dialog) {
  26.                 finish();
  27.             }
  28.         });
  29.         waitDialog = dialog.create();
  30.  
  31.         FirebaseUser currentUser = mAuth.getCurrentUser();
  32.         if (currentUser==null){
  33.             mAuth.signInWithEmailAndPassword("[email protected]", "Scrab2020")
  34.                     .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  35.                         @Override
  36.                         public void onComplete(@NonNull Task<AuthResult> task) {
  37.                             if (task.isSuccessful()) {
  38.                                 initUI();
  39.                             } else {
  40.                                 Toast.makeText(MainActivity.this, "Authentication failed.",
  41.                                         Toast.LENGTH_SHORT).show();
  42.                             }
  43.                         }
  44.                     });
  45.         } else {
  46.             initUI();
  47.         }
  48.  
  49.         findViewById(R.id.btnRules).setOnClickListener(new View.OnClickListener() {
  50.             @Override
  51.             public void onClick(View v) {
  52.                 startActivity(new Intent(MainActivity.this, RulesActivity.class));
  53.             }
  54.         });
  55.  
  56.         findViewById(R.id.btnAbout).setOnClickListener(new View.OnClickListener() {
  57.             @Override
  58.             public void onClick(View v) {
  59.                 startActivity(new Intent(MainActivity.this, AboutActivity.class));
  60.             }
  61.         });
  62.     }
  63.  
  64.     private void initUI(){
  65.         findViewById(R.id.btnCreate).setEnabled(true);
  66.         findViewById(R.id.btnCreate).setOnClickListener(new View.OnClickListener() {
  67.             @Override
  68.             public void onClick(View v) {
  69.                 newSession = true;
  70.                 createSession();
  71.             }
  72.         });
  73.         findViewById(R.id.btnJoin).setEnabled(true);
  74.         findViewById(R.id.btnJoin).setOnClickListener(new View.OnClickListener() {
  75.             @Override
  76.             public void onClick(View v) {
  77.                 newSession = false;
  78.                 joinSession();
  79.             }
  80.         });
  81.     }
  82.  
  83.     private void createSession(){
  84.         final View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_create_session, null);
  85.         AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  86.         dialog.setView(dialogView);
  87.         final TextView title = dialogView.findViewById(R.id.txtTitle);
  88.         final EditText inputId = dialogView.findViewById(R.id.input_id);
  89.         title.setText(R.string.create_session);
  90.         dialog.setPositiveButton("OK",
  91.                 new DialogInterface.OnClickListener() {
  92.                     public void onClick(DialogInterface dialog,int id) {
  93.                         hideKeyboard(dialogView);
  94.                         if (inputId.getText().toString().length()>0){
  95.                             sessionId = inputId.getText().toString();
  96.                             createSessionFB();
  97.                         }
  98.                     }
  99.                 })
  100.                 .setNegativeButton("Отмена",
  101.                         new DialogInterface.OnClickListener() {
  102.                             public void onClick(DialogInterface dialog,int id) {
  103.                                 dialog.dismiss();
  104.                             }
  105.                         });
  106.  
  107.         AlertDialog alertDialog = dialog.create();
  108.         alertDialog.show();
  109.     }
  110.  
  111.     private void createSessionFB(){
  112.         waitDialog.show();
  113.         mDatabase.child("sessions").child(sessionId).addListenerForSingleValueEvent(new ValueEventListener() {
  114.             @Override
  115.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  116.                 if (dataSnapshot.exists()){
  117.                     waitDialog.dismiss();
  118.                     Toast.makeText(MainActivity.this, "Попробуйте другой id для сессии",
  119.                             Toast.LENGTH_SHORT).show();
  120.                 } else {
  121.                     mDatabase.child("sessions").child(sessionId).child("sessionId").setValue(sessionId)
  122.                             .addOnSuccessListener(new OnSuccessListener<Void>() {
  123.                                 @Override
  124.                                 public void onSuccess(Void aVoid) {
  125.                                     showNameDialog("Игрок 1", sessionId);
  126.                                 }
  127.                             });
  128.                 }
  129.             }
  130.  
  131.             @Override
  132.             public void onCancelled(@NonNull DatabaseError databaseError) {
  133.                 waitDialog.dismiss();
  134.             }
  135.         });
  136.     }
  137.  
  138.     private void joinSession(){
  139.         final View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_create_session, null);
  140.         AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  141.         dialog.setView(dialogView);
  142.         final TextView title = dialogView.findViewById(R.id.txtTitle);
  143.         final EditText inputId = dialogView.findViewById(R.id.input_id);
  144.         title.setText(R.string.join_session);
  145.         dialog.setPositiveButton("OK",
  146.                 new DialogInterface.OnClickListener() {
  147.                     public void onClick(DialogInterface dialog,int id) {
  148.                         hideKeyboard(dialogView);
  149.                         if (inputId.getText().toString().length()>0){
  150.                             sessionId = inputId.getText().toString();
  151.                             joinSessionFB();
  152.                         }
  153.                     }
  154.                 })
  155.                 .setNegativeButton("Отмена",
  156.                         new DialogInterface.OnClickListener() {
  157.                             public void onClick(DialogInterface dialog,int id) {
  158.                                 dialog.dismiss();
  159.                             }
  160.                         });
  161.  
  162.         AlertDialog alertDialog = dialog.create();
  163.         alertDialog.show();
  164.     }
  165.  
  166.     private void joinSessionFB(){
  167.         waitDialog.show();
  168.         mDatabase.child("sessions").child(sessionId).addListenerForSingleValueEvent(new ValueEventListener() {
  169.             @Override
  170.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  171.                 if (dataSnapshot.exists()){
  172.                     sessionId = dataSnapshot.getKey();
  173.                     dataSnapshot.getRef().child("users").addListenerForSingleValueEvent(new ValueEventListener() {
  174.                         @Override
  175.                         public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  176.                             if (dataSnapshot.getChildrenCount()==0){
  177.                                 showNameDialog("Игрок 1", sessionId);
  178.                             } else {
  179.                                 if (dataSnapshot.getChildrenCount()==1){
  180.                                     showNameDialog("Игрок 2", sessionId);
  181.                                 } else {
  182.                                     waitDialog.dismiss();
  183.                                     Toast.makeText(MainActivity.this, "В данный момент сессия недоступна",
  184.                                             Toast.LENGTH_SHORT).show();
  185.                                 }
  186.                             }
  187.                         }
  188.  
  189.                         @Override
  190.                         public void onCancelled(@NonNull DatabaseError databaseError) {
  191.                             waitDialog.dismiss();
  192.                         }
  193.                     });
  194.                 } else {
  195.                     waitDialog.dismiss();
  196.                     Toast.makeText(MainActivity.this, "Такой сессии не существует",
  197.                             Toast.LENGTH_SHORT).show();
  198.                 }
  199.             }
  200.  
  201.             @Override
  202.             public void onCancelled(@NonNull DatabaseError databaseError) {
  203.                 waitDialog.dismiss();
  204.             }
  205.         });
  206.     }
  207.  
  208.     private void showNameDialog(final String user, final String sessionId){
  209.         waitDialog.dismiss();
  210.         final View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_create_session, null);
  211.         AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  212.         dialog.setView(dialogView);
  213.         final TextView title = dialogView.findViewById(R.id.txtTitle);
  214.         final EditText inputId = dialogView.findViewById(R.id.input_id);
  215.         title.setText(R.string.name);
  216.         dialog.setPositiveButton("OK",
  217.                 new DialogInterface.OnClickListener() {
  218.                     public void onClick(DialogInterface dialog,int id) {
  219.                         hideKeyboard(dialogView);
  220.                         String userName = user;
  221.                         if (inputId.getText().toString().length()>0){
  222.                             userName = inputId.getText().toString();
  223.                         }
  224.                         Intent game = new Intent(MainActivity.this, GameActivity.class);
  225.                         game.putExtra("sessionId", sessionId);
  226.                         game.putExtra("userName", userName);
  227.                         startActivity(game);
  228.                     }
  229.                 })
  230.                 .setNegativeButton("Отмена",
  231.                         new DialogInterface.OnClickListener() {
  232.                             public void onClick(DialogInterface dialog,int id) {
  233.                                 dialog.dismiss();
  234.                                 if (newSession) {
  235.                                     mDatabase.child("sessions").child(sessionId).removeValue();
  236.                                 }
  237.                             }
  238.                         });
  239.  
  240.         AlertDialog alertDialog = dialog.create();
  241.         alertDialog.show();
  242.     }
  243.  
  244.     private void hideKeyboard(View view){
  245.         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  246.         imm.hideSoftInputFromWindow(view.getWindowToken(),0);
  247.     }
  248. }
Add Comment
Please, Sign In to add comment