Allow to disable bookmark

This commit is contained in:
tom79 2019-05-25 18:58:31 +02:00
parent cc2caa3a66
commit 7d14a0ad85
9 changed files with 78 additions and 29 deletions

View File

@ -21,6 +21,7 @@ import java.lang.ref.WeakReference;
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveFeedsAfterBookmarkInterface;

View File

@ -1015,34 +1015,40 @@ public class API {
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
statuses = new TimelineCacheDAO(context, db).get(max_id);
if( statuses != null){
Iterator<Status> i = statuses.iterator();
List<String> ids = new ArrayList<>();
while (i.hasNext()) {
Status s = i.next();
if( ids.contains(s.getId())) {
i.remove();
new TimelineCacheDAO(context, db).remove(s.getId());
}else{
ids.add(s.getId());
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true);
//TODO: remove forced condition
if( remember_position_home ){
if( statuses != null){
Iterator<Status> i = statuses.iterator();
List<String> ids = new ArrayList<>();
while (i.hasNext()) {
Status s = i.next();
if( ids.contains(s.getId())) {
i.remove();
new TimelineCacheDAO(context, db).remove(s.getId());
}else{
ids.add(s.getId());
}
}
}
}
if( statuses == null){
return getHomeTimeline(max_id);
if( statuses == null){
return getHomeTimeline(max_id);
}else{
if( statuses.size() > 0) {
if( statuses.get(0).getId().matches("\\d+")){
apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1));
apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1));
}else{
apiResponse.setSince_id(statuses.get(0).getId());
apiResponse.setMax_id(statuses.get(statuses.size() - 1).getId());
}
}
apiResponse.setStatuses(statuses);
return apiResponse;
}
}else{
if( statuses.size() > 0) {
if( statuses.get(0).getId().matches("\\d+")){
apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1));
apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1));
}else{
apiResponse.setSince_id(statuses.get(0).getId());
apiResponse.setMax_id(statuses.get(statuses.size() - 1).getId());
}
}
apiResponse.setStatuses(statuses);
return apiResponse;
return getHomeTimeline(max_id);
}
}

View File

@ -17,7 +17,6 @@ package app.fedilab.android.client;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;

View File

@ -540,7 +540,10 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
//Only for the Home timeline
if( type == RetrieveFeedsAsyncTask.Type.HOME && !firstTootsLoaded){
asyncTask = new RetrieveFeedsAfterBookmarkAsyncTask(context, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true);
if( remember_position_home) {
asyncTask = new RetrieveFeedsAfterBookmarkAsyncTask(context, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
firstTootsLoaded = true;
}
//Let's deal with statuses
@ -925,6 +928,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
@Override
public void onRetrieveFeedsAfterBookmark(APIResponse apiResponse) {
if( statusListAdapter == null)
return;
if( apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404) ){
@ -937,10 +941,12 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
return;
}
List<Status> statuses = apiResponse.getStatuses();
if( statuses == null || statuses.size() == 0 || this.statuses == null )
return;
//Find the position of toots between those already present
int position = 0;
if( position < this.statuses.size() && statuses.get(0).getCreated_at() != null && this.statuses.get(position).getCreated_at() != null) {
while (position < this.statuses.size() && statuses.get(0).getCreated_at().before(this.statuses.get(position).getCreated_at())) {
position++;
@ -949,7 +955,9 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
ArrayList<Status> tmpStatuses = new ArrayList<>();
for (Status tmpStatus : statuses) {
//Put the toot at its place in the list (id desc)
if( !this.statuses.contains(tmpStatus) ) { //Element not already added
if (this.statuses.size() == 0){
tmpStatuses.add(tmpStatus);
}else if( tmpStatus.getCreated_at().after(this.statuses.get(0).getCreated_at())) { //Element not already added
//Mark status at new ones when their id is greater than the last read toot id
if (type == RetrieveFeedsAsyncTask.Type.HOME && lastReadTootDate != null && tmpStatus.getCreated_at().after(lastReadTootDate) ) {
tmpStatus.setNew(true);
@ -1025,7 +1033,11 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if( !pagination) {
if (type == RetrieveFeedsAsyncTask.Type.HOME) {
if (context instanceof BaseMainActivity) {
asyncTask = new RetrieveFeedsAsyncTask(context, type, initialBookMark, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true);
if(remember_position_home )
asyncTask = new RetrieveFeedsAsyncTask(context, type, initialBookMark, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
} else { //Most classical search will be done by this call
asyncTask = new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

View File

@ -464,6 +464,19 @@ public class SettingsFragment extends Fragment {
}
});
boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true);
final CheckBox set_remember_position = rootView.findViewById(R.id.set_remember_position);
set_remember_position.setChecked(remember_position_home);
set_remember_position.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SET_REMEMBER_POSITION_HOME, set_remember_position.isChecked());
editor.apply();
}
});
boolean old_direct_timeline = sharedpreferences.getBoolean(Helper.SET_OLD_DIRECT_TIMELINE, false);
final CheckBox set_old_direct_timeline = rootView.findViewById(R.id.set_old_direct_timeline);
set_old_direct_timeline.setChecked(old_direct_timeline);

View File

@ -328,6 +328,7 @@ public class Helper {
public static final String SET_LONG_PRESS_MEDIA = "set_long_press_media";
public static final String SET_DISPLAY_TIMELINE_IN_LIST = "set_display_timeline_in_list";
public static final String SET_ONION_SCHEME = "set_onion_scheme";
public static final String SET_REMEMBER_POSITION_HOME = "set_remember_position";
public static final int S_NO = 0;
static final int S_512KO = 1;
public static final int S_1MO = 2;

View File

@ -98,6 +98,14 @@
android:background="?colorAccent" />
</LinearLayout>
<CheckBox
android:id="@+id/set_remember_position"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/settings_checkbox_margin"
android:layout_marginBottom="@dimen/settings_checkbox_margin"
android:text="@string/set_remember_position"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/set_old_direct_timeline"
android:layout_width="wrap_content"

View File

@ -102,6 +102,14 @@
android:background="?colorAccent" />
</LinearLayout>
<CheckBox
android:id="@+id/set_remember_position"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/settings_checkbox_margin"
android:layout_marginBottom="@dimen/settings_checkbox_margin"
android:text="@string/set_remember_position"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/set_old_direct_timeline"
android:layout_width="wrap_content"

View File

@ -949,6 +949,7 @@
<string name="display_timeline">Display timelines</string>
<string name="set_display_bot_icon">Mark bot accounts in toots</string>
<string name="add_tags">Manage tags</string>
<string name="set_remember_position">Remember the position in Home timeline</string>
<plurals name="number_of_vote">
<item quantity="one">%d vote</item>