Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated ui and theme modules from Java to Kotlin #5942

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private void onClickRemoveLocation() {
*/
private void removeLocationFromImage() {
if (media != null) {
compositeDisposable.add(coordinateEditHelper.makeCoordinatesEdit(getApplicationContext()
getCompositeDisposable().add(coordinateEditHelper.makeCoordinatesEdit(getApplicationContext()
, media, "0.0", "0.0", "0.0f")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down Expand Up @@ -479,7 +479,7 @@ public void updateCoordinates(final String Latitude, final String Longitude,
}

try {
compositeDisposable.add(
getCompositeDisposable().add(
coordinateEditHelper.makeCoordinatesEdit(getApplicationContext(), media,
Latitude, Longitude, Accuracy)
.subscribeOn(Schedulers.io())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.free.nrw.commons.customselector.ui.selector

import android.app.Activity
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import android.os.Bundle
Expand Down Expand Up @@ -346,7 +347,7 @@ class ImageFragment :
context
.getSharedPreferences(
"CustomSelector",
BaseActivity.MODE_PRIVATE,
MODE_PRIVATE,
)?.let { prefs ->
prefs.edit()?.let { editor ->
editor.putLong("ItemId", imageAdapter.getImageIdAt(position))?.apply()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setTabs() {

viewPagerAdapter.setTabData(fragmentList, titleList);
viewPagerAdapter.notifyDataSetChanged();
compositeDisposable.add(RxSearchView.queryTextChanges(binding.searchBox)
getCompositeDisposable().add(RxSearchView.queryTextChanges(binding.searchBox)
.takeUntil(RxView.detaches(binding.searchBox))
.debounce(500, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
Expand Down Expand Up @@ -284,7 +284,7 @@ public void updateText(String query) {
@Override protected void onDestroy() {
super.onDestroy();
//Dispose the disposables when the activity is destroyed
compositeDisposable.dispose();
getCompositeDisposable().dispose();
binding = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void removeNotification(Notification notification) {
}
binding.progressBar.setVisibility(View.GONE);
});
compositeDisposable.add(disposable);
getCompositeDisposable().add(disposable);
}


Expand Down Expand Up @@ -178,7 +178,7 @@ private void addNotifications(boolean archived) {
Timber.d("Add notifications");
if (mNotificationWorkerFragment == null) {
binding.progressBar.setVisibility(View.VISIBLE);
compositeDisposable.add(controller.getNotifications(archived)
getCompositeDisposable().add(controller.getNotifications(archived)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(notificationList -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void setTabs() {
@Override
public void onDestroy() {
super.onDestroy();
compositeDisposable.clear();
getCompositeDisposable().clear();
}

/**
Expand Down
66 changes: 0 additions & 66 deletions app/src/main/java/fr/free/nrw/commons/theme/BaseActivity.java

This file was deleted.

65 changes: 65 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/theme/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fr.free.nrw.commons.theme

import android.content.res.Configuration
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.WindowManager
import javax.inject.Inject
import javax.inject.Named
import fr.free.nrw.commons.R
import fr.free.nrw.commons.di.CommonsDaggerAppCompatActivity
import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.utils.SystemThemeUtils
import io.reactivex.disposables.CompositeDisposable


abstract class BaseActivity : CommonsDaggerAppCompatActivity() {

@Inject
@field:Named("default_preferences")
lateinit var defaultKvStore: JsonKvStore

@Inject
lateinit var systemThemeUtils: SystemThemeUtils

protected val compositeDisposable = CompositeDisposable()
protected var wasPreviouslyDarkTheme: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
wasPreviouslyDarkTheme = systemThemeUtils.isDeviceInNightMode()
setTheme(if (wasPreviouslyDarkTheme) R.style.DarkAppTheme else R.style.LightAppTheme)

val fontScale = android.provider.Settings.System.getFloat(
baseContext.contentResolver,
android.provider.Settings.System.FONT_SCALE,
1f
)
adjustFontScale(resources.configuration, fontScale)
}

override fun onResume() {
// Restart activity if theme is changed
if (wasPreviouslyDarkTheme != systemThemeUtils.isDeviceInNightMode()) {
recreate()
}
super.onResume()
}

override fun onDestroy() {
super.onDestroy()
compositeDisposable.clear()
}

/**
* Apply fontScale on device
*/
fun adjustFontScale(configuration: Configuration, scale: Float) {
configuration.fontScale = scale
val metrics = resources.displayMetrics
val wm = getSystemService(WINDOW_SERVICE) as WindowManager
wm.defaultDisplay.getMetrics(metrics)
metrics.scaledDensity = configuration.fontScale * metrics.density
baseContext.resources.updateConfiguration(configuration, metrics)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.free.nrw.commons.ui

import android.content.Context
import android.content.res.TypedArray
import android.os.Build
import android.os.Build.VERSION
import android.util.AttributeSet
import com.google.android.material.textfield.TextInputEditText
import fr.free.nrw.commons.R


class PasteSensitiveTextInputEditText @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : TextInputEditText(context, attrs) {

private var formattingAllowed: Boolean = true

init {
if (attrs != null) {
formattingAllowed = extractFormattingAttribute(context, attrs)
}
}

override fun onTextContextMenuItem(id: Int): Boolean {
// if not paste command, or formatting is allowed, return default
if (id != android.R.id.paste || formattingAllowed) {
return super.onTextContextMenuItem(id)
}

// if it's paste and formatting not allowed
val proceeded: Boolean = if (VERSION.SDK_INT >= 23) {
super.onTextContextMenuItem(android.R.id.pasteAsPlainText)
} else {
val success = super.onTextContextMenuItem(id)
if (success && text != null) {
// rewrite with plain text so formatting is lost
setText(text.toString())
setSelection(text?.length ?: 0)
}
success
}
return proceeded
}

private fun extractFormattingAttribute(context: Context, attrs: AttributeSet): Boolean {
val a = context.theme.obtainStyledAttributes(
attrs, R.styleable.PasteSensitiveTextInputEditText, 0, 0
)
return try {
a.getBoolean(R.styleable.PasteSensitiveTextInputEditText_allowFormatting, true)
} finally {
a.recycle()
}
}

fun setFormattingAllowed(formattingAllowed: Boolean) {
this.formattingAllowed = formattingAllowed
}
}
36 changes: 0 additions & 36 deletions app/src/main/java/fr/free/nrw/commons/ui/widget/HtmlTextView.java

This file was deleted.

Loading
Loading