Skip to content

Commit

Permalink
add copy and research functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Oct 23, 2024
1 parent 1f9535c commit d9f4213
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class ChatDetailFragment : Fragment() {
val newsRev = arguments?.getString("newsRev")
val newsConversations = arguments?.getString("conversations")
checkAiProviders()
val selectedText = arguments?.getString("selectedText")
if (!selectedText.isNullOrEmpty()) {
// Paste the selected text into the editGchatMessage field
fragmentChatDetailBinding.editGchatMessage.setText(selectedText)

// Simulate a click on the send button
fragmentChatDetailBinding.buttonGchatSend.performClick()
}
if (mAdapter.itemCount > 0) {
fragmentChatDetailBinding.recyclerGchat.scrollToPosition(mAdapter.itemCount - 1)
fragmentChatDetailBinding.recyclerGchat.smoothScrollToPosition(mAdapter.itemCount - 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package org.ole.planet.myplanet.ui.courses

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context.CLIPBOARD_SERVICE
import android.content.Context.MODE_PRIVATE
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.Realm
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.base.BaseContainerFragment
import org.ole.planet.myplanet.callback.OnRatingChangeListener
import org.ole.planet.myplanet.databinding.AddNoteDialogBinding
import org.ole.planet.myplanet.databinding.ChatShareDialogBinding
import org.ole.planet.myplanet.databinding.FragmentCourseDetailBinding
import org.ole.planet.myplanet.databinding.GrandChildRecyclerviewDialogBinding
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmMyCourse
import org.ole.planet.myplanet.model.RealmMyCourse.Companion.getCourseSteps
import org.ole.planet.myplanet.model.RealmMyLibrary
import org.ole.planet.myplanet.model.RealmMyTeam
import org.ole.planet.myplanet.model.RealmRating.Companion.getRatingsById
import org.ole.planet.myplanet.model.RealmStepExam.Companion.getNoOfExam
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.ui.news.ExpandableListAdapter
import org.ole.planet.myplanet.ui.news.GrandChildAdapter
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import org.ole.planet.myplanet.utilities.Markdown.setMarkdownText

class CourseDetailFragment : BaseContainerFragment(), OnRatingChangeListener {
Expand Down Expand Up @@ -58,6 +72,10 @@ class CourseDetailFragment : BaseContainerFragment(), OnRatingChangeListener {
setTextViewVisibility(fragmentCourseDetailBinding.language, courses?.languageOfInstruction, fragmentCourseDetailBinding.ltLanguage)
val markdownContentWithLocalPaths = CourseStepFragment.prependBaseUrlToImages(courses?.description, "file://" + MainApplication.context.getExternalFilesDir(null) + "/ole/")
setMarkdownText(fragmentCourseDetailBinding.description, markdownContentWithLocalPaths)
fragmentCourseDetailBinding.description.setOnLongClickListener {
copyToClipboard("${fragmentCourseDetailBinding.description.getText()}")
true
}
fragmentCourseDetailBinding.noOfExams.text = context?.getString(R.string.number_placeholder, getNoOfExam(cRealm, id))
val resources: List<RealmMyLibrary> = cRealm.where(RealmMyLibrary::class.java).equalTo("courseId", id).equalTo("resourceOffline", false).isNotNull("resourceLocalAddress").findAll()
setResourceButton(resources, fragmentCourseDetailBinding.btnResources)
Expand Down Expand Up @@ -90,4 +108,151 @@ class CourseDetailFragment : BaseContainerFragment(), OnRatingChangeListener {
super.onDownloadComplete()
setCourseData()
}

// private fun shareText(text: String) {
// val intent = Intent(Intent.ACTION_SEND)
// intent.type = "text/plain"
// intent.putExtra(Intent.EXTRA_TEXT, text)
// startActivity(Intent.createChooser(intent, "Share via"))
// }

private fun shareText(selectedText: String) {
val chatShareDialogBinding = ChatShareDialogBinding.inflate(LayoutInflater.from(requireContext()))
var dialog: AlertDialog? = null

val expandableDetailList = getData() as HashMap<String, List<String>>
val expandableTitleList = ArrayList(expandableDetailList.keys)
val expandableListAdapter = ExpandableListAdapter(requireContext(), expandableTitleList, expandableDetailList)
chatShareDialogBinding.listView.setAdapter(expandableListAdapter)

chatShareDialogBinding.listView.setOnChildClickListener { _, _, groupPosition, childPosition, _ ->
if (expandableTitleList[groupPosition] == "share with team/enterprise") {
val teamList = getTeamsOrEnterprises("team")
val enterpriseList = getTeamsOrEnterprises("enterprise")

if (expandableDetailList[expandableTitleList[groupPosition]]?.get(childPosition) == "teams") {
showGrandChildRecyclerView(teamList, "teams", selectedText)
} else {
showGrandChildRecyclerView(enterpriseList, "enterprises", selectedText)
}
} else {
val community = getCommunity()
showEditTextAndShareButton(community, "community", selectedText)
}
dialog?.dismiss()
false
}

val builder = AlertDialog.Builder(requireContext())
builder.setView(chatShareDialogBinding.root)
builder.setPositiveButton("Close") { _, _ ->
dialog?.dismiss()
}
dialog = builder.create()
dialog.show()
}

private fun getTeamsOrEnterprises(type: String): List<RealmMyTeam> {
return mRealm.where(RealmMyTeam::class.java)
.isEmpty("teamId").notEqualTo("status", "archived")
.equalTo("type", type).findAll()
}

private fun getCommunity(): RealmMyTeam? {
val settings = requireContext().getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
val sParentcode = settings?.getString("parentCode", "")
val communityName = settings?.getString("communityName", "")
val teamId = "$communityName@$sParentcode"
return mRealm.where(RealmMyTeam::class.java).equalTo("_id", teamId).findFirst()
}

private fun showGrandChildRecyclerView(items: List<RealmMyTeam>, section: String, selectedText: String) {
val grandChildDialogBinding = GrandChildRecyclerviewDialogBinding.inflate(LayoutInflater.from(requireContext()))
var dialog: AlertDialog? = null

val titleText = if (section == "teams") getString(R.string.team) else getString(R.string.enterprises)
grandChildDialogBinding.title.text = titleText

val grandChildAdapter = GrandChildAdapter(items) { selectedItem ->
showEditTextAndShareButton(selectedItem, section, selectedText)
dialog?.dismiss()
}
grandChildDialogBinding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
grandChildDialogBinding.recyclerView.adapter = grandChildAdapter

val builder = AlertDialog.Builder(requireContext())
builder.setView(grandChildDialogBinding.root)
builder.setPositiveButton("Close") { _, _ ->
dialog?.dismiss()
}
dialog = builder.create()
dialog.show()
}

private fun showEditTextAndShareButton(team: RealmMyTeam?, section: String, selectedText: String) {
val addNoteDialogBinding = AddNoteDialogBinding.inflate(LayoutInflater.from(requireContext()))
val builder = AlertDialog.Builder(requireContext())
builder.setView(addNoteDialogBinding.root)
builder.setPositiveButton(getString(R.string.share_chat)) { dialog, _ ->
val map = HashMap<String?, String>().apply {
put("message", selectedText)
put("viewInId", team?._id ?: "")
put("viewInSection", section)

}
// createNews(map)
dialog.dismiss()
}
builder.setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
dialog.dismiss()
}
val dialog = builder.create()
dialog.show()
}

private fun getData(): Map<String, List<String>> {
val expandableListDetail: MutableMap<String, List<String>> = HashMap()
val community: MutableList<String> = ArrayList()
community.add("community")

val teams: MutableList<String> = ArrayList()
teams.add("teams")
teams.add("enterprises")

expandableListDetail["share with community"] = community
expandableListDetail["share with team/enterprise"] = teams
return expandableListDetail
}

private fun researchText(text: String) {
// val intent = Intent(Intent.ACTION_WEB_SEARCH)
// intent.putExtra(SearchManager.QUERY, text)
// startActivity(intent)
val bundle = Bundle()
bundle.putString("selectedText", text)
val chatDetailFragment = ChatDetailFragment()
chatDetailFragment.arguments = bundle
parentFragmentManager.beginTransaction()
.replace(R.id.fragment_container, chatDetailFragment)
.addToBackStack(null)
.commit()
}

private fun copyToClipboard(text: String) {
val clipboard = context?.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("copied Text", text)
clipboard.setPrimaryClip(clip)

val options = arrayOf("Share", "Research")
val builder = AlertDialog.Builder(requireContext())
builder.setTitle("Select Action")
.setItems(options) { _, which ->
when (which) {
0 -> shareText(text)
1 -> researchText(text)
}
}
builder.create().show()
Toast.makeText(context, context?.getString(R.string.copied_to_clipboard), Toast.LENGTH_SHORT).show()
}
}
22 changes: 19 additions & 3 deletions app/src/main/res/layout/fragment_course_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
tools:context=".ui.courses.CourseDetailFragment">

<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/card_bg"
Expand All @@ -30,6 +31,7 @@
android:layout_height="wrap_content"
android:text="@string/open_resource"
android:theme="@style/AccentButton" />

<Button
android:id="@+id/btn_resources"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -74,14 +76,15 @@
android:padding="4dp"
android:text="@string/zero_total"
android:textColor="@color/daynight_textColor" />

<androidx.appcompat.widget.AppCompatRatingBar
android:id="@+id/rating_bar"
style="@style/Base.Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:progressTint="@color/daynight_textColor"
android:progressBackgroundTint="@color/empty_rating" />
android:progressBackgroundTint="@color/empty_rating"
android:progressTint="@color/daynight_textColor" />
</LinearLayout>

<LinearLayout
Expand All @@ -102,6 +105,7 @@
android:textColor="@color/daynight_textColor"
android:textSize="@dimen/text_size_large"
android:textStyle="bold" />

<TextView
android:id="@+id/average"
android:layout_width="wrap_content"
Expand All @@ -123,6 +127,7 @@
android:layout_height="wrap_content"
android:text="@string/subject_level_colon"
android:textColor="@color/daynight_textColor" />

<TextView
android:id="@+id/subject_level"
android:layout_width="wrap_content"
Expand All @@ -143,6 +148,7 @@
android:layout_height="wrap_content"
android:text="@string/grade_level_colon"
android:textColor="@color/daynight_textColor" />

<TextView
android:id="@+id/grade_level"
android:layout_width="wrap_content"
Expand All @@ -163,6 +169,7 @@
android:layout_height="wrap_content"
android:text="@string/language_colon"
android:textColor="@color/daynight_textColor" />

<TextView
android:id="@+id/language"
android:layout_width="wrap_content"
Expand All @@ -183,6 +190,7 @@
android:layout_height="wrap_content"
android:text="@string/method"
android:textColor="@color/daynight_textColor" />

<TextView
android:id="@+id/method"
android:layout_width="wrap_content"
Expand All @@ -202,6 +210,7 @@
android:layout_height="wrap_content"
android:text="@string/number_of_exams"
android:textColor="@color/daynight_textColor" />

<TextView
android:id="@+id/no_of_exams"
android:layout_width="wrap_content"
Expand All @@ -222,13 +231,15 @@
android:layout_height="wrap_content"
android:text="@string/description_colon"
android:textColor="@color/daynight_textColor" />

<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:paddingBottom="@dimen/_10dp"
android:textColor="@color/daynight_textColor" />
android:textColor="@color/daynight_textColor"
android:textIsSelectable="true" />
</LinearLayout>
</LinearLayout>

Expand All @@ -248,12 +259,17 @@
android:layout_height="wrap_content"
android:text="@string/course_steps"
android:textColor="@color/daynight_textColor" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/steps_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit d9f4213

Please sign in to comment.