Skip to content

Commit

Permalink
Make gallery updatable via context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
nning committed May 21, 2017
1 parent 8f88fc7 commit f2c92bd
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 32 deletions.
4 changes: 4 additions & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
Expand All @@ -98,6 +100,8 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/net/orgizm/imgshr/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.support.v4.app.NotificationCompat;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -252,8 +251,6 @@ public void setConnectionPropertiesForDiscover() throws ProtocolException {
}

public String discoverGallery() throws IOException {
final Gallery gallery = new Gallery(slug);

setConnectionPropertiesForDiscover();

int code = conn.getResponseCode();
Expand Down
21 changes: 15 additions & 6 deletions app/src/main/java/net/orgizm/imgshr/Gallery.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package net.orgizm.imgshr;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

public class Gallery {
public class Gallery implements Comparable<Gallery> {
private String name;
private String slug;

Expand All @@ -32,8 +29,20 @@ public void setSlug(String slug) {
this.slug = slug;
}

public void updateDetailsFromJson(String json) {
final Gallery gallery = new Gson().fromJson(json, Gallery.class);
public void updateDetails(Gallery gallery) {
this.name = gallery.getName();
}

public void updateDetails(String json) {
final Gallery gallery = new Gson().fromJson(json, Gallery.class);
updateDetails(gallery);
}

public int compareTo(Gallery other) {
if (this.getName() == other.getName()) {
return 0;
} else {
return -1;
}
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/net/orgizm/imgshr/GalleryAddActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public void run() {
final Connection conn = new Connection(context, s);
final String json = conn.discoverGallery();

gallery.updateDetailsFromJson(json);

Log.d(LOG_TARGET, json);
gallery.updateDetails(json);
}
catch(Exception e) {
Log.d(LOG_TARGET, Log.getStackTraceString(e));
Expand Down
46 changes: 38 additions & 8 deletions app/src/main/java/net/orgizm/imgshr/GalleryListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
Expand All @@ -19,17 +20,21 @@
import java.util.Set;

public class GalleryListActivity extends Activity {
final String LOG_TARGET = "net.orgizm.imgshr";

private List<Gallery> galleriesList = new ArrayList<>();
private GalleryListAdapter adapter = new GalleryListAdapter(galleriesList);

private Preferences preferences;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_list_activity);

preferences = new Preferences(getApplicationContext());
context = getApplicationContext();

setTitle();
populateListView();
Expand Down Expand Up @@ -57,13 +62,8 @@ protected void populateListView() {
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(adapter);

Set<Gallery> galleries = preferences.getGalleries();

galleriesList.clear();

for (Gallery gallery : galleries) {
galleriesList.add(gallery);
}
galleriesList.addAll(preferences.getGalleries());

adapter.notifyDataSetChanged();
}
Expand Down Expand Up @@ -95,12 +95,42 @@ public boolean onContextItemSelected(MenuItem item) {
break;

case R.id.open_url:
Gallery gallery = galleriesList.get(position);
String url = "https://imgshr.space/!" + gallery.getSlug();
Gallery gallery1 = galleriesList.get(position);
String url = "https://imgshr.space/!" + gallery1.getSlug();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

break;

case R.id.update_details:
final Gallery gallery2 = galleriesList.get(position);

new Thread(new Runnable() {
public void run() {
try {
final Connection conn = new Connection(context, gallery2.getSlug());
final String json = conn.discoverGallery();

Log.d(LOG_TARGET, json);

gallery2.updateDetails(json);
}
catch(Exception e) {
Log.d(LOG_TARGET, Log.getStackTraceString(e));
}

preferences.setLastSlugs(gallery2);

runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, R.string.gallery_saved, Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
}
}).start();

break;
}

Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/net/orgizm/imgshr/GalleryListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.Context
menu.setHeaderTitle(gallery.getSlug());
menu.add(Menu.NONE, R.id.delete_from_list, Menu.NONE, R.string.delete_from_list);
menu.add(Menu.NONE, R.id.open_url, Menu.NONE, R.string.open_url);
menu.add(Menu.NONE, R.id.update_details, Menu.NONE, R.string.update_details);
}

@Override
Expand Down Expand Up @@ -68,9 +69,6 @@ public void onBindViewHolder(final ViewHolder holder, int position) {
String slug = gallery.getSlug();
String name = gallery.getName();

Log.d("net.orgizm.imgshr", "------------------> " + name);
Log.d("net.orgizm.imgshr", "------------------> " + (name == null ? slug : name));

holder.slug.setText(name == null ? slug : name);

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
Expand Down
34 changes: 25 additions & 9 deletions app/src/main/java/net/orgizm/imgshr/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -59,28 +60,43 @@ public String[] getLastSlugsAsArray() {
public void setLastSlugs(Gallery gallery) {
if (gallery == null) return;

SharedPreferences.Editor editor = preferences.edit();
List<Gallery> newGalleries = new ArrayList<>(getGalleries());

Set<String> serializedGalleries = preferences.getStringSet(GALLERIES_KEY, null);
if (serializedGalleries == null) serializedGalleries = new HashSet<>();
int position = -1;
for (int i = 0; i < newGalleries.size(); i++) {
if (gallery.getSlug().equals(newGalleries.get(i).getSlug())) {
position = i;
break;
}
}

Set<String> newSerializedGalleries = new HashSet<>(serializedGalleries);
newSerializedGalleries.add(gson.toJson(gallery));
if (position == -1) {
newGalleries.add(gallery);
} else {
newGalleries.get(position).updateDetails(gallery);
}

editor.putStringSet(GALLERIES_KEY, newSerializedGalleries);
editor.apply();
setLastSlugs(newGalleries);
}

public void setLastSlugs(List<Gallery> galleries) {
setLastSlugs(new HashSet<>(galleries));
}

public void setLastSlugs(Set<Gallery> galleries) {
SharedPreferences.Editor editor = preferences.edit();
editor.putStringSet(GALLERIES_KEY, serializeGalleries(galleries));
editor.apply();
}

public Set<String> serializeGalleries(Set<Gallery> galleries) {
Set<String> serializedGalleries = new HashSet<>();

for (Gallery gallery : galleries) {
if (gallery == null) continue;
serializedGalleries.add(gson.toJson(gallery));
}

editor.putStringSet(GALLERIES_KEY, serializedGalleries);
editor.apply();
return serializedGalleries;
}
}
1 change: 1 addition & 0 deletions app/src/main/res/menu/gallery_context_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/delete_from_list" android:title="@string/delete_from_list"/>
<item android:id="@+id/open_url" android:title="@string/open_url"/>
<item android:id="@+id/update_details" android:title="@string/update_details"/>
</menu>
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 @@ -11,4 +11,5 @@
<string name="open_url">Open URL</string>
<string name="save_button">Save</string>
<string name="add_gallery">Add gallery</string>
<string name="update_details">Update details</string>
</resources>

0 comments on commit f2c92bd

Please sign in to comment.