Skip to content

Commit

Permalink
Now update sections
Browse files Browse the repository at this point in the history
  • Loading branch information
LucBerge committed Sep 13, 2023
1 parent b0dd474 commit 84953d5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/example/ideator/model/idea/IdeaDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public interface IdeaDao {
@Update
void update(Idea idea);

@Update
void update(List<Section> sections);

@Delete
void delete(Idea idea);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void insert(Idea idea, OnInsertResponse onResponce) {
public void insert(IdeaWithSections ideaWithSections, OnInsertResponse onResponce) {
executor.execute(() -> {
long id = ideaDao.insert(ideaWithSections.idea);

for (Section section:ideaWithSections.sections) {
section.setIdeaId(id);
}
Expand All @@ -59,6 +58,16 @@ public void update(Idea idea) {
});
}

public void update(IdeaWithSections ideaWithSections) {
executor.execute(() -> {
ideaDao.update(ideaWithSections.idea);
for (Section section:ideaWithSections.sections) {
section.setIdeaId(ideaWithSections.idea.getId());
}
ideaDao.update(ideaWithSections.sections);
});
}

public void delete(Idea idea) {
executor.execute(() -> {
ideaDao.delete(idea);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ideator.R;
import com.example.ideator.databinding.FragmentSectionBinding;
import com.example.ideator.model.idea.IdeaWithSections;
import com.example.ideator.ui.ideas.IdeasAdapter;
import com.example.ideator.ui.ideas.IdeasViewModel;
Expand All @@ -38,6 +42,8 @@ public class EditIdeaActivity extends AppCompatActivity {
private EditIdeaViewModel editIdeaViewModel;

private IdeaWithSections idea;
private RecyclerView recyclerView;
private SectionsAdapter sectionAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -49,11 +55,11 @@ protected void onCreate(Bundle savedInstanceState) {
descriptionText = findViewById(R.id.edit_idea_description);

//Set the adapter
RecyclerView recyclerView = findViewById(R.id.list_sections);
recyclerView = findViewById(R.id.list_sections);
Context context = recyclerView.getContext();
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setHasFixedSize(true);
SectionsAdapter sectionAdapter = new SectionsAdapter();
sectionAdapter = new SectionsAdapter();
recyclerView.setAdapter(sectionAdapter);

//Set the view model
Expand All @@ -76,8 +82,6 @@ protected void onCreate(Bundle savedInstanceState) {
});

setTitle("Edit idea");

//TODO add sectionAdapter.setOnItemClickListener
}

@Override
Expand All @@ -96,8 +100,20 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
else {
idea.idea.setTitle(titleText.getText().toString());
idea.idea.setDescription(descriptionText.getText().toString());
//TODO update sections
editIdeaViewModel.update(idea.idea);

//Use onFocusChanged in adapter instead ? TODO
for (int i=0; i<sectionAdapter.getItemCount(); i++) {
View sectionView = recyclerView.getLayoutManager().findViewByPosition(i);
if (sectionView != null) {
String title = ((TextView) sectionView.findViewById(R.id.section_name)).getText().toString();
String description = ((TextView) sectionView.findViewById(R.id.section_description)).getText().toString();

idea.sections.get(i).setTitle(title);
idea.sections.get(i).setDescription(description);
}
}

editIdeaViewModel.update(idea);
setResult(RESULT_OK);
finish();
Toast.makeText(this, "Idea saved", Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public EditIdeaViewModel(@NonNull Application application) {
repository = new IdeaRepository(application);
}

public void update(Idea idea) {
repository.update(idea);
public void update(IdeaWithSections ideaWithSections) {
repository.update(ideaWithSections);
}

public void delete(Idea idea) {
Expand Down

0 comments on commit 84953d5

Please sign in to comment.