1
+ package fr.free.nrw.commons.description
2
+
3
+ import android.app.ProgressDialog
4
+ import android.content.Intent
5
+ import android.os.Bundle
6
+ import android.os.Parcelable
7
+ import android.view.View
8
+ import androidx.appcompat.app.AppCompatActivity
9
+ import androidx.recyclerview.widget.LinearLayoutManager
10
+ import androidx.recyclerview.widget.RecyclerView
11
+ import fr.free.nrw.commons.R
12
+ import fr.free.nrw.commons.databinding.ActivityDescriptionEditBinding
13
+ import fr.free.nrw.commons.description.EditDescriptionConstants.LIST_OF_DESCRIPTION_AND_CAPTION
14
+ import fr.free.nrw.commons.description.EditDescriptionConstants.UPDATED_WIKITEXT
15
+ import fr.free.nrw.commons.description.EditDescriptionConstants.WIKITEXT
16
+ import fr.free.nrw.commons.kvstore.JsonKvStore
17
+ import fr.free.nrw.commons.settings.Prefs
18
+ import fr.free.nrw.commons.upload.UploadMediaDetail
19
+ import fr.free.nrw.commons.upload.UploadMediaDetailAdapter
20
+ import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
21
+ import java.util.*
22
+ import javax.inject.Inject
23
+ import javax.inject.Named
24
+
25
+ /* *
26
+ * Activity for populating and editing existing description and caption
27
+ */
28
+ class DescriptionEditActivity : AppCompatActivity (), UploadMediaDetailAdapter.EventListener {
29
+ /* *
30
+ * Adapter for showing UploadMediaDetail in the activity
31
+ */
32
+ private lateinit var uploadMediaDetailAdapter: UploadMediaDetailAdapter
33
+
34
+ /* *
35
+ * For getting default preference
36
+ */
37
+ @JvmField
38
+ @Inject
39
+ @Named(" default_preferences" )
40
+ var defaultKvStore: JsonKvStore ? = null
41
+
42
+ /* *
43
+ * Recyclerview for recycling data in views
44
+ */
45
+ @JvmField
46
+ var rvDescriptions: RecyclerView ? = null
47
+
48
+ /* *
49
+ * Current wikitext
50
+ */
51
+ var wikiText: String? = null
52
+
53
+ /* *
54
+ * For showing progress dialog
55
+ */
56
+ private var progressDialog: ProgressDialog ? = null
57
+
58
+ private lateinit var binding: ActivityDescriptionEditBinding
59
+
60
+ override fun onCreate (savedInstanceState : Bundle ? ) {
61
+ super .onCreate(savedInstanceState)
62
+
63
+ binding = ActivityDescriptionEditBinding .inflate(layoutInflater)
64
+ setContentView(binding.root)
65
+
66
+ val bundle = intent.extras
67
+ val descriptionAndCaptions: ArrayList <UploadMediaDetail > =
68
+ bundle!! .getParcelableArrayList(LIST_OF_DESCRIPTION_AND_CAPTION )!!
69
+ wikiText = bundle.getString(WIKITEXT )
70
+ initRecyclerView(descriptionAndCaptions)
71
+
72
+ binding.btnAddDescription.setOnClickListener(::onButtonAddDescriptionClicked)
73
+ binding.btnEditSubmit.setOnClickListener(::onSubmitButtonClicked)
74
+ binding.toolbarBackButton.setOnClickListener(::onBackButtonClicked)
75
+ }
76
+
77
+ /* *
78
+ * Initializes the RecyclerView
79
+ * @param descriptionAndCaptions list of description and caption
80
+ */
81
+ private fun initRecyclerView (descriptionAndCaptions : ArrayList <UploadMediaDetail >? ) {
82
+ uploadMediaDetailAdapter = UploadMediaDetailAdapter (
83
+ defaultKvStore?.getString(Prefs .DESCRIPTION_LANGUAGE , " " ),
84
+ descriptionAndCaptions)
85
+ uploadMediaDetailAdapter.setCallback { titleStringID: Int , messageStringId: Int ->
86
+ showInfoAlert(
87
+ titleStringID,
88
+ messageStringId
89
+ )
90
+ }
91
+ uploadMediaDetailAdapter.setEventListener(this )
92
+ rvDescriptions = binding.rvDescriptionsCaptions
93
+ rvDescriptions!! .layoutManager = LinearLayoutManager (this )
94
+ rvDescriptions!! .adapter = uploadMediaDetailAdapter
95
+ }
96
+
97
+ /* *
98
+ * show dialog with info
99
+ * @param titleStringID Title ID
100
+ * @param messageStringId Message ID
101
+ */
102
+ private fun showInfoAlert (titleStringID : Int , messageStringId : Int ) {
103
+ showAlertDialog(
104
+ this , getString(titleStringID),
105
+ getString(messageStringId), getString(android.R .string.ok),
106
+ null , true
107
+ )
108
+ }
109
+
110
+ override fun onPrimaryCaptionTextChange (isNotEmpty : Boolean ) {}
111
+
112
+ private fun onBackButtonClicked (view : View ) {
113
+ onBackPressed()
114
+ }
115
+
116
+ private fun onButtonAddDescriptionClicked (view : View ) {
117
+ val uploadMediaDetail = UploadMediaDetail ()
118
+ uploadMediaDetail.isManuallyAdded = true // This was manually added by the user
119
+ uploadMediaDetailAdapter.addDescription(uploadMediaDetail)
120
+ rvDescriptions!! .smoothScrollToPosition(uploadMediaDetailAdapter.itemCount - 1 )
121
+ }
122
+
123
+ private fun onSubmitButtonClicked (view : View ) {
124
+ showLoggingProgressBar()
125
+ val uploadMediaDetails = uploadMediaDetailAdapter.items
126
+ updateDescription(uploadMediaDetails)
127
+ finish()
128
+ }
129
+
130
+ /* *
131
+ * Updates newly added descriptions in the wikiText and send to calling fragment
132
+ * @param uploadMediaDetails descriptions and captions
133
+ */
134
+ private fun updateDescription (uploadMediaDetails : List <UploadMediaDetail ?>) {
135
+ var descriptionIndex = wikiText!! .indexOf(" description=" )
136
+ if (descriptionIndex == - 1 ) {
137
+ descriptionIndex = wikiText!! .indexOf(" Description=" )
138
+ }
139
+ val buffer = StringBuilder ()
140
+ if (descriptionIndex != - 1 ) {
141
+ val descriptionStart = wikiText!! .substring(0 , descriptionIndex + 12 )
142
+ val descriptionToEnd = wikiText!! .substring(descriptionIndex + 12 )
143
+ val descriptionEndIndex = descriptionToEnd.indexOf(" \n " )
144
+ val descriptionEnd = wikiText!! .substring(
145
+ descriptionStart.length
146
+ + descriptionEndIndex
147
+ )
148
+ buffer.append(descriptionStart)
149
+ for (i in uploadMediaDetails.indices) {
150
+ val uploadDetails = uploadMediaDetails[i]
151
+ if (uploadDetails!! .descriptionText != " " ) {
152
+ buffer.append(" {{" )
153
+ buffer.append(uploadDetails.languageCode)
154
+ buffer.append(" |1=" )
155
+ buffer.append(uploadDetails.descriptionText)
156
+ buffer.append(" }}, " )
157
+ }
158
+ }
159
+ buffer.deleteCharAt(buffer.length - 1 )
160
+ buffer.deleteCharAt(buffer.length - 1 )
161
+ buffer.append(descriptionEnd)
162
+ }
163
+ val returningIntent = Intent ()
164
+ returningIntent.putExtra(UPDATED_WIKITEXT , buffer.toString())
165
+ returningIntent.putParcelableArrayListExtra(
166
+ LIST_OF_DESCRIPTION_AND_CAPTION ,
167
+ uploadMediaDetails as ArrayList <out Parcelable ?>
168
+ )
169
+ setResult(RESULT_OK , returningIntent)
170
+ finish()
171
+ }
172
+
173
+ private fun showLoggingProgressBar () {
174
+ progressDialog = ProgressDialog (this )
175
+ progressDialog!! .isIndeterminate = true
176
+ progressDialog!! .setTitle(getString(R .string.updating_caption_title))
177
+ progressDialog!! .setMessage(getString(R .string.updating_caption_message))
178
+ progressDialog!! .setCanceledOnTouchOutside(false )
179
+ progressDialog!! .show()
180
+ }
181
+ }
0 commit comments