Merge pull request #197 from ttt246/feature/planner_android_137
Browse filesFiles changed (9)
- Android/app/build.gradle+1
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/models/chat/AutoTaskModel.ktAdded+31
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/remote/ApiResource.kt+1-1
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/repository/FirebaseRepository.kt+1
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/ChatActivity.kt+21-7
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/fragments/ChatMainFragment.kt+85-57
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/viewmodel/ChatViewModel.kt+77-2
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/utils/constants/CommonConstants.kt+1
- Android/app/src/main/java/com/matthaigh27/chatgptwrapper/utils/constants/TypeResponseConstants.kt+1
90 unmodified lines9192939495969790 unmodified lines implementation 'com.google.firebase:firebase-messaging' implementation 'com.google.firebase:firebase-analytics' implementation 'com.google.firebase:firebase-firestore-ktx:24.4.5' implementation 'com.google.firebase:firebase-database-ktx:19.6.0' implementation 'com.google.firebase:firebase-firestore:15.0.0' implementation 'com.google.firebase:firebase-storage-ktx:20.1.0' implementation 'com.google.firebase:firebase-messaging-ktx'12345678910111213141516171819202122232425262728293031package com.matthaigh27.chatgptwrapper.data.models.chat
/** * This class denotes a model to store AutoTask data in Firebase realtime database. */
data class AutoTaskModel( val command: Command? = null, val result: String? = null, val thoughts: Thoughts? = null)
data class Command( val args: Args? = null, val name: String? = null)
data class Args( val tool_input: String? = null, val file_path: String? = null, val text: String? = null, val response: String? = null)
data class Thoughts( val criticism: String? = null, val plan: String? = null, val reasoning: String? = null, val speak: String? = null, val text: String? = null)5 unmodified lines67899105 unmodified lines) { class Success<T>(data: T) : ApiResource<T>(data) class Error<T>(message: String, data: T? = null) : ApiResource<T>(data, message) class Loading<T> : ApiResource<T>() class Loading<T>(data: T? = null) : ApiResource<T>(data)}1 unmodified line23456781 unmodified line
import com.google.firebase.storage.FirebaseStorageimport com.google.protobuf.Emptyimport com.matthaigh27.chatgptwrapper.data.models.chat.AutoTaskModelimport com.matthaigh27.chatgptwrapper.data.remote.ApiResourceimport com.matthaigh27.chatgptwrapper.utils.helpers.OnFailureimport com.matthaigh27.chatgptwrapper.utils.helpers.OnSuccess2 unmodified lines34567895 unmodified lines151617171819202122181920217 unmodified lines29303132333435363738394041424344454647484936505152532 unmodified linesimport android.Manifestimport android.annotation.SuppressLintimport android.content.pm.PackageManagerimport android.os.Buildimport android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport com.matthaigh27.chatgptwrapper.R5 unmodified linesclass ChatActivity : BaseActivity() {
private val PERMISSIONS_REQUEST_CODE = 1 private val PERMISSIONS = arrayOf( Manifest.permission.SEND_SMS, Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE, Manifest.permission.READ_EXTERNAL_STORAGE ) private lateinit var permissions: Array<String> private val CONFIRM_MESSAGE = "This app requires SMS, Contacts and Phone " + "permissions to function properly. " +7 unmodified lines }
private fun requestPermissions() { /** * In mobile phones that use Google API 33 or higher, the permission for reading external storage * is disabled because the phones don't support the feature. */ permissions = if(Build.VERSION.SDK_INT > Build.VERSION_CODES.S_V2) { arrayOf( Manifest.permission.SEND_SMS, Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE ) } else { arrayOf( Manifest.permission.SEND_SMS, Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE, Manifest.permission.READ_EXTERNAL_STORAGE ) } val notGrantedPermissions = PERMISSIONS.filter { val notGrantedPermissions = permissions.filter { checkSelfPermission(it) != PackageManager.PERMISSION_GRANTED }
41 unmodified lines42434445464748171 unmodified lines220221222222223223224225226107 unmodified lines3343353363373383373383393406 unmodified lines3473483493513523533543553563503513523533543553583593603613623633643653663673563573583593603613623633643653663693703713723673683693703713723733743758 unmodified lines3843853863893903873883893903 unmodified lines39439539639739839940082 unmodified lines48348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652712 unmodified lines5405415425065075435445455465475485135145495505515525535545205215555565575585595605275285615625635642 unmodified lines5675685695375385395705715725735745445455465475485495505515755765775785795805815535545555565575585825835845855865875885605615625635895905915925935945955965975 unmodified lines60360460557757860660760860941 unmodified linesimport com.matthaigh27.chatgptwrapper.utils.constants.TypeChatWidgetConstants.TYPE_WIDGET_SEARCH_CONTACTimport com.matthaigh27.chatgptwrapper.utils.constants.TypeChatWidgetConstants.TYPE_WIDGET_SMSimport com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_ALARMimport com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_AUTO_TASKimport com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_BROWSERimport com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_CONTACTimport com.matthaigh27.chatgptwrapper.utils.constants.TypeResponseConstants.TYPE_RESPONSE_IMAGE171 unmodified lines message: String ) { addMessage( type = TYPE_CHAT_RECEIVE, content = message type = TYPE_CHAT_RECEIVE, content = message ) }
107 unmodified lines is ApiResource.Success -> { showLoading(false) resource.data?.let { data -> helpPromptList = responseToHelpPromptList(data.result.content) helpPromptList = responseToHelpPromptList(data.result.content) } }
6 unmodified lines }
private fun fetchImageRelatedness(imageName: String, message: String) { viewModel.getImageRelatedness(imageName, message) .observe(viewLifecycleOwner) { resource -> when (resource) { is ApiResource.Loading -> { showLoading(true) } viewModel.getImageRelatedness(imageName, message).observe(viewLifecycleOwner) { resource -> when (resource) { is ApiResource.Loading -> { showLoading(true) }
is ApiResource.Success -> { showLoading(false) addMessage( type = TYPE_CHAT_RECEIVE, content = resource.data?.description, data = null, hasImage = true, image = resource.data?.image ) } is ApiResource.Success -> { showLoading(false) addMessage( type = TYPE_CHAT_RECEIVE, content = resource.data?.description, data = null, hasImage = true, image = resource.data?.image ) }
is ApiResource.Error -> { showLoading(false) addErrorMessage(resource.message!!) } is ApiResource.Error -> { showLoading(false) addErrorMessage(resource.message!!) } } } }
private fun sendNotification(message: String) {8 unmodified lines val apiResponse = resource.data when (apiResponse?.result?.program) { TYPE_RESPONSE_MESSAGE -> addMessage( TYPE_CHAT_RECEIVE, apiResponse.result.content.toString() TYPE_CHAT_RECEIVE, apiResponse.result.content.asString )
TYPE_RESPONSE_BROWSER -> fetchResponseBrowser(apiResponse)3 unmodified lines TYPE_RESPONSE_MAIL_READ -> fetchResponseMail(TYPE_RESPONSE_MAIL_READ) TYPE_RESPONSE_MAIL_WRITE -> fetchResponseMail(TYPE_RESPONSE_MAIL_WRITE) TYPE_RESPONSE_MAIL_SEND -> fetchResponseMail(TYPE_RESPONSE_MAIL_SEND) TYPE_RESPONSE_AUTO_TASK -> fetchResponseAutoTask(apiResponse) else -> addMessage(TYPE_CHAT_RECEIVE, apiResponse!!.result.toString()) } }82 unmodified lines } }
/** * This function is implemented to fetch the AutoTask data from the Firebase Realtime Database. * If the 'result' property is present, its value will be displayed. * if not, then the value of the 'thoughts' property will be exhibited. */ private fun fetchResponseAutoTask(apiResponse: ApiResponse<CommonResult>) { val referenceUrl = apiResponse.result.content.asString viewModel.getAutoTaskRealtimeData( referenceUrl ).observe(viewLifecycleOwner) { resource -> when (resource) { is ApiResource.Loading -> { resource.data?.let { data -> if (data.result == null) { data.thoughts?.let { thoughts -> addMessage( type = TYPE_CHAT_RECEIVE, content = thoughts.speak ) } } else { addMessage( type = TYPE_CHAT_RECEIVE, content = data.result ) } } }
is ApiResource.Success -> { addMessage(TYPE_CHAT_RECEIVE, "Task is finished.") }
is ApiResource.Error -> { } } } }
private fun initChatInterface() { chatMessageInterface = object : ChatMessageInterface { override fun sentSms(phoneNumber: String, message: String) {12 unmodified lines
override fun sentHelpPrompt(prompt: String) { addMessage( type = TYPE_CHAT_SENT, content = prompt type = TYPE_CHAT_SENT, content = prompt ) }
override fun canceledHelpPrompt() { addMessage( type = TYPE_CHAT_RECEIVE, content = "You canceled Help prompt." type = TYPE_CHAT_RECEIVE, content = "You canceled Help prompt." ) }
override fun doVoiceCall(phoneNumber: String) { addMessage( type = TYPE_CHAT_RECEIVE, content = "You made a voice call to $phoneNumber" type = TYPE_CHAT_RECEIVE, content = "You made a voice call to $phoneNumber" ) }
override fun doVideoCall(phoneNumber: String) { addMessage( type = TYPE_CHAT_RECEIVE, content = "You made a video call to $phoneNumber" type = TYPE_CHAT_RECEIVE, content = "You made a video call to $phoneNumber" ) }
2 unmodified lines this.addProperty(PROPS_WIDGET_DESC, phoneNumber) } addMessage( type = TYPE_CHAT_WIDGET, content = TYPE_WIDGET_SMS, data = widgetDesc type = TYPE_CHAT_WIDGET, content = TYPE_WIDGET_SMS, data = widgetDesc ) }
override fun pickImage(isSuccess: Boolean, data: ByteArray?) { if (!isSuccess) addErrorMessage("Fail to pick image") viewModel.uploadImageToFirebase(data!!) .observe(viewLifecycleOwner) { resource -> when (resource) { is ApiResource.Loading -> { showLoading(true) } if (!isSuccess) addErrorMessage("Fail to pick image") viewModel.uploadImageToFirebase(data!!).observe(viewLifecycleOwner) { resource -> when (resource) { is ApiResource.Loading -> { showLoading(true) }
is ApiResource.Success -> { showLoading(false) currentSelectedImage = data currentUploadedImageName = resource.data isImagePicked = true } is ApiResource.Success -> { showLoading(false) currentSelectedImage = data currentUploadedImageName = resource.data isImagePicked = true }
is ApiResource.Error -> { addErrorMessage(resource.message!!) showLoading(false) } is ApiResource.Error -> { addErrorMessage(resource.message!!) showLoading(false) } } } }
override fun setAlarm(hours: Int, minutes: Int, label: String) {5 unmodified lines
override fun cancelAlarm() { addMessage( type = TYPE_CHAT_RECEIVE, content = "You canceled setting an alarm." type = TYPE_CHAT_RECEIVE, content = "You canceled setting an alarm." ) } }2 unmodified lines345678910111213141516173 unmodified lines2122231724252627282930199 unmodified lines2302312322332262342352362374 unmodified lines2422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113122 unmodified linesimport android.util.Logimport androidx.lifecycle.MutableLiveDataimport androidx.lifecycle.ViewModelimport com.google.firebase.database.ChildEventListenerimport com.google.firebase.database.DataSnapshotimport com.google.firebase.database.DatabaseErrorimport com.google.firebase.database.ktx.databaseimport com.google.firebase.database.ktx.getValueimport com.google.firebase.ktx.Firebaseimport com.matthaigh27.chatgptwrapper.RisingApplication.Companion.appContextimport com.matthaigh27.chatgptwrapper.data.local.entity.ImageEntityimport com.matthaigh27.chatgptwrapper.data.models.chat.AutoTaskModelimport com.matthaigh27.chatgptwrapper.data.models.chat.ImageRelatenessModelimport com.matthaigh27.chatgptwrapper.data.remote.ApiResourceimport com.matthaigh27.chatgptwrapper.data.remote.requests.ImageRelatednessApiRequest3 unmodified linesimport com.matthaigh27.chatgptwrapper.data.remote.responses.ApiResponseimport com.matthaigh27.chatgptwrapper.data.remote.responses.results.CommonResultimport com.matthaigh27.chatgptwrapper.data.remote.responses.results.HelpCommandResultimport com.matthaigh27.chatgptwrapper.data.remote.responses.results.ImageRelatenessResultimport com.matthaigh27.chatgptwrapper.data.repository.FirebaseRepositoryimport com.matthaigh27.chatgptwrapper.data.repository.RemoteRepositoryimport com.matthaigh27.chatgptwrapper.data.repository.RoomRepositoryimport com.matthaigh27.chatgptwrapper.utils.constants.CommonConstants.FIREBASE_DATABASE_URLimport com.matthaigh27.chatgptwrapper.utils.helpers.chat.ContactHelper.getChangedContactsimport com.matthaigh27.chatgptwrapper.utils.helpers.chat.ContactHelper.getContactsimport com.matthaigh27.chatgptwrapper.utils.helpers.chat.ImageHelper.getBytesFromPath199 unmodified lines FirebaseRepository.downloadImageWithName( name = resultImageName, onSuccess = { response -> resource.value = resource.value = ApiResource.Success(ImageRelatenessModel(response, resultImageDesc)) ApiResource.Success(ImageRelatenessModel(response, resultImageDesc)) }, onFailure = { throwable -> resource.value = ApiResource.Error(throwable) }4 unmodified lines
return resource }
/** * This function is used to retrieve real-time data for the auto task. * Whenever data in the Firebase real-time database changes, * the data is converted to the AutoTaskModel and sent back to the View. */ fun getAutoTaskRealtimeData(referencePath: String): MutableLiveData<ApiResource<AutoTaskModel>> { val resource: MutableLiveData<ApiResource<AutoTaskModel>> = MutableLiveData() val database = Firebase.database(FIREBASE_DATABASE_URL) resource.value = ApiResource.Loading() database.getReference(referencePath).addChildEventListener(object : ChildEventListener { override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) { if (snapshot.exists()) { try { val data = snapshot.getValue<AutoTaskModel>() data?.let { data.command?.let { command -> if(command.name == "finish") { resource.value = ApiResource.Success(data) } else { resource.value = ApiResource.Loading(data) } } data.result?.let { resource.value = ApiResource.Loading(data) }
} } catch (exception: Exception) { resource.value = ApiResource.Error(exception.toString()) } } }
override fun onChildChanged( snapshot: DataSnapshot, previousChildName: String? ) { if (snapshot.exists()) { try { val data = snapshot.getValue<AutoTaskModel>() data?.let { data.command?.let { command -> if(command.name == "finish") { resource.value = ApiResource.Success(data) } else { resource.value = ApiResource.Loading(data) } } data.result?.let { resource.value = ApiResource.Loading(data) } } } catch (exception: Exception) { resource.value = ApiResource.Error(exception.toString()) } } }
override fun onChildRemoved(snapshot: DataSnapshot) {} override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {} override fun onCancelled(error: DatabaseError) { resource.value = ApiResource.Error(error.toException().toString()) } }) return resource }}3 unmodified lines456789103 unmodified lines
object CommonConstants { val API_BASE_URL = BuildConfig.BASE_URL val FIREBASE_DATABASE_URL = "https://test3-83ffc-default-rtdb.firebaseio.com"
val HELP_COMMAND_ERROR_NO_MAIN = "no main command" val HELP_COMMAND_ERROR_NO_INVALID_FORMAT = "Invalid Command Format"12 unmodified lines131415161712 unmodified lines val TYPE_RESPONSE_MAIL_READ = "reademail" val TYPE_RESPONSE_MAIL_WRITE = "writeemail" val TYPE_RESPONSE_MAIL_SEND = "sendemail" val TYPE_RESPONSE_AUTO_TASK = "autotask"}Android/app/build.gradle+1
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/models/chat/AutoTaskModel.ktAdded+31
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/remote/ApiResource.kt+1-1
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/data/repository/FirebaseRepository.kt+1
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/ChatActivity.kt+21-7
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/view/fragments/ChatMainFragment.kt+85-57
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/ui/chat/viewmodel/ChatViewModel.kt+77-2
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/utils/constants/CommonConstants.kt+1
Android/app/src/main/java/com/matthaigh27/chatgptwrapper/utils/constants/TypeResponseConstants.kt+1