Migrated nearby/fragments/NearbyParentFragment.java to nearby/fragments/NearbyParentFragment.kt

This commit is contained in:
Sujal-Gupta-SG 2025-01-22 02:07:45 +05:30
parent 697fa4c8c4
commit 2178c4246d
3 changed files with 23 additions and 26 deletions

View file

@ -138,6 +138,7 @@ open class CommonsApplicationModule(private val applicationContext: Context) {
*/ */
@Provides @Provides
@Named("default_preferences") @Named("default_preferences")
@Singleton
open fun providesDefaultKvStore(context: Context, gson: Gson): JsonKvStore = open fun providesDefaultKvStore(context: Context, gson: Gson): JsonKvStore =
JsonKvStore(context, "${context.packageName}_preferences", gson) JsonKvStore(context, "${context.packageName}_preferences", gson)

View file

@ -3,10 +3,18 @@ package fr.free.nrw.commons.di
import android.app.Activity import android.app.Activity
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.nearby.fragments.NearbyParentFragment import fr.free.nrw.commons.nearby.fragments.NearbyParentFragment
import javax.inject.Named
@Module @Module
class NearbyParentFragmentModule { class NearbyParentFragmentModule {
@Provides @Provides
fun NearbyParentFragment.providesActivity(): Activity = activity!! fun NearbyParentFragment.providesActivity(): Activity = activity!!
@Provides
fun providesApplicationKvStore(
@Named("default_preferences") kvStore: JsonKvStore
): JsonKvStore {
return kvStore
}
} }

View file

@ -306,11 +306,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
*/ */
val WLM_URL = "https://commons.wikimedia.org/wiki/Commons:Mobile_app/Contributing_to_WLM_using_the_app" val WLM_URL = "https://commons.wikimedia.org/wiki/Commons:Mobile_app/Contributing_to_WLM_using_the_app"
fun newInstance(): NearbyParentFragment {
val fragment = NearbyParentFragment()
fragment.retainInstance = true
return fragment
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -701,10 +697,19 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
searchHandler.removeCallbacks(searchRunnable!!) searchRunnable?.let {
presenter!!.removeNearbyPreferences(applicationKvStore!!) searchHandler.removeCallbacks(it)
} ?: run {
Timber.w("NearbyParentFragment: searchRunnable is null")
}
if (presenter == null) Timber.w("NearbyParentFragment: presenter is null")
if (applicationKvStore == null) Timber.w("NearbyParentFragment: applicationKvStore is null")
presenter?.removeNearbyPreferences(applicationKvStore ?: return)
} }
private fun initViews() { private fun initViews() {
Timber.d("init views called") Timber.d("init views called")
initBottomSheets() initBottomSheets()
@ -1341,6 +1346,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
} }
} }
private fun showOpenFileDialog(context: Context, fileName: String, isGPX: Boolean) { private fun showOpenFileDialog(context: Context, fileName: String, isGPX: Boolean) {
val title = getString(R.string.file_saved_successfully) val title = getString(R.string.file_saved_successfully)
val message = val message =
@ -2486,32 +2492,14 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(), NearbyParentFragmen
const val WLM_URL: String = const val WLM_URL: String =
"https://commons.wikimedia.org/wiki/Commons:Mobile_app/Contributing_to_WLM_using_the_app" "https://commons.wikimedia.org/wiki/Commons:Mobile_app/Contributing_to_WLM_using_the_app"
@JvmStatic // This makes it callable as a static method from Java
fun newInstance(): NearbyParentFragment { fun newInstance(): NearbyParentFragment {
val fragment = NearbyParentFragment() val fragment = NearbyParentFragment()
fragment.retainInstance = true fragment.retainInstance = true
return fragment return fragment
} }
fun saveFile(string: String, fileName: String): Boolean {
if (!isExternalStorageWritable) {
return false
}
val downloadsDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
)
val kmlFile = File(downloadsDir, fileName)
try {
val fos = FileOutputStream(kmlFile)
fos.write(string.toByteArray())
fos.close()
return true
} catch (e: IOException) {
e.printStackTrace()
return false
}
}
private val isExternalStorageWritable: Boolean private val isExternalStorageWritable: Boolean
get() { get() {