﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=7.31
@EndOfDesignText@
' Class: 		CLVDragger
' Version: 		1.0.2
' Platforms: 	B4A, B4i, B4J
' Comments: 	Modified by Aeric
Sub Class_Globals
	Private xui As XUI
	#If B4J
	Private fx As JFX
	#End If
	Private list As CustomListView
	Private PressedColor As Int
	Private Top As Int
	Private pnl As B4XView
	Private ListStartY As Int
	#If B4i
	Private dWidth, dBackground, dTextColor As Int
	#End If
End Sub

Public Sub Initialize (clv As CustomListView)
	list = clv
	PressedColor = list.PressedColor
	#If B4i
	dTextColor = list.DefaultTextColor
	dBackground = xui.Color_Transparent
	dWidth = 30dip
	#End If
End Sub

#If B4i
Public Sub SetDefaults (width As Int, backColor As Int, txtColor As Int, bRtnPos As Boolean)
	dWidth = width
	dBackground = backColor
	dTextColor = txtColor
End Sub
#End If

Public Sub Resize
	For i = 0 To list.Size - 1
		Dim p As B4XView = list.GetPanel(i)
		Dim v As B4XView = p.GetView(p.NumberOfViews - 1)
		If v Is Label And v.Tag = list Then
			v.Left = p.Width - 52dip
		End If
	Next
End Sub

Private Sub IsLastViewADragLabel (p As B4XView) As Boolean
	'Log("no of views " & p.NumberOfViews )
	If p.NumberOfViews > 0 Then
		Dim v As B4XView = p.GetView(p.NumberOfViews - 1)
		#if B4A
		If v.IsInitialized = False Then
			Return False
		End If
		If v.Tag = Null Then
			Return False
		End If
		#End If
		If v Is ImageView And v.Tag = Null Then
			Log("image view")
		else If v Is Label And v.Tag = Null Then
			Return False
		End If
		Return v Is Label And v.Tag = list
	End If
	Return False
End Sub

Public Sub AddDragButtons
	list.PressedColor = xui.Color_Transparent
	Dim fnt As B4XFont = xui.CreateMaterialIcons(30)
	For i = 0 To list.Size - 1
		Dim p As B4XView = list.GetPanel(i)
		#Region B4XTodo
		Dim pnl As B4XView = p.GetView(0) 		' pnlItem
		Dim btn1 As B4XView = pnl.GetView(2) 	' lblEdit
		Dim btn2 As B4XView = pnl.GetView(3) 	' lblDelete
		btn1.Visible = False
		btn2.Visible = False
		#End Region
		If IsLastViewADragLabel(p) = False Then
			Dim lbl As Label
			#if B4J
			lbl.Initialize("drag")
			lbl.MouseCursor = fx.Cursors.OPEN_HAND
			#Else
			lbl.Initialize("")
			#End If
			Dim xlbl As B4XView = lbl
			xlbl.Font = fnt
			xlbl.Text = Chr(0xE25D) 'Chr(0xE8FE)
			#If B4i
			xlbl.TextColor = dTextColor
			xlbl.Color = dBackground
			xlbl.SetTextAlignment("CENTER", "CENTER")
			#Else
			xlbl.TextColor = list.DefaultTextColor
			#End If
			xlbl.Tag = list
			#if B4A
			p.AddView(xlbl, p.Width - 52dip, p.Height / 2 - 15dip, 30dip, 30dip)
			Dim r As Reflector
			r.Target = lbl	'DragAndDrop
			r.SetOnTouchListener("Drag_Touch")
			#Else If B4i
			Dim pnDrager As Panel
			pnDrager.Initialize("pnDragger")
			pnDrager.Tag = list
			pnDrager.AddView(xlbl, 0dip, 0dip, dWidth, 30dip)
			pnDrager.Color = Colors.Cyan
			p.AddView(pnDrager, p.Width - dWidth - 52dip, p.Height/2 - 15dip, dWidth, 30dip)
			#Else
			p.AddView(xlbl, p.Width - 52dip, p.Height / 2 - 15dip, 30dip, 30dip)
			#End If
		End If
	Next
End Sub

Public Sub RemoveDragButtons
	For i = 0 To list.Size - 1
		Dim p As B4XView = list.GetPanel(i)
		#Region B4XTodo
		Dim pnl As B4XView = p.GetView(0) 		' pnlItem
		Dim btn1 As B4XView = pnl.GetView(2) 	' lblEdit
		Dim btn2 As B4XView = pnl.GetView(3) 	' lblDelete
		btn1.Visible = True
		btn2.Visible = True
		#End Region
		If IsLastViewADragLabel(p) Then
			p.GetView(p.NumberOfViews - 1).RemoveViewFromParent
		End If
	Next
	list.PressedColor = PressedColor
End Sub

#If B4A
Sub Drag_Touch (ViewTag As Object, Action As Int, X As Float, Y As Float, EventData As Object) As Boolean
	Dim lbl As B4XView = Sender
	Dim list As CustomListView = lbl.Tag

	If Action = 0 Then									'ACTION_DOWN
		Dim r As Reflector
		r.Target = list.sv
		r.RunMethod2("requestDisallowInterceptTouchEvent", True, "java.lang.boolean")
		pnl = list.GetPanel(list.GetItemFromView(lbl)).Parent
		pnl.GetView(0).SetColorAndBorder(xui.Color_Transparent, 3dip, 0xFF503ACD, 0)
		ListStartY = Y + lbl.Top + pnl.Top
		pnl.BringToFront
		Top = pnl.Top
	Else If Action = 1 Then								'ACTION_UP
		Dim index As Int = list.GetItemFromView(lbl)
		pnl = list.GetPanel(index).Parent
		Dim Offset As Int = pnl.Top + pnl.Height / 2
		Dim NewIndex As Int = list.FindIndexFromOffset(Offset)

		Dim UnderlyingItem As CLVItem = list.GetRawListItem(NewIndex)
		If Offset - UnderlyingItem.Offset > UnderlyingItem.Size / 2 Then
			NewIndex = NewIndex + 1
		End If

		Dim ActualItem As B4XView  = pnl.GetView(0)
		ActualItem.RemoveViewFromParent
		ActualItem.SetColorAndBorder(pnl.Color, 0dip, xui.Color_Black, 0)

		Dim RawItem As CLVItem = list.GetRawListItem(index)
		list.RemoveAt(index)
		If NewIndex > index Then 
			NewIndex = NewIndex - 1
		End If
		NewIndex = Max(0, Min(list.Size, NewIndex))
		list.InsertAt(NewIndex, ActualItem, RawItem.Value)
		list.GetRawListItem(NewIndex).TextItem = RawItem.TextItem

	Else If Action = 2 Then 							'ACTION_MOVE
		Dim index As Int = list.GetItemFromView(lbl)
		If pnl.Top < list.sv.ScrollViewOffsetY Then
			list.sv.ScrollViewOffsetY = Max(0, list.sv.ScrollViewOffsetY - 10dip)
		Else If list.sv.ScrollViewOffsetY + list.sv.Height < pnl.Top + pnl.Height Then
			list.sv.ScrollViewOffsetY = list.sv.ScrollViewOffsetY + 10dip
		End If
		Dim ListY As Int = Y + lbl.Top 	+ pnl.Top
		Dim delta As Int = ListY - ListStartY
		pnl.Top = Top + delta
	Else 
		'Log("action " & Action)
	End If
	Return True
End Sub
#Else If B4i
Private Sub pnDragger_Touch(Action As Int, X As Float, Y As Float)
	Dim lbl As B4XView = Sender
	Dim list As CustomListView = lbl.Tag
	
	If Action = 0 Then
		pnl = list.GetPanel(list.GetItemFromView(lbl)).Parent
		pnl.GetView(0).SetColorAndBorder(xui.Color_Transparent, 3dip, 0xFF503ACD, 0)
		ListStartY = Y + lbl.Top + pnl.Top
		pnl.BringToFront
		Top = pnl.Top
		Dim sv As ScrollView = list.sv
		sv.ScrollEnabled=False
		
	Else if Action = 1 Then
		Dim index As Int = list.GetItemFromView(lbl)
		pnl = list.GetPanel(index).Parent
		Dim Offset As Int = pnl.Top + pnl.Height / 2
		Dim NewIndex As Int = list.FindIndexFromOffset(Offset)

		Dim UnderlyingItem As CLVItem = list.GetRawListItem(NewIndex)
		If Offset - UnderlyingItem.Offset > UnderlyingItem.Size / 2 Then
			NewIndex = NewIndex + 1
		End If

		Dim ActualItem As B4XView  = pnl.GetView(0)
		ActualItem.RemoveViewFromParent
		ActualItem.SetColorAndBorder(pnl.Color, 0dip, xui.Color_Black, 0)

		Dim RawItem As CLVItem = list.GetRawListItem(index)
		list.RemoveAt(index)
		If NewIndex > index Then
			NewIndex = NewIndex - 1
		End If
		NewIndex = Max(0, Min(list.Size, NewIndex))
		list.InsertAt(NewIndex, ActualItem, RawItem.Value)
		list.GetRawListItem(NewIndex).TextItem = RawItem.TextItem
		Dim sv As ScrollView = list.sv
		sv.ScrollEnabled=True
		
	else if Action = 2 Then
		Dim index As Int = list.GetItemFromView(lbl)
		If pnl.Top < list.sv.ScrollViewOffsetY Then
			list.sv.ScrollViewOffsetY = Max(0, list.sv.ScrollViewOffsetY - 10dip)
		Else If list.sv.ScrollViewOffsetY + list.sv.Height < pnl.Top + pnl.Height Then
			list.sv.ScrollViewOffsetY = list.sv.ScrollViewOffsetY + 10dip
		End If
		Dim ListY As Int = Y + lbl.Top 	+ pnl.Top
		Dim delta As Int = ListY - ListStartY
		pnl.Top = Top + delta
	End If
End Sub
#Else
Private Sub Drag_MousePressed (EventData As MouseEvent)
	Dim lbl As B4XView = Sender
	Dim list As CustomListView = lbl.Tag
	Dim pnl As B4XView = list.GetPanel(list.GetItemFromView(lbl)).Parent
	pnl.GetView(0).SetColorAndBorder(xui.Color_Transparent, 3dip, 0xFF503ACD, 0)
	Dim ListStartY As Int = EventData.Y + lbl.Top + pnl.Top
	pnl.BringToFront
	Dim Top As Int = pnl.Top
	Do While True
		Wait For (lbl) Drag_MouseDragged (EventData As MouseEvent)
		If pnl.Top < list.sv.ScrollViewOffsetY Then
			list.sv.ScrollViewOffsetY = Max(0, list.sv.ScrollViewOffsetY - 10dip)
		Else If list.sv.ScrollViewOffsetY + list.sv.Height < pnl.Top + pnl.Height Then
			list.sv.ScrollViewOffsetY = list.sv.ScrollViewOffsetY + 10dip
		End If
		Dim ListY As Int = EventData.Y + lbl.Top + pnl.Top
		Dim delta As Int = ListY - ListStartY
		pnl.Top = Top + delta
	Loop
End Sub

Private Sub Drag_MouseReleased (EventData As MouseEvent)
	Dim lbl As B4XView = Sender
	Dim list As CustomListView = lbl.Tag
	Dim index As Int = list.GetItemFromView(lbl)
	Dim pnl As B4XView = list.GetPanel(index).Parent
	Dim Offset As Int = pnl.Top + pnl.Height / 2
	Dim NewIndex As Int = list.FindIndexFromOffset(Offset)
	Dim UnderlyingItem As CLVItem = list.GetRawListItem(NewIndex)
	If Offset - UnderlyingItem.Offset > UnderlyingItem.Size / 2 Then NewIndex = NewIndex + 1
	Dim ActualItem As B4XView = pnl.GetView(0)
	ActualItem.SetColorAndBorder(pnl.Color, 0dip, xui.Color_Black, 0)
	Dim RawItem As CLVItem = list.GetRawListItem(index)
	list.RemoveAt(index)
	If NewIndex > index Then NewIndex = NewIndex - 1
	NewIndex = Max(0, Min(list.Size, NewIndex))
	list.InsertAt(NewIndex, ActualItem, RawItem.Value)
	list.GetRawListItem(NewIndex).TextItem = RawItem.TextItem
End Sub
#End If

'#if B4i
'RemoveClickRecognizer(pnl)
'#End If

'#if B4i
'Private Sub RemoveClickRecognizer (pnl1 As B4XView)
'	Dim no As NativeObject = pnl1.Parent
'	Dim recs As List = no.GetField("gestureRecognizers")
'	For Each rec As Object In recs
'		no.RunMethod("removeGestureRecognizer:", Array(rec))
'	Next
'End Sub
'#End If