Skip to content

Commit

Permalink
docs: update example apps with custom aString
Browse files Browse the repository at this point in the history
  • Loading branch information
tynn committed Jul 12, 2022
1 parent 6e2b647 commit e41a922
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/java/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/AppTheme.NativeActionBar">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static xyz.tynn.astring.app.shared.AStringsKt.getError;
import static xyz.tynn.astring.app.shared.AStringsKt.getErrorIcon;
import static xyz.tynn.astring.app.shared.AStringsKt.getHint;
import static xyz.tynn.astring.app.shared.AStringsKt.getKotlin;
import static xyz.tynn.astring.app.shared.AStringsKt.getMessage;
import static xyz.tynn.astring.app.shared.AStringsKt.getStateDescription;
import static xyz.tynn.astring.app.shared.AStringsKt.getTextType;
Expand All @@ -36,6 +37,7 @@
import static xyz.tynn.astring.material.AStringSnackbar.make;
import static xyz.tynn.astring.material.AStringSnackbar.setAction;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
Expand All @@ -46,20 +48,18 @@

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;

import xyz.tynn.astring.app.shared.AStringsKt;
import xyz.tynn.astring.app.shared.R;
import xyz.tynn.astring.app.shared.databinding.ActivityMainBinding;
import xyz.tynn.astring.appcompat.AStringAlertDialog;
import xyz.tynn.astring.appcompat.AStringAlertDialogBuilder;
import xyz.tynn.astring.material.AStringSnackbar;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends Activity {

private CharSequence tag = null;

Expand Down Expand Up @@ -89,6 +89,7 @@ private void setupAStrings(ActivityMainBinding binding) {
setupDialogs(binding);
setText(binding.viewAppId, AStringsKt.getAStringPackage());
setText(binding.viewAppVersion, AStringsKt.getAStringVersion());
setText(binding.viewAppLocale, AStringsKt.getAStringLocale());
setText(binding.viewText, AStringsKt.getText());
setText(binding.viewTextType, getTextType(), SPANNABLE);
append(binding.viewAppend, getAppend());
Expand All @@ -104,7 +105,7 @@ private void setupAStrings(ActivityMainBinding binding) {

private void setupDialogs(ActivityMainBinding binding) {
binding.buttonAlertDialog.setOnClickListener(v -> {
MaterialAlertDialogBuilder b = new MaterialAlertDialogBuilder(this);
MaterialAlertDialogBuilder b = new MaterialAlertDialogBuilder(this, R.style.DialogTheme);
AStringAlertDialogBuilder.setMessage(b, getMessage());
AStringAlertDialogBuilder.setPositiveButton(b, getAction1(), null);
AStringAlertDialogBuilder.setNeutralButton(b, getAction2(), null);
Expand Down Expand Up @@ -142,6 +143,10 @@ private void setupDialogs(ActivityMainBinding binding) {
t.setText(getDialog().invoke(this));
t.show();
});
binding.buttonKotlinSupport.setOnClickListener(v -> {
makeText(this, getKotlin(), LENGTH_SHORT)
.show();
});
}

private void doNothing(View v) {
Expand Down
13 changes: 13 additions & 0 deletions app/java/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<resources>

<style name="AppTheme.NativeActionBar">
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="android:actionBarTheme">@android:style/ThemeOverlay.Material.Dark.ActionBar</item>
</style>

<style name="DialogTheme">
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MainActivity : AppCompatActivity() {
setupDialogs()
viewAppId.setText(aStringPackage)
viewAppVersion.setText(aStringVersion)
viewAppLocale.setText(aStringLocale)
viewText.setText(text)
viewTextType.setText(textType, TextView.BufferType.SPANNABLE)
viewAppend.append(append)
Expand Down Expand Up @@ -100,5 +101,9 @@ class MainActivity : AppCompatActivity() {
.apply { setText(aString(dialog)) }
.show()
}
buttonKotlinSupport.setOnClickListener {
makeToast(this@MainActivity, kotlin, LENGTH_SHORT)
.show()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2022 Christian Schmitz
// SPDX-License-Identifier: Apache-2.0

package xyz.tynn.astring.app.shared;

import static android.util.Log.w;
import static xyz.tynn.astring.app.shared.KotlinAStringKt.kotlinAString;

import android.content.Context;
import android.os.Parcel;

import androidx.annotation.NonNull;

import xyz.tynn.astring.AString;

final class KotlinAString implements AString {

@NonNull
@Override
public CharSequence invoke(@NonNull Context context) {
try {
return kotlinAString("Kotlin supported");
} catch (Throwable t) {
w("KotlinAString", t);
return "Java only";
}
}

@Override
public void writeToParcel(Parcel dest, int flags) {
}

public static final Creator<KotlinAString> CREATOR = new Creator<KotlinAString>() {

@Override
public KotlinAString createFromParcel(Parcel source) {
return new KotlinAString();
}

@Override
public KotlinAString[] newArray(int size) {
return new KotlinAString[size];
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2022 Christian Schmitz
// SPDX-License-Identifier: Apache-2.0

package xyz.tynn.astring.app.shared;

import static androidx.core.os.ConfigurationCompat.getLocales;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Parcel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Locale;

import xyz.tynn.astring.AString;

final class LocaleAString implements AString {

@Nullable
@Override
public CharSequence invoke(@NonNull Context context) {
Configuration configuration = context.getResources().getConfiguration();
Locale locale = getLocales(configuration).get(0);
return locale == null ? null : locale.getDisplayName(locale);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
}

public static final Creator<LocaleAString> CREATOR = new Creator<LocaleAString>() {

@Override
public LocaleAString createFromParcel(Parcel source) {
return new LocaleAString();
}

@Override
public LocaleAString[] newArray(int size) {
return new LocaleAString[size];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ public val dialog: AString = StringResource(R.string.astring_dialog)
public val error: AString = StringResource(R.string.astring_error)
public val errorIcon: AString = StringResource(R.string.astring_error_with_icon)
public val hint: AString = QuantityStringResource(R.plurals.astring_text_hint, 0)
public val kotlin: AString = KotlinAString()
public val message: AString = StringResource(R.string.astring_message)
public val stateDescription: AString = "state description".asAString()
public val text: AString = TextResource(R.string.astring_text)
public val textType: AString = QuantityTextResource(R.plurals.astring_text_type, 5)
public val title: AString = StringResource(R.string.astring_title)
public val tooltipText: AString = QuantityStringResource(R.plurals.astring_tooltip_text, 2, 2)

public val aStringLocale: AString = LocaleAString()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2022 Christian Schmitz
// SPDX-License-Identifier: Apache-2.0

package xyz.tynn.astring.app.shared

internal fun kotlinAString(
message: String?,
) = message!!
15 changes: 15 additions & 0 deletions app/shared/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
android:text="@string/string_text"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
android:id="@+id/view_app_locale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="@string/string_text"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -171,5 +180,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/action_toast_update" />

<Button
android:id="@+id/button_kotlin_support"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/action_kotlin_support" />
</LinearLayout>
</ScrollView>
1 change: 1 addition & 0 deletions app/shared/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<string name="action_alert_dialog">AlertDialog</string>
<string name="action_alert_dialog_legacy">AlertDialog from framework</string>
<string name="action_kotlin_support">Kotlin Support</string>
<string name="action_snackbar">Snackbar</string>
<string name="action_snackbar_update">Snackbar with update</string>
<string name="action_toast">Toast</string>
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ allprojects {
subprojects {
pluginManager.withPlugin('com.android.application') {
apply from: "$rootDir/android.gradle"
android.defaultConfig.minSdk 21
}

pluginManager.withPlugin('com.android.library') {
Expand Down

0 comments on commit e41a922

Please sign in to comment.