123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package com.noahvogt.miniprojekt;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.appcompat.widget.Toolbar;
- import androidx.drawerlayout.widget.DrawerLayout;
- import androidx.fragment.app.DialogFragment;
- import com.google.android.material.floatingactionbutton.FloatingActionButton;
- import com.noahvogt.miniprojekt.ui.DataBase.Message;
- import com.noahvogt.miniprojekt.ui.home.CustomAdapter;
- import androidx.fragment.app.Fragment;
- import androidx.fragment.app.FragmentManager;
- import androidx.lifecycle.LifecycleOwner;
- import androidx.lifecycle.ViewModelProvider;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
- import com.google.android.material.navigation.NavigationView;
- import androidx.navigation.NavController;
- import androidx.navigation.Navigation;
- import androidx.navigation.ui.AppBarConfiguration;
- import androidx.navigation.ui.NavigationUI;
- import com.google.android.material.snackbar.Snackbar;
- import com.noahvogt.miniprojekt.ui.slideshow.EmailViewModel;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import static com.noahvogt.miniprojekt.R.id.drawer_layout;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private AppBarConfiguration mAppBarConfiguration;
- public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
- public static EmailViewModel mEmailViewModel;
- public static RecyclerView recyclerView;
- public static final CustomAdapter adapter = new CustomAdapter(new CustomAdapter.EmailDiff());
- private AlertDialog dialog;
- private EditText newemail_name, newemail_email, newemail_password; // may not be private
- // empty descriptor
- public MainActivity() {
- }
- public String getVisibleFragment(){
- FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
- List<Fragment> fragments = fragmentManager.getFragments();
- if(fragments != null){
- showToast("not null");
- for(Fragment fragment : fragments){
- showToast(fragment.toString());
- if(fragment.isVisible())
- showToast("found visible fragment");
- return "is gallery";
- }
- } else {
- showToast("null");}
- return null;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // define button listeners
- Button add_email_button = (Button) findViewById(R.id.addEmailButton);
- add_email_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- createNewEmailDialog();
- }
- });
- /*creates accountmanager by clicking on profil */
- View accountView = findViewById(R.id.accountView);
- accountView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- createNewEmailDialog();
- }
- });
- Toolbar toolbar = findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- DrawerLayout drawer = findViewById(drawer_layout);
- NavigationView navigationView = findViewById(R.id.nav_view);
- // Passing each menu ID as a set of Ids because each
- // menu should be considered as top level destinations.
- mAppBarConfiguration = new AppBarConfiguration.Builder(
- R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_archive, R.id.nav_spam)
- .setDrawerLayout(drawer)
- .build();
- NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
- NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
- NavigationUI.setupWithNavController(navigationView, navController);
- /* Lookup the recyclerview in activity layout */
- recyclerView = findViewById(R.id.recyclerView);
- /* Attach the adapter to the recyclerview to populate items */
- recyclerView.setAdapter(adapter);
- recyclerView.setLayoutManager(new LinearLayoutManager(this));
- /* get Inbox Messages in Recyclerviewer at begining is overwritten by Fragments but has to stay*/
- mEmailViewModel = new ViewModelProvider(this).get(EmailViewModel.class);
- mEmailViewModel.getInboxMessage().observe(this, messages -> {
- /* Update the cached copy of the messages in the adapter*/
- adapter.submitList(messages);
- });
- /* Start email Writer*/
- FloatingActionButton message_create_button = findViewById(R.id.messageButton);
- message_create_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- DialogFragment dialog = messageCreateFragment.newInstance();
- dialog.show(getSupportFragmentManager(), "tag");
- }
- });
- }
- /* gets the data from the Email writer and adds it to the Database */
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, messageCreateFragment.replyIntent);
- /* Creates class for the Date when Email is written */
- Date dNow = new Date();
- SimpleDateFormat ft =
- new SimpleDateFormat("dd.MM.yy");
- // if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
- Message word = new Message(
- messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_TO),
- null,
- null,
- messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_FROM),
- ft.format(dNow),
- messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_SUBJECT),
- messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_MESSAGE),
- "Draft",false);
- mEmailViewModel.insert(word);
- // } else {
- Toast.makeText(
- getApplicationContext(),
- R.string.empty_not_saved,
- Toast.LENGTH_LONG).show();
- Toast.makeText(
- getApplicationContext(),
- messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_FROM),
- Toast.LENGTH_LONG).show();
- // }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.create_message_options_menu, menu);
- return true;
- }
- @Override
- public boolean onSupportNavigateUp() {
- NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
- return NavigationUI.navigateUp(navController, mAppBarConfiguration)
- || super.onSupportNavigateUp();
- }
- // better leave empty to avoid any listener disambiguity
- public void onClick(View view) {}
- public void createNewEmailDialog(){
- // define View window
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
- final View emailPopupView = getLayoutInflater().inflate(R.layout.popup, null);
- // init text field variables
- newemail_name = emailPopupView.findViewById(R.id.popup_material_name_asking_text);
- newemail_email = emailPopupView.findViewById(R.id.popup_material_email_asking_text);
- newemail_password = emailPopupView.findViewById(R.id.popup_material_password_asking_text);
- // init button variables
- Button newemail_save_button = (Button) emailPopupView.findViewById(R.id.saveButton);
- // may not be private
- Button newemail_cancel_button = (Button) emailPopupView.findViewById(R.id.cancelButton);
- // open View window
- dialogBuilder.setView(emailPopupView);
- dialog = dialogBuilder.create();
- dialog.show();
- // store user input
- newemail_save_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // store user input (only needed for DEBUGGING)
- String name = newemail_name.getText().toString();
- String email = newemail_email.getText().toString();
- String password = newemail_password.getText().toString();
- if (!mailFunctions.validateEmail(newemail_email) | !mailFunctions.validateName(newemail_name) | !mailFunctions.validatePassword(newemail_password)) {
- return;
- }
- // show all strings the user gave, this will later be stored to a secure database and checked for validation
- showToast(name);
- showToast(email);
- showToast(password);
- showSnackbar(emailPopupView,"save button clicked");
- }
- });
- newemail_cancel_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // define save button here
- dialog.dismiss();
- }
- });
- }
- // show debug output in specific view
- private void showSnackbar(View View, String text) {
- Snackbar.make(View, text, Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- // like showSnackbar(), but global and uglier
- private void showToast(String text) {
- Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
- }
- }
|