Store ID of selected agent
Restore agent ID from shared prefs Still one bug with agent.agency ...
This commit is contained in:
parent
e6a1d6616c
commit
204c820c67
@ -0,0 +1,21 @@
|
|||||||
|
package fr.romanet.vj.apps.myrealestateagency;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||||
|
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
private static String sharedPreferences = "prefs";
|
||||||
|
private static String selected_agency_index;
|
||||||
|
|
||||||
|
public static String getSharedPreferences() {
|
||||||
|
return sharedPreferences;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String get_selected_agency_index() {
|
||||||
|
return selected_agency_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setSelected_agency_index(String selected_agency_index) {
|
||||||
|
Config.selected_agency_index = selected_agency_index;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ package fr.romanet.vj.apps.myrealestateagency.adapter;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@ -11,13 +12,19 @@ import android.widget.TextView;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.recyclerview.widget.RecyclerView.Adapter;
|
import androidx.recyclerview.widget.RecyclerView.Adapter;
|
||||||
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
|
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.Config;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.adapter.AgentsAdapter.AgentViewHolder;
|
import fr.romanet.vj.apps.myrealestateagency.adapter.AgentsAdapter.AgentViewHolder;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.R;
|
import fr.romanet.vj.apps.myrealestateagency.R;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.view.PropertiesActivity;
|
import fr.romanet.vj.apps.myrealestateagency.view.PropertiesActivity;
|
||||||
|
|
||||||
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
|
|
||||||
public final class AgentsAdapter extends Adapter<AgentViewHolder>{
|
public final class AgentsAdapter extends Adapter<AgentViewHolder>{
|
||||||
|
|
||||||
|
|
||||||
public static final class AgentViewHolder extends ViewHolder{
|
public static final class AgentViewHolder extends ViewHolder{
|
||||||
|
|
||||||
private final TextView agentFirstName;
|
private final TextView agentFirstName;
|
||||||
@ -26,6 +33,9 @@ public final class AgentsAdapter extends Adapter<AgentViewHolder>{
|
|||||||
|
|
||||||
private final TextView agencyName;
|
private final TextView agencyName;
|
||||||
|
|
||||||
|
SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
|
|
||||||
public AgentViewHolder(@NonNull View itemView)
|
public AgentViewHolder(@NonNull View itemView)
|
||||||
{
|
{
|
||||||
super(itemView);
|
super(itemView);
|
||||||
@ -48,6 +58,13 @@ public final class AgentsAdapter extends Adapter<AgentViewHolder>{
|
|||||||
final Intent intent = new Intent(itemView.getContext(), PropertiesActivity.class);
|
final Intent intent = new Intent(itemView.getContext(), PropertiesActivity.class);
|
||||||
intent.putExtra(PropertiesActivity.AGENCY_EXTRA, agent.getAgency());
|
intent.putExtra(PropertiesActivity.AGENCY_EXTRA, agent.getAgency());
|
||||||
itemView.getContext().startActivity(intent);
|
itemView.getContext().startActivity(intent);
|
||||||
|
|
||||||
|
sharedPreferences = MyApp.getContext().getSharedPreferences(Config.getSharedPreferences(), MODE_PRIVATE);
|
||||||
|
sharedPreferences.edit()
|
||||||
|
.putInt(Config.get_selected_agency_index(), agent.agentId)
|
||||||
|
.apply();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,9 @@ import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
|||||||
@Dao
|
@Dao
|
||||||
public interface AgentDao {
|
public interface AgentDao {
|
||||||
|
|
||||||
|
@Query("SELECT * FROM agent WHERE agent_id = :idAgent")
|
||||||
|
Agent getAgentById(int idAgent);
|
||||||
|
|
||||||
@Query("SELECT * FROM agent")
|
@Query("SELECT * FROM agent")
|
||||||
LiveData<List<Agent>> getAgentList();
|
LiveData<List<Agent>> getAgentList();
|
||||||
|
|
||||||
|
|||||||
@ -30,4 +30,6 @@ public class AgentRepository {
|
|||||||
{
|
{
|
||||||
return agentDao.getAgency(agent.agentId);
|
return agentDao.getAgency(agent.agentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Agent getAgentById(Integer id){return agentDao.getAgentById(id);}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,12 +6,17 @@ import androidx.lifecycle.ViewModelProvider;
|
|||||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.R;
|
import fr.romanet.vj.apps.myrealestateagency.R;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.adapter.AgentsAdapter;
|
import fr.romanet.vj.apps.myrealestateagency.adapter.AgentsAdapter;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.repository.AgentRepository;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.viewmodel.AgentsActivityViewModel;
|
import fr.romanet.vj.apps.myrealestateagency.viewmodel.AgentsActivityViewModel;
|
||||||
|
|
||||||
final public class AgentsActivity extends AppCompatActivity{
|
final public class AgentsActivity extends AppCompatActivity{
|
||||||
@ -30,8 +35,20 @@ final public class AgentsActivity extends AppCompatActivity{
|
|||||||
|
|
||||||
viewModel = new ViewModelProvider(this).get(AgentsActivityViewModel.class);
|
viewModel = new ViewModelProvider(this).get(AgentsActivityViewModel.class);
|
||||||
observeAgents();
|
observeAgents();
|
||||||
|
|
||||||
|
|
||||||
|
getLifecycle().addObserver(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// private void observe_selected_agency(){
|
||||||
|
// viewModel.selected_agency.observe(this, new Observer<Integer>() {
|
||||||
|
// @Override
|
||||||
|
// public void onChanged(Integer integer) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
private void observeAgents()
|
private void observeAgents()
|
||||||
{
|
{
|
||||||
viewModel.agents.observe(this, new Observer<List<Agent>>()
|
viewModel.agents.observe(this, new Observer<List<Agent>>()
|
||||||
@ -42,6 +59,7 @@ final public class AgentsActivity extends AppCompatActivity{
|
|||||||
viewModel.setAgents();
|
viewModel.setAgents();
|
||||||
final AgentsAdapter usersAdapter = new AgentsAdapter(users);
|
final AgentsAdapter usersAdapter = new AgentsAdapter(users);
|
||||||
recyclerView.setAdapter(usersAdapter);
|
recyclerView.setAdapter(usersAdapter);
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,22 +1,37 @@
|
|||||||
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
import androidx.lifecycle.AndroidViewModel;
|
||||||
|
import androidx.lifecycle.Lifecycle;
|
||||||
|
import androidx.lifecycle.LifecycleObserver;
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
import androidx.lifecycle.OnLifecycleEvent;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.Config;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.MyApp;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.repository.AgentRepository;
|
import fr.romanet.vj.apps.myrealestateagency.repository.AgentRepository;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.view.PropertiesActivity;
|
||||||
|
|
||||||
public class AgentsActivityViewModel extends AndroidViewModel {
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
|
|
||||||
|
public class AgentsActivityViewModel extends AndroidViewModel implements LifecycleObserver {
|
||||||
|
|
||||||
public LiveData<List<Agent>> agents = new MutableLiveData<>();
|
public LiveData<List<Agent>> agents = new MutableLiveData<>();
|
||||||
|
public MutableLiveData<Integer> selected_agency = new MutableLiveData<>();
|
||||||
|
|
||||||
public AgentRepository agentRepository;
|
public AgentRepository agentRepository;
|
||||||
|
SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
|
|
||||||
public AgentsActivityViewModel(@NonNull Application application)
|
public AgentsActivityViewModel(@NonNull Application application)
|
||||||
{
|
{
|
||||||
@ -35,4 +50,33 @@ public class AgentsActivityViewModel extends AndroidViewModel {
|
|||||||
currentAgent.setAgency(agency);
|
currentAgent.setAgency(agency);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
|
||||||
|
private void restore_shared_prefs(){
|
||||||
|
|
||||||
|
sharedPreferences = MyApp.getContext().getSharedPreferences(Config.getSharedPreferences(), MODE_PRIVATE);
|
||||||
|
int stored_agent_id = sharedPreferences.getInt(Config.get_selected_agency_index(), -1);
|
||||||
|
// selected_agency.postValue(stored_agent_id);
|
||||||
|
|
||||||
|
if (stored_agent_id >= 0){
|
||||||
|
Log.d("CAT", "=========+++===" + stored_agent_id + "===========");
|
||||||
|
// if(recyclerView.findViewHolderForAdapterPosition(integer) != null){
|
||||||
|
// recyclerView.findViewHolderForAdapterPosition(integer).itemView.performClick();
|
||||||
|
// }else{
|
||||||
|
// Log.d("CAT", "============" + integer + "== NULL =========");
|
||||||
|
// }
|
||||||
|
Agent agent = agentRepository.getAgentById(stored_agent_id);
|
||||||
|
|
||||||
|
// Log.d("CAT", "========ID====" + agent.getAgency().agencyId + "===========");
|
||||||
|
|
||||||
|
final Intent intent = new Intent(MyApp.getContext(), PropertiesActivity.class);
|
||||||
|
intent.putExtra(PropertiesActivity.AGENCY_EXTRA, agent.getAgency());
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
MyApp.getContext().startActivity(intent);
|
||||||
|
|
||||||
|
}else{
|
||||||
|
Log.d("CAT", "============" + stored_agent_id + "===========");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user