Skip to content

Commit

Permalink
Merge pull request #115 from arpitmx/Mohit
Browse files Browse the repository at this point in the history
Recent tasks in settings
  • Loading branch information
arpitmx authored Feb 9, 2024
2 parents cadc452 + 86087c4 commit d9ec63f
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 10 deletions.
32 changes: 27 additions & 5 deletions app/src/main/java/com/ncs/o2/HelperClasses/PrefManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ object PrefManager {
private val projectIconUrlMapKey = "project_icon_url_map"
private val projectDeeplinkMapKey = "project_deeplink_map"
private val projectAliasCodeMapKey = "project_alias_map"
private val projectRecentsMapKey = "project_recents_map"


private lateinit var projectTimestampMap: MutableMap<String, Long>
Expand All @@ -65,6 +66,8 @@ object PrefManager {

private lateinit var ProjectIconUrlMap: MutableMap<String, String>
private lateinit var ProjectAliasCodeMap: MutableMap<String, String>
private lateinit var ProjectRecentsMap: MutableMap<String, String>


private lateinit var ProjectDeepLinkMap: MutableMap<String, String>

Expand Down Expand Up @@ -195,6 +198,15 @@ object PrefManager {
}
} ?: mutableMapOf()

val savedProjectRecentsMapString = sharedPreferences.getString(
projectRecentsMapKey, null)
ProjectRecentsMap = savedProjectRecentsMapString?.let {
try {
Gson().fromJson(it, object : TypeToken<MutableMap<String, String>>() {}.type)
} catch (e: JsonSyntaxException) {
mutableMapOf()
}
} ?: mutableMapOf()
}


Expand Down Expand Up @@ -889,12 +901,22 @@ object PrefManager {
editor.apply()
}

fun getProjectListVisblity(): Boolean {
return sharedPreferences.getBoolean("project_list_visible", true)
fun saveProjectRecents(projectName: String, recents: List<String>) {
val gson = Gson()
val channelsJson = gson.toJson(recents)
editor.putString("project_recents_$projectName", channelsJson)
editor.apply()
}

fun setProjectListVisblity(bool:Boolean) {
editor.putBoolean("project_list_visible", bool)
editor.apply()
fun getProjectRecents(projectName: String): List<String> {
val favsJson = sharedPreferences.getString("project_recents_$projectName", null)
val gson = Gson()
val type = object : TypeToken<List<String>>() {}.type
return if (favsJson != null) {
gson.fromJson(favsJson, type)
} else {
emptyList()
}
}

}
81 changes: 80 additions & 1 deletion app/src/main/java/com/ncs/o2/UI/SearchScreen/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class SearchFragment : Fragment(),FilterBottomSheet.SendText,UserListBottomSheet
private val selectedTags = mutableListOf<Tag>()
lateinit var binding: FragmentSearchBinding
private var taskList: MutableList<TaskItem> = mutableListOf()
private var recentsTaskList: MutableList<Task> = mutableListOf()
private val tasks:MutableList<Task> = mutableListOf()
private lateinit var viewModel: SearchViewModel
private lateinit var recyclerView: RecyclerView
Expand Down Expand Up @@ -150,6 +151,7 @@ class SearchFragment : Fragment(),FilterBottomSheet.SendText,UserListBottomSheet
}

}

return binding.root
}

Expand Down Expand Up @@ -213,7 +215,10 @@ class SearchFragment : Fragment(),FilterBottomSheet.SendText,UserListBottomSheet
defaultButtons()
filterButtons()
initViews()

val recents=PrefManager.getProjectRecents(PrefManager.getcurrentProject())
for (recent in recents){
fetchTasksforID(recent)
}


}
Expand Down Expand Up @@ -253,6 +258,7 @@ class SearchFragment : Fragment(),FilterBottomSheet.SendText,UserListBottomSheet
}
}
private fun setRecyclerView(){
binding.recentsRV.gone()
binding.results.visible()
binding.results.text="Matches ${taskList.size.toString()} tasks"
recyclerView = binding.recyclerView
Expand Down Expand Up @@ -285,6 +291,79 @@ class SearchFragment : Fragment(),FilterBottomSheet.SendText,UserListBottomSheet
})
taskListAdapter.notifyDataSetChanged()
}

private fun setRecentsRecyclerView(){
binding.recyclerView.gone()
binding.results.visible()
binding.results.text="Recent ${recentsTaskList.size.toString()} tasks"
binding.placeholder.gone()
binding.searchPlaceholder.gone()

val _recyclerView = binding.recentsRV
_recyclerView.visible()
val _taskList = recentsTaskList.map { task ->
TaskItem(
title = task.title!!,
id = task.id,
assignee_id = task.assignee!!,
difficulty = task.difficulty!!,
timestamp = task.time_STAMP,
completed = SwitchFunctions.getStringStateFromNumState(task.status)=="Completed",
tagList = task.tags,
last_updated = task.last_updated
)
}.toMutableList()
val _taskListAdapter = TaskListAdapter(firestoreRepository,requireContext(),_taskList.sortedByDescending { it.last_updated }.toMutableList(),db)
_taskListAdapter.setOnClickListener(this)
val layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
layoutManager.reverseLayout = false
with(_recyclerView) {
this.layoutManager = layoutManager
adapter = _taskListAdapter
edgeEffectFactory = BounceEdgeEffectFactory()
}
_recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(
recyclerView: RecyclerView,
newState: Int
) {
super.onScrollStateChanged(recyclerView, newState)
state[0] = newState
}

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0 && (state[0] == 0 || state[0] == 2)) {
hideSearch()
} else if (dy < -10) {
showSearch()
}
}
})
_taskListAdapter.notifyDataSetChanged()
}
private fun fetchTasksforID(taskID:String){
viewModel.getTasksForID(
PrefManager.getcurrentProject(),taskID
) { result ->
when (result) {
is DBResult.Success -> {
recentsTaskList.add(result.data)
recentsTaskList.sortedByDescending { it.last_updated }
if (recentsTaskList.size==PrefManager.getProjectRecents(PrefManager.getcurrentProject()).size){
setRecentsRecyclerView()
}
}

is DBResult.Failure -> {
val errorMessage = result.exception.message
}

is DBResult.Progress -> {
}
}
}
}
private fun searchQuery(text:String){
binding.searchPlaceholder.gone()
binding.recyclerView.gone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class SearchViewModel @Inject constructor(private val firestoreRepository: Fire
firestoreRepository.getSearchedTasks(projectName = projectName, assignee =assignee, type = type, state = state, text = text, creator = creator, result = resultCallback)
}

fun getTasksForID(
projectName: String,
taskID:String,
resultCallback: (DBResult<Task>) -> Unit
) {
CoroutineScope(Dispatchers.Main).launch {
taskRepository.getTaskbyID(projectName, taskID, resultCallback)
}
}
suspend fun getSearchTasksFromDB(
projectName: String,
assignee:String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.firestore.FirebaseFirestore
Expand Down Expand Up @@ -69,8 +71,10 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {
private lateinit var recyclerView: RecyclerView
private lateinit var taskListAdapter: TaskListAdapter
private lateinit var taskList: ArrayList<TaskItem>
private var taskItems: MutableList<TaskItem> = mutableListOf()

private lateinit var tasks: ArrayList<Task>
private lateinit var taskList2: ArrayList<Task>
private var taskList2: MutableList<Task> = mutableListOf()
private lateinit var projectName: String
private lateinit var segmentName: String
val state = arrayOf(1)
Expand All @@ -84,6 +88,7 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {
(requireActivity() as MainActivity)
}


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -105,10 +110,10 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {

override fun onResume() {
super.onResume()

}



override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(this).get(TaskSectionViewModel::class.java)
Expand All @@ -122,6 +127,7 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {
manageViews()
}


private fun manageViews(){
activityBinding.binding.gioActionbar.tabLayout.visible()
activityBinding.binding.gioActionbar.actionbar.visible()
Expand Down Expand Up @@ -368,7 +374,7 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {
} else {

recyclerView = binding.recyclerView
val taskItems: List<TaskItem> = filteredList.map { task ->
taskItems= filteredList.map { task ->
TaskItem(
title = task.title!!,
id = task.id,
Expand All @@ -379,7 +385,7 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {
tagList = task.tags,
last_updated = task.last_updated
)
}
}.toMutableList()
val taskadapter = TaskListAdapter(
firestoreRepository,
requireContext(),
Expand Down Expand Up @@ -445,6 +451,58 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {
}
}

fun fetchNewTasks() {
viewModel.getTasksForSegmentFromDB(
projectName,
segmentName,
viewModel.sectionName!!
) { result ->
when (result) {
is DBResult.Success -> {
val filteredList = filterTasks(result.data)
val newtaskItems= filteredList.map { task ->
TaskItem(
title = task.title!!,
id = task.id,
assignee_id = task.assignee!!,
difficulty = task.difficulty!!,
timestamp = task.time_STAMP,
completed = if (SwitchFunctions.getStringStateFromNumState(task.status!!) == "Completed") true else false,
tagList = task.tags,
last_updated = task.last_updated
)
}.toMutableList()
val diffResult = calculateDiff(taskItems, newtaskItems)
diffResult.dispatchUpdatesTo(taskListAdapter)
}

is DBResult.Failure -> {
val errorMessage = result.exception.message
// Handle failure if needed
}

is DBResult.Progress -> {
// Handle progress if needed
}
}
}
}

private fun calculateDiff(oldList: List<TaskItem>, newList: List<TaskItem>): DiffUtil.DiffResult {
return DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun getOldListSize(): Int = oldList.size
override fun getNewListSize(): Int = newList.size

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition].id == newList[newItemPosition].id
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition] == newList[newItemPosition]
}
})
}

private fun showSearch() {
}

Expand All @@ -453,6 +511,8 @@ class TaskSectionFragment() : Fragment(), TaskListAdapter.OnClickListener {
}

override fun onCLick(position: Int, task: TaskItem) {
viewModel.isReturning=true
viewModel.scrollPosition=position
val intent = Intent(requireContext(), TaskDetailActivity::class.java)
intent.putExtra("task_id", task.id)
startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.datafaker.providers.base.Bool
import javax.inject.Inject

/*
Expand Down Expand Up @@ -44,6 +45,10 @@ class TaskSectionViewModel @Inject constructor(private val firestoreRepository:

var sectionName: String? = null

var isReturning : Boolean = false

var scrollPosition : Int = 0


private val _currentSegment = MutableLiveData<String>()
val currentSegment: LiveData<String> get() = _currentSegment
Expand Down Expand Up @@ -148,4 +153,7 @@ class TaskSectionViewModel @Inject constructor(private val firestoreRepository:






}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ class TaskDetailActivity : AppCompatActivity(), TaskDetailsFragment.ViewVisibili
}
}
}
val oldRecents=PrefManager.getProjectRecents(PrefManager.getcurrentProject()).distinct().toMutableList()
if (!oldRecents.contains(taskId)){
if (oldRecents.size>=10){
oldRecents.removeAt(oldRecents.size-1)
oldRecents.add(taskId!!)
}
else{
oldRecents.add(taskId!!)
}
}
PrefManager.saveProjectRecents(PrefManager.getcurrentProject(),oldRecents.distinct())
}

if (_type!=null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class TasksHolderFragment : Fragment(),SegmentSelectionBottomSheet.sendSectionsL
activityBinding.gioActionbar.tabLayout.visible()
}


}

private fun setUpBackPress() {
Expand Down
Loading

0 comments on commit d9ec63f

Please sign in to comment.