Skip to content

Commit

Permalink
Merge pull request #970 from psykzz/drag-favs
Browse files Browse the repository at this point in the history
Support drag and drop moving of favourites
  • Loading branch information
Neamar authored May 6, 2018
2 parents faace6a + bea096d commit be1fe18
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 49 deletions.
47 changes: 47 additions & 0 deletions app/src/main/java/fr/neamar/kiss/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.graphics.Bitmap.CompressFormat;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

Expand Down Expand Up @@ -431,6 +432,52 @@ public ArrayList<Pojo> getFavorites(int limit) {
return favorites;
}

/**
* This method is used to set the specific position of an app in the fav array.
*
* @param context The mainActivity context
* @param id the app you want to set the position of
* @param position the new position of the fav
*/
public void setFavoritePosition(MainActivity context, String id, int position) {
String favApps = PreferenceManager.getDefaultSharedPreferences(this.context).
getString("favorite-apps-list", "");
List<String> favAppsList = new ArrayList<>(Arrays.asList(favApps.split(";")));

int currentPos = favAppsList.indexOf(id);
if(currentPos == -1) {
Log.e(TAG, "Couldn't find id in favAppsList");
return;
}
// Clamp the position so we dont just extend past the end of the array.
position = Math.min(position, favAppsList.size() - 1);

favAppsList.remove(currentPos);
// Because we're removing ourselves from the array, positions may change, we should take that into account
favAppsList.add(currentPos > position ? position + 1 : position, id);
String newFavList = TextUtils.join(";", favAppsList);

PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString("favorite-apps-list", newFavList + ";").apply();

context.onFavoriteChange();
}

/**
* Helper function to get the position of a favorite. Used mainly by the drag and drop system to know where to place the dropped app.
*
* @param context mainActivity context
* @param id the app you want to get the position of.
* @return
*/
public int getFavoritePosition(MainActivity context, String id) {
String favApps = PreferenceManager.getDefaultSharedPreferences(this.context).
getString("favorite-apps-list", "");
List<String> favAppsList = new ArrayList<>(Arrays.asList(favApps.split(";")));

return favAppsList.indexOf(id);
}

public void addToFavorites(MainActivity context, String id) {

String favApps = PreferenceManager.getDefaultSharedPreferences(context).
Expand Down
53 changes: 25 additions & 28 deletions app/src/main/java/fr/neamar/kiss/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.text.TextWatcher;
import android.util.Log;
import android.view.ContextMenu;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
Expand Down Expand Up @@ -276,6 +277,18 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});


// Fixes bug when dropping onto a textEdit widget which can cause a NPE
// This fix should be on ALL TextEdit Widgets !!!
// See : https://stackoverflow.com/a/23483957
searchEditText.setOnDragListener( new View.OnDragListener() {
@Override
public boolean onDrag( View v, DragEvent event) {
return true;
}
});


// On validate, launch first record
searchEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
Expand Down Expand Up @@ -360,9 +373,7 @@ protected void onResume() {
return;
}

if (mPopup != null) {
mPopup.dismiss();
}
dismissPopup();

if (KissApplication.getApplication(this).getDataHandler().allProvidersHaveLoaded) {
displayLoader(false);
Expand Down Expand Up @@ -531,24 +542,9 @@ public void onLauncherButtonClicked(View launcherButton) {

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mPopup != null) {
View popupContentView = mPopup.getContentView();
int[] popupPos = {0, 0};
popupContentView.getLocationOnScreen(popupPos);
final float offsetX = -popupPos[0];
final float offsetY = -popupPos[1];
ev.offsetLocation(offsetX, offsetY);
try {
boolean handled = popupContentView.dispatchTouchEvent(ev);
ev.offsetLocation(-offsetX, -offsetY);
if (!handled)
handled = super.dispatchTouchEvent(ev);
return handled;
}
catch(IllegalArgumentException e) {
// Quick temporary fix for #925
return false;
}
if (mPopup != null && ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
dismissPopup();
return true;
}
return super.dispatchTouchEvent(ev);
}
Expand Down Expand Up @@ -675,9 +671,7 @@ public void updateSearchRecords() {
*/
private void updateSearchRecords(String query) {
resetTask();

if (mPopup != null)
mPopup.dismiss();
dismissPopup();

forwarderManager.updateSearchRecords(query);

Expand Down Expand Up @@ -722,8 +716,7 @@ public void launchOccurred() {
public void registerPopup(ListPopup popup) {
if (mPopup == popup)
return;
if (mPopup != null)
mPopup.dismiss();
dismissPopup();
mPopup = popup;
popup.setVisibilityHelper(systemUiVisibilityHelper);
popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
Expand Down Expand Up @@ -764,8 +757,7 @@ public void hideKeyboard() {
}

systemUiVisibilityHelper.onKeyboardVisibilityChanged(false);
if (mPopup != null)
mPopup.dismiss();
dismissPopup();
}

@Override
Expand Down Expand Up @@ -795,4 +787,9 @@ public void beforeListChange() {
public void afterListChange() {
list.animateChange();
}

public void dismissPopup() {
if (mPopup != null)
mPopup.dismiss();
}
}
Loading

0 comments on commit be1fe18

Please sign in to comment.