Skip to content

Commit

Permalink
Improved the way bookmarks are saved
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsotrepa committed Nov 20, 2017
1 parent 4ecc5e6 commit a0e2e79
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 133 deletions.
179 changes: 88 additions & 91 deletions app/src/main/java/com/gmail/afonsotrepa/pocketgopher/Bookmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gmail.afonsotrepa.pocketgopher.gopherclient.Page;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -26,7 +34,7 @@ public class Bookmark

public Integer id; //a unique id that identifies the bookmark

private static final Integer BOOKMARKS_FILE = R.string.booksmarks_file;
private static final String BOOKMARKS_FILE = "bookmarks";

private Bookmark(String name, String url, Integer id)
{
Expand All @@ -43,12 +51,12 @@ public Bookmark(Context context, String name, String url)
this.name = name;

//generate a new unique id
String file = context.getResources().getString(BOOKMARKS_FILE);
SharedPreferences sharedPref = context.getSharedPreferences(file, Context.MODE_PRIVATE);
id = sharedPref.getInt("id", 0);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences
(context);
id = sharedPreferences.getInt("id", 0);

//update the (id part in the) file
SharedPreferences.Editor editor = sharedPref.edit();
//update the file
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("id", id + 1);
editor.apply();
}
Expand All @@ -68,67 +76,92 @@ public Bookmark(Context context, String name, Character type, String selector, S


/**
* Save bookmarks to a SharedPreferences file
* Add a bookmark to the file
*/
static void save(Context context, List<Bookmark> bookmarks)
void add(Context context)
{
String file = context.getResources().getString(BOOKMARKS_FILE);
//open/create the file in private mode
SharedPreferences sharedPref = context.getSharedPreferences(file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();

//transform the list into a StringBuilder
StringBuilder csvbookmarks = new StringBuilder();
for (Bookmark b : bookmarks)
try
{
csvbookmarks.append(b.name).append("\n");
csvbookmarks.append(b.url).append("\n");
csvbookmarks.append(b.id.toString()).append("\n");
csvbookmarks.append("\u0000");
FileOutputStream outputStream = context.openFileOutput(BOOKMARKS_FILE,
Context.MODE_APPEND
);

outputStream.write((
this.name + "\t" +
this.url + "\t" +
this.id.toString() + "\n"
).getBytes());
outputStream.close();
}
catch (IOException e)
{
throw new RuntimeException(e);
}

//write csvbookmarks to the editor
editor.putString("bookmarks", csvbookmarks.toString());
//apply the changes to the file
editor.apply();
}


/**
* Read the bookmarks from a SharedPreferences file
* Read the bookmarks from the bookmarks file
*
* @return a list of all the bookmarks in the bookmarks file
*/
static List<Bookmark> read(Context context) throws Exception
static List<Bookmark> read(Context context)
{
String file = context.getResources().getString(BOOKMARKS_FILE);
//open/create the file in private mode
SharedPreferences sharedPref = context.getSharedPreferences(file, Context.MODE_PRIVATE);

try
{
FileInputStream inputStream = context.openFileInput(BOOKMARKS_FILE);
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputStream));

List<Bookmark> bookmarks = new ArrayList<>();
//read the bookmark(s) from the file
List<Bookmark> bookmarks = new ArrayList<>();
String b;
while ((b = bufferedreader.readLine()) != null)
{
String[] bsplit = b.split("\t");
if (bsplit.length > 1)
{
//parse the bookmark
Bookmark bookmark = new Bookmark(
bsplit[0], //name
bsplit[1], //url
Integer.parseInt(bsplit[2]) //id
);

//add it to the list of bookmarks
bookmarks.add(bookmark);
}
}

//read the bookmark(s) from the file
String[] csvbookmarks = sharedPref.getString("bookmarks", "").split("\u0000");
return bookmarks;
}
catch (IOException e)
{
return null;
}
}

for (String b : csvbookmarks)
public void remove(Context context)
{
try
{
String[] bsplit = b.split("\n");
if (bsplit.length > 1)
List<Bookmark> bookmarks = Bookmark.read(context);

//clear the file
context.openFileOutput(BOOKMARKS_FILE, Context.MODE_PRIVATE ).write("".getBytes());

for (Bookmark b : bookmarks)
{
//parse the bookmark
Bookmark bookmark = new Bookmark(
bsplit[0], //name
bsplit[1], //url
Integer.parseInt(bsplit[2])
); //id

//add it to the list of bookmarks
bookmarks.add(bookmark);
if (b.id != this.id)
{
b.add(context);
}
}
}

return bookmarks;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}


Expand Down Expand Up @@ -163,6 +196,7 @@ public void editBookmark(final Context context)
//apply the layout
dialog.setView(layout);

final Bookmark bookmark = this;

dialog.setPositiveButton("Save",
new DialogInterface.OnClickListener()
Expand All @@ -172,35 +206,11 @@ public void onClick(final DialogInterface dialog, int which)
{
try
{
//list of current bookmarks
List<Bookmark> bookmarks = Bookmark.read(context);

//if editing a bookmark that already exists
if (id != 0)
{
//remove the old bookmark
for (Bookmark b : bookmarks)
{
if (b.id.equals(id))
{
bookmarks.remove(b);
}
}
}
//create the new bookmark
Bookmark b = new Bookmark(
context,
editName.getText().toString(),
editUrl.getText().toString()
);

//add it to the list
bookmarks.add(b);

//save the changes to the file
Bookmark.save(context, bookmarks);

//show "Bookmark saved" and exit this activity
bookmark.name = editName.getText().toString();
bookmark.url = editUrl.getText().toString();

bookmark.add(context);

Toast.makeText(context, "Bookmark saved", Toast.LENGTH_SHORT)
.show();
dialog.cancel();
Expand Down Expand Up @@ -230,20 +240,7 @@ public void onClick(DialogInterface dialog, int which)
{
try
{
List<Bookmark> bookmarks = Bookmark.read(context);

//remove the bookmark from bookmarks (if it exists)
for (Bookmark bookmark : bookmarks)
{
if (bookmark.id.equals(id))
{
bookmarks.remove(bookmark);
break;
}
}

//save the edited bookmarks list
Bookmark.save(context, bookmarks);
bookmark.remove(context);

dialog.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ static public void add(Context context, String url)
Context.MODE_APPEND
);

outputStream.write((url + "\n").getBytes());
outputStream.write(url.getBytes());
outputStream.write("\n".getBytes());
outputStream.close();
}
catch (IOException e)
Expand Down
Loading

0 comments on commit a0e2e79

Please sign in to comment.