Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
fix: list of projects was not updated when creating a new project (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
aikrq authored Mar 11, 2023
2 parents 3b53a49 + 0d49210 commit 02ed04f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import org.cosmicide.project.Java
import org.cosmicide.project.Kotlin
import org.cosmicide.project.Project
import org.cosmicide.rewrite.R
import org.cosmicide.rewrite.databinding.FragmentNewProjectBinding
import org.cosmicide.rewrite.model.ProjectViewModel
import org.cosmicide.rewrite.util.Constants
import org.cosmicide.rewrite.util.FileUtil
import java.io.File
Expand All @@ -22,6 +24,7 @@ class NewProjectFragment : Fragment() {
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
private val viewModel by activityViewModels<ProjectViewModel>()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand Down Expand Up @@ -57,6 +60,7 @@ class NewProjectFragment : Fragment() {
binding.packageName.text.toString()
)
)
viewModel.loadProjects()
findNavController().navigate(R.id.NewProjectFragment_to_EditorFragment, Bundle().apply {
putString(Constants.PROJECT_DIR, project.root.absolutePath)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
Expand All @@ -22,7 +22,7 @@ class ProjectFragment : Fragment(), ProjectAdapter.OnProjectEventListener {

private var _binding: FragmentProjectBinding? = null
private val projectAdapter = ProjectAdapter(this)
private val viewModel by viewModels<ProjectViewModel>()
private val viewModel by activityViewModels<ProjectViewModel>()

private val binding get() = _binding!!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class ProjectViewModel : ViewModel() {
val projects: LiveData<List<Project>> = _projects

init {
loadProjects()
}

fun loadProjects() {
viewModelScope.launch(Dispatchers.IO) {
val projectsList = mutableListOf<Project>()
val projectDir = FileUtil.projectDir
val directories = projectDir.listFiles { file -> file.isDirectory }
if (directories != null) {
Arrays.sort(directories, Comparator.comparingLong(File::lastModified).reversed())
for (directory in directories) {
projectsList.add(Project(directory, Java))
}
}
val projectsList = FileUtil.projectDir.listFiles { file -> file.isDirectory }
?.sortedByDescending { it.lastModified() }
?.map { Project(it, Java) }
?: emptyList()

withContext(Dispatchers.Main) {
_projects.value = projectsList
Log.d(TAG, "Projects: $projectsList")
Expand Down

0 comments on commit 02ed04f

Please sign in to comment.