@@ -29,15 +29,21 @@ import java.util.UUID
2929open class HWRResult (val word : String , val events : Array <PointerEvent >)
3030object 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+
3242data 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
0 commit comments