Skip to content

Commit

Permalink
open shared chats for continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Jul 29, 2024
1 parent cd18bdf commit 5e7f3d2
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ChatDetailFragment : Fragment() {
private var aiModel: String = ""
private lateinit var mRealm: Realm
var user: RealmUserModel? = null
var newsId: String? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -53,6 +54,9 @@ class ChatDetailFragment : Fragment() {
fragmentChatDetailBinding.recyclerGchat.layoutManager = layoutManager
fragmentChatDetailBinding.recyclerGchat.isNestedScrollingEnabled = true
fragmentChatDetailBinding.recyclerGchat.setHasFixedSize(true)
newsId = arguments?.getString("newsId")
val newsRev = arguments?.getString("newsRev")
val newsConversations = arguments?.getString("conversations")
checkAiProviders()
if (mAdapter.itemCount > 0) {
fragmentChatDetailBinding.recyclerGchat.scrollToPosition(mAdapter.itemCount - 1)
Expand All @@ -64,7 +68,8 @@ class ChatDetailFragment : Fragment() {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.GONE
if (TextUtils.isEmpty("${fragmentChatDetailBinding.editGchatMessage.text}".trim())) {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = getString(R.string.kindly_enter_message)
fragmentChatDetailBinding.textGchatIndicator.text =
getString(R.string.kindly_enter_message)
} else {
val message = "${fragmentChatDetailBinding.editGchatMessage.text}".replace("\n", " ")
mAdapter.addQuery(message)
Expand All @@ -83,7 +88,6 @@ class ChatDetailFragment : Fragment() {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.GONE
}
}

fragmentChatDetailBinding.editGchatMessage.setOnKeyListener { _, _, event ->
if (event.action == KeyEvent.ACTION_DOWN) {
if (event.keyCode == KeyEvent.KEYCODE_ENTER && event.isShiftPressed) {
Expand All @@ -96,45 +100,58 @@ class ChatDetailFragment : Fragment() {
}
false
}

fragmentChatDetailBinding.editGchatMessage.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.GONE
}

override fun afterTextChanged(s: Editable?) {}
})

sharedViewModel.getSelectedChatHistory().observe(viewLifecycleOwner) { conversations ->
mAdapter.clearData()
fragmentChatDetailBinding.editGchatMessage.text.clear()
fragmentChatDetailBinding.textGchatIndicator.visibility = View.GONE
if (conversations.isValid) {
for (conversation in conversations) {
val query = conversation.query
val response = conversation.response
if (query != null) {
mAdapter.addQuery(query)
}
mAdapter.responseSource = ChatAdapter.RESPONSE_SOURCE_SHARED_VIEW_MODEL
if (response != null) {
mAdapter.addResponse(response)
if (newsId != null) {
_id = "$newsId"
_rev = newsRev ?: ""
val conversations = Gson().fromJson(newsConversations, Array<Conversation>::class.java).toList()
for (conversation in conversations) {
val query = conversation.query
val response = conversation.response
if (query != null) {
mAdapter.addQuery(query)
}
mAdapter.responseSource = ChatAdapter.RESPONSE_SOURCE_SHARED_VIEW_MODEL
if (response != null) {
mAdapter.addResponse(response)
}
}
} else {
sharedViewModel.getSelectedChatHistory().observe(viewLifecycleOwner) { conversations ->
mAdapter.clearData()
fragmentChatDetailBinding.editGchatMessage.text.clear()
fragmentChatDetailBinding.textGchatIndicator.visibility = View.GONE
if (conversations.isValid) {
for (conversation in conversations) {
val query = conversation.query
val response = conversation.response
if (query != null) {
mAdapter.addQuery(query)
}
mAdapter.responseSource = ChatAdapter.RESPONSE_SOURCE_SHARED_VIEW_MODEL
if (response != null) {
mAdapter.addResponse(response)
}
}
}
}
}

sharedViewModel.getSelected_id().observe(viewLifecycleOwner) { selectedId ->
_id = selectedId
}

sharedViewModel.getSelected_rev().observe(viewLifecycleOwner) { selectedRev ->
_rev = selectedRev
}
view.post {
clearChatDetail()
sharedViewModel.getSelected_id().observe(viewLifecycleOwner) { selectedId ->
_id = selectedId
}
sharedViewModel.getSelected_rev().observe(viewLifecycleOwner) { selectedRev ->
_rev = selectedRev
}
view.post {
clearChatDetail()
}
}
}

Expand Down Expand Up @@ -166,7 +183,6 @@ class ChatDetailFragment : Fragment() {

fragmentChatDetailBinding.tvGemini.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.disable_color))
fragmentChatDetailBinding.tvGemini.setTextColor(ContextCompat.getColor(requireContext(), R.color.md_black_1000))

clearChatDetail()
fragmentChatDetailBinding.textGchatIndicator.visibility = View.GONE
aiName = getString(R.string.openai)
Expand Down Expand Up @@ -406,10 +422,12 @@ class ChatDetailFragment : Fragment() {
}

private fun clearChatDetail() {
if (::mAdapter.isInitialized) {
mAdapter.clearData()
_id = ""
_rev = ""
if (newsId == null) {
if (::mAdapter.isInitialized) {
mAdapter.clearData()
_id = ""
_rev = ""
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,24 @@ import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.model.RealmUserModel
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.ui.resources.ResourcesFragment
import org.ole.planet.myplanet.ui.news.AdapterNews
import org.ole.planet.myplanet.ui.news.ReplyActivity
import org.ole.planet.myplanet.ui.resources.ResourcesFragment

class CommunityFragment : BaseContainerFragment(), AdapterNews.OnNewsItemClickListener {
private lateinit var fragmentCommunityBinding: FragmentCommunityBinding
private var newList: RealmResults<RealmNews>? = null
override fun addImage(llImage: LinearLayout?) {}
override fun onNewsItemClick(news: RealmNews?) {}

override fun showReply(news: RealmNews?, fromLogin: Boolean) {
if (news != null) {
startActivity(
Intent(activity, ReplyActivity::class.java).putExtra("id", news.id)
.putExtra("fromLogin", fromLogin)
)
startActivity(Intent(activity, ReplyActivity::class.java).putExtra("id", news.id).putExtra("fromLogin", fromLogin))
}
}

var user: RealmUserModel? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
fragmentCommunityBinding = FragmentCommunityBinding.inflate(inflater, container, false)
newList = mRealm.where(RealmNews::class.java).equalTo("docType", "message", Case.INSENSITIVE)
.equalTo("viewableBy", "community", Case.INSENSITIVE)
Expand All @@ -60,9 +56,9 @@ class CommunityFragment : BaseContainerFragment(), AdapterNews.OnNewsItemClickLi
homeItemClickListener?.openCallFragment(ResourcesFragment())
}
newList = mRealm.where(RealmNews::class.java).equalTo("docType", "message", Case.INSENSITIVE)
.equalTo("viewableBy", "community", Case.INSENSITIVE)
.equalTo("createdOn", user?.planetCode, Case.INSENSITIVE).isEmpty("replyTo")
.sort("time", Sort.DESCENDING).findAll()
.equalTo("viewableBy", "community", Case.INSENSITIVE)
.equalTo("createdOn", user?.planetCode, Case.INSENSITIVE).isEmpty("replyTo")
.sort("time", Sort.DESCENDING).findAll()
val orientation = resources.configuration.orientation
changeLayoutManager(orientation)
updatedNewsList(newList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.ole.planet.myplanet.callback.OnHomeItemClickListener
import org.ole.planet.myplanet.databinding.FragmentServicesBinding
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmMyTeam
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.ui.team.BaseTeamFragment
import org.ole.planet.myplanet.ui.team.TeamDetailFragment
Expand Down Expand Up @@ -59,6 +60,8 @@ class ServicesFragment : BaseTeamFragment() {
}
}

override fun onNewsItemClick(news: RealmNews?) {}

private fun setRecyclerView(links: RealmResults<RealmMyTeam>?) {
fragmentServicesBinding.llServices.removeAllViews()
links?.forEach { team ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.google.gson.*
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.*
import org.ole.planet.myplanet.model.RealmMeetup
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.ui.mymeetup.AdapterMeetup
import org.ole.planet.myplanet.ui.team.BaseTeamFragment
import org.ole.planet.myplanet.utilities.*
Expand Down Expand Up @@ -138,6 +139,8 @@ class EnterpriseCalendarFragment : BaseTeamFragment() {
refreshCalendarView()
}

override fun onNewsItemClick(news: RealmNews?) {}

private fun showMeetupDetails(dateInMillis: Long) {
fragmentEnterpriseCalendarBinding.meetup.visibility = View.VISIBLE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.ole.planet.myplanet.databinding.AddTransactionBinding
import org.ole.planet.myplanet.databinding.FragmentFinanceBinding
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmMyTeam
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.ui.team.BaseTeamFragment
import org.ole.planet.myplanet.utilities.TimeUtils.formatDateTZ
import org.ole.planet.myplanet.utilities.Utilities
Expand Down Expand Up @@ -117,6 +118,8 @@ class FinanceFragment : BaseTeamFragment() {
showNoData(fragmentFinanceBinding.tvNodata, list?.size, "finances")
}

override fun onNewsItemClick(news: RealmNews?) {}

private fun calculateTotal(list: List<RealmMyTeam>?) {
var debit = 0
var credit = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.ole.planet.myplanet.databinding.FragmentReportsBinding
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmMyTeam
import org.ole.planet.myplanet.model.RealmMyTeam.Companion.insertReports
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.ui.team.BaseTeamFragment
import org.ole.planet.myplanet.utilities.SharedPrefManager
import org.ole.planet.myplanet.utilities.Utilities
Expand Down Expand Up @@ -179,6 +180,8 @@ class ReportsFragment : BaseTeamFragment() {
updatedReportsList(list as RealmResults<RealmMyTeam>)
}

override fun onNewsItemClick(news: RealmNews?) {}

private fun updatedReportsList(results: RealmResults<RealmMyTeam>) {
activity?.runOnUiThread {
adapterReports = AdapterReports(requireContext(), results)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class AdapterNews(var context: Context, private val list: MutableList<RealmNews?
holder.rowNewsBinding.sharedChat.visibility = View.VISIBLE

holder.rowNewsBinding.linearLayout5.setOnClickListener {
clickListener(news.conversations, news.newsId, news.newsRev)
listener?.onNewsItemClick(news)
}
} else {
holder.rowNewsBinding.recyclerGchat.visibility = View.GONE
Expand Down Expand Up @@ -377,6 +377,7 @@ class AdapterNews(var context: Context, private val list: MutableList<RealmNews?
interface OnNewsItemClickListener {
fun showReply(news: RealmNews?, fromLogin: Boolean)
fun addImage(llImage: LinearLayout?)
fun onNewsItemClick(news: RealmNews?)
}

private fun getLabel(s: String): String {
Expand Down
27 changes: 21 additions & 6 deletions app/src/main/java/org/ole/planet/myplanet/ui/news/NewsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.model.RealmNews.Companion.createNews
import org.ole.planet.myplanet.model.RealmUserModel
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.ui.chat.ChatDetailFragment
import org.ole.planet.myplanet.utilities.Constants
import org.ole.planet.myplanet.utilities.Constants.showBetaFeature
import org.ole.planet.myplanet.utilities.FileUtils.openOleFolder
Expand All @@ -35,6 +36,7 @@ class NewsFragment : BaseNewsFragment() {
var user: RealmUserModel? = null
private var updatedNewsList: RealmResults<RealmNews>? = null
private var filteredNewsList: List<RealmNews?> = listOf()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
fragmentNewsBinding = FragmentNewsBinding.inflate(inflater, container, false)
llImage = fragmentNewsBinding.llImages
Expand Down Expand Up @@ -166,12 +168,7 @@ class NewsFragment : BaseNewsFragment() {
.findAll()
getUrlsAndStartDownload(lib, urls)
adapterNews = activity?.let {
AdapterNews(
it,
list?.toMutableList() ?: mutableListOf(),
user,
null
)
AdapterNews(it, list?.toMutableList() ?: mutableListOf(), user, null)
}
adapterNews?.setmRealm(mRealm)
adapterNews?.setFromLogin(requireArguments().getBoolean("fromLogin"))
Expand All @@ -185,6 +182,24 @@ class NewsFragment : BaseNewsFragment() {
}
}

override fun onNewsItemClick(news: RealmNews?) {
val fromLogin = arguments?.getBoolean("fromLogin")
if (fromLogin == false) {
val bundle = Bundle()
bundle.putString("newsId", news?.newsId)
bundle.putString("newsRev", news?.newsRev)
bundle.putString("conversations", news?.conversations)

val chatDetailFragment = ChatDetailFragment()
chatDetailFragment.arguments = bundle

parentFragmentManager.beginTransaction()
.replace(R.id.fragment_container, chatDetailFragment)
.addToBackStack(null)
.commit()
}
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val orientation = newConfig.orientation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ open class ReplyActivity : AppCompatActivity(), OnNewsItemClickListener {
openFolderLauncher.launch(Intent.createChooser(intent, "Open folder"))
}

override fun onNewsItemClick(news: RealmNews?) {}

private fun handleImageSelection(url: Uri?) {
var path: String? = getRealPathFromURI(this, url)
if (TextUtils.isEmpty(path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class MyTeamsDetailFragment : BaseNewsFragment() {
setTeamList()
}

override fun onNewsItemClick(news: RealmNews?) {}

private fun setTeamList() {
val users: List<RealmUserModel> = getUsers(teamId, mRealm, "")
createTeamLog()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.ViewGroup
import androidx.annotation.RequiresApi
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.FragmentPlanBinding
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.utilities.TimeUtils.formatDate

@RequiresApi(Build.VERSION_CODES.O)
Expand Down Expand Up @@ -50,4 +51,6 @@ class PlanFragment : BaseTeamFragment() {
}
fragmentPlanBinding.tvDate.text = getString(R.string.two_strings, getString(R.string.created_on), formatDate(team.createdDate))
}

override fun onNewsItemClick(news: RealmNews?) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.ole.planet.myplanet.databinding.FragmentTeamDetailBinding
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmMyTeam
import org.ole.planet.myplanet.model.RealmMyTeam.Companion.isTeamLeader
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.model.RealmTeamLog
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.utilities.Utilities
Expand Down Expand Up @@ -73,6 +74,8 @@ class TeamDetailFragment : BaseTeamFragment() {
createTeamLog()
}

override fun onNewsItemClick(news: RealmNews?) {}

private fun createTeamLog() {
val user = UserProfileDbHandler(requireContext()).userModel
if (!mRealm.isInTransaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import org.ole.planet.myplanet.databinding.FragmentTeamCourseBinding
import org.ole.planet.myplanet.model.RealmMyCourse
import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.ui.team.BaseTeamFragment

@RequiresApi(Build.VERSION_CODES.O)
Expand All @@ -29,4 +30,6 @@ class TeamCourseFragment : BaseTeamFragment() {
showNoData(fragmentTeamCourseBinding.tvNodata, adapterTeamCourse.itemCount, "teamCourses")
}
}

override fun onNewsItemClick(news: RealmNews?) {}
}
Loading

0 comments on commit 5e7f3d2

Please sign in to comment.