Skip to content

Commit 3a837f2

Browse files
committed
[HWG] Adding notifications
1 parent 566acea commit 3a837f2

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

samples/hwgeneration/src/main/java/com/myscript/iink/samples/hwgeneration/GenerationViewModel.kt

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,21 @@ import java.util.UUID
2929
open class HWRResult(val word: String, val events: Array<PointerEvent>)
3030
object None: HWRResult("", emptyArray())
3131

32+
data class Message(
33+
val type: Type,
34+
val message: String,
35+
val exception: Exception? = null,
36+
) {
37+
enum class Type { NOTIFICATION, ERROR }
38+
39+
internal val id = UUID.randomUUID()
40+
}
41+
3242
data class GenerationProfile(
3343
val id: PredefinedHandwritingProfileId = PredefinedHandwritingProfileId.DEFAULT,
3444
val profilePath: String? = null) {
3545

3646
companion object {
37-
fun fromString(id: String): GenerationProfile {
38-
return fromId(PredefinedHandwritingProfileId.valueOf(id))
39-
}
40-
4147
fun fromId(id: PredefinedHandwritingProfileId): GenerationProfile {
4248
return GenerationProfile(id, null)
4349
}
@@ -64,6 +70,10 @@ class GenerationViewModel(application: Application, private val engine: Engine)
6470
val hwrResults: LiveData<List<HWRResult>>
6571
get() = _hwrResults
6672

73+
private var _message = MutableLiveData<Message?>(null)
74+
val message: LiveData<Message?>
75+
get() = _message
76+
6777
init {
6878
_isGenerating.value = false
6979

@@ -108,20 +118,29 @@ class GenerationViewModel(application: Application, private val engine: Engine)
108118

109119
withContext(Dispatchers.IO) {
110120
val builder = generator.createHandwritingProfileBuilder()
111-
val profile = if (selection != null) {
112-
builder.createFromSelection(selection)
113-
} else if (iinkFile != null && iinkFile.exists()) {
114-
builder.createFromFile(iinkFile.absolutePath)
115-
} else {
121+
122+
val profile = try {
123+
if (selection != null) {
124+
builder.createFromSelection(selection)
125+
} else if (iinkFile != null && iinkFile.exists()) {
126+
builder.createFromFile(iinkFile.absolutePath)
127+
} else {
128+
return@withContext
129+
}
130+
} catch (e: IllegalArgumentException) {
131+
notify(Message(Message.Type.ERROR, "Error while building profile ${e.message}", e))
116132
return@withContext
117133
}
118134

119135
val profileDirectory = File(getApplication<Application>().filesDir, PROFILE_FOLDER)
120136
if (!profileDirectory.exists()) {
121137
profileDirectory.mkdirs()
122138
}
123-
val profileFile = File(profileDirectory, "${UUID.randomUUID()}.profile")
139+
val profileId = UUID.randomUUID()
140+
val profileFile = File(profileDirectory, "$profileId.profile")
124141
builder.store(profile, profileFile.absolutePath)
142+
143+
notify(Message(Message.Type.NOTIFICATION, "Profile built with ID $profileId"))
125144
}
126145

127146
_isProfileBuilding.value = false
@@ -210,6 +229,25 @@ class GenerationViewModel(application: Application, private val engine: Engine)
210229
return HWRResult(word, events)
211230
}
212231

232+
private fun notify(message: Message) {
233+
synchronized(_message) {
234+
viewModelScope.launch(Dispatchers.Main) {
235+
_message.value = message
236+
}
237+
}
238+
}
239+
240+
fun dismissMessage(message: Message) {
241+
synchronized(_message) {
242+
viewModelScope.launch(Dispatchers.Main) {
243+
val currentError = _message.value
244+
if (currentError?.id == message.id) {
245+
_message.value = null
246+
}
247+
}
248+
}
249+
}
250+
213251
companion object {
214252
private const val LINE_GAP_RATIO = 3
215253

samples/hwgeneration/src/main/java/com/myscript/iink/samples/hwgeneration/MainActivity.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.net.Uri
99
import android.os.Build
1010
import android.os.Build.VERSION_CODES
1111
import android.os.Bundle
12+
import android.util.Log
1213
import android.view.ActionMode
1314
import android.view.LayoutInflater
1415
import android.view.Menu
@@ -181,6 +182,22 @@ class MainActivity : AppCompatActivity() {
181182
actionMode?.invalidate()
182183
binding.readOnlyLayer.visibility = if (isProfileBuilding) View.VISIBLE else View.GONE
183184
}
185+
186+
generationViewModel.message.observe(this) { message ->
187+
if (message == null) {
188+
return@observe
189+
}
190+
Log.d("MainActivity", message.toString(), message.exception)
191+
when (message.type) {
192+
Message.Type.ERROR ->
193+
AlertDialog.Builder(this)
194+
.setMessage(message.message)
195+
.setPositiveButton(android.R.string.ok, null)
196+
.show()
197+
else -> Toast.makeText(applicationContext, message.message, Toast.LENGTH_LONG).show()
198+
}
199+
generationViewModel.dismissMessage(message)
200+
}
184201
}
185202

186203
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@@ -339,8 +356,8 @@ class MainActivity : AppCompatActivity() {
339356

340357
dialogBuilder.setPositiveButton(android.R.string.ok) { _, _ ->
341358
val textSize = inputTextSize.value
342-
val profileId = spinner.selectedItem.toString().uppercase()
343-
generationViewModel.generateHandwriting(inputText.text.toString().trim(), GenerationProfile.fromString(profileId), textSize, x, y, editorView?.width?.toFloat() ?: 0f, editorViewModel.transform())
359+
val profile = spinner.selectedItem as GenerationProfile
360+
generationViewModel.generateHandwriting(inputText.text.toString().trim(), profile, textSize, x, y, editorView?.width?.toFloat() ?: 0f, editorViewModel.transform())
344361
}
345362
dialogBuilder.setNegativeButton(android.R.string.cancel, null)
346363

0 commit comments

Comments
 (0)