-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dataAndAi' into feature/home
# Conflicts: # app/src/main/java/com/example/pa/ui/home/HomeFragment.kt # app/src/main/res/layout/fragment_home.xml # app/src/main/res/layout/fragment_slideshow.xml # app/src/main/res/menu/activity_main_drawer.xml # app/src/main/res/navigation/mobile_navigation.xml # app/src/main/res/values/strings.xml # gradle.properties
- Loading branch information
Showing
19 changed files
with
329 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/example/pa/data/local/LocalDateTimeConverters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.pa.data.local | ||
import androidx.room.TypeConverter | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
class LocalDateTimeConverters { | ||
|
||
@TypeConverter | ||
fun changeString(value: String?): LocalDate? { | ||
return value?.let { | ||
LocalDate.parse(it, DateTimeFormatter.ISO_LOCAL_DATE) | ||
} | ||
} | ||
|
||
@TypeConverter | ||
fun changeLocalDate(date: LocalDate?): String? { | ||
return date?.format(DateTimeFormatter.ISO_LOCAL_DATE) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/src/main/java/com/example/pa/ui/aigenerate/AiGenerateFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.example.pa.ui.aigenerate | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.example.pa.R | ||
import com.example.pa.data.DatabaseRepository | ||
import com.example.pa.data.local.PlannerDatabase | ||
import com.example.pa.databinding.FragmentAiGenerateBinding | ||
|
||
class AiGenerateFragment : Fragment() { | ||
|
||
private var _binding: FragmentAiGenerateBinding? = null | ||
private val binding get() = _binding!! | ||
private lateinit var viewModel: AiGenerateViewModel | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
restorePreviousState: Bundle? | ||
): View { | ||
val database = PlannerDatabase.getDatabase(requireContext()) | ||
val repository = DatabaseRepository(database.taskDao()) | ||
val factory = AiGenerateViewData(repository) | ||
|
||
viewModel = ViewModelProvider(this, factory).get(AiGenerateViewModel::class.java) | ||
|
||
|
||
_binding = FragmentAiGenerateBinding.inflate(inflater, container, false) | ||
val root: View = binding.root | ||
|
||
viewModel.text.observe(viewLifecycleOwner) { responseText -> | ||
binding.aiGenerateResponse.text = responseText | ||
} | ||
|
||
binding.aiGenerateButton.setOnClickListener { | ||
val topicInput = binding.aiGenerateTopicInput.text.toString() | ||
val dateInput = binding.aiGenerateDateInput.text.toString() | ||
val durationInput = binding.aiGenerateDurationInput.text.toString() | ||
|
||
if (topicInput.isNotEmpty() && dateInput.isNotEmpty() && durationInput.isNotEmpty()) { | ||
val durationInt = durationInput.filter { it.isDigit() }.toIntOrNull() ?: 0 | ||
viewModel.fetchChatCompletion(topicInput, dateInput, durationInt) | ||
viewModel.insertTask(topicInput, dateInput, durationInt) | ||
|
||
} else { | ||
binding.aiGenerateResponse.text = getString(R.string.ai_generate_topic_input_hint) | ||
|
||
} | ||
|
||
} | ||
|
||
return root | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
_binding = null | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/example/pa/ui/aigenerate/AiGenerateViewData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.pa.ui.aigenerate | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.example.pa.data.DatabaseRepository | ||
|
||
// Implement the viewModelProvider.Factory | ||
class AiGenerateViewData(private val repository: DatabaseRepository) : ViewModelProvider.Factory { | ||
|
||
//Deliver to the ViewModel creator to create an instance and provide this instance when requested by the ViewModelProvider | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(AiGenerateViewModel::class.java)) { | ||
@Suppress("UNCHECKED_CAST") | ||
return AiGenerateViewModel(repository) as T | ||
} | ||
throw IllegalArgumentException("No correspond ViewModel") | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
app/src/main/java/com/example/pa/ui/aigenerate/AiGenerateViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.example.pa.ui.aigenerate | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.aallam.openai.api.BetaOpenAI | ||
import com.aallam.openai.api.chat.ChatCompletion | ||
import com.aallam.openai.api.chat.ChatCompletionRequest | ||
import com.aallam.openai.api.chat.ChatMessage | ||
import com.aallam.openai.api.chat.ChatRole | ||
import com.aallam.openai.api.model.ModelId | ||
import com.aallam.openai.client.OpenAI | ||
import com.example.pa.BuildConfig | ||
import com.example.pa.data.DatabaseRepository | ||
import com.example.pa.data.local.Tasks | ||
import kotlinx.coroutines.launch | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
class AiGenerateViewModel(private val repository: DatabaseRepository) : ViewModel() { | ||
|
||
private val openAI = OpenAI(BuildConfig.OPEN_AI_KEY) | ||
|
||
private val _text = MutableLiveData<String>() | ||
val text: LiveData<String> = _text | ||
fun insertTask(topic: String, startDate: String, duration: Int) { | ||
// Convert the start date from String to LocalDateTime | ||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||
val startDate = LocalDate.parse(startDate, formatter) | ||
|
||
// Convert duration from String to Integer | ||
val taskDuration = duration | ||
|
||
// Create the Tasks entity | ||
val task = Tasks( | ||
taskId = 0, | ||
taskInput = topic, | ||
startDate = startDate, | ||
taskDuration = taskDuration, | ||
isTaskComplete = false | ||
) | ||
viewModelScope.launch { | ||
repository.insert(task) | ||
} | ||
} | ||
@OptIn(BetaOpenAI::class) | ||
fun fetchChatCompletion(topic: String, date: String, duration: Int) { | ||
viewModelScope.launch { | ||
val schedule = "Create a schedule for the following details: " + | ||
"Topic - $topic, Date - $date, Duration - $duration weeks." | ||
val chatCompletionRequest = ChatCompletionRequest( | ||
model = ModelId("gpt-3.5-turbo"), | ||
messages = listOf( | ||
ChatMessage( | ||
role = ChatRole.System, | ||
content = "You are a helpful assistant!" | ||
), | ||
ChatMessage( | ||
role = ChatRole.User, | ||
content = schedule | ||
) | ||
) | ||
) | ||
|
||
val completion: ChatCompletion = openAI.chatCompletion(chatCompletionRequest) | ||
_text.postValue(completion.choices.first().message?.content ?: "No response") | ||
} | ||
} | ||
} |
Oops, something went wrong.