-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathTemplateDetailsFragment.kt
More file actions
140 lines (121 loc) · 5.31 KB
/
TemplateDetailsFragment.kt
File metadata and controls
140 lines (121 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.fragments
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.transition.TransitionManager
import com.itsaky.androidide.R
import com.itsaky.androidide.R.string
import com.itsaky.androidide.activities.MainActivity
import com.itsaky.androidide.adapters.TemplateWidgetsListAdapter
import com.itsaky.androidide.databinding.FragmentTemplateDetailsBinding
import com.itsaky.androidide.roomData.recentproject.RecentProject
import com.itsaky.androidide.tasks.executeAsyncProvideError
import com.itsaky.androidide.templates.ProjectTemplateRecipeResult
import com.itsaky.androidide.templates.StringParameter
import com.itsaky.androidide.templates.Template
import com.itsaky.androidide.templates.impl.ConstraintVerifier
import com.itsaky.androidide.utils.TemplateRecipeExecutor
import com.itsaky.androidide.utils.flashError
import com.itsaky.androidide.utils.flashSuccess
import com.itsaky.androidide.viewmodel.MainViewModel
import com.itsaky.androidide.viewmodel.RecentProjectsViewModel
import org.slf4j.LoggerFactory
import java.util.Date
/**
* A fragment which shows a wizard-like interface for creating templates.
*
* @author Akash Yadav
*/
class TemplateDetailsFragment :
FragmentWithBinding<FragmentTemplateDetailsBinding>(
R.layout.fragment_template_details, FragmentTemplateDetailsBinding::bind
) {
private val viewModel by viewModels<MainViewModel>(
ownerProducer = { requireActivity() })
private val recentProjectsViewModel: RecentProjectsViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.template.observe(viewLifecycleOwner) {
binding.widgets.adapter = null
viewModel.postTransition(viewLifecycleOwner) { bindWithTemplate(it) }
}
viewModel.creatingProject.observe(viewLifecycleOwner) {
TransitionManager.beginDelayedTransition(binding.root)
binding.finish.isEnabled = !it
binding.previous.isEnabled = !it
}
binding.previous.setOnClickListener {
viewModel.setScreen(MainViewModel.SCREEN_TEMPLATE_LIST)
}
binding.finish.setOnClickListener {
viewModel.creatingProject.value = true
val template = viewModel.template.value ?: run {
viewModel.setScreen(MainViewModel.SCREEN_MAIN)
return@setOnClickListener
}
val isValid = template.parameters.fold(true) { isValid, param ->
if (param is StringParameter) {
return@fold isValid && ConstraintVerifier.isValid(
param.value,
param.constraints
)
} else isValid
}
if (!isValid) {
viewModel.creatingProject.value = false
flashError(string.msg_invalid_project_details)
return@setOnClickListener
}
viewModel.creatingProject.value = true
executeAsyncProvideError({
template.recipe.execute(TemplateRecipeExecutor())
}) { result, err ->
viewModel.creatingProject.value = false
if (result == null || err != null || result !is ProjectTemplateRecipeResult) {
err?.printStackTrace()
if (err != null) {
flashError(err.cause?.message ?: err.message)
} else {
flashError(string.project_creation_failed)
}
return@executeAsyncProvideError
}
val projectDir = result.data.projectDir
recentProjectsViewModel.insertProject(
RecentProject(
location = projectDir.path,
name = result.data.name,
createdAt = Date().toString()
)
)
flashSuccess(string.project_created_successfully)
(requireActivity() as MainActivity).openProject(projectDir)
}
}
binding.widgets.layoutManager = LinearLayoutManager(requireContext())
}
private fun bindWithTemplate(template: Template<*>?) {
template ?: return
binding.widgets.adapter = TemplateWidgetsListAdapter(template.widgets)
binding.title.setText(template.templateName)
}
}