Issue #903 - Keep 2 timelines visible at least

This commit is contained in:
tom79 2019-04-27 17:37:52 +02:00
parent 1e5e9e98d9
commit 20c66087a7
9 changed files with 228 additions and 152 deletions

View File

@ -208,6 +208,11 @@
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
/>
<activity android:name=".activities.ReorderTimelinesActivity"
android:windowSoftInputMode="stateAlwaysHidden"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
/>
<activity android:name=".activities.OpencollectiveActivity"
android:windowSoftInputMode="stateAlwaysHidden"
android:configChanges="orientation|screenSize"

View File

@ -121,7 +121,6 @@ import fr.gouv.etalab.mastodon.fragments.DisplayListsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayMutedInstanceFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayPeertubeNotificationsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayReorderTabFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment;
import fr.gouv.etalab.mastodon.fragments.SettingsPeertubeFragment;
import fr.gouv.etalab.mastodon.fragments.TabLayoutNotificationsFragment;
@ -157,6 +156,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_TARGETED_ACCOUNT;
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
import static fr.gouv.etalab.mastodon.helper.Helper.REFRESH_TIMELINE;
import static fr.gouv.etalab.mastodon.helper.Helper.RELOAD_MYVIDEOS;
import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_INSTANCE;
import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_REMOTE;
@ -1217,6 +1217,11 @@ public abstract class BaseMainActivity extends BaseActivity
startActivity(myIntent);
}else if( extras.getInt(INTENT_ACTION) == SEARCH_TAG){
new SyncTimelinesAsyncTask(BaseMainActivity.this, -1, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else if( extras.getInt(INTENT_ACTION) == REFRESH_TIMELINE){
int position = 0;
if( tabLayout != null)
position = tabLayout.getSelectedTabPosition();
new SyncTimelinesAsyncTask(BaseMainActivity.this, position, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else if (extras.getInt(INTENT_ACTION) == SEARCH_REMOTE) {
String url = extras.getString(SEARCH_URL);
intent.replaceExtras(new Bundle());
@ -1328,10 +1333,6 @@ public abstract class BaseMainActivity extends BaseActivity
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
DisplayReorderTabFragment displayReorderTabFragment = (DisplayReorderTabFragment)getSupportFragmentManager().findFragmentByTag("REORDER_TIMELINES");
if (displayReorderTabFragment != null && displayReorderTabFragment.isVisible() && DisplayReorderTabFragment.updated) {
new SyncTimelinesAsyncTask(BaseMainActivity.this, tabLayout.getSelectedTabPosition(), BaseMainActivity.this ).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
@ -1420,6 +1421,10 @@ public abstract class BaseMainActivity extends BaseActivity
Intent myIntent = new Intent(BaseMainActivity.this, OwnerStatusActivity.class);
startActivity(myIntent);
return false;
}else if(id == R.id.nav_drag_timelines){
Intent intent = new Intent(getApplicationContext(), ReorderTimelinesActivity.class);
startActivity(intent);
return false;
} else if( id == R.id.nav_about) {
Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
startActivity(intent);
@ -1609,12 +1614,6 @@ public abstract class BaseMainActivity extends BaseActivity
fragmentTag = "WHO_TO_FOLLOW";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, whoToFollowFragment, fragmentTag).commit();
}else if(id == R.id.nav_drag_timelines){
toot.hide();
DisplayReorderTabFragment displayReorderTabFragment = new DisplayReorderTabFragment();
fragmentTag = "REORDER_TIMELINES";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, displayReorderTabFragment, fragmentTag).commit();
}
populateTitleWithTag(fragmentTag, item.getTitle().toString(), item.getItemId());
@ -1772,7 +1771,7 @@ public abstract class BaseMainActivity extends BaseActivity
@Override
public void syncedTimelines(List<ManageTimelines> manageTimelines, int position) {
DisplayReorderTabFragment.updated = false;
ReorderTimelinesActivity.updated = false;
new ManageTimelines().createTabs(BaseMainActivity.this, manageTimelines);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
boolean optimize_loading = sharedpreferences.getBoolean(Helper.SET_OPTIMIZE_LOADING, false);

View File

@ -0,0 +1,180 @@
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
import fr.gouv.etalab.mastodon.drawers.ReorderTabAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.OnStartDragListener;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.OnUndoListener;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.SimpleItemTouchHelperCallback;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.TimelinesDAO;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.REFRESH_TIMELINE;
import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT;
/**
* Created by Thomas on 26/04/2019.
* Reorder timelines activity
*/
public class ReorderTimelinesActivity extends BaseActivity implements OnStartDragListener, OnUndoListener {
public static boolean updated;
private ItemTouchHelper touchHelper;
private RelativeLayout undo_container;
private TextView undo_action;
private List<ManageTimelines> timelines;
private ReorderTabAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
switch (theme){
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme);
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
}
if( getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
if( actionBar != null ) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.simple_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
TextView toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
toolbar_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
toolbar_title.setText(R.string.action_reorder_timeline);
if (theme == THEME_LIGHT){
Toolbar toolbar = actionBar.getCustomView().findViewById(R.id.toolbar);
Helper.colorizeToolbar(toolbar, R.color.black, ReorderTimelinesActivity.this);
}
}
setContentView(R.layout.activity_reorder_tabs);
updated = false;
RecyclerView lv_reorder_tabs = findViewById(R.id.lv_reorder_tabs);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
timelines = new TimelinesDAO(getApplicationContext(), db).getAllTimelines();
adapter = new ReorderTabAdapter(getApplicationContext(), timelines, ReorderTimelinesActivity.this, ReorderTimelinesActivity.this);
ItemTouchHelper.Callback callback =
new SimpleItemTouchHelperCallback(adapter);
touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(lv_reorder_tabs);
undo_action = findViewById(R.id.undo_action);
undo_container = findViewById(R.id.undo_container);
lv_reorder_tabs.setAdapter(adapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
lv_reorder_tabs.setLayoutManager(mLayoutManager);
}
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
touchHelper.startDrag(viewHolder);
}
@Override
public void onUndo(ManageTimelines manageTimelines, int position) {
undo_container.setVisibility(View.VISIBLE);
undo_action.setPaintFlags(undo_action.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
undo_action.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timelines.add(position, manageTimelines);
adapter.notifyItemInserted(position);
undo_container.setVisibility(View.GONE);
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
undo_container.setVisibility(View.GONE);
}
}, 2000);
}
@Override
public void onStop(){
super.onStop();
if( updated ) {
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra(INTENT_ACTION, REFRESH_TIMELINE);
startActivity(intent);
updated = false;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -18,26 +18,26 @@ package fr.gouv.etalab.mastodon.drawers;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
import es.dmoral.toasty.Toasty;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.activities.ReorderTimelinesActivity;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
import fr.gouv.etalab.mastodon.fragments.DisplayReorderTabFragment;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.ItemTouchHelperAdapter;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.ItemTouchHelperViewHolder;
@ -171,15 +171,20 @@ public class ReorderTabAdapter extends RecyclerView.Adapter<ReorderTabAdapter.It
holder.hideView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tl.setDisplayed(! tl.isDisplayed());
if(tl.isDisplayed()){
holder.hideView.setImageResource(R.drawable.ic_make_tab_visible);
}else{
holder.hideView.setImageResource(R.drawable.ic_make_tab_unvisible);
}
DisplayReorderTabFragment.updated = true;
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new TimelinesDAO(context, db).update(tl);
int count = new TimelinesDAO(context, db).countVisibleTimelines();
if( count > 2 || !tl.isDisplayed()) {
tl.setDisplayed(!tl.isDisplayed());
if (tl.isDisplayed()) {
holder.hideView.setImageResource(R.drawable.ic_make_tab_visible);
} else {
holder.hideView.setImageResource(R.drawable.ic_make_tab_unvisible);
}
ReorderTimelinesActivity.updated = true;
new TimelinesDAO(context, db).update(tl);
}else{
Toasty.info(context, context.getString(R.string.visible_tabs_needed), Toast.LENGTH_SHORT).show();
}
}
});
@ -216,7 +221,7 @@ public class ReorderTabAdapter extends RecyclerView.Adapter<ReorderTabAdapter.It
new TimelinesDAO(context, db).update(timelines);
i++;
}
DisplayReorderTabFragment.updated = true;
ReorderTimelinesActivity.updated = true;
return true;
}

View File

@ -1,127 +0,0 @@
package fr.gouv.etalab.mastodon.fragments;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.BaseMainActivity;
import fr.gouv.etalab.mastodon.asynctasks.SyncTimelinesAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.ManageTimelines;
import fr.gouv.etalab.mastodon.drawers.ReorderTabAdapter;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.OnStartDragListener;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.OnUndoListener;
import fr.gouv.etalab.mastodon.helper.itemtouchhelper.SimpleItemTouchHelperCallback;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.TimelinesDAO;
/**
* Created by Thomas on 31/03/2019.
* Fragment to display tags
*/
public class DisplayReorderTabFragment extends Fragment implements OnStartDragListener, OnUndoListener {
private Context context;
public static boolean updated;
private ItemTouchHelper touchHelper;
private RelativeLayout undo_container;
private TextView undo_action;
private List<ManageTimelines> timelines;
private ReorderTabAdapter adapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_reorder_tabs, container, false);
context = getContext();
updated = false;
RecyclerView lv_reorder_tabs = rootView.findViewById(R.id.lv_reorder_tabs);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
timelines = new TimelinesDAO(context, db).getAllTimelines();
adapter = new ReorderTabAdapter(context, timelines, DisplayReorderTabFragment.this, DisplayReorderTabFragment.this);
ItemTouchHelper.Callback callback =
new SimpleItemTouchHelperCallback(adapter);
touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(lv_reorder_tabs);
undo_action = rootView.findViewById(R.id.undo_action);
undo_container = rootView.findViewById(R.id.undo_container);
lv_reorder_tabs.setAdapter(adapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
lv_reorder_tabs.setLayoutManager(mLayoutManager);
return rootView;
}
@Override
public void onCreate(Bundle saveInstance) {
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
touchHelper.startDrag(viewHolder);
}
@Override
public void onUndo(ManageTimelines manageTimelines, int position) {
undo_container.setVisibility(View.VISIBLE);
undo_action.setPaintFlags(undo_action.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
undo_action.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timelines.add(position, manageTimelines);
adapter.notifyItemInserted(position);
undo_container.setVisibility(View.GONE);
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
undo_container.setVisibility(View.GONE);
}
}, 2000);
}
}

View File

@ -271,6 +271,7 @@ public class Helper {
public static final int SEARCH_INSTANCE = 8;
public static final int SEARCH_REMOTE = 9;
public static final int RELOAD_MYVIDEOS = 10;
public static final int REFRESH_TIMELINE = 11;
//Settings
public static final String SET_TOOTS_PER_PAGE = "set_toots_per_page";
public static final String SET_ACCOUNTS_PER_PAGE = "set_accounts_per_page";

View File

@ -95,6 +95,17 @@ public class TimelinesDAO {
new String[]{String.valueOf(timeline.getId())});
}
public int countVisibleTimelines(){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
Cursor mCount= db.rawQuery("select count(*) from " + Sqlite.TABLE_TIMELINES
+ " where " + Sqlite.COL_USER_ID + " = '" + userId+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " + Sqlite.COL_DISPLAYED + "= 1", null);
mCount.moveToFirst();
int count = mCount.getInt(0);
mCount.close();
return count;
}
public List<ManageTimelines> getAllTimelines(){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);

View File

@ -928,6 +928,8 @@
<string name="hide_timeline">Hide timeline</string>
<string name="reorder_timelines">Reorder timelines</string>
<string name="undo">Undo</string>
<string name="visible_tabs_needed">You need to keep two visible tabs!</string>
<string name="action_reorder_timeline">Reorder timelines</string>
<plurals name="number_of_vote">
<item quantity="one">%d vote</item>