Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加历史选中,打开后能看到上一次的选中 #841

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions matisse/src/main/java/com/zhihu/matisse/Matisse.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.zhihu.matisse.internal.entity.Item;
import com.zhihu.matisse.ui.MatisseActivity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -85,6 +87,17 @@ public static List<Uri> obtainResult(Intent data) {
return data.getParcelableArrayListExtra(MatisseActivity.EXTRA_RESULT_SELECTION);
}

/**
* Obtain user selected media path list in the starting Activity or Fragment.
*
* @param data Intent passed by {@link Activity#onActivityResult(int, int, Intent)} or
* {@link Fragment#onActivityResult(int, int, Intent)}.
* @return User selected media' {@link Item} list.
*/
public static ArrayList<Item> obtainListResult(Intent data) {
return data.getParcelableArrayListExtra(MatisseActivity.EXTRA_RESULT_SELECTION_ITEM);
}

/**
* Obtain user selected media path list in the starting Activity or Fragment.
*
Expand Down
23 changes: 23 additions & 0 deletions matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.zhihu.matisse.engine.ImageEngine;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;
import com.zhihu.matisse.internal.entity.Item;
import com.zhihu.matisse.internal.entity.SelectionSpec;
import com.zhihu.matisse.listener.OnCheckedListener;
import com.zhihu.matisse.listener.OnSelectedListener;
Expand Down Expand Up @@ -364,6 +365,28 @@ public void forResult(int requestCode) {
}
}

/**
* Start to select media and wait for result.
*
* @param requestCode Identity of the request Activity or Fragment.
* @param selectionsList Last selections List
*/
public void forResult(int requestCode, ArrayList<Item> selectionsList) {
Activity activity = mMatisse.getActivity();
if (activity == null) {
return;
}

Intent intent = new Intent(activity, MatisseActivity.class);
intent.putExtra(MatisseActivity.EXTRA_REQUEST_SELECTION, selectionsList);
Fragment fragment = mMatisse.getFragment();
if (fragment != null) {
fragment.startActivityForResult(intent, requestCode);
} else {
activity.startActivityForResult(intent, requestCode);
}
}

public SelectionCreator showPreview(boolean showPreview) {
mSelectionSpec.showPreview = showPreview;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public void setDefaultSelection(List<Item> uris) {
mItems.addAll(uris);
}

public void addSelections(List<Item> uris) {
mItems.addAll(uris);
}

public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(STATE_SELECTION, new ArrayList<>(mItems));
outState.putInt(STATE_COLLECTION_TYPE, mCollectionType);
Expand Down Expand Up @@ -142,6 +146,10 @@ public List<Item> asList() {
return new ArrayList<>(mItems);
}

public ArrayList<Item> asArrayList() {
return new ArrayList<>(mItems);
}

public List<Uri> asListOfUri() {
List<Uri> uris = new ArrayList<>();
for (Item item : mItems) {
Expand Down
10 changes: 10 additions & 0 deletions matisse/src/main/java/com/zhihu/matisse/ui/MatisseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public class MatisseActivity extends AppCompatActivity implements

public static final String EXTRA_RESULT_SELECTION = "extra_result_selection";
public static final String EXTRA_RESULT_SELECTION_PATH = "extra_result_selection_path";
public static final String EXTRA_RESULT_SELECTION_ITEM = "extra_result_selection_item";
public static final String EXTRA_RESULT_ORIGINAL_ENABLE = "extra_result_original_enable";
public static final String EXTRA_REQUEST_SELECTION = "extra_request_selection";
private static final int REQUEST_CODE_PREVIEW = 23;
private static final int REQUEST_CODE_CAPTURE = 24;
public static final String CHECK_STATE = "checkState";
Expand Down Expand Up @@ -142,6 +144,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
mOriginalEnable = savedInstanceState.getBoolean(CHECK_STATE);
}

ArrayList<Item> selections = getIntent().getParcelableArrayListExtra(EXTRA_REQUEST_SELECTION);
if(selections!=null && !selections.isEmpty()) {
mSelectedCollection.addSelections(selections);
}

updateBottomToolbar();

mAlbumsAdapter = new AlbumsAdapter(this, null, false);
Expand Down Expand Up @@ -210,6 +218,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selectedUris);
result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths);
result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION_ITEM, selected);
result.putExtra(EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
setResult(RESULT_OK, result);
finish();
Expand Down Expand Up @@ -322,6 +331,7 @@ public void onClick(View v) {
result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION, selectedUris);
ArrayList<String> selectedPaths = (ArrayList<String>) mSelectedCollection.asListOfString();
result.putStringArrayListExtra(EXTRA_RESULT_SELECTION_PATH, selectedPaths);
result.putParcelableArrayListExtra(EXTRA_RESULT_SELECTION_ITEM, mSelectedCollection.asArrayList());
result.putExtra(EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
setResult(RESULT_OK, result);
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
import com.zhihu.matisse.engine.impl.PicassoEngine;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;
import com.zhihu.matisse.internal.entity.Item;

import java.util.ArrayList;
import java.util.List;

public class SampleActivity extends AppCompatActivity implements View.OnClickListener {
Expand Down Expand Up @@ -104,7 +106,7 @@ private void startAction(View v) {
.setOnCheckedListener(isChecked -> {
Log.e("isChecked", "onCheck: isChecked=" + isChecked);
})
.forResult(REQUEST_CODE_CHOOSE);
.forResult(REQUEST_CODE_CHOOSE,selections);
break;
case R.id.dracula:
Matisse.from(SampleActivity.this)
Expand Down Expand Up @@ -141,11 +143,15 @@ private void startAction(View v) {
mAdapter.setData(null, null);
}

private ArrayList<Item> selections = new ArrayList<>();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
mAdapter.setData(Matisse.obtainResult(data), Matisse.obtainPathResult(data));
selections.clear();
selections.addAll(Matisse.obtainListResult(data));
Log.e("OnActivityResult ", String.valueOf(Matisse.obtainOriginalState(data)));
}
}
Expand Down