Compare commits

...

6 Commits

Author SHA1 Message Date
BGbaderguet
132aeadd54 Updating dao with a new query 2020-12-29 17:29:37 +01:00
BGbaderguet
0be4898b7d Adding some activities file and the view model file of the agent activity 2020-12-29 17:29:04 +01:00
BGbaderguet
7325dbbc1b Activities added 2020-12-29 17:28:22 +01:00
BGbaderguet
7b1b326425 Deleting the older repository and creating a new one (for each table) 2020-12-29 17:27:59 +01:00
BGbaderguet
00830fb0d1 Pre populating the database with agents and agencies 2020-12-29 17:27:02 +01:00
BGbaderguet
2a8d8063c1 Adding the adapter for the recycler view in order to display the agents 2020-12-29 17:26:19 +01:00
11 changed files with 154 additions and 100 deletions

View File

@ -19,7 +19,11 @@
android:supportsRtl="true"
android:theme="@style/Theme.MyRealEstateAgency">
<activity android:name=".view.AgentsActivity">
<activity
android:name=".view.AgentsActivity"
android:theme="@style/Theme.MyRealEstateAgency"></activity>
<activity android:name=".view.HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -22,21 +22,17 @@ public final class AgentsAdapter extends Adapter<AgentViewHolder>{
private final TextView agentLastName;
private final TextView nameOfAgency;
public AgentViewHolder(@NonNull View itemView)
{
super(itemView);
agentFirstName = itemView.findViewById(R.id.agentFirstName);
agentLastName = itemView.findViewById(R.id.agentLastName);
nameOfAgency = itemView.findViewById(R.id.nameOfAgency);
}
public void update(final Agent agent)
{
agentFirstName.setText(agent.agentFirstName);
agentLastName.setText(agent.agentLastName);
nameOfAgency.setText(agent.agencyEmployerId);
}
}
@ -51,7 +47,7 @@ public final class AgentsAdapter extends Adapter<AgentViewHolder>{
@Override
public AgentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_agent, parent, false);
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_agents, parent, false);
return new AgentViewHolder(view);
}
@ -66,4 +62,4 @@ public final class AgentsAdapter extends Adapter<AgentViewHolder>{
{
return agents.size();
}
}
}

View File

@ -1,5 +1,6 @@
package fr.romanet.vj.apps.myrealestateagency.dao;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
@ -12,11 +13,14 @@ import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
public interface AgentDao {
@Query("SELECT * FROM agent")
List<Agent> getAgentList();
LiveData<List<Agent>> getAgentList();
@Query("SELECT agency_name FROM agency INNER JOIN agent ON agent.agent_id = agency.agency_id WHERE agent.agency_employer_id = :idAgencyEmployer")
String getAgencyName(int idAgencyEmployer);
@Insert
void insertAgent(Agent agent);
@Insert
void insertPreAgents(Agent[] agents);
}

View File

@ -1,8 +1,15 @@
package fr.romanet.vj.apps.myrealestateagency.database;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
import androidx.sqlite.db.SupportSQLiteDatabase;
import java.util.concurrent.Executors;
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyDao;
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyWithAgentsDao;
@ -28,4 +35,32 @@ public abstract class RealEstateAgencyDatabase extends RoomDatabase {
public abstract AgencyWithPropertiesDao agencyWithPropertiesDaoDao();
public abstract DollarCurrencyDao dollarCurrencyDao();
private static RealEstateAgencyDatabase INSTANCE;
public synchronized static RealEstateAgencyDatabase getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = buildDatabase(context);
}
return INSTANCE;
}
private static RealEstateAgencyDatabase buildDatabase(final Context context) {
return Room.databaseBuilder(context,
RealEstateAgencyDatabase.class,
"my-real_estate_agency_db")
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
@Override
public void run() {
getInstance(context).agencyDao().insertPreData(Agency.populateAgencyTable());
getInstance(context).agentDao().insertPreAgents(Agent.populateAgentsTable());
}
});
}
})
.allowMainThreadQueries().build();
}
}

View File

@ -0,0 +1,27 @@
package fr.romanet.vj.apps.myrealestateagency.repository;
import android.app.Application;
import androidx.lifecycle.LiveData;
import java.util.List;
import fr.romanet.vj.apps.myrealestateagency.dao.AgentDao;
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
public class AgentRepository {
private AgentDao agentDao;
private LiveData<List<Agent>> allAgents;
public AgentRepository(Application application) {
RealEstateAgencyDatabase realEstateAgencyDatabase = RealEstateAgencyDatabase.getInstance(application);
agentDao = realEstateAgencyDatabase.agentDao();
allAgents = agentDao.getAgentList();
}
public LiveData<List<Agent>> getAllAgents() {
return allAgents;
}
}

View File

@ -1,44 +0,0 @@
package fr.romanet.vj.apps.myrealestateagency.repository;
import android.content.Context;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
import java.util.List;
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
public final class MyRealEstateAgencyRepository {
private static volatile MyRealEstateAgencyRepository instance;
public static MyRealEstateAgencyRepository getInstance(Context context)
{
if (instance == null)
{
synchronized (MyRealEstateAgencyRepository.class)
{
if (instance == null)
{
instance = new MyRealEstateAgencyRepository(context);
}
}
}
return instance;
}
private final RealEstateAgencyDatabase realEstateAgencyDatabaseDatabase;
public MyRealEstateAgencyRepository(Context context)
{
realEstateAgencyDatabaseDatabase = Room.databaseBuilder(context, RealEstateAgencyDatabase.class, "real_estate_agency_db").allowMainThreadQueries().build();
}
public List<Agent> getAgents(){return realEstateAgencyDatabaseDatabase.agentDao().getAgentList();}
public void addAgency(Agency agency){realEstateAgencyDatabaseDatabase.agencyDao().insertAgency(agency);}
public void addAgent(Agent agent){realEstateAgencyDatabaseDatabase.agentDao().insertAgent(agent);}
public void addProperty(Property property){realEstateAgencyDatabaseDatabase.propertyDao().insertProperty(property);}
public String getAgencyName(Agent agent){return realEstateAgencyDatabaseDatabase.agentDao().getAgencyName(agent.agencyEmployerId);}
}

View File

@ -6,11 +6,7 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.List;
import fr.romanet.vj.apps.myrealestateagency.R;
@ -18,7 +14,7 @@ import fr.romanet.vj.apps.myrealestateagency.adapter.AgentsAdapter;
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
import fr.romanet.vj.apps.myrealestateagency.viewmodel.AgentsActivityViewModel;
final public class AgentsActivity extends AppCompatActivity implements OnClickListener {
final public class AgentsActivity extends AppCompatActivity{
private RecyclerView recyclerView;
@ -31,26 +27,22 @@ final public class AgentsActivity extends AppCompatActivity implements OnClickLi
setContentView(R.layout.activity_agents);
recyclerView = findViewById(R.id.recyclerViewAgents);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
viewModel = new ViewModelProvider(this).get(AgentsActivityViewModel.class);
observeAgents();
Intent i = new Intent(AgentsActivity.this, MapsActivity.class);
AgentsActivity.this.startActivity(i);
}
private void observeAgents()
{
viewModel.agents.observe(this, new Observer<List<Agent>>() {
viewModel.agents.observe(this, new Observer<List<Agent>>()
{
@Override
public void onChanged(List<Agent> agents) {
final AgentsAdapter agentsAdapter = new AgentsAdapter(agents);
recyclerView.setAdapter(agentsAdapter);
public void onChanged(List<Agent> users)
{
final AgentsAdapter usersAdapter = new AgentsAdapter(users);
recyclerView.setAdapter(usersAdapter);
}
});
}
@Override
public void onClick(View view) {
}
}
}

View File

@ -0,0 +1,46 @@
package fr.romanet.vj.apps.myrealestateagency.view;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import java.util.ArrayList;
import java.util.List;
import fr.romanet.vj.apps.myrealestateagency.R;
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
final public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button button = (Button) findViewById(R.id.buttonFirst);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(HomeActivity.this, AgentsActivity.class);
startActivity(intent);
}
});
}
@Override
public void onClick(View view) {
}
}

View File

@ -1,36 +1,26 @@
package fr.romanet.vj.apps.myrealestateagency.viewmodel;
import android.app.Application;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ViewModel;
import java.util.List;
import fr.romanet.vj.apps.myrealestateagency.repository.MyRealEstateAgencyRepository;
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
import fr.romanet.vj.apps.myrealestateagency.repository.AgentRepository;
public final class AgentsActivityViewModel extends AndroidViewModel implements LifecycleObserver {
public class AgentsActivityViewModel extends AndroidViewModel {
public MutableLiveData<List<Agent>> agents = new MutableLiveData<>();
private MyRealEstateAgencyRepository myRealEstateAgencyRepository;
public LiveData<List<Agent>> agents = new MutableLiveData<>();
public AgentRepository agentRepository;
public AgentsActivityViewModel(@NonNull Application application)
{
super(application);
agentRepository = new AgentRepository(application);
agents = agentRepository.getAllAgents();
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void start_counter_on_activity_start(){
agents.postValue(MyRealEstateAgencyRepository.getInstance(getApplication()).getAgents());
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -21,24 +21,10 @@
android:id="@+id/agentLastName"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/nameOfAgency"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/agentFirstName"
app:layout_constraintTop_toBottomOf="@id/agentFirstName"
tools:text="@tools:sample/full_names"
/>
<TextView
android:id="@+id/nameOfAgency"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/agentLastName"
tools:text="@tools:sample/us_phones"
/>
</androidx.constraintlayout.widget.ConstraintLayout>