Skip to content

Commit

Permalink
Merge pull request #230 from CaiJingLong/ignore-size
Browse files Browse the repository at this point in the history
Ignore size
  • Loading branch information
CaiJingLong authored Mar 24, 2020
2 parents 8e4b042 + ecec186 commit aefded2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FilterCond {
var maxWidth = 0
var minHeight = 0
var maxHeight = 0

var ignoreSize = false
}

class DurationConstraint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object AndroidQDBUtils : IDBUtils {

val dateSelection = getDateCond(args, timeStamp, option)

val sizeWhere = sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val selections = "${MediaStore.Images.Media.BUCKET_ID} IS NOT NULL $typeSelection $dateSelection $sizeWhere"

Expand Down Expand Up @@ -88,7 +88,7 @@ object AndroidQDBUtils : IDBUtils {

val dateSelection = getDateCond(args, timeStamp, option)

val sizeWhere = sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val selections = "${MediaStore.Images.Media.BUCKET_ID} IS NOT NULL $typeSelection $dateSelection $sizeWhere"

Expand Down Expand Up @@ -119,7 +119,7 @@ object AndroidQDBUtils : IDBUtils {
}
val typeSelection: String = getCondFromType(requestType, option, args)

val sizeWhere = sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val dateSelection = getDateCond(args, timeStamp, option)

Expand Down Expand Up @@ -160,7 +160,7 @@ object AndroidQDBUtils : IDBUtils {
}
val typeSelection: String = getCondFromType(requestType, option, args)

val sizeWhere = sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val dateSelection = getDateCond(args, timestamp, option)

Expand Down Expand Up @@ -252,7 +252,7 @@ object AndroidQDBUtils : IDBUtils {
args.add(galleryId)
}

val sizeWhere = sizeWhere(null)
val sizeWhere = sizeWhere(null, option)

val selection = "${MediaStore.Images.Media.BUCKET_ID} IS NOT NULL $typeSelection $dateSelection $idSelection $sizeWhere"
val cursor = context.contentResolver.query(uri, projection, selection, args.toTypedArray(), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ object ConvertUtils {
sizeConstraint.maxWidth = sizeMap["maxWidth"] as Int
sizeConstraint.minHeight = sizeMap["minHeight"] as Int
sizeConstraint.maxHeight = sizeMap["maxHeight"] as Int
sizeConstraint.ignoreSize = sizeMap["ignoreSize"] as Boolean

val durationConstraint = FilterCond.DurationConstraint()
filterOptions.durationConstraint = durationConstraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object DBUtils : IDBUtils {

val dateSelection = getDateCond(args, timeStamp, option)

val sizeWhere = AndroidQDBUtils.sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val selection = "${MediaStore.Images.Media.BUCKET_ID} IS NOT NULL $typeSelection $dateSelection $sizeWhere) GROUP BY (${MediaStore.Images.Media.BUCKET_ID}"
val cursor = context.contentResolver.query(uri, projection, selection, args.toTypedArray(), null)
Expand All @@ -84,7 +84,7 @@ object DBUtils : IDBUtils {

val dateSelection = getDateCond(args, timeStamp, option)

val sizeWhere = AndroidQDBUtils.sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val selections = "${MediaStore.Images.Media.BUCKET_ID} IS NOT NULL $typeSelection $dateSelection $sizeWhere"

Expand Down Expand Up @@ -117,7 +117,7 @@ object DBUtils : IDBUtils {
args.add(galleryId)
}

val sizeWhere = AndroidQDBUtils.sizeWhere(null)
val sizeWhere = sizeWhere(null, option)

val selection = "${MediaStore.Images.Media.BUCKET_ID} IS NOT NULL $typeSelection $dateSelection $idSelection $sizeWhere) GROUP BY (${MediaStore.Images.Media.BUCKET_ID}"
val cursor = context.contentResolver.query(uri, projection, selection, args.toTypedArray(), null)
Expand Down Expand Up @@ -164,7 +164,7 @@ object DBUtils : IDBUtils {

val dateSelection = getDateCond(args, timeStamp, option)

val sizeWhere = AndroidQDBUtils.sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val keys = (storeImageKeys + storeVideoKeys + typeKeys + locationKeys).distinct().toTypedArray()
val selection = if (isAll) {
Expand Down Expand Up @@ -204,7 +204,7 @@ object DBUtils : IDBUtils {

val dateSelection = getDateCond(args, timestamp, option)

val sizeWhere = AndroidQDBUtils.sizeWhere(requestType)
val sizeWhere = sizeWhere(requestType, option)

val keys = (storeImageKeys + storeVideoKeys + typeKeys + locationKeys).distinct().toTypedArray()
val selection = if (isAll) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,6 @@ interface IDBUtils {
val allUri: Uri
get() = IDBUtils.allUri

/**
* Just filter [MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE]
*/
fun sizeWhere(requestType: Int?): String {
if (requestType == null || !typeUtils.containsImage(requestType)) {
return ""
}
val mediaType = MediaStore.Files.FileColumns.MEDIA_TYPE


var result = ""

if (typeUtils.containsVideo(requestType)) {
result = "OR ( $mediaType = ${MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO} )"
}

if (typeUtils.containsAudio(requestType)) {
result = "$result OR ( $mediaType = ${MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO} )"
}

val size = "${MediaStore.MediaColumns.WIDTH} > 0 AND ${MediaStore.MediaColumns.HEIGHT} > 0"

val imageCondString = "( $mediaType = ${MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE} AND $size )"

result = "AND ($imageCondString $result)"

return result
}

private val typeUtils: RequestTypeUtils
get() = RequestTypeUtils

Expand Down Expand Up @@ -190,8 +161,41 @@ interface IDBUtils {

fun cacheOriginFile(context: Context, asset: AssetEntity, byteArray: ByteArray)

/**
* Just filter [MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE]
*/
fun sizeWhere(requestType: Int?, option: FilterOption): String {
if (option.imageOption.sizeConstraint.ignoreSize) {
return ""
}

if (requestType == null || !typeUtils.containsImage(requestType)) {
return ""
}
val mediaType = MediaStore.Files.FileColumns.MEDIA_TYPE


var result = ""

if (typeUtils.containsVideo(requestType)) {
result = "OR ( $mediaType = ${MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO} )"
}

if (typeUtils.containsAudio(requestType)) {
result = "$result OR ( $mediaType = ${MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO} )"
}

val size = "${MediaStore.MediaColumns.WIDTH} > 0 AND ${MediaStore.MediaColumns.HEIGHT} > 0"

val imageCondString = "( $mediaType = ${MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE} AND $size )"

result = "AND ($imageCondString $result)"

return result
}

fun getCondFromType(type: Int, filterOption: FilterOption, args: ArrayList<String>): String {
val cond = StringBuilder();
val cond = StringBuilder()
val typeKey = MediaStore.Files.FileColumns.MEDIA_TYPE

val haveImage = RequestTypeUtils.containsImage(type)
Expand All @@ -204,11 +208,15 @@ interface IDBUtils {

if (haveImage) {
val imageCond = filterOption.imageOption
val sizeCond = imageCond.sizeCond()
val sizeArgs = imageCond.sizeArgs()
imageCondString = "$typeKey = ? AND $sizeCond"
imageCondString = "$typeKey = ? "
args.add(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString())
args.addAll(sizeArgs)

if (!imageCond.sizeConstraint.ignoreSize) {
val sizeCond = imageCond.sizeCond()
val sizeArgs = imageCond.sizeArgs()
imageCondString = "$imageCondString AND $sizeCond"
args.addAll(sizeArgs)
}
}

if (haveVideo) {
Expand Down Expand Up @@ -250,9 +258,7 @@ interface IDBUtils {
cond.append("( $audioCondString )")
}

val condString = "AND ( $cond )"

return condString
return "AND ( $cond )"
}

private fun getCond(cond: Boolean, condString: String): String {
Expand Down
11 changes: 10 additions & 1 deletion example/lib/model/photo_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ class PhotoProvider extends ChangeNotifier {
String maxWidth = "10000";
String minHeight = "0";
String maxHeight = "10000";
bool _ignoreSize = false;

Duration _minDuration = Duration(seconds: 10);
bool get ignoreSize => _ignoreSize;

set ignoreSize(bool ignoreSize) {
_ignoreSize = ignoreSize;
notifyListeners();
}

Duration _minDuration = Duration.zero;

Duration get minDuration => _minDuration;

Expand Down Expand Up @@ -153,6 +161,7 @@ class PhotoProvider extends ChangeNotifier {
maxWidth: maxW,
minHeight: minH,
maxHeight: maxH,
ignoreSize: ignoreSize,
);
} catch (e) {
showToast("Cannot convert your size.");
Expand Down
16 changes: 16 additions & 0 deletions example/lib/page/filter_option_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class _FilterOptionPageState extends State<FilterOptionPage> {
buildInput(provider.maxHeight, "maxHeight", (value) {
provider.maxHeight = value;
}),
buildIgnoreSize(provider),
buildNeedTitleCheck(provider),
buildDurationWidget(
provider,
Expand Down Expand Up @@ -103,6 +104,21 @@ class _FilterOptionPageState extends State<FilterOptionPage> {
);
}

Widget buildIgnoreSize(PhotoProvider provider) {
return AnimatedBuilder(
animation: provider,
builder: (context, snapshot) {
return CheckboxListTile(
title: Text('Ignore size with image'),
onChanged: (bool value) {
provider.ignoreSize = value;
},
value: provider.ignoreSize,
);
},
);
}

Widget buildDurationWidget(Listenable listenable, String title,
Duration value, void Function(Duration duration) onChanged) {
return AnimatedBuilder(
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/core/ConvertUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ + (PMFilterOption *)convertMapToPMFilterOption:(NSDictionary *)map {
sizeConstraint.maxWidth = [sizeMap[@"maxWidth"] unsignedIntValue];
sizeConstraint.minHeight = [sizeMap[@"minHeight"] unsignedIntValue];
sizeConstraint.maxHeight = [sizeMap[@"maxHeight"] unsignedIntValue];
sizeConstraint.ignoreSize = [sizeMap[@"ignoreSize"] boolValue];
option.sizeConstraint = sizeConstraint;

PMDurationConstraint durationConstraint;
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/core/PMFilterOption.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct PMSizeConstraint {
unsigned int maxWidth;
unsigned int minHeight;
unsigned int maxHeight;
BOOL ignoreSize;

} PMSizeConstraint;

Expand Down
11 changes: 5 additions & 6 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,11 @@ - (PHFetchOptions *)getAssetOptions:(int)type filterOption:(PMFilterOptionGroup
[cond appendString:@"mediaType == %d"];
[args addObject:@(PHAssetMediaTypeImage)];

[cond appendString:@" AND "];
[cond appendString:sizeCond];
[args addObjectsFromArray:sizeArgs];
if (!imageOption.sizeConstraint.ignoreSize) {
[cond appendString:@" AND "];
[cond appendString:sizeCond];
[args addObjectsFromArray:sizeArgs];
}

[cond appendString:@")"];
}
Expand Down Expand Up @@ -684,9 +686,6 @@ - (PHFetchOptions *)getAssetOptions:(int)type filterOption:(PMFilterOptionGroup
[cond insertString:@"(" atIndex:0];
[cond appendString:@")"];

// [cond appendString:@" AND ( creationDate <= %@ )"];
// [args addObject:date];

PMDateOption *dateOption = optionGroup.dateOption;
[cond appendString:[dateOption dateCond]];
[args addObjectsFromArray:[dateOption dateArgs]];
Expand Down
5 changes: 5 additions & 0 deletions lib/src/filter/filter_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ class SizeConstraint {
final int minHeight;
final int maxHeight;

/// When set to true, all constraints are ignored and all sizes of images are displayed.
final bool ignoreSize;

const SizeConstraint({
this.minWidth = 0,
this.maxWidth = 100000,
this.minHeight = 0,
this.maxHeight = 100000,
this.ignoreSize = false,
});

Map<String, dynamic> toMap() {
Expand All @@ -90,6 +94,7 @@ class SizeConstraint {
"maxWidth": maxWidth,
"minHeight": minHeight,
"maxHeight": maxHeight,
"ignoreSize": ignoreSize,
};
}
}
Expand Down

0 comments on commit aefded2

Please sign in to comment.