Skip to content

Commit

Permalink
Merge pull request #18 from developerchan1/master
Browse files Browse the repository at this point in the history
feat: Add edit feature for transcript result (Lecturer)
  • Loading branch information
werdna521 authored Jan 28, 2021
2 parents 76d3c03 + a1b24ef commit e9680f9
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 50 deletions.
138 changes: 111 additions & 27 deletions app/src/main/java/com/ambinusian/adab/all/ActivityLive.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ambinusian.adab.all;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Handler;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
Expand All @@ -13,6 +15,8 @@
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
Expand Down Expand Up @@ -45,20 +49,21 @@ public class ActivityLive extends AppCompatActivity{
private Toolbar toolbar;
private TextView className;
private TextView classSession;
private TextView textContent;
private TextView textLiveNow;
private TextView courseTitle;
private TextView toolbarTitle;
private EditText textContent;
private Socket socket;
private RelativeLayout loadingLayout;
private RelativeLayout contentLoadingLayout;
private ScrollView scrollViewMain;
private UserPreferences userPreferences;
private Integer sessionId;
private Integer canTalk;
private MaterialButton talkButton;
private LinearLayout layoutButtons;
private MaterialButton talkButton, editButton, cancelButton, saveButton;
private LinearLayout layoutButtons, layoutButtonsEdit;
private String courseLanguage;
private Boolean singleResult = true; //keep that the speech recognizer just return single result, not double

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -84,7 +89,11 @@ protected void onCreate(Bundle savedInstanceState) {
contentLoadingLayout = findViewById(R.id.layout_loading_content);
scrollViewMain = findViewById(R.id.scrollview_main);
talkButton = findViewById(R.id.button_talk);
editButton = findViewById(R.id.button_edit);
cancelButton = findViewById(R.id.button_cancel);
saveButton = findViewById(R.id.button_save);
layoutButtons = findViewById(R.id.layout_buttons);
layoutButtonsEdit = findViewById(R.id.layout_buttons_edit);
APIManager apiManager = new APIManager(this);
userPreferences = new UserPreferences(this);

Expand All @@ -107,6 +116,9 @@ protected void onCreate(Bundle savedInstanceState) {
setTextSize();
setTextTypeface();

//the first time, set content to not editable
textContent.setEnabled(false);

apiManager.getClassDetails(userPreferences.getUserToken(), sessionId, new NetworkHelper.getClassDetails() {
@Override
public void onResponse(Boolean success, Map<String, Object> classDetails) {
Expand All @@ -123,19 +135,6 @@ public void onResponse(Boolean success, Map<String, Object> classDetails) {
classSession.setText(sessionText);
textContent.setText(content);

Date endDate = null;
try {
endDate = new SimpleDateFormat("yy-MM-dd HH:mm").parse((String) classDetails.get("session_enddate"));
} catch (Exception e) { }



// if (Calendar.getInstance().getTime().before(endDate)) {
// toolbarTitle.setText(R.string.live_class_transcribe);
// } else {
// toolbarTitle.setText(R.string.class_transcribe_history);
// }

connectSocket();
loadingLayout.setVisibility(View.GONE);
toolbarTitle.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -218,18 +217,29 @@ public void onError(int i) {

@Override
public void onResults(Bundle bundle) {
//getting all the matches
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

//displaying the first match
if (matches != null)
socket.emit("message", matches.get(0));

// listening again
if (currentlyTalking.get()) {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
if (singleResult){
//getting all the matches
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

//displaying the first match
if (matches != null)
socket.emit("message", matches.get(0));

// listening again
if (currentlyTalking.get()) {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
singleResult = false;
}

//after 100 ms, set back singleResult to true
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
singleResult = true;
}
},100);
}

@Override
Expand All @@ -249,17 +259,50 @@ public void onEvent(int i, Bundle bundle) {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
talkButton.setText("Stop Talking");

//don't show edit transcript button
editButton.setVisibility(View.GONE);

// don't let the device go to sleep
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
currentlyTalking.set(false);
mSpeechRecognizer.stopListening();
talkButton.setText("Start Talking");

//show edit transcript button
editButton.setVisibility(View.VISIBLE);

// let the device go to sleep
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});

editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//save current content before edit
String buffer = textContent.getText().toString();
setupEditMode();

//listener for cancel button
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textContent.setText(buffer);
setupTalkMode();
}
});

//listener for save button
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
socket.emit("edit",textContent.getText().toString());
setupTalkMode();
}
});
}
});
}else{
toolbarTitle.setText(R.string.class_transcribe_history);
}
Expand Down Expand Up @@ -322,6 +365,47 @@ private void checkPermission() {
}
}

private void setupEditMode(){
//show layout button for edit
layoutButtons.setVisibility(View.GONE);
layoutButtonsEdit.setVisibility(View.VISIBLE);

//enable textContent
textContent.setEnabled(true);
textContent.setSelection(0);
textContent.requestFocus();

//show soft keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textContent, InputMethodManager.SHOW_IMPLICIT);

//set new layout param for scrollViewMain
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE,layoutButtonsEdit.getId());
lp.addRule(RelativeLayout.BELOW,R.id.appbarLayout);
scrollViewMain.setLayoutParams(lp);
}

private void setupTalkMode(){
//show layout buttons
layoutButtons.setVisibility(View.VISIBLE);
layoutButtonsEdit.setVisibility(View.GONE);

//disable textContent
textContent.setEnabled(false);
textContent.clearFocus();

//set new layout param for scrollViewMain
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE,layoutButtons.getId());
lp.addRule(RelativeLayout.BELOW,R.id.appbarLayout);
scrollViewMain.setLayoutParams(lp);
}

private void setHighConstrastTheme() {
getTheme().applyStyle(R.style.Theme_Adab_HighContrast,true);
toolbar.setBackgroundTintList(getResources().getColorStateList(android.R.color.black));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public void onClick(View view) {
latestClassLayout.setVisibility(View.GONE);
}
else {
latestClassLayout.setVisibility(View.VISIBLE);
for (int i = nextClass - 1; i >= 0 && i >= nextClass-10; i--) {
ClassEntity classEntity = classEntities.get(i);
coursesList.add(new CourseModel(classEntity.getSessionId(),
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/cancel_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/check_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/edit_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
</vector>
57 changes: 52 additions & 5 deletions app/src/main/res/layout/activity_live_session.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/live_session_background"
xmlns:tools="http://schemas.android.com/tools">

Expand Down Expand Up @@ -31,8 +32,6 @@
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>



<ScrollView
android:id="@+id/scrollview_main"
android:layout_width="match_parent"
Expand Down Expand Up @@ -140,7 +139,7 @@
android:layout_width="wrap_content"
android:layout_height="10dp" />

<TextView
<EditText
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -149,6 +148,7 @@
android:lineSpacingExtra="5dp"
android:scrollbars="vertical"
android:gravity="bottom"
android:background="@null"
android:text=""/>

<RelativeLayout
Expand Down Expand Up @@ -177,18 +177,65 @@
</ScrollView>

<LinearLayout
android:id="@+id/layout_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/layout_buttons"
android:layout_alignParentBottom="true"
android:visibility="gone">
android:visibility="gone"
android:padding="4dp">

<com.google.android.material.button.MaterialButton
android:id="@+id/button_talk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Talking"
style="@style/RoundedMaterialButton" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="Edit Transcript"
android:textColor="@color/colorAccent"
app:strokeColor="@color/colorAccent"
app:icon="@drawable/edit_icon"
app:iconTint="@color/colorAccent"
app:iconPadding="5dp"
style="@style/RoundedMaterialButton" />
</LinearLayout>

<LinearLayout
android:id="@+id/layout_buttons_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:padding="4dp">

<com.google.android.material.button.MaterialButton
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textColor="@android:color/holo_red_light"
app:strokeColor="@android:color/holo_red_light"
app:icon="@drawable/cancel_icon"
app:iconTint="@android:color/holo_red_light"
app:iconPadding="5dp"
style="@style/RoundedMaterialButton" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="Save"
app:icon="@drawable/check_icon"
app:iconPadding="5dp"
style="@style/RoundedMaterialButton" />
</LinearLayout>

<RelativeLayout
Expand Down
Loading

0 comments on commit e9680f9

Please sign in to comment.