<repo_name>ospych/Inventory-MVP<file_sep>/app/src/main/java/com/example/inventorymvp/InventoryAdapter.java
package com.example.inventorymvp;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.inventorymvp.Data.Inventory;

public class InventoryAdapter extends ListAdapter<Inventory, InventoryAdapter.Holder> {
    private OnItemClickListener listener;

    public InventoryAdapter() {super(DIFF_CALLBACK);}

    private static final DiffUtil.ItemCallback<Inventory> DIFF_CALLBACK = new DiffUtil.ItemCallback<Inventory>() {
        @Override
        public boolean areItemsTheSame(@NonNull Inventory oldItem, @NonNull Inventory newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull Inventory oldItem, @NonNull Inventory newItem) {
            return oldItem.getTitle().equals(newItem.getTitle()) &&
                    oldItem.getSupplier().equals(newItem.getSupplier()) &&
                    oldItem.getQuantity() == newItem.getQuantity();
        }
    };

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.inventory_item, parent, false);
        return new Holder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        Inventory current = getItem(position);

        holder.titleTV.setText("Item: " + current.getTitle());
        holder.supplierTV.setText("Supplier: " + current.getSupplier());
        holder.quantityTV.setText("Quantity: " + current.getQuantity());
        holder.priceTV.setText(current.getPrice() + "$");

        Glide.with(holder.imageView.getContext()).load(current.getImage()).into(holder.imageView);

    }

    public Inventory getItemAt(int position) {
        return getItem(position);
    }

    public class Holder extends RecyclerView.ViewHolder {
        private TextView titleTV;
        private TextView supplierTV;
        private TextView quantityTV;
        private TextView priceTV;
        private ImageView imageView;

        public Holder(@NonNull View itemView) {
            super(itemView);

            titleTV = itemView.findViewById(R.id.titleTV);
            supplierTV = itemView.findViewById(R.id.supplierTV);
            quantityTV = itemView.findViewById(R.id.quantityTV);
            priceTV = itemView.findViewById(R.id.priceTV);
            imageView = itemView.findViewById(R.id.image);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (listener != null && position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(getItem(position));
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Inventory inventory);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
<file_sep>/app/src/main/java/com/example/inventorymvp/MainActivity.java
package com.example.inventorymvp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.inventorymvp.Data.Inventory;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private MainPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton buttonAddNote = findViewById(R.id.button_add);
        buttonAddNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, EditActivity.class);
                startActivity(intent);
            }
        });

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        final InventoryAdapter adapter = new InventoryAdapter();
        recyclerView.setAdapter(adapter);

        presenter = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory
                .getInstance(this.getApplication()))
                .get(MainPresenter.class);
        presenter.getItems().observe(this, new Observer<List<Inventory>>() {
            @Override
            public void onChanged(List<Inventory> inventories) {
                adapter.submitList(inventories);
            }
        });

        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                presenter.delete(adapter.getItemAt(viewHolder.getAdapterPosition()));
                Toast.makeText(MainActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
            }
        }).attachToRecyclerView(recyclerView);

        adapter.setOnItemClickListener(new InventoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Inventory inventory) {
                Intent intent = new Intent(MainActivity.this, EditActivity.class);
                intent.putExtra(EditActivity.EXTRA_ID, inventory.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if ((item.getItemId()) == R.id.delete_all) {
            DialogButton();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    public void DialogButton() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Delete all");
        builder.setMessage("Are you sure?");
        builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                presenter.deleteAllItems();
                Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT)
                        .show();
            }
        });

        builder.setNegativeButton("Cancel", null);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}<file_sep>/app/src/main/java/com/example/inventorymvp/MainContract.java
package com.example.inventorymvp;

import com.example.inventorymvp.Data.Inventory;

public interface MainContract {
    interface Presenter {
        void insert(Inventory inventory);

        void update(Inventory inventory);

        void delete(Inventory inventory);

        void deleteAllItems();
    }
    interface View {

    }
}
<file_sep>/app/src/main/java/com/example/inventorymvp/EditPresenter.java
package com.example.inventorymvp;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import com.example.inventorymvp.Data.Inventory;
import com.example.inventorymvp.Data.InventoryRepository;

public class EditPresenter extends AndroidViewModel implements EditContract.Presenter {
    private InventoryRepository repository;
    LiveData<Inventory> inventory;

    public EditPresenter(@NonNull Application application) {
        super(application);
        repository = new InventoryRepository(application);
    }

    @Override
    public void insert(Inventory inventory) {
        repository.insert(inventory);
    }

    @Override
    public void getItem(int id) {
        inventory = repository.getItem(id);
    }

    @Override
    public void update(Inventory inventory) {
        repository.update(inventory);
    }

    @Override
    public void delete(Inventory inventory) {
        repository.delete(inventory);
    }

    @Override
    public void save(String title, String supplier, double price, int quantity, String image) {
        Inventory item = new Inventory(title, supplier, price, quantity, image);

        if (this.inventory == null){
            insert(item);
        } else {
            Inventory inventoryItem = this.inventory.getValue();
            if (inventoryItem != null)
                item.setId(inventoryItem.getId());
            update(item);
        }
    }
}
