diff --git a/app/src/main/java/com/example/examengapsi/MainActivity.java b/app/src/main/java/com/example/examengapsi/MainActivity.java index a41e0d9..eb968f2 100644 --- a/app/src/main/java/com/example/examengapsi/MainActivity.java +++ b/app/src/main/java/com/example/examengapsi/MainActivity.java @@ -1,5 +1,6 @@ package com.example.examengapsi; +import android.annotation.SuppressLint; import android.os.AsyncTask; import android.os.Bundle; @@ -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; @@ -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; @@ -38,7 +38,6 @@ public class MainActivity extends AppCompatActivity implements MainInterface.Mai SearchHistory searchHistory; InserBDTask inserBDTask; SearchHistoryDBTask searchHistoryDBTask; - private List listHistory; AlertDialog alertDialog; @Override @@ -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(); @@ -60,6 +59,7 @@ protected void onCreate(Bundle savedInstanceState) { } + @SuppressLint("NotifyDataSetChanged") private void createRecyclerView(ArrayList products) { if (products.size() > 0) llEmpty.setVisibility(View.GONE); recyclerView = findViewById(R.id.idRvProducts); @@ -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 @@ -134,6 +132,7 @@ public void selectItem(String text) { searchProduct(text); } + @SuppressLint("StaticFieldLeak") private class InserBDTask extends AsyncTask { @Override @@ -143,12 +142,12 @@ protected Void doInBackground(SearchHistory... searchHistories) { } } + @SuppressLint("StaticFieldLeak") private class SearchHistoryDBTask extends AsyncTask> { @Override protected List doInBackground(Void... voids) { - listHistory = AppDataBase.getAppDB(getApplicationContext()).searchHistoryDAO().findAllHistory(); - return listHistory; + return AppDataBase.getAppDB(getApplicationContext()).searchHistoryDAO().findAllHistory(); } @Override @@ -162,6 +161,7 @@ private void showHistory(List searchHistory) { } + @SuppressLint("NotifyDataSetChanged") private void createRecyclerHistory(List searchHistory) { if (searchHistory.size() > 0) llEmpty.setVisibility(View.GONE); recyclerViewHistory = findViewById(R.id.idRvProducts); diff --git a/app/src/main/java/com/example/examengapsi/MainPresenter.java b/app/src/main/java/com/example/examengapsi/MainPresenter.java index e35d727..db11fe5 100644 --- a/app/src/main/java/com/example/examengapsi/MainPresenter.java +++ b/app/src/main/java/com/example/examengapsi/MainPresenter.java @@ -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; @@ -15,6 +15,7 @@ import org.json.JSONObject; import java.util.ArrayList; +import java.util.Objects; import retrofit2.Call; import retrofit2.Callback; @@ -22,14 +23,12 @@ 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 @@ -38,7 +37,7 @@ public void searchProduct(String text) { call.enqueue(new Callback() { @RequiresApi(api = Build.VERSION_CODES.N) @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { try { if (response.body() != null) { @@ -46,7 +45,7 @@ public void onResponse(Call call, Response response) { 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(); } @@ -54,13 +53,15 @@ public void onResponse(Call call, Response response) { } + @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override - public void onFailure(Call call, Throwable t) { - view.resultError(t.getCause().toString(), t.getMessage()); + public void onFailure(@NonNull Call 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 products = new ArrayList<>(); for (int x = 0; x < records.length(); x++) { @@ -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(); } } diff --git a/app/src/main/java/com/example/examengapsi/adapter/ProductsListAdapter.java b/app/src/main/java/com/example/examengapsi/adapter/ProductsListAdapter.java index 8fc5fd7..f27a5aa 100644 --- a/app/src/main/java/com/example/examengapsi/adapter/ProductsListAdapter.java +++ b/app/src/main/java/com/example/examengapsi/adapter/ProductsListAdapter.java @@ -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; @@ -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()); @@ -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; diff --git a/app/src/main/java/com/example/examengapsi/adapter/SearchHistoryAdapter.java b/app/src/main/java/com/example/examengapsi/adapter/SearchHistoryAdapter.java index 64e9464..4ffa999 100644 --- a/app/src/main/java/com/example/examengapsi/adapter/SearchHistoryAdapter.java +++ b/app/src/main/java/com/example/examengapsi/adapter/SearchHistoryAdapter.java @@ -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; @@ -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 @@ -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; diff --git a/app/src/main/java/com/example/examengapsi/db/AppDataBase.java b/app/src/main/java/com/example/examengapsi/db/AppDataBase.java index 53e72f0..93931b1 100644 --- a/app/src/main/java/com/example/examengapsi/db/AppDataBase.java +++ b/app/src/main/java/com/example/examengapsi/db/AppDataBase.java @@ -21,8 +21,4 @@ public static AppDataBase getAppDB(Context context){ } return INSTANCE; } - - public static void destroyBD(){ - INSTANCE=null; - } } diff --git a/app/src/main/java/com/example/examengapsi/model/Producto.java b/app/src/main/java/com/example/examengapsi/model/Producto.java index 8c9a998..d730283 100644 --- a/app/src/main/java/com/example/examengapsi/model/Producto.java +++ b/app/src/main/java/com/example/examengapsi/model/Producto.java @@ -1,9 +1,9 @@ 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; @@ -11,18 +11,6 @@ public Producto(String productDisplayName, Double listPrice, String smImage) { 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; } diff --git a/app/src/main/java/com/example/examengapsi/model/SearchHistory.java b/app/src/main/java/com/example/examengapsi/model/SearchHistory.java index f2f7209..56eca1a 100644 --- a/app/src/main/java/com/example/examengapsi/model/SearchHistory.java +++ b/app/src/main/java/com/example/examengapsi/model/SearchHistory.java @@ -9,7 +9,6 @@ public class SearchHistory { @PrimaryKey - @NonNull @ColumnInfo(name="text") private String text; diff --git a/app/src/main/java/com/example/examengapsi/service/ApiRoutes.java b/app/src/main/java/com/example/examengapsi/service/ApiRoutes.java new file mode 100644 index 0000000..e605ab2 --- /dev/null +++ b/app/src/main/java/com/example/examengapsi/service/ApiRoutes.java @@ -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"; +} diff --git a/app/src/main/java/com/example/examengapsi/service/ApiService.java b/app/src/main/java/com/example/examengapsi/service/ApiService.java index 3042ba0..1bbe6ea 100644 --- a/app/src/main/java/com/example/examengapsi/service/ApiService.java +++ b/app/src/main/java/com/example/examengapsi/service/ApiService.java @@ -1,11 +1,6 @@ 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; @@ -13,31 +8,15 @@ 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 mapHeaders = null; - - public static Map getMapHeaders(Context context) { - if (mapHeaders == null) { - mapHeaders = new HashMap<>(); - } else { - mapHeaders.clear(); - } - mapHeaders.put("Content-Type", "application/x-www-form-urlencoded"); - return mapHeaders; - } - } diff --git a/app/src/main/java/com/example/examengapsi/service/ApiServiceInterface.java b/app/src/main/java/com/example/examengapsi/service/ApiServiceInterface.java index 906dc67..1ad07bf 100644 --- a/app/src/main/java/com/example/examengapsi/service/ApiServiceInterface.java +++ b/app/src/main/java/com/example/examengapsi/service/ApiServiceInterface.java @@ -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 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); } diff --git a/creadoportfm b/creadoportfm deleted file mode 100644 index cedf93e..0000000 --- a/creadoportfm +++ /dev/null @@ -1 +0,0 @@ -dbnwsbxkjsbxjskabxasxasxasxasxasx