1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-22 04:12:08 +01:00

Cache last podlist

This commit is contained in:
Gregor Santner 2016-06-05 13:57:34 +02:00
parent d8ca356d6c
commit 85038e90f2
3 changed files with 51 additions and 18 deletions

View file

@ -19,7 +19,6 @@
package com.github.dfa.diaspora_android.activity;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -29,7 +28,7 @@ import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
@ -79,7 +78,8 @@ public class PodSelectionActivity extends AppCompatActivity {
listPods.setTextFilterEnabled(true);
registerReceiver(podListReceiver, new IntentFilter(GetPodsService.MESSAGE));
setListedPods(app.getSettings().getPreviousPodlist());
LocalBroadcastManager.getInstance(this).registerReceiver(podListReceiver, new IntentFilter(GetPodsService.MESSAGE_PODS_RECEIVED));
if (!Helpers.isOnline(PodSelectionActivity.this)) {
Snackbar.make(listPods, R.string.no_internet, Snackbar.LENGTH_LONG).show();
@ -93,11 +93,12 @@ public class PodSelectionActivity extends AppCompatActivity {
if (intent.hasExtra("pods")) {
Bundle extras = intent.getExtras();
String[] pods = extras.getStringArray("pods");
if (pods != null && pods.length > 0)
if (pods != null && pods.length > 0) {
app.getSettings().setPreviousPodlist(pods);
setListedPods(pods);
else {
Snackbar.make(listPods, R.string.podlist_error, Snackbar.LENGTH_LONG).show();
} else {
setListedPods(app.getSettings().getPreviousPodlist());
Snackbar.make(listPods, R.string.podlist_error, Snackbar.LENGTH_SHORT).show();
}
}
}
@ -122,7 +123,6 @@ public class PodSelectionActivity extends AppCompatActivity {
private void setListedPods(String[] listedPodsArr) {
final ArrayList<String> listedPodsList = new ArrayList<>();
for (String pod : listedPodsArr) {
listedPodsList.add(pod.toLowerCase());
}
@ -131,7 +131,13 @@ public class PodSelectionActivity extends AppCompatActivity {
PodSelectionActivity.this,
android.R.layout.simple_list_item_1,
listedPodsList);
// save index and top position
int index = listPods.getFirstVisiblePosition();
View v = listPods.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - listPods.getPaddingTop());
listPods.setAdapter(adapter);
listPods.setSelectionFromTop(index, top);
adapter.getFilter().filter(editFilter.getText());
editFilter.addTextChangedListener(new TextWatcher() {
@ -213,7 +219,7 @@ public class PodSelectionActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
unregisterReceiver(podListReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(podListReceiver);
super.onDestroy();
}

View file

@ -37,10 +37,28 @@ public class AppSettings {
pref.edit().putBoolean(key, value).apply();
}
private void setStringArray(SharedPreferences pref, String key, String[] values){
StringBuffer sb = new StringBuffer();
for(String value : values){
sb.append("%%%");
sb.append(value);
}
setString(pref,key,sb.toString().replaceFirst("%%%",""));
}
private String[] getStringArray(SharedPreferences pref, String key){
String value = pref.getString(key,"%%%");
if (value.equals("%%%")){
return new String[0];
}
return value.split("%%%");
}
/*
// Preferences
*/
public static class PREF {
private static final String PREVIOUS_PODLIST = "previousPodlist";
private static final String IS_LOAD_IMAGES = "loadImages";
private static final String MINIMUM_FONT_SIZE = "minimumFontSize";
private static final String PODUSERPROFILE_AVATAR_URL = "podUserProfile_avatar";
@ -106,4 +124,12 @@ public class AppSettings {
public boolean hasPodDomain(){
return !prefPod.getString(PREF.PODDOMAIN, "").equals("");
}
public String[] getPreviousPodlist(){
return getStringArray(prefApp, PREF.PREVIOUS_PODLIST);
}
public void setPreviousPodlist(String[] pods){
setStringArray(prefApp, PREF.PREVIOUS_PODLIST, pods);
}
}

View file

@ -23,8 +23,11 @@ import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.github.dfa.diaspora_android.App;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
@ -42,9 +45,8 @@ import java.util.ArrayList;
import java.util.List;
public class GetPodsService extends Service {
public static final String MESSAGE = "com.github.dfa.diaspora.podsreceived";
private static final String TAG = "Diaspora Pod Service";
public static final String MESSAGE_PODS_RECEIVED = "com.github.dfa.diaspora.podsreceived";
private static final String TAG = App.TAG;
public GetPodsService() {
}
@ -62,7 +64,7 @@ public class GetPodsService extends Service {
* A few modifications and adaptations were made by me.
* Source:
* https://github.com/voidcode/Diaspora-Webclient/blob/master/src/com/voidcode/diasporawebclient/getPodlistTask.java
* Thanks to Terkel Sørensen
* Thanks to Terkel Sørensen ; License : GPLv3
*/
AsyncTask<Void, Void, String[]> getPodsAsync = new AsyncTask<Void, Void, String[]>() {
@Override
@ -118,11 +120,10 @@ public class GetPodsService extends Service {
}
@Override
protected void onPostExecute(String[] strings) {
Intent broadcastIntent = new Intent(MESSAGE);
if (strings != null)
broadcastIntent.putExtra("pods", strings);
sendBroadcast(broadcastIntent);
protected void onPostExecute(String[] pods) {
Intent broadcastIntent = new Intent(MESSAGE_PODS_RECEIVED);
broadcastIntent.putExtra("pods", pods != null ? pods : new String[0]);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent);
stopSelf();
}
};