Skip to content

Commit

Permalink
fix(android): more views created using inflator allowing to customize…
Browse files Browse the repository at this point in the history
… the theme
  • Loading branch information
farfromrefug committed Feb 5, 2024
1 parent fd8aeba commit 0387a46
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<android.widget.ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:indeterminate="true">
</android.widget.ProgressBar>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CircularProgress" parent="@android:style/Widget.Material.ProgressBar">
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.animation.StateListAnimator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
Expand All @@ -19,6 +20,7 @@ public class Utils {
static final int shortAnimTime = android.R.integer.config_shortAnimTime;
static final int statePressed = android.R.attr.state_pressed;
static final int stateEnabled = android.R.attr.state_enabled;
static LayoutInflater inflater = null;

public static void createStateListAnimator(Context context, View view, float elevation, float pressedZ) {
int duration = context.getResources().getInteger(shortAnimTime);
Expand Down Expand Up @@ -102,4 +104,12 @@ public static void handleClearFocus(View view) {
}
}
}

public static View inflateLayout(Context context, String layoutStringId) {
int layoutId = context.getResources().getIdentifier(layoutStringId, "layout", context.getPackageName());
if (inflater == null) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
return (View)inflater.inflate(layoutId, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.progressindicator.LinearProgressIndicator xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/LinearProgress">
</com.google.android.material.progressindicator.LinearProgressIndicator>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="LinearProgress" parent="@style/Widget.MaterialComponents.LinearProgressIndicator">
</style>
</resources>
7 changes: 6 additions & 1 deletion src/activityindicator/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { heightProperty } from '@nativescript/core';
import { ActivityIndicatorBase, indeterminateProperty, maxValueProperty, valueProperty } from './index-common';
import { inflateLayout } from '@nativescript-community/ui-material-core/android/utils';

export class ActivityIndicator extends ActivityIndicatorBase {
// nativeViewProtected: com.google.android.material.progressindicator.CircularProgressIndicator;
nativeViewProtected: com.google.android.material.progressindicator.CircularProgressIndicator;

// createNativeView() {
// const progressBar = new com.google.android.material.progressindicator.CircularProgressIndicator(this._context);
// progressBar.setVisibility(android.view.View.INVISIBLE);
// progressBar.setIndeterminate(true);
// return progressBar;
// }
createNativeView() {
return inflateLayout(this._context, 'ns_material_circular_progress') as com.google.android.material.progressindicator.CircularProgressIndicator;
}

[valueProperty.getDefault](): number {
return 0;
Expand Down
19 changes: 3 additions & 16 deletions src/button/button.android.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
import { dynamicElevationOffsetProperty, elevationProperty, getRippleColor, rippleColorAlphaProperty, rippleColorProperty, shapeProperty, themer } from '@nativescript-community/ui-material-core';
import { createStateListAnimator, getColorStateList, getHorizontalGravity, getLayout, getVerticalGravity, isPostLollipop } from '@nativescript-community/ui-material-core/android/utils';
import { createStateListAnimator, getColorStateList, getHorizontalGravity, getVerticalGravity, inflateLayout, isPostLollipop } from '@nativescript-community/ui-material-core/android/utils';
import {
Background,
Color,
CoreTypes,
Font,
ImageSource,
Length,
Utils,
Expand All @@ -18,10 +17,6 @@ import {
import { textAlignmentProperty, textTransformProperty } from '@nativescript/core/ui/text-base';
import { ButtonBase, imageSourceProperty, srcProperty } from './button-common';

let LayoutInflater: typeof android.view.LayoutInflater;

const layoutIds = {};

export class Button extends ButtonBase {
nativeViewProtected: com.google.android.material.button.MaterialButton;
nativeTextViewProtected: com.google.android.material.button.MaterialButton;
Expand All @@ -30,7 +25,7 @@ export class Button extends ButtonBase {

@profile
public createNativeView() {
let layoutId;
// let layoutId;
const variant = this.variant;
// let layoutIdName = 'ns_material_button';
let layoutStringId: string;
Expand All @@ -52,16 +47,8 @@ export class Button extends ButtonBase {
if (this.src) {
layoutStringId += '_icon';
}
layoutId = layoutIds[layoutStringId];
if (!layoutId) {
layoutId = layoutIds[layoutStringId] = getLayout(this._context, layoutStringId);
}
if (!LayoutInflater) {
LayoutInflater = android.view.LayoutInflater;
}
const view = LayoutInflater.from(this._context).inflate(layoutId, null, false) as com.google.android.material.button.MaterialButton;
const view = inflateLayout(this._context, layoutStringId) as com.google.android.material.button.MaterialButton;
if (this.src) {
layoutStringId += '_icon';
view.setIconGravity(0x2); //com.google.android.material.button.MaterialButton.ICON_GRAVITY_TEXT_START
// view.setIconSize(Utils.layout.toDevicePixels(24));
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/android/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,10 @@ export function getVerticalGravity(textAlignment: VerticalTextAlignment) {
return 80; //Gravity.BOTTOM
}
}

export function inflateLayout(context: android.content.Context, layoutId: string) {
if (!NUtils) {
NUtils = (com as any).nativescript.material.core.Utils;
}
return NUtils.inflateLayout(context, layoutId);
}
5 changes: 2 additions & 3 deletions src/core/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Background, Button, Color, Length, PercentLength, Utils, View, androidDynamicElevationOffsetProperty, androidElevationProperty, backgroundInternalProperty } from '@nativescript/core';
import { ad } from '@nativescript/core/utils';
import { ShapeProperties } from '.';
import { createRippleDrawable, createStateListAnimator, getAttrColor, getColorStateList, handleClearFocus, isPostLollipop, isPostMarshmallow } from './android/utils';
import { cssProperty, dynamicElevationOffsetProperty, elevationProperty, rippleColorAlphaProperty, rippleColorProperty } from './cssproperties';
Expand All @@ -20,7 +19,7 @@ function cornerTreat(cornerFamily: CornerFamily): com.google.android.material.sh
let context: android.content.Context;
function getContext() {
if (!context) {
context = Utils.ad.getApplicationContext();
context = Utils.android.getApplicationContext();
}
return context;
}
Expand Down Expand Up @@ -299,7 +298,7 @@ export function overrideViewBase() {
this.focus();
}
public clearFocus() {
ad.dismissSoftInput(this.nativeViewProtected);
Utils.android.dismissSoftInput(this.nativeViewProtected);
handleClearFocus(this.nativeViewProtected);
}

Expand Down
4 changes: 2 additions & 2 deletions src/progress/progress.android.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Color, PercentLength, backgroundColorProperty, backgroundInternalProperty, colorProperty, heightProperty } from '@nativescript/core';
import { ProgressBase, busyProperty, indeterminateProperty, progressBackgroundColorProperty, progressColorProperty } from './progress-common';
import { getRippleColor } from '@nativescript-community/ui-material-core';
import { inflateLayout } from '@nativescript-community/ui-material-core/android/utils';

export class Progress extends ProgressBase {
nativeViewProtected: com.google.android.material.progressindicator.LinearProgressIndicator;

// added in 1.3.0
createNativeView() {
return new com.google.android.material.progressindicator.LinearProgressIndicator(this._context);
return inflateLayout(this._context, 'ns_material_linear_progress') as com.google.android.material.progressindicator.LinearProgressIndicator;
}

[progressColorProperty.setNative](color: Color) {
Expand Down
39 changes: 8 additions & 31 deletions src/textfield/textfield.android.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
import { themer } from '@nativescript-community/ui-material-core';
import { getColorStateList, getFullColorStateList, getHorizontalGravity, getLayout, getVerticalGravity } from '@nativescript-community/ui-material-core/android/utils';
import { getColorStateList, getFullColorStateList, getHorizontalGravity, getVerticalGravity, inflateLayout } from '@nativescript-community/ui-material-core/android/utils';
import {
counterMaxLengthProperty,
digitsProperty,
Expand Down Expand Up @@ -29,23 +29,14 @@ import {
editableProperty,
fontInternalProperty,
hintProperty,
paddingBottomProperty,
paddingLeftProperty,
paddingRightProperty,
paddingTopProperty,
placeholderColorProperty,
profile,
textAlignmentProperty
} from '@nativescript/core';
import { secureProperty } from '@nativescript/core/ui/text-field';
import { TextFieldBase } from './textfield.common';

let LayoutInflater: typeof android.view.LayoutInflater;
let FrameLayoutLayoutParams: typeof android.widget.FrameLayout.LayoutParams;
let filledId;
let outlineId;
let noneId;
let inflater;
export class TextField extends TextFieldBase {
editText: com.nativescript.material.textfield.TextInputEditText;
layoutView: com.google.android.material.textfield.TextInputLayout;
Expand All @@ -68,37 +59,23 @@ export class TextField extends TextFieldBase {

@profile
public createNativeView() {
let layoutId = 0;
let layoutIdString = 'ns_material_text_field';
const variant = this.variant;
let needsTransparent = false;
if (variant === 'filled') {
if (!filledId) {
filledId = getLayout(this._context, 'ns_material_text_field_filled');
}
layoutId = filledId;

layoutIdString = 'ns_material_text_field_filled';
} else if (variant === 'outline') {
if (!outlineId) {
outlineId = getLayout(this._context, 'ns_material_text_field_outline');
}
layoutId = outlineId;

layoutIdString = 'ns_material_text_field_outline';
} else {
if (!noneId) {
noneId = getLayout(this._context, 'ns_material_text_field');
}
layoutId = noneId;
needsTransparent = true;
}

let layoutView: com.google.android.material.textfield.TextInputLayout;
let editText: com.nativescript.material.textfield.TextInputEditText;
if (layoutId !== 0) {
if (!LayoutInflater) {
LayoutInflater = android.view.LayoutInflater;
}
if (!inflater) {
inflater = LayoutInflater.from(this._context);
}
layoutView = this.layoutView = inflater.inflate(layoutId, null) as com.google.android.material.textfield.TextInputLayout;
if (layoutIdString) {
layoutView = this.layoutView = inflateLayout(this._context, layoutIdString) as com.google.android.material.textfield.TextInputLayout;
editText = this.editText = layoutView.getEditText() as any as com.nativescript.material.textfield.TextInputEditText;
} else {
layoutView = this.layoutView = new com.google.android.material.textfield.TextInputLayout(this._context);
Expand Down
24 changes: 8 additions & 16 deletions src/textview/textview.android.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
import { themer } from '@nativescript-community/ui-material-core';
import { getColorStateList, getFullColorStateList, getHorizontalGravity, getLayout, getVerticalGravity } from '@nativescript-community/ui-material-core/android/utils';
import { getColorStateList, getFullColorStateList, getHorizontalGravity, getLayout, getVerticalGravity, inflateLayout } from '@nativescript-community/ui-material-core/android/utils';
import {
counterMaxLengthProperty,
errorColorProperty,
Expand Down Expand Up @@ -60,32 +60,24 @@ export class TextView extends TextViewBase {
}

public createNativeView() {
let layoutId;
let layoutIdString = 'ns_material_text_view';
const variant = this.variant;
let needsTransparent = false;
if (variant === 'filled') {
if (!filledId) {
filledId = getLayout(this._context, 'ns_material_text_view_filled');
}
layoutId = filledId;

layoutIdString = 'ns_material_text_view_filled';
} else if (variant === 'outline') {
if (!outlineId) {
outlineId = getLayout(this._context, 'ns_material_text_view_outline');
}
layoutId = outlineId;

layoutIdString = 'ns_material_text_view_outline';
} else {
if (!noneId) {
noneId = getLayout(this._context, 'ns_material_text_view');
}
layoutId = noneId;
needsTransparent = true;
}

let layoutView: com.google.android.material.textfield.TextInputLayout;
let editText: com.nativescript.material.textview.TextViewInputEditText;

if (layoutId !== 0) {
layoutView = this.layoutView = android.view.LayoutInflater.from(this._context).inflate(layoutId, null, false) as com.google.android.material.textfield.TextInputLayout;
if (layoutIdString) {
layoutView = this.layoutView = inflateLayout(this._context, layoutIdString) as com.google.android.material.textfield.TextInputLayout;
editText = this.editText = layoutView.getEditText() as com.nativescript.material.textview.TextViewInputEditText;
} else {
layoutView = this.layoutView = new com.google.android.material.textfield.TextInputLayout(this._context);
Expand Down

0 comments on commit 0387a46

Please sign in to comment.