konvert InterfaceUtils

pull/594/head
olegivo 5 years ago
parent 6d34b7ce0b
commit 98f06f4fa8

@ -1,104 +0,0 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker 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.
*
* Loop Habit Tracker 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.androidbase.utils;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.*;
import android.util.*;
import android.view.*;
import android.widget.*;
public abstract class InterfaceUtils
{
private static Typeface fontAwesome;
@Nullable
private static Float fixedResolution = null;
public static void setFixedResolution(@NonNull Float f)
{
fixedResolution = f;
}
public static Typeface getFontAwesome(Context context)
{
if(fontAwesome == null) fontAwesome =
Typeface.createFromAsset(context.getAssets(),
"fontawesome-webfont.ttf");
return fontAwesome;
}
public static float dpToPixels(Context context, float dp)
{
if(fixedResolution != null) return dp * fixedResolution;
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}
public static float spToPixels(Context context, float sp)
{
if(fixedResolution != null) return sp * fixedResolution;
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics);
}
public static float getDimension(Context context, int id)
{
float dim = context.getResources().getDimension(id);
if (fixedResolution == null) return dim;
else
{
DisplayMetrics dm = context.getResources().getDisplayMetrics();
float actualDensity = dm.density;
return dim / actualDensity * fixedResolution;
}
}
public static void setupEditorAction(@NonNull ViewGroup parent,
@NonNull TextView.OnEditorActionListener listener)
{
for (int i = 0; i < parent.getChildCount(); i++)
{
View child = parent.getChildAt(i);
if (child instanceof ViewGroup)
setupEditorAction((ViewGroup) child, listener);
if (child instanceof TextView)
((TextView) child).setOnEditorActionListener(listener);
}
}
public static boolean isLayoutRtl(View view)
{
return ViewCompat.getLayoutDirection(view) ==
ViewCompat.LAYOUT_DIRECTION_RTL;
}
}

@ -0,0 +1,85 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker 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.
*
* Loop Habit Tracker 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.androidbase.utils
import android.content.Context
import android.graphics.Typeface
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import androidx.core.view.ViewCompat
object InterfaceUtils {
private var fontAwesome: Typeface? = null
private var fixedResolution: Float? = null
@JvmStatic
fun setFixedResolution(f: Float) {
fixedResolution = f
}
@JvmStatic
fun getFontAwesome(context: Context): Typeface? {
if (fontAwesome == null) fontAwesome = Typeface.createFromAsset(context.assets,
"fontawesome-webfont.ttf")
return fontAwesome
}
@JvmStatic
fun dpToPixels(context: Context, dp: Float): Float {
if (fixedResolution != null) return dp * fixedResolution!!
val resources = context.resources
val metrics = resources.displayMetrics
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics)
}
@JvmStatic
fun spToPixels(context: Context, sp: Float): Float {
if (fixedResolution != null) return sp * fixedResolution!!
val resources = context.resources
val metrics = resources.displayMetrics
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics)
}
@JvmStatic
fun getDimension(context: Context, id: Int): Float {
val dim = context.resources.getDimension(id)
return if (fixedResolution == null) dim else {
val dm = context.resources.displayMetrics
val actualDensity = dm.density
dim / actualDensity * fixedResolution!!
}
}
fun setupEditorAction(parent: ViewGroup,
listener: OnEditorActionListener) {
for (i in 0 until parent.childCount) {
val child = parent.getChildAt(i)
if (child is ViewGroup) setupEditorAction(child, listener)
if (child is TextView) child.setOnEditorActionListener(listener)
}
}
fun isLayoutRtl(view: View?): Boolean {
return ViewCompat.getLayoutDirection(view!!) ==
ViewCompat.LAYOUT_DIRECTION_RTL
}
}

@ -19,17 +19,21 @@
package org.isoron.uhabits.activities.common.dialogs package org.isoron.uhabits.activities.common.dialogs
import android.content.* import android.content.Context
import androidx.appcompat.app.* import android.content.DialogInterface
import android.text.* import android.text.InputFilter
import android.view.* import android.view.LayoutInflater
import android.view.inputmethod.* import android.view.WindowManager
import android.widget.* import android.view.inputmethod.EditorInfo
import org.isoron.androidbase.activities.* import android.widget.EditText
import org.isoron.androidbase.utils.* import android.widget.NumberPicker
import org.isoron.uhabits.* import android.widget.TextView
import org.isoron.uhabits.core.ui.screens.habits.list.* import androidx.appcompat.app.AlertDialog
import javax.inject.* import org.isoron.androidbase.activities.ActivityContext
import org.isoron.androidbase.utils.InterfaceUtils
import org.isoron.uhabits.R
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior
import javax.inject.Inject
class NumberPickerFactory class NumberPickerFactory
@Inject constructor( @Inject constructor(
@ -73,11 +77,11 @@ class NumberPickerFactory
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
InterfaceUtils.setupEditorAction(picker) { _, actionId, _ -> InterfaceUtils.setupEditorAction(picker, TextView.OnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) if (actionId == EditorInfo.IME_ACTION_DONE)
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick() dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick()
false false
} })
return dialog return dialog
} }

@ -27,7 +27,7 @@ import android.view.View.*
import com.google.auto.factory.* import com.google.auto.factory.*
import org.isoron.androidbase.activities.* import org.isoron.androidbase.activities.*
import org.isoron.androidbase.utils.* import org.isoron.androidbase.utils.*
import org.isoron.androidbase.utils.InterfaceUtils.* import org.isoron.androidbase.utils.InterfaceUtils.getDimension
import org.isoron.uhabits.* import org.isoron.uhabits.*
import org.isoron.uhabits.core.preferences.* import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.utils.* import org.isoron.uhabits.utils.*

Loading…
Cancel
Save