BoothActivity.java 9.22 KB
package com.styleteq.app;

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

import com.styleteq.app.adapters.SwipeScreenAdapter;
import com.styleteq.app.api.STCallback;
import com.styleteq.app.api.STRequest;
import com.styleteq.app.api.ServiceGenerator;
import com.styleteq.app.errors.ErrorBag;
import com.styleteq.app.errors.ErrorsSerializer;
import com.styleteq.app.helpers.Logger;
import com.styleteq.app.helpers.STActivity;
import com.styleteq.app.helpers.TemporaryValues;
import com.styleteq.app.helpers.Utils;
import com.styleteq.app.models.Attachment;
import com.styleteq.app.models.Booth;
import com.styleteq.app.models.Session;
import com.styleteq.app.services.BoothService;
import com.styleteq.app.ui.ConfirmDeleteDialog;
import com.styleteq.app.ui.DynamicHeightViewPager;
import com.styleteq.app.ui.LoadingScreen;
import com.viewpagerindicator.CirclePageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class BoothActivity extends STActivity {
    @BindView(R.id.booth)
    public TextView boothName;
    @BindView(R.id.name)
    public TextView name;
    @BindView(R.id.address)
    public TextView address;
    @BindView(R.id.view_pager)
    public DynamicHeightViewPager pager;
    @BindView(R.id.indicator)
    public CirclePageIndicator indicator;
    @BindView(R.id.price)
    public TextView price;
    @BindView(R.id.method)
    public TextView method;
    @BindView(R.id.businessType)
    public TextView businessType;
    @BindView(R.id.boothContainer)
    public View boothContainer;

    private ConfirmDeleteDialog deleteDialog;
    private Booth booth;
    private LoadingScreen loadingScreen;
    private Menu menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = getIntent();

        setContentView(R.layout.activity_booth);
        ButterKnife.bind(this);

        if (savedInstanceState != null) {
            String s = savedInstanceState.getString("booth");
            if (s != null)
                booth = Booth.createFromJsonString(s);
        }
        if (booth == null) {
            if (i.hasExtra("booth")) {
                booth = Booth.createFromJsonString(i.getStringExtra("booth"));
            } else if (i.hasExtra("booth_id")) {
                BoothService s = ServiceGenerator.createService(BoothService.class, "booth");
                new STRequest<>(s.get(i.getLongExtra("booth_id", 0)), new STCallback<Booth>() {
                    @Override
                    public void onCustomResponse(Call<Booth> call, Response<Booth> response) {
                        if (response.isSuccessful()) {
                            booth = response.body();
                            populate();
                        } else
                            showResponseError(response);
                    }

                    @Override
                    public void onFailure(Call<Booth> call, Throwable t) {
                        Toast.makeText(BoothActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }).execute();
            } else
                Logger.e("Booth not found");
        }
        init();
        addListeners();
        if (booth != null) {
            populate();
        }
    }

    private void init() {
        setToolbar(R.id.tool_bar);
        loadingScreen = new LoadingScreen();
        loadingScreen.show(true);
    }

    private void populate() {
        boothName.setText(String.format("%s %s for Rent", booth.slots, getResources().getQuantityString(R.plurals.booths, booth.slots)));
        name.setText(booth.salon.name);
        address.setText(booth.salon.address());

        List<Attachment> images = booth.images.isEmpty() ? booth.salon.images : booth.images;
        if (images.isEmpty()) {
            images = new ArrayList<>();
            images.add(new Attachment());
        }

        SwipeScreenAdapter a = new SwipeScreenAdapter(this, images, pager);
        pager.setAdapter(a);

        indicator.setViewPager(pager);
        price.setText(String.format("%s %s", Utils.formatDollar(booth.price.doubleValue()), booth.terms));
        method.setText(booth.payMethod());
        businessType.setText(booth.salon.businessType);

        deleteDialog = new ConfirmDeleteDialog(this, "Are you sure you want to delete this Booth?",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        showProgressDialogWithAction("Deleting booth");
                        BoothService s = ServiceGenerator.createService(BoothService.class);
                        new STRequest<>(s.delete(booth.id), new STCallback<Booth>() {
                            @Override
                            public void onCustomResponse(Call<Booth> call, Response<Booth> response) {
                                hideProgressDialog();
                                if (response.isSuccessful()) {
                                    if (!getIntent().hasExtra(Utils.BACK)) {
                                        TemporaryValues.setBooth(booth);
                                        TemporaryValues.setMode(TemporaryValues.DELETE_MODE);
                                        finish();
                                    } else
                                        startActivity(new Intent(BoothActivity.this, BoothListActivity.class).putExtra("salon", booth.salon.toJsonString()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                                    Toast.makeText(BoothActivity.this, "Successfully deleted booth!", Toast.LENGTH_LONG).show();
                                } else {
                                    ErrorBag e = ErrorsSerializer.parseResourceErrors(response);
                                    if (e != null)
                                        Toast.makeText(BoothActivity.this, e.concatenateErrors(), Toast.LENGTH_LONG).show();
                                }
                            }

                            @Override
                            public void onFailure(Call<Booth> call, Throwable t) {
                                hideProgressDialog();
                                Toast.makeText(BoothActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }).execute();
                    }
                });

        setupMenu();
        loadingScreen.show(false);
    }

    private void addListeners() {
        boothContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BoothActivity.this, SalonActivity.class)
                        .putExtra("salon", booth.salon.toJsonString()).putExtra(Utils.BACK, Utils.BACK));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.menu = menu;
        if (booth != null)
            setupMenu();
        return true;
    }

    private void setupMenu() {
        if (menu == null)
            return;
        if (Session.isCurrentUser(booth.salon.userId)) {
            getMenuInflater().inflate(R.menu.menu_salon, menu);
            menu.findItem(R.id.edit).setTitle("Edit Booth");
            menu.findItem(R.id.manage_photos).setTitle("Manage Images");
        } else
            getMenuInflater().inflate(R.menu.menu_report, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (!Session.get().exists()) {
            STApplication.promptLogin();
            return true;
        }
        switch (item.getItemId()) {
            case R.id.edit:
                startActivity(new Intent(this, BoothFormActivity.class).putExtra("booth", booth.toJsonString()));
                return true;
            case R.id.manage_photos:
                startActivity(new Intent(this, UploadBoothPhotosActivity.class).putExtra("booth", booth.toJsonString()).putExtra(Utils.BACK, Utils.BACK));
                return true;
            case R.id.delete:
                deleteDialog.show();
                return true;
            case R.id.report:
                startActivity(new Intent(this, ReportActivity.class).putExtra("booth", booth.toJsonString()));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        if (!loadingScreen.isLoading()) {
            TemporaryValues.setBooth(booth);
            super.onBackPressed();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Booth tempBooth = TemporaryValues.useBooth();
        if (tempBooth != null) {
            booth = tempBooth;
            init();
            populate();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (booth != null)
            outState.putString("booth", booth.toJsonString());
    }
}