Skip to content

Commit

Permalink
muestra mejor el historial de busqueda
Browse files Browse the repository at this point in the history
  • Loading branch information
ESimonLeon committed Jul 11, 2020
1 parent d462a56 commit 7afe6db
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 34 deletions.
12 changes: 7 additions & 5 deletions app/src/main/java/com/example/examengapsi/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
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.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
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;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -66,7 +70,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Busca producto");
searchView.setQueryHint(getString(R.string.busca_producto_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Expand All @@ -89,7 +93,7 @@ public boolean onQueryTextChange(String newText) {


private void searchProduct(String query) {
showAlert("Buscando "+ query, "Cargando datos...");
showAlert("Buscando "+ query, getString(R.string.cargando_datos));
mainPresenter.searchProduct(query);
}

Expand Down Expand Up @@ -128,7 +132,6 @@ private class InserBDTask extends AsyncTask<SearchHistory, Void, Void> {

@Override
protected Void doInBackground(SearchHistory... searchHistories) {
Log.wtf("doinBack", "" + searchHistories[0]);
AppDataBase.getAppDB(getApplicationContext()).searchHistoryDAO().insertSearchHistory(searchHistories[0]);
return null;
}
Expand All @@ -147,7 +150,6 @@ public void onPostExecute(List<SearchHistory> searchHistory){
}

private void showHistory(List<SearchHistory> searchHistory) {
Log.wtf("MainActivityt", "showHistory | searchlist: " + searchHistory);
createRecyclerHistory(searchHistory);
}
}
Expand All @@ -156,7 +158,7 @@ private void showHistory(List<SearchHistory> searchHistory) {
private void createRecyclerHistory(List<SearchHistory> searchHistory) {
recyclerViewHistory = findViewById(R.id.idRvProducts);
searchHistoryAdapter = new SearchHistoryAdapter(this, searchHistory);
recyclerViewHistory.setLayoutManager(new GridLayoutManager(this, 1));
recyclerViewHistory.setLayoutManager(new LinearLayoutManager(this));
recyclerViewHistory.setAdapter(searchHistoryAdapter);
recyclerViewHistory.setHasFixedSize(true);
recyclerViewHistory.isNestedScrollingEnabled();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.examengapsi.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import com.example.examengapsi.model.SearchHistory;

@Database(entities = {SearchHistory.class}, version = 2)
@Database(entities = {SearchHistory.class}, version = 1)
public abstract class AppDataBase extends RoomDatabase {

private static AppDataBase INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import com.example.examengapsi.model.SearchHistory;
Expand All @@ -12,8 +13,8 @@
@Dao
public interface SearchHistoryDAO {

@Insert
void insertSearchHistory(SearchHistory searchHistory);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertSearchHistory(SearchHistory ... searchHistory);

@Query("SELECT * FROM search_history")
List<SearchHistory> findAllHistory();
Expand Down
13 changes: 2 additions & 11 deletions app/src/main/java/com/example/examengapsi/model/SearchHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@
@Entity(tableName = "search_history")
public class SearchHistory {

@PrimaryKey(autoGenerate = true)
private int uid;

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

public void setUid(int uid) {
this.uid = uid;
}

public int getUid() {
return uid;
}

public void setText(@NonNull String text) {
this.text = text;
}
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/idRvHistory"
android:id="@+id/idRvProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/idRvProducts"
android:id="@+id/idRvHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />


</RelativeLayout>
16 changes: 6 additions & 10 deletions app/src/main/res/layout/item_search_history.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/idTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_margin="7dp"/>

<TextView
android:id="@+id/idTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>

</LinearLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@

<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
<string name="historial_de_busqueda">Historial de busqueda</string>
<string name="cargando_datos">Cargando datos...</string>
<string name="busca_producto_hint">Busca producto</string>
</resources>

0 comments on commit 7afe6db

Please sign in to comment.