Skip to content

Commit

Permalink
Merge 18e04ea into 826edd2
Browse files Browse the repository at this point in the history
  • Loading branch information
sephiroth74 committed Jan 8, 2019
2 parents 826edd2 + 18e04ea commit c4716a3
Show file tree
Hide file tree
Showing 50 changed files with 1,548 additions and 138 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,34 @@ Internally they will switch between `Possible`, `Ended`, `Failed` or `Cancelled`
#### UIContinuousRecognizer
A continuous gesture which will dispatch `Began`, `Changed` and `Ended` state changed events (for instance a pinch gesture, or a rotate gesture).

---

# Installation

### Maven
Add the library dependency:

implementation 'it.sephiroth.android.library.uigestures:uigesture-recognizer-kotlin:**version**'

### JitPack
**Step 1.** Add the JitPack repository to your build file:

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

**Step 2.** Add the dependency to your project's build.gradle file:

dependencies {
implementation 'com.github.sephiroth74:AndroidUIGestureRecognizer:Tag'
}

To See the last release version: https://jitpack.io/private#sephiroth74/AndroidUIGestureRecognizer/

---

# Example

Expand Down Expand Up @@ -119,7 +141,10 @@ Add the library dependency:
}

```
---


# JavaDoc

https://sephiroth74.github.io/AndroidUIGestureRecognizer/it.sephiroth.android.library.uigestures/
https://github.com/sephiroth74/AndroidUIGestureRecognizer/blob/master/docs/it.sephiroth.android.library.uigestures/index.md

15 changes: 12 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
buildToolsVersion "28.0.3"

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
minSdkVersion 21
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0.0"
Expand Down Expand Up @@ -37,11 +37,20 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')

implementation project(':uigesturerecognizer')

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.annotation:annotation:1.0.1'
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation project(':uigesturerecognizer')
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation 'com.jakewharton.timber:timber:4.7.1'
implementation 'com.michaelmuenzer.android:ScrollableNumberPicker:0.2.2'
}
repositories {
mavenCentral()
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
android:name=".MainApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning"
android:name=".MainApplication">
<activity android:name=".BaseTest" />
<activity android:name=".MainActivity">
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_tap"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BaseTest" />

<uses-library
android:name="android.test.base"
android:required="false" />
<uses-library
android:name="android.test.mock"
android:required="false" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,145 @@ package it.sephiroth.android.library.uigestures.demo

import android.os.Bundle
import android.os.Handler
import android.view.ViewGroup
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import it.sephiroth.android.library.uigestures.*
import kotlinx.android.synthetic.main.activity_main.*
import it.sephiroth.android.library.uigestures.demo.fragments.*
import kotlinx.android.synthetic.main.activity_tap.*
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*
import kotlin.reflect.full.primaryConstructor

class MainActivity : AppCompatActivity() {
open class MainActivity : AppCompatActivity() {

private lateinit var mRoot: ViewGroup
private lateinit var mDelegate: UIGestureRecognizerDelegate
private var currentRecognizerClassName: String = ""
private val handler = Handler()
private val delegate = UIGestureRecognizerDelegate()
private val dateFormat = SimpleDateFormat("HH:mm:ss.SSS", Locale.US)
val handler = Handler()
private lateinit var recognizer: UIGestureRecognizer

private val clearTextRunnable: Runnable = Runnable {
textState.text = "Test me!"
}

init {
UIGestureRecognizer.logEnabled = true
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setContentView(R.layout.activity_tap)
setSupportActionBar(toolbar)

setupTouchListener()
setupSpinner()
}

UIGestureRecognizer.logEnabled = BuildConfig.DEBUG
private fun setupTouchListener() {
testView.setOnTouchListener { v, event ->
v.onTouchEvent(event)
delegate.onTouchEvent(v, event)
}
}

@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
private fun setupContent(simpleClassName: String) {
Timber.i("setupContent: $simpleClassName")

mDelegate = UIGestureRecognizerDelegate()
val packageName = UIGestureRecognizer::class.java.`package`.name
Timber.v("package: $packageName")

val recognizer1 = UITapGestureRecognizer(this)
recognizer1.tapsRequired = 1
recognizer1.touchesRequired = 1
recognizer1.actionListener = actionListener
recognizer1.stateListener = stateListener
val kClass = Class.forName("${packageName}.$simpleClassName").kotlin
Timber.v("kClass: ${kClass.simpleName}")

val recognizer2 = UIPanGestureRecognizer(this)
recognizer2.tag = "pan"
recognizer2.requireFailureOf = recognizer1
val newRecognizer = kClass.primaryConstructor?.call(this) as UIGestureRecognizer?

recognizer2.actionListener = actionListener
recognizer2.stateListener = stateListener
newRecognizer?.let { rec ->
var fragment: Fragment? = null

mDelegate.addGestureRecognizer(recognizer1)
mDelegate.addGestureRecognizer(recognizer2)
when (kClass) {
UITapGestureRecognizer::class -> fragment = UITapGestureRecognizerFragment.newInstance(rec)
UIRotateGestureRecognizer::class -> fragment = UIRotateGestureRecognizerFragment.newInstance(rec)
UIPinchGestureRecognizer::class -> fragment = UIPinchGestureRecognizerFragment.newInstance(rec)
UIScreenEdgePanGestureRecognizer::class -> fragment = UIScreenEdgePanGestureRecognizerFragment.newInstance(rec)
UIPanGestureRecognizer::class -> fragment = UIPanGestureRecognizerFragment.newInstance(rec)
UISwipeGestureRecognizer::class -> fragment = UISwipeGestureRecognizerFragment.newInstance(rec)
UILongPressGestureRecognizer::class -> fragment = UILongPressGestureRecognizerFragment.newInstance(rec)
}

fragment?.let { frag ->
title = kClass.simpleName
delegate.clear()
recognizer = rec

mRoot.setGestureDelegate(mDelegate)
supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainer, frag, simpleClassName)
.commit()

mDelegate.shouldReceiveTouch = { true }
mDelegate.shouldBegin = { true }
setupRecognizer(recognizer)
currentRecognizerClassName = simpleClassName

mDelegate.shouldRecognizeSimultaneouslyWithGestureRecognizer = { recognizer, other ->
Timber.v("shouldRecognizeSimultaneouslyWithGestureRecognizer: ${recognizer.tag}, ${other.tag}")
true
} ?: kotlin.run {
}
} ?: kotlin.run {
Toast.makeText(this, "Unable to find ${kClass.simpleName}", Toast.LENGTH_SHORT).show()
}

}

override fun onContentChanged() {
super.onContentChanged()
private fun setupSpinner() {

mRoot = findViewById(R.id.activity_main)
}
recognizerSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>) {}

private val runner = Runnable {
text2.text = ""
text.text = ""
text3.text = ""
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val selected = parent.selectedItem.toString()
setupContent(selected)
}
}
}

private val stateListener =
{ recognizer: UIGestureRecognizer, oldState: UIGestureRecognizer.State?, newState: UIGestureRecognizer.State? ->
text3.text = "${recognizer.javaClass.simpleName}: $oldState --> $newState"

private fun setupRecognizer(recognizer: UIGestureRecognizer) {
recognizer.actionListener = { rec ->
Timber.d("actionListener: ${rec.currentLocationX}, ${rec.currentLocationY}")
testView.drawableHotspotChanged(rec.currentLocationX, rec.currentLocationY)
testView.isPressed = true
testView.performClick()
testView.isPressed = false

val dateTime = dateFormat.format(recognizer.lastEvent!!.eventTime)

textState.append("[$dateTime] ${rec.javaClass.simpleName}: ${rec.state} \n")
textState.append("[$dateTime] location: ${rec.currentLocationX.toInt()}, ${rec.currentLocationY.toInt()}\n")

supportFragmentManager.findFragmentByTag(currentRecognizerClassName)?.let { frag ->
val status = (frag as IRecognizerFragment<*>).getRecognizerStatus()
status?.let {
textState.append("[$dateTime] $it\n")
}
}

private val actionListener = { recognizer: UIGestureRecognizer ->
val dateTime = dateFormat.format(recognizer.lastEvent!!.eventTime)
Timber.d("**********************************************")
Timber.d("onGestureRecognized($recognizer)")
Timber.d("**********************************************")
textState.append("\n")

handler.removeCallbacks(clearTextRunnable)
handler.postDelayed(clearTextRunnable, 4000)

text.text = "${recognizer.javaClass.simpleName}: ${recognizer.state}"
text2.append("[$dateTime] tag: ${recognizer.tag}, state: ${recognizer.state?.name} \n")
text2.append("[coords] ${recognizer.currentLocationX.toInt()}, ${recognizer.currentLocationY.toInt()}\n")

if (recognizer is UIPanGestureRecognizer) {
text2.append("[origin] ${recognizer.startLocationX}, ${recognizer.startLocationY}\n")
} else if (recognizer is UILongPressGestureRecognizer) {
text2.append("[origin] ${recognizer.startLocationX}, ${recognizer.startLocationY}\n")
}

handler.removeCallbacks(runner)
handler.postDelayed(runner, 5000)
recognizer.stateListener =
{ it: UIGestureRecognizer, oldState: UIGestureRecognizer.State?, newState: UIGestureRecognizer.State? ->
Timber.v("state changed: $oldState --> $newState")
}

delegate.addGestureRecognizer(recognizer)

}

}
Loading

0 comments on commit c4716a3

Please sign in to comment.