Skip to content

Commit 1732b16

Browse files
committed
add swipe functionality and update docs
1 parent 0e2f882 commit 1732b16

19 files changed

Lines changed: 135 additions & 17 deletions

app/src/main/java/com/fredhappyface/ewesticker/ImageKeyboard.kt

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.fredhappyface.ewesticker
33
import android.content.SharedPreferences
44
import android.inputmethodservice.InputMethodService
55
import android.os.Build.VERSION.SDK_INT
6+
import android.view.GestureDetector
7+
import android.view.MotionEvent
68
import android.view.View
79
import android.view.ViewGroup
810
import android.view.inputmethod.EditorInfo
@@ -28,8 +30,11 @@ import com.fredhappyface.ewesticker.utilities.StickerClickListener
2830
import com.fredhappyface.ewesticker.utilities.StickerSender
2931
import com.fredhappyface.ewesticker.utilities.Toaster
3032
import java.io.File
33+
import kotlin.math.abs
3134
import kotlin.math.min
3235

36+
private const val SWIPE_THRESHOLD = 1
37+
private const val SWIPE_VELOCITY_THRESHOLD = 1
3338

3439
/**
3540
* ImageKeyboard class inherits from the InputMethodService class - provides the keyboard
@@ -41,6 +46,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
4146
private lateinit var sharedPreferences: SharedPreferences
4247
private var restoreOnClose = false
4348
private var vertical = false
49+
private var scroll = false
4450
private var iconsPerX = 0
4551
private var iconSize = 0
4652

@@ -67,6 +73,9 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
6773
private var keyboardHeight = 0
6874
private var fullIconSize = 0
6975

76+
private lateinit var gestureDetector: GestureDetector
77+
78+
7079
/**
7180
* When the activity is created...
7281
* - ensure coil can decode (and display) animated images
@@ -94,6 +103,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
94103
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(baseContext)
95104
this.restoreOnClose = this.sharedPreferences.getBoolean("restoreOnClose", false)
96105
this.vertical = this.sharedPreferences.getBoolean("vertical", false)
106+
this.scroll = this.sharedPreferences.getBoolean("scroll", false)
97107
this.iconsPerX = this.sharedPreferences.getInt("iconsPerX", 3)
98108
this.totalIconPadding =
99109
(resources.getDimension(R.dimen.sticker_padding) * 2 * (this.iconsPerX + 1)).toInt()
@@ -141,6 +151,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
141151
*/
142152
override fun onCreateInputView(): View {
143153
val keyboardLayout = View.inflate(baseContext, R.layout.keyboard_layout, null)
154+
gestureDetector = GestureDetector(baseContext, GestureListener())
144155

145156
this.keyboardRoot = keyboardLayout.findViewById(R.id.keyboardRoot)
146157
this.packsList = keyboardLayout.findViewById(R.id.packsList)
@@ -228,7 +239,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
228239
stickers = loadedPacks[packName]?.stickerList ?: return
229240
}
230241
val recyclerView = RecyclerView(this)
231-
val adapter = StickerPackAdapter(iconSize, stickers, this)
242+
val adapter = StickerPackAdapter(iconSize, stickers, this, gestureDetector)
232243
val layoutManager = GridLayoutManager(
233244
this,
234245
iconsPerX,
@@ -320,6 +331,76 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
320331
fSticker.setOnClickListener { this.keyboardRoot.removeView(fullStickerLayout) }
321332
this.keyboardRoot.addView(fullStickerLayout)
322333
}
334+
335+
internal fun switchToPreviousPack() {
336+
// Get a list of sorted pack names
337+
val sortedPackNames = loadedPacks.keys.sorted()
338+
339+
// Find the index of the current active pack
340+
val currentIndex = sortedPackNames.indexOf(activePack)
341+
342+
// Calculate the index of the previous pack, considering wrap-around
343+
val previousIndex = if (currentIndex > 0) currentIndex - 1 else sortedPackNames.size - 1
344+
345+
// Get the name of the previous pack
346+
val previousPack = sortedPackNames[previousIndex]
347+
348+
// Switch to the previous pack layout
349+
switchPackLayout(previousPack)
350+
}
351+
352+
internal fun switchToNextPack() {
353+
// Get a list of sorted pack names
354+
val sortedPackNames = loadedPacks.keys.sorted()
355+
356+
// Find the index of the current active pack
357+
val currentIndex = sortedPackNames.indexOf(activePack)
358+
359+
// Calculate the index of the next pack, considering wrap-around
360+
val nextIndex = (currentIndex + 1) % sortedPackNames.size
361+
362+
// Get the name of the next pack
363+
val nextPack = sortedPackNames[nextIndex]
364+
365+
// Switch to the next pack layout
366+
switchPackLayout(nextPack)
367+
}
368+
369+
370+
private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
371+
override fun onDown(e: MotionEvent): Boolean {
372+
return false
373+
}
374+
375+
override fun onScroll(
376+
e1: MotionEvent,
377+
e2: MotionEvent,
378+
velocityX: Float,
379+
velocityY: Float
380+
): Boolean {
381+
val diffX = e2.x - e1.x
382+
val diffY = e2.y - e1.y
383+
384+
if (
385+
scroll &&
386+
abs(if (vertical) diffX else diffY) > SWIPE_THRESHOLD &&
387+
abs(if (vertical) velocityX else velocityY) > SWIPE_VELOCITY_THRESHOLD
388+
) {
389+
if (diffX > 0) {
390+
// Swipe right
391+
switchToPreviousPack()
392+
} else {
393+
// Swipe left
394+
switchToNextPack()
395+
}
396+
return true
397+
}
398+
399+
return false
400+
}
401+
}
402+
403+
323404
}
324405

325406
fun trimString(str: String): String {
@@ -328,3 +409,4 @@ fun trimString(str: String): String {
328409
}
329410
return str
330411
}
412+

app/src/main/java/com/fredhappyface/ewesticker/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class MainActivity : AppCompatActivity() {
5151
findViewById<SeekBar>(R.id.iconSizeSb).isEnabled = !isChecked
5252
}
5353
toggle(findViewById(R.id.restoreOnClose), "restoreOnClose", false) {}
54+
toggle(findViewById(R.id.scroll), "scroll", false) {}
5455
}
5556

5657
/**

app/src/main/java/com/fredhappyface/ewesticker/adapter/StickerPackAdapter.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fredhappyface.ewesticker.adapter
22

3+
import android.view.GestureDetector
34
import android.view.LayoutInflater
45
import android.view.ViewGroup
56
import androidx.recyclerview.widget.RecyclerView
@@ -12,7 +13,8 @@ import java.io.File
1213
class StickerPackAdapter(
1314
private val iconSize: Int,
1415
private val stickers: Array<File>,
15-
private val listener: StickerClickListener
16+
private val listener: StickerClickListener,
17+
private val gestureDetector: GestureDetector,
1618
) :
1719

1820

@@ -39,6 +41,9 @@ class StickerPackAdapter(
3941
listener.onStickerLongClicked(file)
4042
return@setOnLongClickListener true
4143
}
44+
holder.stickerThumbnail.setOnTouchListener { _, event ->
45+
gestureDetector.onTouchEvent(event)
46+
}
4247
}
4348

4449
override fun getItemCount(): Int = stickers.size

app/src/main/res/layout/activity_main.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@
206206
android:text="@string/options_restore_on_close" />
207207
</LinearLayout>
208208

209+
<LinearLayout
210+
style="@style/widthMatchHeightWrap"
211+
android:orientation="horizontal">
212+
213+
<CheckBox
214+
android:id="@+id/scroll"
215+
style="@style/checkbox" />
216+
217+
<TextView
218+
style="@style/body_text"
219+
android:paddingBottom="@dimen/content_margin"
220+
android:text="@string/options_scroll" />
221+
</LinearLayout>
222+
209223
<LinearLayout
210224
style="@style/widthMatchHeightWrap"
211225
android:orientation="horizontal">

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<string name="options_show_back_button">Show back button in navbar</string>
2323
<string name="options_vertical">Use vertical layout</string>
2424
<string name="options_restore_on_close">Restore previous keyboard on keyboard close</string>
25+
<string name="options_scroll">Enable swipe between packs (perpendicular to scroll direction)</string>
2526
<string name="options_icons_per_x_lbl">"Number of Rows: "</string>
2627
<string name="options_icon_size_status_lbl">"Icon size: "</string>
2728
<!-- Info -->

documentation/tutorials/README.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ sticker collection.
1010
- [Get it on Google Play](#get-it-on-google-play)
1111
- [Download the APK](#download-the-apk)
1212
- [Step 3 - Activate the keyboard](#step-3---activate-the-keyboard)
13-
- [Step 4 - Select Directory with EweSticker (and wait...)](#step-4---select-directory-with-ewesticker-and-wait)
13+
- [Step 4 - Select Directory with EweSticker )](#step-4---select-directory-with-ewesticker-)
1414
- [Step 5 - Send Stickers in your favourite apps](#step-5---send-stickers-in-your-favourite-apps)
1515

1616
## Step 1 - Create Sticker Directory (and transfer to device)
1717

18-
<img src="assets/step1.png" alt="Step 1" width="600">
18+
<img src="assets/make-packs.png" alt="VSCode showing an example sticker pack structure" width="600">
1919

2020
The sticker directory has the following structure:
2121

@@ -78,30 +78,45 @@ Navigate to the releases page by clicking on the badge above.
7878

7979
## Step 3 - Activate the keyboard
8080

81-
1. Click the "LAUNCH SETTINGS" button
81+
Click the `Launch Settings` button
8282

83-
<img src="assets/screenshot-1.png" alt="Step 3.1" width="300">
83+
<img src="assets/enable-import.png" alt="EweSticker UI with 'Launch Settings' button" width="300">
8484

85-
2. Toggle EweSticker on
85+
Toggle EweSticker on
8686

87-
<img src="assets/step3.png" alt="Step 3" width="300">
87+
<img src="assets/sys-enable.png" alt="System UI 'On Screen Keyboard' with EweSticker toggle" width="300">
8888

89-
## Step 4 - Select Directory with EweSticker (and wait...)
89+
## Step 4 - Select Directory with EweSticker )
9090

91-
1. Click the "CHOOSE STICKER SOURCE DIRECTORY" button
91+
Click the `Choose sticker source directory` button
9292

93-
<img src="assets/screenshot-1.png" alt="Step 4.1" width="300">
93+
<img src="assets/enable-import.png" alt="EweSticker UI with 'Choose sticker source directory' button" width="300">
9494

95-
2. Select the sticker directory created in step 1
95+
Select the sticker directory created in step 1
9696

97-
<img src="assets/screenshot-2.png" alt="Step 4.2" width="300">
97+
<img src="assets/sys-import.png" alt="System UI file chooser" width="300">
9898

9999
## Step 5 - Send Stickers in your favourite apps
100100

101-
1. Tap the keyboard switcher icon and select EweSticker
101+
Tap the keyboard switcher icon and select EweSticker
102102

103-
<img src="assets/screenshot-3.png" alt="Step 5" width="300">
103+
<img src="assets/sys-switcher.png" alt="System UI keyboard switcher wit select EweSticker" width="300">
104104

105-
2. Find and send a sticker of your choosing
105+
Find and send a sticker of your choosing
106106

107-
<img src="assets/screenshot-4.png" alt="Step 5.2" width="300">
107+
<img src="assets/few.png" alt="EweSticker UI showing horizontal layout 5 wide x 3 stickers deep" width="300">
108+
109+
110+
**Note:** That you can configure EweSticker to:
111+
112+
- enable/disable the back button
113+
- use the vertical scroll layout
114+
- restore the previous keyboard when the keyboard is closed (through tapping away from a text input)
115+
- enable swipe between sticker packs (perpendicular to scroll direction, i.e. vertical swipe if not using the vertical layout)
116+
- number of rows (between 2 and 6)
117+
- icon size if not in vertical scroll layout
118+
119+
<img src="assets/configure.png" alt="EweSticker UI showing configuration options" width="300">
120+
121+
122+
<img src="assets/many.png" alt="EweSticker UI showing horizontal layout 5 wide x 6 stickers deep" width="300">
77.1 KB
Loading
107 KB
Loading
95.3 KB
Loading
File renamed without changes.

0 commit comments

Comments
 (0)