Thomas Richardson commited on
Commit
6958aa4
·
unverified ·
2 Parent(s): 652af9f2c7c81f

Merge pull request #197 from ttt246/feature/planner_android_137

Browse files
Android/app/build.gradle CHANGED
@@ -91,6 +91,7 @@ dependencies {
91
  implementation 'com.google.firebase:firebase-messaging'
92
  implementation 'com.google.firebase:firebase-analytics'
93
  implementation 'com.google.firebase:firebase-firestore-ktx:24.4.5'
 
94
  implementation 'com.google.firebase:firebase-firestore:15.0.0'
95
  implementation 'com.google.firebase:firebase-storage-ktx:20.1.0'
96
  implementation 'com.google.firebase:firebase-messaging-ktx'
 
91
  implementation 'com.google.firebase:firebase-messaging'
92
  implementation 'com.google.firebase:firebase-analytics'
93
  implementation 'com.google.firebase:firebase-firestore-ktx:24.4.5'
94
+ implementation 'com.google.firebase:firebase-database-ktx:19.6.0'
95
  implementation 'com.google.firebase:firebase-firestore:15.0.0'
96
  implementation 'com.google.firebase:firebase-storage-ktx:20.1.0'
97
  implementation 'com.google.firebase:firebase-messaging-ktx'
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/models/chat/AutoTaskModel.kt ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package com.matthaigh27.chatgptwrapper.data.models.chat
2
+
3
+ /**
4
+ * This class denotes a model to store AutoTask data in Firebase realtime database.
5
+ */
6
+
7
+ data class AutoTaskModel(
8
+ val command: Command? = null,
9
+ val result: String? = null,
10
+ val thoughts: Thoughts? = null
11
+ )
12
+
13
+ data class Command(
14
+ val args: Args? = null,
15
+ val name: String? = null
16
+ )
17
+
18
+ data class Args(
19
+ val tool_input: String? = null,
20
+ val file_path: String? = null,
21
+ val text: String? = null,
22
+ val response: String? = null
23
+ )
24
+
25
+ data class Thoughts(
26
+ val criticism: String? = null,
27
+ val plan: String? = null,
28
+ val reasoning: String? = null,
29
+ val speak: String? = null,
30
+ val text: String? = null
31
+ )
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/remote/ApiResource.kt CHANGED
@@ -6,5 +6,5 @@ sealed class ApiResource<T>(
6
  ) {
7
  class Success<T>(data: T) : ApiResource<T>(data)
8
  class Error<T>(message: String, data: T? = null) : ApiResource<T>(data, message)
9
- class Loading<T> : ApiResource<T>()
10
  }
 
6
  ) {
7
  class Success<T>(data: T) : ApiResource<T>(data)
8
  class Error<T>(message: String, data: T? = null) : ApiResource<T>(data, message)
9
+ class Loading<T>(data: T? = null) : ApiResource<T>(data)
10
  }
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/repository/FirebaseRepository.kt CHANGED
@@ -2,6 +2,7 @@ package com.matthaigh27.chatgptwrapper.data.repository
2
 
3
  import com.google.firebase.storage.FirebaseStorage
4
  import com.google.protobuf.Empty
 
5
  import com.matthaigh27.chatgptwrapper.data.remote.ApiResource
6
  import com.matthaigh27.chatgptwrapper.utils.helpers.OnFailure
7
  import com.matthaigh27.chatgptwrapper.utils.helpers.OnSuccess
 
2
 
3
  import com.google.firebase.storage.FirebaseStorage
4
  import com.google.protobuf.Empty
5
+ import com.matthaigh27.chatgptwrapper.data.models.chat.AutoTaskModel
6
  import com.matthaigh27.chatgptwrapper.data.remote.ApiResource
7
  import com.matthaigh27.chatgptwrapper.utils.helpers.OnFailure
8
  import com.matthaigh27.chatgptwrapper.utils.helpers.OnSuccess
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/ChatActivity.kt CHANGED
@@ -3,6 +3,7 @@ package com.matthaigh27.chatgptwrapper.ui.chat.view
3
  import android.Manifest
4
  import android.annotation.SuppressLint
5
  import android.content.pm.PackageManager
 
6
  import android.os.Bundle
7
  import androidx.appcompat.app.AppCompatActivity
8
  import com.matthaigh27.chatgptwrapper.R
@@ -14,12 +15,7 @@ import com.matthaigh27.chatgptwrapper.ui.chat.view.fragments.ChatMainFragment
14
  class ChatActivity : BaseActivity() {
15
 
16
  private val PERMISSIONS_REQUEST_CODE = 1
17
- private val PERMISSIONS = arrayOf(
18
- Manifest.permission.SEND_SMS,
19
- Manifest.permission.READ_CONTACTS,
20
- Manifest.permission.CALL_PHONE,
21
- Manifest.permission.READ_EXTERNAL_STORAGE
22
- )
23
  private val CONFIRM_MESSAGE =
24
  "This app requires SMS, Contacts and Phone " +
25
  "permissions to function properly. " +
@@ -33,7 +29,25 @@ class ChatActivity : BaseActivity() {
33
  }
34
 
35
  private fun requestPermissions() {
36
- val notGrantedPermissions = PERMISSIONS.filter {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  checkSelfPermission(it) != PackageManager.PERMISSION_GRANTED
38
  }
39
 
 
3
  import android.Manifest
4
  import android.annotation.SuppressLint
5
  import android.content.pm.PackageManager
6
+ import android.os.Build
7
  import android.os.Bundle
8
  import androidx.appcompat.app.AppCompatActivity
9
  import com.matthaigh27.chatgptwrapper.R
 
15
  class ChatActivity : BaseActivity() {
16
 
17
  private val PERMISSIONS_REQUEST_CODE = 1
18
+ private lateinit var permissions: Array<String>
 
 
 
 
 
19
  private val CONFIRM_MESSAGE =
20
  "This app requires SMS, Contacts and Phone " +
21
  "permissions to function properly. " +
 
29
  }
30
 
31
  private fun requestPermissions() {
32
+ /**
33
+ * In mobile phones that use Google API 33 or higher, the permission for reading external storage
34
+ * is disabled because the phones don't support the feature.
35
+ */
36
+ permissions = if(Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) {
37
+ arrayOf(
38
+ Manifest.permission.SEND_SMS,
39
+ Manifest.permission.READ_CONTACTS,
40
+ Manifest.permission.CALL_PHONE
41
+ )
42
+ } else {
43
+ arrayOf(
44
+ Manifest.permission.SEND_SMS,
45
+ Manifest.permission.READ_CONTACTS,
46
+ Manifest.permission.CALL_PHONE,
47
+ Manifest.permission.READ_EXTERNAL_STORAGE
48
+ )
49
+ }
50
+ val notGrantedPermissions = permissions.filter {
51
  checkSelfPermission(it) != PackageManager.PERMISSION_GRANTED
52
  }
53
 
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/fragments/ChatMainFragment.kt CHANGED
@@ -42,6 +42,7 @@ import com.matthaigh27.chatgptwrapper.utils.constants.TypeChatWidgetConstants.TY
42
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeChatWidgetConstants.TYPE_WIDGET_SEARCH_CONTACT
43
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeChatWidgetConstants.TYPE_WIDGET_SMS
44
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_ALARM
 
45
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_BROWSER
46
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_CONTACT
47
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_IMAGE
@@ -219,8 +220,7 @@ class ChatMainFragment : Fragment(), OnClickListener {
219
  message: String
220
  ) {
221
  addMessage(
222
- type = TYPE_CHAT_RECEIVE,
223
- content = message
224
  )
225
  }
226
 
@@ -334,8 +334,7 @@ class ChatMainFragment : Fragment(), OnClickListener {
334
  is ApiResource.Success -> {
335
  showLoading(false)
336
  resource.data?.let { data ->
337
- helpPromptList =
338
- responseToHelpPromptList(data.result.content)
339
  }
340
  }
341
 
@@ -348,30 +347,29 @@ class ChatMainFragment : Fragment(), OnClickListener {
348
  }
349
 
350
  private fun fetchImageRelatedness(imageName: String, message: String) {
351
- viewModel.getImageRelatedness(imageName, message)
352
- .observe(viewLifecycleOwner) { resource ->
353
- when (resource) {
354
- is ApiResource.Loading -> {
355
- showLoading(true)
356
- }
357
 
358
- is ApiResource.Success -> {
359
- showLoading(false)
360
- addMessage(
361
- type = TYPE_CHAT_RECEIVE,
362
- content = resource.data?.description,
363
- data = null,
364
- hasImage = true,
365
- image = resource.data?.image
366
- )
367
- }
368
 
369
- is ApiResource.Error -> {
370
- showLoading(false)
371
- addErrorMessage(resource.message!!)
372
- }
373
  }
374
  }
 
375
  }
376
 
377
  private fun sendNotification(message: String) {
@@ -386,8 +384,7 @@ class ChatMainFragment : Fragment(), OnClickListener {
386
  val apiResponse = resource.data
387
  when (apiResponse?.result?.program) {
388
  TYPE_RESPONSE_MESSAGE -> addMessage(
389
- TYPE_CHAT_RECEIVE,
390
- apiResponse.result.content.toString()
391
  )
392
 
393
  TYPE_RESPONSE_BROWSER -> fetchResponseBrowser(apiResponse)
@@ -397,6 +394,7 @@ class ChatMainFragment : Fragment(), OnClickListener {
397
  TYPE_RESPONSE_MAIL_READ -> fetchResponseMail(TYPE_RESPONSE_MAIL_READ)
398
  TYPE_RESPONSE_MAIL_WRITE -> fetchResponseMail(TYPE_RESPONSE_MAIL_WRITE)
399
  TYPE_RESPONSE_MAIL_SEND -> fetchResponseMail(TYPE_RESPONSE_MAIL_SEND)
 
400
  else -> addMessage(TYPE_CHAT_RECEIVE, apiResponse!!.result.toString())
401
  }
402
  }
@@ -485,6 +483,45 @@ class ChatMainFragment : Fragment(), OnClickListener {
485
  }
486
  }
487
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
  private fun initChatInterface() {
489
  chatMessageInterface = object : ChatMessageInterface {
490
  override fun sentSms(phoneNumber: String, message: String) {
@@ -503,29 +540,25 @@ class ChatMainFragment : Fragment(), OnClickListener {
503
 
504
  override fun sentHelpPrompt(prompt: String) {
505
  addMessage(
506
- type = TYPE_CHAT_SENT,
507
- content = prompt
508
  )
509
  }
510
 
511
  override fun canceledHelpPrompt() {
512
  addMessage(
513
- type = TYPE_CHAT_RECEIVE,
514
- content = "You canceled Help prompt."
515
  )
516
  }
517
 
518
  override fun doVoiceCall(phoneNumber: String) {
519
  addMessage(
520
- type = TYPE_CHAT_RECEIVE,
521
- content = "You made a voice call to $phoneNumber"
522
  )
523
  }
524
 
525
  override fun doVideoCall(phoneNumber: String) {
526
  addMessage(
527
- type = TYPE_CHAT_RECEIVE,
528
- content = "You made a video call to $phoneNumber"
529
  )
530
  }
531
 
@@ -534,35 +567,31 @@ class ChatMainFragment : Fragment(), OnClickListener {
534
  this.addProperty(PROPS_WIDGET_DESC, phoneNumber)
535
  }
536
  addMessage(
537
- type = TYPE_CHAT_WIDGET,
538
- content = TYPE_WIDGET_SMS,
539
- data = widgetDesc
540
  )
541
  }
542
 
543
  override fun pickImage(isSuccess: Boolean, data: ByteArray?) {
544
- if (!isSuccess)
545
- addErrorMessage("Fail to pick image")
546
- viewModel.uploadImageToFirebase(data!!)
547
- .observe(viewLifecycleOwner) { resource ->
548
- when (resource) {
549
- is ApiResource.Loading -> {
550
- showLoading(true)
551
- }
552
 
553
- is ApiResource.Success -> {
554
- showLoading(false)
555
- currentSelectedImage = data
556
- currentUploadedImageName = resource.data
557
- isImagePicked = true
558
- }
559
 
560
- is ApiResource.Error -> {
561
- addErrorMessage(resource.message!!)
562
- showLoading(false)
563
- }
564
  }
565
  }
 
566
  }
567
 
568
  override fun setAlarm(hours: Int, minutes: Int, label: String) {
@@ -574,8 +603,7 @@ class ChatMainFragment : Fragment(), OnClickListener {
574
 
575
  override fun cancelAlarm() {
576
  addMessage(
577
- type = TYPE_CHAT_RECEIVE,
578
- content = "You canceled setting an alarm."
579
  )
580
  }
581
  }
 
42
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeChatWidgetConstants.TYPE_WIDGET_SEARCH_CONTACT
43
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeChatWidgetConstants.TYPE_WIDGET_SMS
44
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_ALARM
45
+ import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_AUTO_TASK
46
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_BROWSER
47
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_CONTACT
48
  import com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_IMAGE
 
220
  message: String
221
  ) {
222
  addMessage(
223
+ type = TYPE_CHAT_RECEIVE, content = message
 
224
  )
225
  }
226
 
 
334
  is ApiResource.Success -> {
335
  showLoading(false)
336
  resource.data?.let { data ->
337
+ helpPromptList = responseToHelpPromptList(data.result.content)
 
338
  }
339
  }
340
 
 
347
  }
348
 
349
  private fun fetchImageRelatedness(imageName: String, message: String) {
350
+ viewModel.getImageRelatedness(imageName, message).observe(viewLifecycleOwner) { resource ->
351
+ when (resource) {
352
+ is ApiResource.Loading -> {
353
+ showLoading(true)
354
+ }
 
355
 
356
+ is ApiResource.Success -> {
357
+ showLoading(false)
358
+ addMessage(
359
+ type = TYPE_CHAT_RECEIVE,
360
+ content = resource.data?.description,
361
+ data = null,
362
+ hasImage = true,
363
+ image = resource.data?.image
364
+ )
365
+ }
366
 
367
+ is ApiResource.Error -> {
368
+ showLoading(false)
369
+ addErrorMessage(resource.message!!)
 
370
  }
371
  }
372
+ }
373
  }
374
 
375
  private fun sendNotification(message: String) {
 
384
  val apiResponse = resource.data
385
  when (apiResponse?.result?.program) {
386
  TYPE_RESPONSE_MESSAGE -> addMessage(
387
+ TYPE_CHAT_RECEIVE, apiResponse.result.content.asString
 
388
  )
389
 
390
  TYPE_RESPONSE_BROWSER -> fetchResponseBrowser(apiResponse)
 
394
  TYPE_RESPONSE_MAIL_READ -> fetchResponseMail(TYPE_RESPONSE_MAIL_READ)
395
  TYPE_RESPONSE_MAIL_WRITE -> fetchResponseMail(TYPE_RESPONSE_MAIL_WRITE)
396
  TYPE_RESPONSE_MAIL_SEND -> fetchResponseMail(TYPE_RESPONSE_MAIL_SEND)
397
+ TYPE_RESPONSE_AUTO_TASK -> fetchResponseAutoTask(apiResponse)
398
  else -> addMessage(TYPE_CHAT_RECEIVE, apiResponse!!.result.toString())
399
  }
400
  }
 
483
  }
484
  }
485
 
486
+ /**
487
+ * This function is implemented to fetch the AutoTask data from the Firebase Realtime Database.
488
+ * If the 'result' property is present, its value will be displayed.
489
+ * if not, then the value of the 'thoughts' property will be exhibited.
490
+ */
491
+ private fun fetchResponseAutoTask(apiResponse: ApiResponse<CommonResult>) {
492
+ val referenceUrl = apiResponse.result.content.asString
493
+ viewModel.getAutoTaskRealtimeData(
494
+ referenceUrl
495
+ ).observe(viewLifecycleOwner) { resource ->
496
+ when (resource) {
497
+ is ApiResource.Loading -> {
498
+ resource.data?.let { data ->
499
+ if (data.result == null) {
500
+ data.thoughts?.let { thoughts ->
501
+ addMessage(
502
+ type = TYPE_CHAT_RECEIVE,
503
+ content = thoughts.speak
504
+ )
505
+ }
506
+ } else {
507
+ addMessage(
508
+ type = TYPE_CHAT_RECEIVE,
509
+ content = data.result
510
+ )
511
+ }
512
+ }
513
+ }
514
+
515
+ is ApiResource.Success -> {
516
+ addMessage(TYPE_CHAT_RECEIVE, "Task is finished.")
517
+ }
518
+
519
+ is ApiResource.Error -> {
520
+ }
521
+ }
522
+ }
523
+ }
524
+
525
  private fun initChatInterface() {
526
  chatMessageInterface = object : ChatMessageInterface {
527
  override fun sentSms(phoneNumber: String, message: String) {
 
540
 
541
  override fun sentHelpPrompt(prompt: String) {
542
  addMessage(
543
+ type = TYPE_CHAT_SENT, content = prompt
 
544
  )
545
  }
546
 
547
  override fun canceledHelpPrompt() {
548
  addMessage(
549
+ type = TYPE_CHAT_RECEIVE, content = "You canceled Help prompt."
 
550
  )
551
  }
552
 
553
  override fun doVoiceCall(phoneNumber: String) {
554
  addMessage(
555
+ type = TYPE_CHAT_RECEIVE, content = "You made a voice call to $phoneNumber"
 
556
  )
557
  }
558
 
559
  override fun doVideoCall(phoneNumber: String) {
560
  addMessage(
561
+ type = TYPE_CHAT_RECEIVE, content = "You made a video call to $phoneNumber"
 
562
  )
563
  }
564
 
 
567
  this.addProperty(PROPS_WIDGET_DESC, phoneNumber)
568
  }
569
  addMessage(
570
+ type = TYPE_CHAT_WIDGET, content = TYPE_WIDGET_SMS, data = widgetDesc
 
 
571
  )
572
  }
573
 
574
  override fun pickImage(isSuccess: Boolean, data: ByteArray?) {
575
+ if (!isSuccess) addErrorMessage("Fail to pick image")
576
+ viewModel.uploadImageToFirebase(data!!).observe(viewLifecycleOwner) { resource ->
577
+ when (resource) {
578
+ is ApiResource.Loading -> {
579
+ showLoading(true)
580
+ }
 
 
581
 
582
+ is ApiResource.Success -> {
583
+ showLoading(false)
584
+ currentSelectedImage = data
585
+ currentUploadedImageName = resource.data
586
+ isImagePicked = true
587
+ }
588
 
589
+ is ApiResource.Error -> {
590
+ addErrorMessage(resource.message!!)
591
+ showLoading(false)
 
592
  }
593
  }
594
+ }
595
  }
596
 
597
  override fun setAlarm(hours: Int, minutes: Int, label: String) {
 
603
 
604
  override fun cancelAlarm() {
605
  addMessage(
606
+ type = TYPE_CHAT_RECEIVE, content = "You canceled setting an alarm."
 
607
  )
608
  }
609
  }
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/viewmodel/ChatViewModel.kt CHANGED
@@ -3,8 +3,15 @@ package com.matthaigh27.chatgptwrapper.ui.chat.viewmodel
3
  import android.util.Log
4
  import androidx.lifecycle.MutableLiveData
5
  import androidx.lifecycle.ViewModel
 
 
 
 
 
 
6
  import com.matthaigh27.chatgptwrapper.RisingApplication.Companion.appContext
7
  import com.matthaigh27.chatgptwrapper.data.local.entity.ImageEntity
 
8
  import com.matthaigh27.chatgptwrapper.data.models.chat.ImageRelatenessModel
9
  import com.matthaigh27.chatgptwrapper.data.remote.ApiResource
10
  import com.matthaigh27.chatgptwrapper.data.remote.requests.ImageRelatednessApiRequest
@@ -14,10 +21,10 @@ import com.matthaigh27.chatgptwrapper.data.remote.requests.TrainImageApiRequest
14
  import com.matthaigh27.chatgptwrapper.data.remote.responses.ApiResponse
15
  import com.matthaigh27.chatgptwrapper.data.remote.responses.results.CommonResult
16
  import com.matthaigh27.chatgptwrapper.data.remote.responses.results.HelpCommandResult
17
- import com.matthaigh27.chatgptwrapper.data.remote.responses.results.ImageRelatenessResult
18
  import com.matthaigh27.chatgptwrapper.data.repository.FirebaseRepository
19
  import com.matthaigh27.chatgptwrapper.data.repository.RemoteRepository
20
  import com.matthaigh27.chatgptwrapper.data.repository.RoomRepository
 
21
  import com.matthaigh27.chatgptwrapper.utils.helpers.chat.ContactHelper.getChangedContacts
22
  import com.matthaigh27.chatgptwrapper.utils.helpers.chat.ContactHelper.getContacts
23
  import com.matthaigh27.chatgptwrapper.utils.helpers.chat.ImageHelper.getBytesFromPath
@@ -223,7 +230,8 @@ class ChatViewModel : ViewModel() {
223
  FirebaseRepository.downloadImageWithName(
224
  name = resultImageName,
225
  onSuccess = { response ->
226
- resource.value = ApiResource.Success(ImageRelatenessModel(response, resultImageDesc))
 
227
  }, onFailure = { throwable ->
228
  resource.value = ApiResource.Error(throwable)
229
  }
@@ -234,4 +242,71 @@ class ChatViewModel : ViewModel() {
234
 
235
  return resource
236
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  }
 
3
  import android.util.Log
4
  import androidx.lifecycle.MutableLiveData
5
  import androidx.lifecycle.ViewModel
6
+ import com.google.firebase.database.ChildEventListener
7
+ import com.google.firebase.database.DataSnapshot
8
+ import com.google.firebase.database.DatabaseError
9
+ import com.google.firebase.database.ktx.database
10
+ import com.google.firebase.database.ktx.getValue
11
+ import com.google.firebase.ktx.Firebase
12
  import com.matthaigh27.chatgptwrapper.RisingApplication.Companion.appContext
13
  import com.matthaigh27.chatgptwrapper.data.local.entity.ImageEntity
14
+ import com.matthaigh27.chatgptwrapper.data.models.chat.AutoTaskModel
15
  import com.matthaigh27.chatgptwrapper.data.models.chat.ImageRelatenessModel
16
  import com.matthaigh27.chatgptwrapper.data.remote.ApiResource
17
  import com.matthaigh27.chatgptwrapper.data.remote.requests.ImageRelatednessApiRequest
 
21
  import com.matthaigh27.chatgptwrapper.data.remote.responses.ApiResponse
22
  import com.matthaigh27.chatgptwrapper.data.remote.responses.results.CommonResult
23
  import com.matthaigh27.chatgptwrapper.data.remote.responses.results.HelpCommandResult
 
24
  import com.matthaigh27.chatgptwrapper.data.repository.FirebaseRepository
25
  import com.matthaigh27.chatgptwrapper.data.repository.RemoteRepository
26
  import com.matthaigh27.chatgptwrapper.data.repository.RoomRepository
27
+ import com.matthaigh27.chatgptwrapper.utils.constants.CommonConstants.FIREBASE_DATABASE_URL
28
  import com.matthaigh27.chatgptwrapper.utils.helpers.chat.ContactHelper.getChangedContacts
29
  import com.matthaigh27.chatgptwrapper.utils.helpers.chat.ContactHelper.getContacts
30
  import com.matthaigh27.chatgptwrapper.utils.helpers.chat.ImageHelper.getBytesFromPath
 
230
  FirebaseRepository.downloadImageWithName(
231
  name = resultImageName,
232
  onSuccess = { response ->
233
+ resource.value =
234
+ ApiResource.Success(ImageRelatenessModel(response, resultImageDesc))
235
  }, onFailure = { throwable ->
236
  resource.value = ApiResource.Error(throwable)
237
  }
 
242
 
243
  return resource
244
  }
245
+
246
+ /**
247
+ * This function is used to retrieve real-time data for the auto task.
248
+ * Whenever data in the Firebase real-time database changes,
249
+ * the data is converted to the AutoTaskModel and sent back to the View.
250
+ */
251
+ fun getAutoTaskRealtimeData(referencePath: String): MutableLiveData<ApiResource<AutoTaskModel>> {
252
+ val resource: MutableLiveData<ApiResource<AutoTaskModel>> =
253
+ MutableLiveData()
254
+ val database = Firebase.database(FIREBASE_DATABASE_URL)
255
+ resource.value = ApiResource.Loading()
256
+ database.getReference(referencePath).addChildEventListener(object : ChildEventListener {
257
+ override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
258
+ if (snapshot.exists()) {
259
+ try {
260
+ val data = snapshot.getValue<AutoTaskModel>()
261
+ data?.let {
262
+ data.command?.let { command ->
263
+ if(command.name == "finish") {
264
+ resource.value = ApiResource.Success(data)
265
+ } else {
266
+ resource.value = ApiResource.Loading(data)
267
+ }
268
+ }
269
+ data.result?.let {
270
+ resource.value = ApiResource.Loading(data)
271
+ }
272
+
273
+ }
274
+ } catch (exception: Exception) {
275
+ resource.value = ApiResource.Error(exception.toString())
276
+ }
277
+ }
278
+ }
279
+
280
+ override fun onChildChanged(
281
+ snapshot: DataSnapshot, previousChildName: String?
282
+ ) {
283
+ if (snapshot.exists()) {
284
+ try {
285
+ val data = snapshot.getValue<AutoTaskModel>()
286
+ data?.let {
287
+ data.command?.let { command ->
288
+ if(command.name == "finish") {
289
+ resource.value = ApiResource.Success(data)
290
+ } else {
291
+ resource.value = ApiResource.Loading(data)
292
+ }
293
+ }
294
+ data.result?.let {
295
+ resource.value = ApiResource.Loading(data)
296
+ }
297
+ }
298
+ } catch (exception: Exception) {
299
+ resource.value = ApiResource.Error(exception.toString())
300
+ }
301
+ }
302
+ }
303
+
304
+ override fun onChildRemoved(snapshot: DataSnapshot) {}
305
+ override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {}
306
+ override fun onCancelled(error: DatabaseError) {
307
+ resource.value = ApiResource.Error(error.toException().toString())
308
+ }
309
+ })
310
+ return resource
311
+ }
312
  }
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/utils/constants/CommonConstants.kt CHANGED
@@ -4,6 +4,7 @@ import com.matthaigh27.chatgptwrapper.BuildConfig
4
 
5
  object CommonConstants {
6
  val API_BASE_URL = BuildConfig.BASE_URL
 
7
 
8
  val HELP_COMMAND_ERROR_NO_MAIN = "no main command"
9
  val HELP_COMMAND_ERROR_NO_INVALID_FORMAT = "Invalid Command Format"
 
4
 
5
  object CommonConstants {
6
  val API_BASE_URL = BuildConfig.BASE_URL
7
+ val FIREBASE_DATABASE_URL = "https://test3-83ffc-default-rtdb.firebaseio.com"
8
 
9
  val HELP_COMMAND_ERROR_NO_MAIN = "no main command"
10
  val HELP_COMMAND_ERROR_NO_INVALID_FORMAT = "Invalid Command Format"
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/utils/constants/TypeResponseConstants.kt CHANGED
@@ -13,4 +13,5 @@ object TypeResponseConstants {
13
  val TYPE_RESPONSE_MAIL_READ = "reademail"
14
  val TYPE_RESPONSE_MAIL_WRITE = "writeemail"
15
  val TYPE_RESPONSE_MAIL_SEND = "sendemail"
 
16
  }
 
13
  val TYPE_RESPONSE_MAIL_READ = "reademail"
14
  val TYPE_RESPONSE_MAIL_WRITE = "writeemail"
15
  val TYPE_RESPONSE_MAIL_SEND = "sendemail"
16
+ val TYPE_RESPONSE_AUTO_TASK = "autotask"
17
  }