Skip to content

Commit

Permalink
Add Pros and Cons sections
Browse files Browse the repository at this point in the history
  • Loading branch information
LucBerge committed Sep 19, 2023
1 parent f16df5c commit d6b9419
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.room.Query;
import androidx.room.Transaction;
import androidx.room.Update;
import androidx.room.Upsert;

import com.example.ideator.model.section.Section;

Expand All @@ -24,7 +25,7 @@ public interface IdeaDao {
@Update
void update(Idea idea);

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

@Delete
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/example/ideator/model/section/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Section {

public static String TITLE_PROBLEMATIC = "Problematic";
public static String TITLE_SOLUTION = "Solution";
public static String TITLE_PROS = "Pros";
public static String TITLE_CONS = "Cons";

public static Section createProblematic(String description) {
return new Section(TITLE_PROBLEMATIC, description);
Expand All @@ -21,6 +23,14 @@ public static Section createSolution(String description) {
return new Section(TITLE_SOLUTION, description);
}

public static Section createPros(String description) {
return new Section(TITLE_PROS, description);
}

public static Section createCons(String description) {
return new Section(TITLE_CONS, description);
}

@PrimaryKey(autoGenerate = true)
private int id;
private String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
Expand All @@ -25,10 +26,13 @@

import com.example.ideator.R;
import com.example.ideator.databinding.FragmentSectionBinding;
import com.example.ideator.model.idea.Idea;
import com.example.ideator.model.idea.IdeaRepository;
import com.example.ideator.model.idea.IdeaWithSections;
import com.example.ideator.model.section.Section;
import com.example.ideator.ui.ideas.IdeasAdapter;
import com.example.ideator.ui.ideas.IdeasViewModel;
import com.example.ideator.utils.openai.BusinessPlanningAssistant;

public class EditIdeaActivity extends AppCompatActivity {
public static final long INVALID_ID = -1;
Expand Down Expand Up @@ -62,9 +66,36 @@ protected void onCreate(Bundle savedInstanceState) {
//Set the adapter
Button nextButton = findViewById(R.id.button_next_idea);
nextButton.setOnClickListener(v -> {
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setMessage(this.getText(R.string.create_idea_loading));
progressDialog.setCancelable(false);
progressDialog.setInverseBackgroundForced(false);

saveIdea(() -> {
//TODO call openai
new BusinessPlanningAssistant().getProsCons(
this,
idea,
new BusinessPlanningAssistant.OnProsConsResponse() {
@Override
public void onSuccess(String pros, String cons) {
progressDialog.hide();
idea.sections.add(Section.createPros(pros));
idea.sections.add(Section.createCons(cons));
//TODO sections not added to the idea, use onCreateView instead ?
editIdeaViewModel.update(idea, null);
}

@Override
public void onError(Throwable error) {
progressDialog.hide();
error.printStackTrace();
Toast.makeText(EditIdeaActivity.this,
R.string.error_offline,
Toast.LENGTH_LONG).show();
}
});
});
progressDialog.show();
});

//Set the view model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ public void onSuccess(String title, String description, String problematic, Stri

@Override
public void onError(Throwable error) {
progressDialog.hide();
error.printStackTrace();
Toast.makeText(getActivity(),
R.string.error_offline,
Toast.LENGTH_LONG).show();

progressDialog.hide();
Idea idea = new Idea(descriptionText.getText().toString());
ideaViewModel.insert(idea, id -> {
openEditIdeaActivity(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import android.content.Context;

import com.example.ideator.model.idea.IdeaWithSections;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,6 +22,8 @@ public class BusinessPlanningAssistant extends Assistant {
public static final String PARAMETER_DESCRIPTION = "description";
public static final String PARAMETER_PROBLEMATIC = "problematic";
public static final String PARAMETER_SOLUTION = "solution";
public static final String PARAMETER_PROS = "pros";
public static final String PARAMETER_CONS = "cons";

public BusinessPlanningAssistant() {
super(INSTRUCTIONS);
Expand All @@ -33,7 +37,7 @@ public void getTitleDescriptionProblematicSolution(Context context, String short
@Override
public void onSuccess(String response) {
Function function = Function.builder()
.name("get_title_sescription_sroblematic_solution")
.name("get_title_description_problematic_solution")
.description("Get the idea title, description, problematic and solution")
.addParameter(PARAMETER_TITLE, STRING, description("Title of the business idea"))
.addParameter(PARAMETER_DESCRIPTION, STRING, description("Short description of the business idea"))
Expand Down Expand Up @@ -70,8 +74,57 @@ public void onError(Throwable error) {
}
}

public void getProsCons(Context context, IdeaWithSections ideaWithSections, final OnProsConsResponse onResponse) {
try {
//First request = Enhance idea
String instruction = this.computeInstructions(context, "openai.2.pros.cons",
ideaWithSections.idea.getDescription(),
ideaWithSections.sections.get(0).getDescription(),
ideaWithSections.sections.get(1).getDescription());
this.answer(instruction, new Assistant.OnResponse<String>() {
@Override
public void onSuccess(String response) {
Function function = Function.builder()
.name("get_pros_cons")
.description("Get the pros and cons")
.addParameter(PARAMETER_PROS, STRING, description("Pros of the business idea"))
.addParameter(PARAMETER_CONS, STRING, description("Cons of the business idea"))
.build();

BusinessPlanningAssistant.this.answer(function, new Assistant.OnResponse<FunctionCall>(){
@Override
public void onSuccess(FunctionCall functionCall) {
Map<String, Object> arguments = functionCall.argumentsAsMap();
onResponse.onSuccess(
arguments.get(PARAMETER_PROS).toString(),
arguments.get(PARAMETER_CONS).toString()
);
}

@Override
public void onError(Throwable error) {
onResponse.onError(error);
}
});
}

@Override
public void onError(Throwable error) {
onResponse.onError(error);
}
});
} catch (IOException e) {
onResponse.onError(e);
}
}

public interface OnResponse {
void onSuccess(String title, String description, String problematic, String solution);
void onError(Throwable error);
}

public interface OnProsConsResponse {
void onSuccess(String pros, String cons);
void onError(Throwable error);
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_edit_idea.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

Expand Down Expand Up @@ -54,4 +54,4 @@
android:text="@string/button_next"/>

</LinearLayout>
</ScrollView>
</androidx.core.widget.NestedScrollView>

0 comments on commit d6b9419

Please sign in to comment.