mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 21:33:53 +01:00
Migrated ui and theme modules from Java to Kotlin (#5942)
* Rename .java to .kt * Migrated ui and theme module to Kotlin
This commit is contained in:
parent
cb4ffd8ca8
commit
ed18a37577
13 changed files with 230 additions and 245 deletions
|
|
@ -1,65 +0,0 @@
|
|||
package fr.free.nrw.commons.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Build.VERSION;
|
||||
import android.util.AttributeSet;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import fr.free.nrw.commons.R;
|
||||
|
||||
public class PasteSensitiveTextInputEditText extends TextInputEditText {
|
||||
|
||||
private boolean formattingAllowed = true;
|
||||
|
||||
public PasteSensitiveTextInputEditText(final Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public PasteSensitiveTextInputEditText(final Context context, final AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
formattingAllowed = extractFormattingAttribute(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTextContextMenuItem(int id) {
|
||||
|
||||
// if not paste command, or formatting is allowed, return default
|
||||
if(id != android.R.id.paste || formattingAllowed){
|
||||
return super.onTextContextMenuItem(id);
|
||||
}
|
||||
|
||||
// if its paste and formatting not allowed
|
||||
boolean proceeded;
|
||||
if(VERSION.SDK_INT >= 23) {
|
||||
proceeded = super.onTextContextMenuItem(android.R.id.pasteAsPlainText);
|
||||
}else {
|
||||
proceeded = super.onTextContextMenuItem(id);
|
||||
if (proceeded && getText() != null) {
|
||||
// rewrite with plain text so formatting is lost
|
||||
setText(getText().toString());
|
||||
setSelection(getText().length());
|
||||
}
|
||||
}
|
||||
return proceeded;
|
||||
}
|
||||
|
||||
private boolean extractFormattingAttribute(Context context, AttributeSet attrs){
|
||||
|
||||
boolean formatAllowed = true;
|
||||
|
||||
TypedArray a = context.getTheme().obtainStyledAttributes(
|
||||
attrs, R.styleable.PasteSensitiveTextInputEditText, 0, 0);
|
||||
|
||||
try {
|
||||
formatAllowed = a.getBoolean(
|
||||
R.styleable.PasteSensitiveTextInputEditText_allowFormatting, true);
|
||||
} finally {
|
||||
a.recycle();
|
||||
}
|
||||
return formatAllowed;
|
||||
}
|
||||
|
||||
public void setFormattingAllowed(boolean formattingAllowed){
|
||||
this.formattingAllowed = formattingAllowed;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
package fr.free.nrw.commons.ui.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.appcompat.widget.AppCompatTextView;
|
||||
|
||||
import fr.free.nrw.commons.utils.StringUtil;
|
||||
|
||||
/**
|
||||
* An {@link AppCompatTextView} which formats the text to HTML displayable text and makes any
|
||||
* links clickable.
|
||||
*/
|
||||
public class HtmlTextView extends AppCompatTextView {
|
||||
|
||||
/**
|
||||
* Constructs a new instance of HtmlTextView
|
||||
* @param context the context of the view
|
||||
* @param attrs the set of attributes for the view
|
||||
*/
|
||||
public HtmlTextView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
setMovementMethod(LinkMovementMethod.getInstance());
|
||||
setText(StringUtil.fromHtml(getText().toString()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be displayed
|
||||
* @param newText the text to be displayed
|
||||
*/
|
||||
public void setHtmlText(String newText) {
|
||||
setText(StringUtil.fromHtml(newText));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package fr.free.nrw.commons.ui.widget
|
||||
|
||||
import android.content.Context
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.util.AttributeSet
|
||||
|
||||
import androidx.appcompat.widget.AppCompatTextView
|
||||
|
||||
import fr.free.nrw.commons.utils.StringUtil
|
||||
|
||||
/**
|
||||
* An [AppCompatTextView] which formats the text to HTML displayable text and makes any
|
||||
* links clickable.
|
||||
*/
|
||||
class HtmlTextView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null
|
||||
) : AppCompatTextView(context, attrs) {
|
||||
|
||||
init {
|
||||
movementMethod = LinkMovementMethod.getInstance()
|
||||
text = StringUtil.fromHtml(text.toString())
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be displayed
|
||||
* @param newText the text to be displayed
|
||||
*/
|
||||
fun setHtmlText(newText: String) {
|
||||
text = StringUtil.fromHtml(newText)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
package fr.free.nrw.commons.ui.widget;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
|
||||
/**
|
||||
* a formatted dialog fragment
|
||||
* This class is used by NearbyInfoDialog
|
||||
*/
|
||||
public abstract class OverlayDialog extends DialogFragment {
|
||||
|
||||
/**
|
||||
* creates a DialogFragment with the correct style and theme
|
||||
* @param savedInstanceState bundle re-constructed from a previous saved state
|
||||
*/
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
|
||||
}
|
||||
|
||||
/**
|
||||
* When the view is created, sets the dialog layout to full screen
|
||||
*
|
||||
* @param view the view being used
|
||||
* @param savedInstanceState bundle re-constructed from a previous saved state
|
||||
*/
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
setDialogLayoutToFullScreen();
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the dialog layout to fullscreen
|
||||
*/
|
||||
private void setDialogLayoutToFullScreen() {
|
||||
Window window = getDialog().getWindow();
|
||||
WindowManager.LayoutParams wlp = window.getAttributes();
|
||||
window.requestFeature(Window.FEATURE_NO_TITLE);
|
||||
wlp.gravity = Gravity.BOTTOM;
|
||||
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
wlp.height = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
window.setAttributes(wlp);
|
||||
}
|
||||
|
||||
/**
|
||||
* builds custom dialog container
|
||||
*
|
||||
* @param savedInstanceState the previously saved state
|
||||
* @return the dialog
|
||||
*/
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
Dialog dialog = super.onCreateDialog(savedInstanceState);
|
||||
Window window = dialog.getWindow();
|
||||
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||
return dialog;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package fr.free.nrw.commons.ui.widget
|
||||
|
||||
import android.app.Dialog
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.Window
|
||||
import android.view.WindowManager
|
||||
|
||||
import androidx.fragment.app.DialogFragment
|
||||
|
||||
/**
|
||||
* A formatted dialog fragment
|
||||
* This class is used by NearbyInfoDialog
|
||||
*/
|
||||
abstract class OverlayDialog : DialogFragment() {
|
||||
|
||||
/**
|
||||
* Creates a DialogFragment with the correct style and theme
|
||||
* @param savedInstanceState bundle re-constructed from a previous saved state
|
||||
*/
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light)
|
||||
}
|
||||
|
||||
/**
|
||||
* When the view is created, sets the dialog layout to full screen
|
||||
*
|
||||
* @param view the view being used
|
||||
* @param savedInstanceState bundle re-constructed from a previous saved state
|
||||
*/
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
setDialogLayoutToFullScreen()
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the dialog layout to fullscreen
|
||||
*/
|
||||
private fun setDialogLayoutToFullScreen() {
|
||||
val window = dialog?.window ?: return
|
||||
val wlp = window.attributes
|
||||
window.requestFeature(Window.FEATURE_NO_TITLE)
|
||||
wlp.gravity = Gravity.BOTTOM
|
||||
wlp.width = WindowManager.LayoutParams.MATCH_PARENT
|
||||
wlp.height = WindowManager.LayoutParams.MATCH_PARENT
|
||||
window.attributes = wlp
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds custom dialog container
|
||||
*
|
||||
* @param savedInstanceState the previously saved state
|
||||
* @return the dialog
|
||||
*/
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val dialog = super.onCreateDialog(savedInstanceState)
|
||||
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
||||
return dialog
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue