peterluo3131 commited on
Commit
2c7c81f
·
1 Parent(s): 9adae15

feature(#137): add comments for changed classes and functions

Browse files
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/models/chat/AutoTaskModel.kt CHANGED
@@ -1,6 +1,8 @@
1
  package com.matthaigh27.chatgptwrapper.data.models.chat
2
 
3
- import javax.annotation.ParametersAreNullableByDefault
 
 
4
 
5
  data class AutoTaskModel(
6
  val command: Command? = null,
 
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,
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/ChatActivity.kt CHANGED
@@ -29,6 +29,10 @@ class ChatActivity : BaseActivity() {
29
  }
30
 
31
  private fun requestPermissions() {
 
 
 
 
32
  permissions = if(Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) {
33
  arrayOf(
34
  Manifest.permission.SEND_SMS,
 
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,
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/fragments/ChatMainFragment.kt CHANGED
@@ -2,7 +2,6 @@ package com.matthaigh27.chatgptwrapper.ui.chat.view.fragments
2
 
3
  import android.annotation.SuppressLint
4
  import android.os.Bundle
5
- import android.util.Log
6
  import android.view.KeyEvent
7
  import android.view.LayoutInflater
8
  import android.view.View
@@ -18,16 +17,9 @@ import androidx.fragment.app.Fragment
18
  import androidx.lifecycle.ViewModelProvider
19
  import androidx.recyclerview.widget.LinearLayoutManager
20
  import androidx.recyclerview.widget.RecyclerView
21
- import com.google.firebase.database.ChildEventListener
22
- import com.google.firebase.database.DataSnapshot
23
- import com.google.firebase.database.DatabaseError
24
- import com.google.firebase.database.ktx.database
25
- import com.google.firebase.database.ktx.getValue
26
- import com.google.firebase.ktx.Firebase
27
  import com.google.gson.JsonElement
28
  import com.google.gson.JsonObject
29
  import com.matthaigh27.chatgptwrapper.R
30
- import com.matthaigh27.chatgptwrapper.data.models.chat.AutoTaskModel
31
  import com.matthaigh27.chatgptwrapper.data.models.chat.ChatMessageModel
32
  import com.matthaigh27.chatgptwrapper.data.models.chat.HelpCommandModel
33
  import com.matthaigh27.chatgptwrapper.data.models.chat.HelpPromptModel
@@ -356,28 +348,28 @@ class ChatMainFragment : Fragment(), OnClickListener {
356
 
357
  private fun fetchImageRelatedness(imageName: String, message: String) {
358
  viewModel.getImageRelatedness(imageName, message).observe(viewLifecycleOwner) { resource ->
359
- when (resource) {
360
- is ApiResource.Loading -> {
361
- showLoading(true)
362
- }
363
 
364
- is ApiResource.Success -> {
365
- showLoading(false)
366
- addMessage(
367
- type = TYPE_CHAT_RECEIVE,
368
- content = resource.data?.description,
369
- data = null,
370
- hasImage = true,
371
- image = resource.data?.image
372
- )
373
- }
374
 
375
- is ApiResource.Error -> {
376
- showLoading(false)
377
- addErrorMessage(resource.message!!)
378
- }
379
  }
380
  }
 
381
  }
382
 
383
  private fun sendNotification(message: String) {
@@ -491,6 +483,11 @@ class ChatMainFragment : Fragment(), OnClickListener {
491
  }
492
  }
493
 
 
 
 
 
 
494
  private fun fetchResponseAutoTask(apiResponse: ApiResponse<CommonResult>) {
495
  val referenceUrl = apiResponse.result.content.asString
496
  viewModel.getAutoTaskRealtimeData(
@@ -499,7 +496,7 @@ class ChatMainFragment : Fragment(), OnClickListener {
499
  when (resource) {
500
  is ApiResource.Loading -> {
501
  resource.data?.let { data ->
502
- if(data.result == null) {
503
  data.thoughts?.let { thoughts ->
504
  addMessage(
505
  type = TYPE_CHAT_RECEIVE,
@@ -577,24 +574,24 @@ class ChatMainFragment : Fragment(), OnClickListener {
577
  override fun pickImage(isSuccess: Boolean, data: ByteArray?) {
578
  if (!isSuccess) addErrorMessage("Fail to pick image")
579
  viewModel.uploadImageToFirebase(data!!).observe(viewLifecycleOwner) { resource ->
580
- when (resource) {
581
- is ApiResource.Loading -> {
582
- showLoading(true)
583
- }
584
 
585
- is ApiResource.Success -> {
586
- showLoading(false)
587
- currentSelectedImage = data
588
- currentUploadedImageName = resource.data
589
- isImagePicked = true
590
- }
591
 
592
- is ApiResource.Error -> {
593
- addErrorMessage(resource.message!!)
594
- showLoading(false)
595
- }
596
  }
597
  }
 
598
  }
599
 
600
  override fun setAlarm(hours: Int, minutes: Int, label: String) {
 
2
 
3
  import android.annotation.SuppressLint
4
  import android.os.Bundle
 
5
  import android.view.KeyEvent
6
  import android.view.LayoutInflater
7
  import android.view.View
 
17
  import androidx.lifecycle.ViewModelProvider
18
  import androidx.recyclerview.widget.LinearLayoutManager
19
  import androidx.recyclerview.widget.RecyclerView
 
 
 
 
 
 
20
  import com.google.gson.JsonElement
21
  import com.google.gson.JsonObject
22
  import com.matthaigh27.chatgptwrapper.R
 
23
  import com.matthaigh27.chatgptwrapper.data.models.chat.ChatMessageModel
24
  import com.matthaigh27.chatgptwrapper.data.models.chat.HelpCommandModel
25
  import com.matthaigh27.chatgptwrapper.data.models.chat.HelpPromptModel
 
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) {
 
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(
 
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,
 
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) {
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/viewmodel/ChatViewModel.kt CHANGED
@@ -243,6 +243,11 @@ class ChatViewModel : ViewModel() {
243
  return resource
244
  }
245
 
 
 
 
 
 
246
  fun getAutoTaskRealtimeData(referencePath: String): MutableLiveData<ApiResource<AutoTaskModel>> {
247
  val resource: MutableLiveData<ApiResource<AutoTaskModel>> =
248
  MutableLiveData()
 
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()