Advertisement
Guest User

AddActivity

a guest
Jun 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.15 KB | None | 0 0
  1. package cegeka.goalfollower.ro.goalfollower;
  2.  
  3. import android.app.Activity;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.nfc.NdefMessage;
  9. import android.nfc.NdefRecord;
  10. import android.nfc.NfcAdapter;
  11. import android.nfc.NfcEvent;
  12. import android.os.Parcelable;
  13. import android.provider.ContactsContract;
  14. import android.support.annotation.NonNull;
  15. import android.support.v7.app.AppCompatActivity;
  16. import android.os.Bundle;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.EditText;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23.  
  24. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  25. import com.google.firebase.auth.FirebaseAuth;
  26. import com.google.firebase.database.DataSnapshot;
  27. import com.google.firebase.database.DatabaseError;
  28. import com.google.firebase.database.DatabaseReference;
  29. import com.google.firebase.database.FirebaseDatabase;
  30. import com.google.firebase.database.ValueEventListener;
  31.  
  32. import java.io.File;
  33. import java.io.FileInputStream;
  34. import java.io.FileOutputStream;
  35. import java.io.ObjectInputStream;
  36. import java.io.ObjectOutputStream;
  37. import java.text.ParseException;
  38. import java.text.SimpleDateFormat;
  39. import java.util.ArrayList;
  40. import java.util.Date;
  41.  
  42. import static android.nfc.NdefRecord.createMime;
  43.  
  44.  
  45. public class AddActivity extends AppCompatActivity {
  46. EditText editTextdesc = null;
  47. EditText textDate = null;
  48. EditText editTextdescrip=null;
  49. EditText editpass;
  50. Button addbtn = null;
  51. Button beam = null;
  52. static String filename = "goals";
  53. Goal item = new Goal();
  54. Button mSendBtn = null;
  55. static String concat=null;
  56. ArrayList<Goal> items = new ArrayList<>();
  57. String stupiddesc=null;
  58. String stupiddate=null;
  59. String stupiddescrip=null;
  60. String stupidpass=null;
  61. int s;
  62. FirebaseDatabase database = FirebaseDatabase.getInstance();
  63. DatabaseReference myRef = database.getReference();
  64. static Goal prostie = new Goal();
  65. @Override
  66. protected void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.activity_add);
  69.  
  70. editTextdesc = findViewById(R.id.editText);
  71. editTextdescrip=findViewById(R.id.editText5);
  72. textDate = findViewById(R.id.editText3);
  73. addbtn = findViewById(R.id.button3);
  74. beam = findViewById(R.id.beambttn);
  75. mSendBtn = findViewById(R.id.sendBtn);
  76.  
  77. beam.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View v) {
  80.  
  81. if (Validate()) {
  82. stupiddesc = editTextdesc.getText().toString();
  83. stupiddate=textDate.getText().toString();
  84. stupiddescrip=editTextdescrip.getText().toString();
  85. stupidpass=FirebaseAuth.getInstance().getCurrentUser().getUid();
  86. concat=stupiddesc+"/"+stupiddate+"/"+stupiddescrip+"/"+stupidpass+"/";
  87. Intent intent =
  88. new Intent(AddActivity.this, BeamActivity.class);
  89. startActivity(intent);
  90. }
  91. }
  92. });
  93.  
  94. addbtn.setOnClickListener(new View.OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97.  
  98. if (Validate()) {
  99. Readf();
  100. item.desc = editTextdesc.getText().toString();
  101. item.descrip=editTextdescrip.getText().toString();
  102. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm");
  103. try {
  104. item.dueDate = sdf.parse(textDate.getText().toString());
  105. } catch (Exception ex) {
  106. ex.printStackTrace();
  107. }
  108.  
  109. DatabaseReference ref= FirebaseDatabase.getInstance().getReference("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Nr Goals");
  110. ref.addListenerForSingleValueEvent(new ValueEventListener() {
  111. @Override
  112. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  113. s = dataSnapshot.getValue(int.class);
  114. }
  115.  
  116. @Override
  117. public void onCancelled(@NonNull DatabaseError databaseError) {
  118.  
  119. }
  120. });
  121. s++;
  122. myRef.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Nr Goals").setValue(s);
  123. myRef.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Goals").child(item.desc).setValue(item);
  124. items.add(item);
  125. Addg();
  126. finish();
  127. }
  128. }
  129. });
  130. mSendBtn.setOnClickListener(new View.OnClickListener() {
  131. @Override
  132. public void onClick(View v) {
  133. if(!Validate()) return;
  134.  
  135. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm");
  136. try{
  137. prostie.dueDate = sdf.parse(textDate.getText().toString());
  138. } catch(Exception ex){
  139. ex.printStackTrace();
  140. }
  141. prostie.desc = editTextdesc.getText().toString();
  142. prostie.descrip = editTextdescrip.getText().toString();
  143. prostie.pass = FirebaseAuth.getInstance().getCurrentUser().getUid();
  144. Intent intent = new Intent(AddActivity.this, SendViaNetActivity.class);
  145. startActivity(intent);
  146. }
  147. });
  148. }
  149.  
  150. public void Addg() {
  151. File myfile = new File(this.getFilesDir(), filename);
  152. FileOutputStream outputStream;
  153. try {
  154. outputStream = openFileOutput(filename, MODE_PRIVATE);
  155. ObjectOutputStream o = new ObjectOutputStream(outputStream);
  156. o.reset();
  157. o.writeObject(items);
  158. o.flush();
  159. o.close();
  160. Intent intent = new Intent(AddActivity.this, MainActivity.class);
  161. if (myfile.exists()) Toast.makeText(AddActivity.this, "yes", Toast.LENGTH_LONG).show();
  162. setResult(Activity.RESULT_OK, intent);
  163. finish();
  164. } catch (Exception e) {
  165. Toast.makeText(AddActivity.this, "no", Toast.LENGTH_LONG).show();
  166. e.printStackTrace();
  167.  
  168. }
  169. }
  170.  
  171. public void Readf() {
  172. FileInputStream fis;
  173. try {
  174. fis = openFileInput("goals");
  175. ObjectInputStream ois = new ObjectInputStream(fis);
  176. items = (ArrayList<Goal>) ois.readObject();
  177.  
  178. ois.close();
  179. } catch (Exception e) {
  180. e.printStackTrace();
  181. }
  182. }
  183.  
  184. private boolean Validate() {
  185. if (editTextdesc.getText().toString().trim().equals("") || editTextdescrip.getText().toString().trim().equals("")||
  186. textDate.getText().toString().trim().equals("")) {
  187. Toast.makeText(AddActivity.this,
  188. "All the fields are mandatory",
  189. Toast.LENGTH_LONG).show();
  190. return false;
  191. }
  192. SimpleDateFormat format =
  193. new SimpleDateFormat("yyyy-MM-dd-hh-mm");
  194. Date date = null;
  195. try {
  196. date =
  197. format.parse(textDate.getText().toString());
  198. } catch (ParseException e) {
  199. e.printStackTrace();
  200. Toast.makeText(AddActivity.this,
  201. "Invalid date format",
  202. Toast.LENGTH_LONG).show();
  203. return false;
  204.  
  205. }
  206. Date currentDate = new Date();
  207. if (date != null && date.compareTo(currentDate) <= 0) {
  208. Toast.makeText(AddActivity.this,
  209. "Date should be at least today",
  210. Toast.LENGTH_LONG).show();
  211. return false;
  212. }
  213.  
  214. return true;
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement