forked from organicmaps/organicmaps
[android] Implemented toRooms method (#13575)
* [android] Implemented toRooms method * [android] Implemented toCounts method * [android] Added serialization for mAgeOfChildrenList field
This commit is contained in:
parent
e46972bfcb
commit
6b0b4049d6
3 changed files with 129 additions and 10 deletions
|
@ -515,7 +515,7 @@ public:
|
|||
m_roomsId = env->GetFieldID(m_bookingFilterParamsClass, "mRooms",
|
||||
"[Lcom/mapswithme/maps/search/BookingFilterParams$Room;");
|
||||
m_roomAdultsCountId = env->GetFieldID(m_roomClass, "mAdultsCount", "I");
|
||||
m_roomAgeOfChildrenId = env->GetFieldID(m_roomClass, "mAgeOfChildren", "[I");
|
||||
m_roomAgeOfChildrenId = env->GetMethodID(m_roomClass, "getAgeOfChildren", "()[I");
|
||||
|
||||
m_initialized = true;
|
||||
}
|
||||
|
@ -550,7 +550,7 @@ public:
|
|||
auto & room = orderingParams.m_rooms[i];
|
||||
room.SetAdultsCount(static_cast<uint8_t>(env->GetIntField(jroom, m_roomAdultsCountId)));
|
||||
|
||||
auto const childrenObject = env->GetObjectField(jroom, m_roomAgeOfChildrenId);
|
||||
auto const childrenObject = env->CallObjectMethod(jroom, m_roomAgeOfChildrenId);
|
||||
if (childrenObject != nullptr)
|
||||
{
|
||||
auto const children = static_cast<jintArray>(childrenObject);
|
||||
|
@ -626,7 +626,7 @@ private:
|
|||
jfieldID m_checkoutMillisecId = nullptr;
|
||||
jfieldID m_roomsId = nullptr;
|
||||
jfieldID m_roomAdultsCountId = nullptr;
|
||||
jfieldID m_roomAgeOfChildrenId = nullptr;
|
||||
jmethodID m_roomAgeOfChildrenId = nullptr;
|
||||
|
||||
bool m_initialized = false;
|
||||
} g_bookingBuilder;
|
||||
|
|
|
@ -2,11 +2,14 @@ package com.mapswithme.maps.search;
|
|||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.mapswithme.util.ConnectionState;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BookingFilterParams implements Parcelable
|
||||
{
|
||||
protected BookingFilterParams(Parcel in)
|
||||
|
@ -51,6 +54,41 @@ public class BookingFilterParams implements Parcelable
|
|||
private int mAdultsCount;
|
||||
@Nullable
|
||||
private int[] mAgeOfChildren;
|
||||
@NonNull
|
||||
private final List<Integer> mAgeOfChildrenList = new ArrayList<>();
|
||||
|
||||
Room()
|
||||
{
|
||||
// No op.
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public int[] getAgeOfChildren()
|
||||
{
|
||||
if (!mAgeOfChildrenList.isEmpty())
|
||||
{
|
||||
mAgeOfChildren = new int[mAgeOfChildrenList.size()];
|
||||
for (int i = 0; i < mAgeOfChildren.length; i++)
|
||||
mAgeOfChildren[i] = mAgeOfChildrenList.get(i);
|
||||
}
|
||||
|
||||
return mAgeOfChildren;
|
||||
}
|
||||
|
||||
public int getAdultsCount()
|
||||
{
|
||||
return mAdultsCount;
|
||||
}
|
||||
|
||||
void incrementAdultsCount()
|
||||
{
|
||||
mAdultsCount++;
|
||||
}
|
||||
|
||||
public void addAge(int age)
|
||||
{
|
||||
mAgeOfChildrenList.add(age);
|
||||
}
|
||||
|
||||
Room(int adultsCount, @Nullable int[] ageOfChildren)
|
||||
{
|
||||
|
@ -62,6 +100,7 @@ public class BookingFilterParams implements Parcelable
|
|||
{
|
||||
mAdultsCount = in.readInt();
|
||||
mAgeOfChildren = in.createIntArray();
|
||||
in.readList(mAgeOfChildrenList, Integer.class.getClassLoader());
|
||||
}
|
||||
|
||||
public static final Creator<Room> CREATOR = new Creator<Room>()
|
||||
|
@ -90,6 +129,7 @@ public class BookingFilterParams implements Parcelable
|
|||
{
|
||||
dest.writeInt(mAdultsCount);
|
||||
dest.writeIntArray(mAgeOfChildren);
|
||||
dest.writeList(mAgeOfChildrenList);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,6 +29,8 @@ import java.util.Locale;
|
|||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.mapswithme.maps.search.BookingFilterParams.Room;
|
||||
|
||||
public class FilterUtils
|
||||
{
|
||||
public static final int REQ_CODE_NO_NETWORK_CONNECTION_DIALOG = 301;
|
||||
|
@ -36,6 +38,8 @@ public class FilterUtils
|
|||
private static final int MAX_CHECKIN_WINDOW_IN_DAYS = 365;
|
||||
private static final String DAY_OF_MONTH_PATTERN = "MMM d";
|
||||
private static final String NO_NETWORK_CONNECTION_DIALOG_TAG = "no_network_connection_dialog";
|
||||
private static final int AGE_OF_CHILD = 7;
|
||||
private static final int AGE_OF_INFANT = 1;
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({ RATING_ANY, RATING_GOOD, RATING_VERYGOOD, RATING_EXCELLENT })
|
||||
|
@ -288,17 +292,92 @@ public class FilterUtils
|
|||
@NonNull
|
||||
public static BookingFilterParams.Room[] toRooms(@Nullable RoomGuestCounts counts)
|
||||
{
|
||||
// TODO: coming soon.
|
||||
BookingFilterParams.Room[] rooms = new BookingFilterParams.Room[1];
|
||||
rooms[0] = BookingFilterParams.Room.DEFAULT;
|
||||
if (counts == null)
|
||||
{
|
||||
final Room[] rooms = new Room[1];
|
||||
rooms[0] = Room.DEFAULT;
|
||||
return rooms;
|
||||
}
|
||||
|
||||
if (counts.getRooms() < 1)
|
||||
throw new AssertionError("Room count must be greater than 1!");
|
||||
|
||||
if (counts.getAdults() < 1)
|
||||
throw new AssertionError("Adults count must be greater than 1!");
|
||||
|
||||
if (counts.getRooms() == 1)
|
||||
{
|
||||
final Room[] rooms = new Room[1];
|
||||
final int[] agesArray = toAgesArray(counts.getInfants(), counts.getChildren());
|
||||
final Room room = new Room(counts.getAdults(), agesArray);
|
||||
rooms[0] = room;
|
||||
return rooms;
|
||||
}
|
||||
|
||||
// Adult count must be not less than room count.
|
||||
int roomCount = Math.min(counts.getAdults(), counts.getRooms());
|
||||
final Room[] rooms = emptyRooms(roomCount);
|
||||
for (int i = 0; i < counts.getAdults(); i++)
|
||||
{
|
||||
int roomIndex = i % roomCount;
|
||||
rooms[roomIndex].incrementAdultsCount();
|
||||
}
|
||||
|
||||
int[] agesArray = toAgesArray(counts.getInfants(), counts.getChildren());
|
||||
for (int i = 0; i < agesArray.length; i++)
|
||||
{
|
||||
int roomIndex = i % roomCount;
|
||||
rooms[roomIndex].addAge(agesArray[i]);
|
||||
}
|
||||
|
||||
return rooms;
|
||||
}
|
||||
|
||||
private static int[] toAgesArray(int infantCount, int childrenCount)
|
||||
{
|
||||
final int size = infantCount + childrenCount;
|
||||
final int[] result = new int[size];
|
||||
int i = 0;
|
||||
for (; i < infantCount; i++)
|
||||
result[i] = AGE_OF_INFANT;
|
||||
for (; i < size; i++)
|
||||
result[i] = AGE_OF_CHILD;
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Room[] emptyRooms(int count)
|
||||
{
|
||||
Room[] rooms = new Room[count];
|
||||
for (int i = 0; i < rooms.length; i++)
|
||||
rooms[i] = new Room();
|
||||
return rooms;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static RoomGuestCounts toCounts(@NonNull BookingFilterParams.Room... roms)
|
||||
public static RoomGuestCounts toCounts(@NonNull BookingFilterParams.Room... rooms)
|
||||
{
|
||||
// TODO: coming soon.
|
||||
return new RoomGuestCounts(5, 5, 5,5);
|
||||
final int roomsCount = rooms.length;
|
||||
int adultsCount = 0;
|
||||
int infantsCount = 0;
|
||||
int childrenCount = 0;
|
||||
for (Room room : rooms)
|
||||
{
|
||||
adultsCount += room.getAdultsCount();
|
||||
int[] ageOfChildren = room.getAgeOfChildren();
|
||||
if (ageOfChildren == null)
|
||||
continue;
|
||||
for (int age : ageOfChildren)
|
||||
{
|
||||
if (age == AGE_OF_INFANT)
|
||||
infantsCount++;
|
||||
else if (age == AGE_OF_CHILD)
|
||||
childrenCount++;
|
||||
else
|
||||
throw new AssertionError("Unexpected age '" + age + "' detected!");
|
||||
}
|
||||
}
|
||||
|
||||
return new RoomGuestCounts(roomsCount, adultsCount, childrenCount, infantsCount);
|
||||
}
|
||||
|
||||
public static class RoomGuestCounts
|
||||
|
|
Loading…
Add table
Reference in a new issue