Skip to content

Commit

Permalink
Add save button to synonym fragment.
Browse files Browse the repository at this point in the history
Add toast to notify user save status after pressing save button.
  • Loading branch information
matthewnbrown committed Nov 10, 2020
1 parent bc70e0c commit dbc7fcd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static SynonymCacher loadLocalSynonymCache (Context ctx) {
return synonymCacher;
}

public static void saveLocalSynonymCache(Context ctx, SynonymCacher cache) {
public static Boolean saveLocalSynonymCache(Context ctx, SynonymCacher cache) {
JSONObject obj = cache.toJSONObject();
File outputDir = ctx.getCacheDir();
File outputFile = new File(outputDir, FILENAME);
Expand All @@ -69,22 +69,24 @@ public static void saveLocalSynonymCache(Context ctx, SynonymCacher cache) {
Log.d(TAG, "saveLocalSynonymCache: Saved cache to JSON file");
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try{
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}

public static Date getLastSaveTime(Context ctx) {
File outputDir = ctx.getCacheDir();
File file = new File(outputDir, FILENAME);

return new Date(file.lastModified());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,19 @@ public void addSynonymToBaseWord(String baseWord, Synonym synonym) {
ArrayList<Synonym> synonyms = cache.get(baseWord);
int index = synonyms.size();

boolean repeated = false;

for(int i = 0; i < synonyms.size(); i++) {
if (synonym.getScore() > synonyms.get(i).getScore()) {
index = i;
break;
}
// if (synonyms.get(i).getWord().equals(synonym.getWord())) {
// repeated = true;
// break;
// }
}

synonyms.add(index, synonym);
sortSynonyms(synonyms);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public View getChildView(int groupPosition, final int childPosition,
//holder.btnDeleteSyn.setOnClickListener(view -> );
holder.btnEditSyn.setOnClickListener(view -> onEditSynonymClick(view, null, holder));


return row;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;

import androidx.annotation.NonNull;
Expand All @@ -19,10 +20,12 @@
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.Toast;

import com.everspysolutions.everspinner.R;
import com.everspysolutions.everspinner.SavedFileRecyclerViewAdapter;
import com.everspysolutions.everspinner.SynonymFinder.Synonym;
import com.everspysolutions.everspinner.SynonymFinder.SynonymCacheLoaderSaver;
import com.everspysolutions.everspinner.SynonymFinder.SynonymCacher;

import java.util.ArrayList;
Expand Down Expand Up @@ -89,6 +92,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
ImageButton newBaseWordBtn = rootView.findViewById(R.id.btn_add_new_baseword);
newBaseWordBtn.setOnClickListener(this::onNewBaseWordClick);

ImageButton saveCacheBtn = rootView.findViewById(R.id.btn_save_synonymcache);
saveCacheBtn.setOnClickListener(this::onSaveSynCacheClick);

// Inflate the layout for this fragment
return rootView;
}
Expand Down Expand Up @@ -132,5 +138,28 @@ public void onClick(DialogInterface dialog, int which) {
builder.show();
}

public void onSaveSynCacheClick(View view) {
final Boolean res = SynonymCacheLoaderSaver
.saveLocalSynonymCache(view.getContext(), synonymCacher);

CharSequence text;

if (res) {
text = "Saved synonym cache.";
} else {
text = "Failed to save cache.";
}

Toast toast = Toast.makeText(view.getContext(), text, Toast.LENGTH_SHORT);
toast.show();

}

}
@Override
public void onStop() {

super.onStop();


}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_baseline_save_24.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="M17,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,7l-4,-4zM12,19c-1.66,0 -3,-1.34 -3,-3s1.34,-3 3,-3 3,1.34 3,3 -1.34,3 -3,3zM15,9L5,9L5,5h10v4z"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/fragment_synonym_item_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
android:textSize="18sp"
android:text="@string/synonym_cache_list_label"
android:textColor="@color/colorPrimary"/>

<ImageButton
android:id="@+id/btn_save_synonymcache"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_add_new_baseword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_gravity="end"
android:contentDescription="@string/btn_save_cache_desc"
android:background="@drawable/ic_baseline_save_24"/>

<ImageButton
android:id="@+id/btn_add_new_baseword"
app:layout_constraintTop_toTopOf="parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
<string name="btn_delete_baseword_desc">Delete baseword from cache</string>
<string name="btn_delete_synonym_desc">Delete synonym</string>
<string name="btn_edit_synonym_desc">Edit synonym score</string>
<string name="btn_save_cache_desc">Save synonym cache changes</string>
</resources>

0 comments on commit dbc7fcd

Please sign in to comment.