summaryrefslogtreecommitdiff
path: root/contacts-widget-edit-app/src/WidgetEditView.cpp
blob: 9ea72cd6278274000a115a7568f4a33bfccf7651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2017 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "WidgetEditView.h"
#include "Common/Animator.h"
#include "Common/WidgetItem.h"
#include "App/Path.h"

#include "MultiWidgetLayouts.h"

using namespace Common;
using namespace Common::Model;
using namespace std::placeholders;

WidgetEditView::WidgetEditView(Common::Model::ContactWidgetItems &contactItems,
		bool isEditable)
	: ContactWidgetItemsView(contactItems, isEditable),
	  m_ReleasedItem(nullptr),
	  m_IsItemMoving(false)
{
}

Evas_Object *WidgetEditView::onCreate(Evas_Object *parent)
{
	Evas_Object *layout = elm_layout_add(parent);
	elm_layout_file_set(layout, App::getResourcePath(PATH_MULTI_WIDGET_LAYOUTS).c_str(), LAYOUT_4ITEMS);

	return layout;
}

WidgetItem *WidgetEditView::createItem(int id)
{
	auto item = ContactWidgetItemsView::createItem(id);
	item->setItemGrabCallback(std::bind(&WidgetEditView::onItemGrabbed, this, item));
	item->setItemReleaseCallback(std::bind(&WidgetEditView::onItemReleased, this));
	item->setItemMoveCallback(std::bind(&WidgetEditView::onItemMove, this, _1));

	return item;
}

void WidgetEditView::move(WidgetItem *item)
{
	Evas_Point destPoint = { 0 };
	const Evas_Object *part = edje_object_part_object_get(elm_layout_edje_get(getEvasObject()),
			getPart(m_ReleasedItem->getIndex()));
	evas_object_geometry_get(part, &destPoint.x, &destPoint.y, nullptr, nullptr);

	m_IsItemMoving = true;
	elm_object_part_content_unset(getEvasObject(), getPart(item->getIndex()));

	auto animator = new Animator(item->getEvasObject());
	animator->setAnimationFinishedCallback([this, item] {
		elm_object_part_content_set(getEvasObject(), getPart(item->getIndex()), item->getEvasObject());
		m_IsItemMoving = false;
	});
	animator->moveTo(destPoint, 0.3, ECORE_POS_MAP_CUBIC_BEZIER, { 0.25, 0.46, 0.45, 1.0 });
}

void WidgetEditView::onItemGrabbed(WidgetItem *item)
{
	elm_object_part_content_unset(getEvasObject(), getPart(item->getIndex()));
	m_ReleasedItem = item;
}

void WidgetEditView::onItemReleased()
{
	elm_object_part_content_set(getEvasObject(), getPart(m_ReleasedItem->getIndex()),
			m_ReleasedItem->getEvasObject());

	onReorderingFinished();
}

void WidgetEditView::onItemMove(Evas_Point itemPos)
{
	if (m_IsItemMoving) {
		return;
	}

	for (auto &&item : getWidgetItems()) {
		if (item == m_ReleasedItem) {
			continue;
		}
		if (item && item->hits(itemPos)) {
			move(item);

			size_t newReleasedIndex = item->getIndex();
			size_t oldReleasedIndex = m_ReleasedItem->getIndex();

			item->setIndex(oldReleasedIndex);
			m_ReleasedItem->setIndex(newReleasedIndex);
			break;
		}
	}
}

void WidgetEditView::onReorderingFinished()
{
	ContactWidgetItems::ContactIds ids = { 0 };
	for (auto &&item : getWidgetItems()) {
		const ContactData *contact = nullptr;
		if (item && (contact = item->getContactData())) {
			ids[item->getIndex()] = contact->getId();
		}
	}

	getContactWidgetItems().setIds(ids);
}