Skip to content

Commit

Permalink
Se restructura y limpia el proyecto
Browse files Browse the repository at this point in the history
  • Loading branch information
ESimonLeon committed Oct 20, 2021
1 parent 9fe18a8 commit a7b85e3
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 82 deletions.
20 changes: 10 additions & 10 deletions app/src/main/java/com/example/examengapsi/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.examengapsi;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;

Expand All @@ -8,8 +9,8 @@
import com.example.examengapsi.db.AppDataBase;
import com.example.examengapsi.model.Producto;
import com.example.examengapsi.model.SearchHistory;
import com.google.gson.Gson;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
Expand All @@ -19,7 +20,6 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -38,7 +38,6 @@ public class MainActivity extends AppCompatActivity implements MainInterface.Mai
SearchHistory searchHistory;
InserBDTask inserBDTask;
SearchHistoryDBTask searchHistoryDBTask;
private List<SearchHistory> listHistory;
AlertDialog alertDialog;

@Override
Expand All @@ -51,7 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {

llEmpty = findViewById(R.id.llEmpty);

mainPresenter = new MainPresenter(this, this);
mainPresenter = new MainPresenter( this);

searchHistory = new SearchHistory();

Expand All @@ -60,6 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {

}

@SuppressLint("NotifyDataSetChanged")
private void createRecyclerView(ArrayList<Producto> products) {
if (products.size() > 0) llEmpty.setVisibility(View.GONE);
recyclerView = findViewById(R.id.idRvProducts);
Expand Down Expand Up @@ -104,10 +104,8 @@ private void searchProduct(String query) {
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

return super.onOptionsItemSelected(item);
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
Expand All @@ -134,6 +132,7 @@ public void selectItem(String text) {
searchProduct(text);
}

@SuppressLint("StaticFieldLeak")
private class InserBDTask extends AsyncTask<SearchHistory, Void, Void> {

@Override
Expand All @@ -143,12 +142,12 @@ protected Void doInBackground(SearchHistory... searchHistories) {
}
}

@SuppressLint("StaticFieldLeak")
private class SearchHistoryDBTask extends AsyncTask<Void, Void, List<SearchHistory>> {

@Override
protected List<SearchHistory> doInBackground(Void... voids) {
listHistory = AppDataBase.getAppDB(getApplicationContext()).searchHistoryDAO().findAllHistory();
return listHistory;
return AppDataBase.getAppDB(getApplicationContext()).searchHistoryDAO().findAllHistory();
}

@Override
Expand All @@ -162,6 +161,7 @@ private void showHistory(List<SearchHistory> searchHistory) {
}


@SuppressLint("NotifyDataSetChanged")
private void createRecyclerHistory(List<SearchHistory> searchHistory) {
if (searchHistory.size() > 0) llEmpty.setVisibility(View.GONE);
recyclerViewHistory = findViewById(R.id.idRvProducts);
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/java/com/example/examengapsi/MainPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.example.examengapsi.model.Producto;
Expand All @@ -15,21 +15,20 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainPresenter implements MainInterface.MainPresenter {

private Context context;
private MainInterface.MainView view;
private ApiService apiService;
private final MainInterface.MainView view;
private final ApiService apiService;

public MainPresenter(Context context, MainInterface.MainView mainView) {
this.context = context;
public MainPresenter(MainInterface.MainView mainView) {
this.view = mainView;
apiService = new ApiService().initApiService(context);
apiService = new ApiService().initApiService();
}

@Override
Expand All @@ -38,29 +37,31 @@ public void searchProduct(String text) {
call.enqueue(new Callback<String>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(Call<String> call, Response<String> response) {
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
try {
if (response.body() != null) {
JSONObject jsonObject = new JSONObject(response.body());
getResultProductos(jsonObject.getJSONObject("plpResults").getJSONArray("records"));
}
} catch (JSONException e) {
view.resultError(e.getCause().toString(), e.getMessage());
view.resultError(Objects.requireNonNull(e.getCause()).toString(), e.getMessage());
e.printStackTrace();
}

}

}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onFailure(Call<String> call, Throwable t) {
view.resultError(t.getCause().toString(), t.getMessage());
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
view.resultError(Objects.requireNonNull(t.getCause()).toString(), t.getMessage());
}
});
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void getResultProductos(JSONArray records) {
ArrayList<Producto> products = new ArrayList<>();
for (int x = 0; x < records.length(); x++) {
Expand All @@ -69,7 +70,7 @@ private void getResultProductos(JSONArray records) {
Producto product = new Gson().fromJson(String.valueOf(jsonObject), Producto.class);
products.add(product);
} catch (JSONException e) {
view.resultError(e.getCause().toString(), e.getMessage());
view.resultError(Objects.requireNonNull(e.getCause()).toString(), e.getMessage());
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.examengapsi.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -32,6 +33,7 @@ public ProductsListHolder onCreateViewHolder(@NonNull ViewGroup parent, int view
return new ProductsListHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_producto, parent, false));
}

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ProductsListHolder holder, int position) {
holder.idTvPrice.setText(productsList.get(position).getListPrice().toString());
Expand All @@ -44,7 +46,7 @@ public int getItemCount() {
return productsList.size();
}

public class ProductsListHolder extends RecyclerView.ViewHolder {
public static class ProductsListHolder extends RecyclerView.ViewHolder {

TextView idTvPrice, idTvName;
ImageView idTvImage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.examengapsi.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -39,7 +39,7 @@ public SearchHistoryAdapter.SearchHistoryHolder onCreateViewHolder(@NonNull View
}

@Override
public void onBindViewHolder(@NonNull SearchHistoryAdapter.SearchHistoryHolder holder, final int position) {
public void onBindViewHolder(@NonNull SearchHistoryAdapter.SearchHistoryHolder holder, @SuppressLint("RecyclerView") final int position) {
holder.text.setText(searchHistory.get(position).getText());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -56,7 +56,7 @@ public int getItemCount() {
return searchHistory.size();
}

public class SearchHistoryHolder extends RecyclerView.ViewHolder {
public static class SearchHistoryHolder extends RecyclerView.ViewHolder {

TextView text;

Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/com/example/examengapsi/db/AppDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ public static AppDataBase getAppDB(Context context){
}
return INSTANCE;
}

public static void destroyBD(){
INSTANCE=null;
}
}
18 changes: 3 additions & 15 deletions app/src/main/java/com/example/examengapsi/model/Producto.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package com.example.examengapsi.model;

public class Producto {
private String productDisplayName;
private Double listPrice;
private String smImage;
private final String productDisplayName;
private final Double listPrice;
private final String smImage;

public Producto(String productDisplayName, Double listPrice, String smImage) {
this.productDisplayName = productDisplayName;
this.listPrice = listPrice;
this.smImage = smImage;
}

public void setProductDisplayName(String productDisplayName) {
this.productDisplayName = productDisplayName;
}

public void setListPrice(Double listPrice) {
this.listPrice = listPrice;
}

public void setSmImage(String smImage) {
this.smImage = smImage;
}

public String getProductDisplayName() {
return productDisplayName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
public class SearchHistory {

@PrimaryKey
@NonNull
@ColumnInfo(name="text")
private String text;

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/example/examengapsi/service/ApiRoutes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.examengapsi.service;

public class ApiRoutes {

public static final String URLSERVICE = "https://shoppapp.liverpool.com.mx";

public static final String SEARCH_PRODUCTS = "/appclienteservices/services/v3/plp";
public static final String FORCE_PLP = "force-plp";
public static final String QUERY_SEARCH = "search-string";
public static final String PAGE_NUMBER = "page-number";
public static final String NUM_FOR_ITEM = "number-of-items-per-page";
}
27 changes: 3 additions & 24 deletions app/src/main/java/com/example/examengapsi/service/ApiService.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
package com.example.examengapsi.service;

import android.content.Context;

import com.example.examengapsi.R;

import java.util.HashMap;
import java.util.Map;
import static com.example.examengapsi.service.ApiRoutes.URLSERVICE;

import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class ApiService {

public ApiServiceInterface apiInterface;
private String URL = "https://shoppapp.liverpool.com.mx";

public ApiService() {
}

public ApiService initApiService(Context context) {
public ApiService initApiService() {

Retrofit.Builder apiClientConfig = new Retrofit.Builder()
.baseUrl(URL)
.baseUrl(URLSERVICE)
.addConverterFactory(ScalarsConverterFactory.create());

apiInterface = apiClientConfig.build().create(ApiServiceInterface.class);
return this;
}

private static Map<String, String> mapHeaders = null;

public static Map<String, String> getMapHeaders(Context context) {
if (mapHeaders == null) {
mapHeaders = new HashMap<>();
} else {
mapHeaders.clear();
}
mapHeaders.put("Content-Type", "application/x-www-form-urlencoded");
return mapHeaders;
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.example.examengapsi.service;

import java.util.Map;
import static com.example.examengapsi.service.ApiRoutes.FORCE_PLP;
import static com.example.examengapsi.service.ApiRoutes.NUM_FOR_ITEM;
import static com.example.examengapsi.service.ApiRoutes.PAGE_NUMBER;
import static com.example.examengapsi.service.ApiRoutes.QUERY_SEARCH;
import static com.example.examengapsi.service.ApiRoutes.SEARCH_PRODUCTS;


import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface ApiServiceInterface {

@GET("/appclienteservices/services/v3/plp")
@GET(SEARCH_PRODUCTS)
Call<String> getProductos(
@Query("force-plp") Boolean forcePlp,
@Query("search-string") String text,
@Query("page-number") Integer page,
@Query("number-of-items-per-page") Integer items);
@Query(FORCE_PLP) Boolean forcePlp,
@Query(QUERY_SEARCH) String text,
@Query(PAGE_NUMBER) Integer page,
@Query(NUM_FOR_ITEM) Integer items);
}
1 change: 0 additions & 1 deletion creadoportfm

This file was deleted.

0 comments on commit a7b85e3

Please sign in to comment.