-
-
Notifications
You must be signed in to change notification settings - Fork 410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Locally saved posts #1472
Open
flipdp
wants to merge
4
commits into
Docile-Alligator:master
Choose a base branch
from
flipdp:locally-saved-posts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Locally saved posts #1472
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
320 changes: 320 additions & 0 deletions
320
app/src/main/java/ml/docilealligator/infinityforreddit/LocalSave.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,320 @@ | ||
package ml.docilealligator.infinityforreddit; | ||
|
||
import android.content.ComponentCallbacks; | ||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.os.ParcelFileDescriptor; | ||
import android.widget.Toast; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.io.Serializable; | ||
import java.security.SecureRandom; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import ml.docilealligator.infinityforreddit.post.Post; | ||
|
||
public class LocalSave | ||
{ | ||
public static class SavedPost implements Serializable, Comparable<SavedPost> | ||
{ | ||
String id; | ||
String title; | ||
String subreddit; | ||
String flair; | ||
long time; | ||
|
||
String tags; | ||
|
||
public SavedPost(String _id, String _title, String _subreddit, String _flair, long _time) | ||
{ | ||
id = _id; | ||
title = _title; | ||
subreddit = _subreddit; | ||
flair = _flair; | ||
time = _time; | ||
tags = ""; | ||
} | ||
|
||
public String getId() | ||
{ | ||
return id; | ||
} | ||
|
||
public long getTime() { return time; } | ||
|
||
public String getTags() { return tags; } | ||
public void setTags(String newTags) { tags = newTags; } | ||
|
||
private boolean Filter(String filter) | ||
{ | ||
filter = filter.toLowerCase(); | ||
if(title.toLowerCase().contains(filter)) { return true; } | ||
if(subreddit.toLowerCase().contains(filter)) { return true; } | ||
if(flair.toLowerCase().contains(filter)) { return true; } | ||
|
||
if(tags.toLowerCase().contains(filter)) { return true; } | ||
|
||
return false; | ||
} | ||
|
||
public boolean GetFilters(String[] filters) | ||
{ | ||
for(String filter : filters) | ||
{ | ||
filter = filter.replace("_", " "); | ||
if(Filter(filter)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public int compareTo(SavedPost o) { | ||
return Long.compare(time, o.getTime()); | ||
} | ||
} | ||
|
||
public static final int SORT_RANDOM = 0; | ||
public static final int SORT_NEWEST_ADDED = 1; | ||
public static final int SORT_OLDEST_ADDED = 2; | ||
public static final int SORT_NEWEST_UPLOAD= 3; | ||
public static final int SORT_OLDEST_UPLOAD= 4; | ||
public static final CharSequence[] SortTypes = new CharSequence[] { "Random", "Newest Added", "Oldest Added", "Newest Upload", "Oldest Upload" }; | ||
public static int sortType = SORT_NEWEST_ADDED; | ||
|
||
public static Context globalCtx; | ||
|
||
private static boolean useFilter; | ||
private static LinkedHashMap<String, SavedPost> savedPosts = new LinkedHashMap<String, SavedPost>(); | ||
private static ArrayList<SavedPost> filteredPosts = new ArrayList<SavedPost>(); | ||
|
||
private static LinkedHashMap<String, Post> cachedPosts = new LinkedHashMap<String, Post>(); | ||
|
||
public static SavedPost GetPost(String postId) | ||
{ | ||
return savedPosts.get(postId); | ||
} | ||
|
||
public static boolean cacheHistory = false; | ||
public static boolean cacheSaved = true; | ||
|
||
|
||
public static int GetCachedPostsCount() | ||
{ | ||
return cachedPosts.size(); | ||
} | ||
|
||
public static void CachePosts(LinkedHashSet<Post> posts) | ||
{ | ||
for(Post post : posts) | ||
{ | ||
cachedPosts.put(post.getId(), post); | ||
} | ||
} | ||
|
||
public static List<SavedPost> GetPosts() | ||
{ | ||
ArrayList<SavedPost> posts; | ||
posts = useFilter ? filteredPosts : new ArrayList<SavedPost>(savedPosts.values()); | ||
|
||
switch(sortType) | ||
{ | ||
default: | ||
case SORT_OLDEST_ADDED: | ||
break; | ||
case SORT_NEWEST_ADDED: | ||
Collections.reverse(posts); | ||
break; | ||
case SORT_RANDOM: | ||
Collections.shuffle(posts, new SecureRandom()); | ||
break; | ||
case SORT_OLDEST_UPLOAD: | ||
Collections.sort(posts); | ||
break; | ||
case SORT_NEWEST_UPLOAD: | ||
Collections.sort(posts); | ||
Collections.reverse(posts); | ||
break; | ||
} | ||
|
||
//return posts.stream().limit(100).collect(Collectors.toList()); // This requires API level 24 at least | ||
if(posts.size() <= 100) | ||
{ | ||
return posts; | ||
} | ||
else | ||
{ | ||
return posts.subList(0, 100); | ||
} | ||
} | ||
|
||
public static void LoadLocalPosts() | ||
{ | ||
ObjectInputStream input = null; | ||
try | ||
{ | ||
File directory = new File(globalCtx.getFilesDir(), "localPosts.txt"); | ||
if(!directory.exists()) { return; } | ||
|
||
input = new ObjectInputStream(new FileInputStream(directory)); | ||
savedPosts = (LinkedHashMap<String, SavedPost>) input.readObject(); | ||
input.close(); | ||
|
||
Toast.makeText(globalCtx, "Loaded Local Posts", Toast.LENGTH_SHORT).show(); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
public static void SaveLocalPosts() | ||
{ | ||
ObjectOutputStream output = null; | ||
File f = new File(globalCtx.getFilesDir(), "localPosts.txt"); | ||
try { | ||
f.createNewFile(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
try | ||
{ | ||
FileOutputStream fo = new FileOutputStream(f, false); | ||
output = new ObjectOutputStream(fo); | ||
output.writeObject(savedPosts); | ||
output.close(); | ||
fo.close(); | ||
|
||
Toast.makeText(globalCtx, "Saved Local Posts", Toast.LENGTH_SHORT).show(); | ||
} | ||
catch (IOException e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void AddPost(String id, String title, String subreddit, String flair, long time) | ||
{ | ||
savedPosts.put(id, new SavedPost(id, title, subreddit, flair, time)); | ||
|
||
Toast.makeText(globalCtx, "Saved Local Post", Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
public static void RemovePost(String id) | ||
{ | ||
savedPosts.remove(id); | ||
Toast.makeText(globalCtx, "Removed Local Post", Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
public static void GetAllSaved() | ||
{ | ||
for(Post post : cachedPosts.values()) | ||
{ | ||
savedPosts.put(post.getId(), new SavedPost(post.getId(), post.getTitle(), post.getSubredditName(), post.getFlair(), post.getPostTimeMillis())); | ||
} | ||
Toast.makeText(globalCtx, "Saved Cached Posts Locally", Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
public static void ClearCachedPosts() | ||
{ | ||
cachedPosts.clear(); | ||
|
||
Toast.makeText(globalCtx, "Cached Posts Cleard", Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
public static void ClearSavedPosts() | ||
{ | ||
savedPosts.clear(); | ||
|
||
Toast.makeText(globalCtx, "Removed All Local Posts", Toast.LENGTH_SHORT).show(); | ||
} | ||
|
||
public static void Filter(String text) | ||
{ | ||
if(text.isBlank()) | ||
{ | ||
useFilter = false; | ||
return; | ||
} | ||
|
||
useFilter = true; | ||
filteredPosts.clear(); | ||
|
||
String[] filters = text.split(" "); | ||
int i = 0; | ||
for(SavedPost post : savedPosts.values()) | ||
{ | ||
useFilter = true; | ||
|
||
if(post.GetFilters(filters)) | ||
{ | ||
filteredPosts.add(post); | ||
} | ||
} | ||
} | ||
|
||
public static void LoadBackup(Uri uri) | ||
{ | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
try (InputStream inputStream = globalCtx.getContentResolver().openInputStream(uri); | ||
BufferedReader reader = new BufferedReader( | ||
new InputStreamReader(Objects.requireNonNull(inputStream)))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
stringBuilder.append(line); | ||
} | ||
} catch (FileNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
//TODO: Maybe make this a bit cleaner. Save HashMap directly instead of recreating it from an array? | ||
Gson gson = new Gson(); | ||
SavedPost[] posts = gson.fromJson(stringBuilder.toString(), SavedPost[].class); | ||
savedPosts.clear(); | ||
for(SavedPost post : posts) | ||
{ | ||
savedPosts.put(post.getId(), post); | ||
} | ||
} | ||
public static void SaveBackup(Uri uri) | ||
{ | ||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
String json = gson.toJson(savedPosts.values()); | ||
|
||
try { | ||
ParcelFileDescriptor pfd = globalCtx.getContentResolver(). | ||
openFileDescriptor(uri, "w"); | ||
FileOutputStream fileOutputStream = | ||
new FileOutputStream(pfd.getFileDescriptor()); | ||
fileOutputStream.write(json.getBytes()); | ||
fileOutputStream.close(); | ||
pfd.close(); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line can also be removed. doesnt actually do anything