Skip to content
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
@@ -0,0 +1,11 @@
package com.d4rk.androidtutorials.java.data.model;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

public record PromotedApp(
@DrawableRes int iconResId,
@StringRes int nameResId,
@StringRes int descriptionResId,
String packageName
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
binding.tipText.setText(tip);
binding.shareTipButton.setOnClickListener(v -> shareTip(tip));
});
setupPromotions(LayoutInflater.from(requireContext()));
new FastScrollerBuilder(binding.scrollView)
.useMd2Style()
.build();
Expand All @@ -62,4 +63,17 @@ private void shareTip(String tip) {
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, tip);
startActivity(android.content.Intent.createChooser(shareIntent, getString(com.d4rk.androidtutorials.java.R.string.share_using)));
}

private void setupPromotions(LayoutInflater inflater) {
ViewGroup container = binding.promotedAppsContainer;
for (com.d4rk.androidtutorials.java.data.model.PromotedApp app : homeViewModel.getPromotedApps()) {
com.d4rk.androidtutorials.java.databinding.PromotedAppItemBinding itemBinding =
com.d4rk.androidtutorials.java.databinding.PromotedAppItemBinding.inflate(inflater, container, false);
itemBinding.appIcon.setImageResource(app.iconResId());
itemBinding.appName.setText(app.nameResId());
itemBinding.appDescription.setText(app.descriptionResId());
itemBinding.appButton.setOnClickListener(v -> startActivity(homeViewModel.getPromotedAppIntent(app.packageName())));
container.addView(itemBinding.getRoot());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import android.app.Application;
import android.content.Intent;
import java.util.ArrayList;
import java.util.List;

import com.d4rk.androidtutorials.java.data.model.PromotedApp;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
Expand All @@ -19,6 +23,7 @@ public class HomeViewModel extends AndroidViewModel {
private final MutableLiveData<String> announcementTitle = new MutableLiveData<>();
private final MutableLiveData<String> announcementSubtitle = new MutableLiveData<>();
private final MutableLiveData<String> dailyTip = new MutableLiveData<>();
private final List<PromotedApp> promotedApps = new ArrayList<>();

public HomeViewModel(@NonNull Application application) {
super(application);
Expand All @@ -27,6 +32,22 @@ public HomeViewModel(@NonNull Application application) {
announcementTitle.setValue(application.getString(R.string.announcement_title));
announcementSubtitle.setValue(application.getString(R.string.announcement_subtitle));
dailyTip.setValue(homeRepository.getDailyTip());

promotedApps.add(new PromotedApp(
R.mipmap.ic_shortcut_kotlin_edition,
R.string.kotlin_edition_name,
R.string.kotlin_edition_description,
application.getString(R.string.package_ast_kotlin)));
promotedApps.add(new PromotedApp(
R.drawable.ic_shop,
R.string.cart_calculator_name,
R.string.cart_calculator_description,
application.getString(R.string.package_cart_calculator)));
promotedApps.add(new PromotedApp(
R.drawable.ic_safety_check_tinted,
R.string.cleaner_android_name,
R.string.cleaner_android_description,
application.getString(R.string.package_cleaner_android)));
}

/**
Expand Down Expand Up @@ -57,4 +78,18 @@ public LiveData<String> getDailyTip() {
public Intent getOpenPlayStoreIntent() {
return homeRepository.getPlayStoreIntent();
}

/**
* List of apps to promote on the Home screen.
*/
public List<PromotedApp> getPromotedApps() {
return promotedApps;
}

/**
* Builds an intent to open the Google Play listing for the provided package.
*/
public Intent getPromotedAppIntent(String packageName) {
return homeRepository.getAppPlayStoreIntent(packageName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public Intent getPlayStoreIntent() {
return intent;
}

/**
* Returns an Intent that opens the Google Play Store page for the provided package.
*/
public Intent getAppPlayStoreIntent(String packageName) {
String url = "https://play.google.com/store/apps/details?id=" + packageName;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setPackage("com.android.vending");
return intent;
}

/**
* Returns a daily tip based on the current date.
*/
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,35 @@
app:adSize="MEDIUM_RECTANGLE"
app:adUnitId="@string/ad_banner_unit_id" />

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/other_apps_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/other_apps_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/other_apps_title"
android:textAppearance="@style/TextAppearance.Material3.TitleMedium" />

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/promoted_apps_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</HorizontalScrollView>
</androidx.appcompat.widget.LinearLayoutCompat>

<com.google.android.material.card.MaterialCardView
android:id="@+id/tip_card"
style="@style/Widget.Material3.CardView.Outlined"
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/res/layout/promoted_app_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.Material3.CardView.Elevated"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp">

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/app_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
android:src="@drawable/ic_android" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="App"
android:textAppearance="@style/TextAppearance.Material3.BodyMedium" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/app_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />

<com.google.android.material.button.MaterialButton
android:id="@+id/app_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/get_on_google_play"
app:icon="@drawable/ic_play_store_tinted" />
</androidx.appcompat.widget.LinearLayoutCompat>
</com.google.android.material.card.MaterialCardView>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,11 @@
<item>Use Material Design 3 components for a modern look.</item>
<item>Profile your app regularly to track performance.</item>
</string-array>
<string name="other_apps_title">More apps by the developer</string>
<string name="kotlin_edition_name">AST - Kotlin Edition</string>
<string name="kotlin_edition_description">Learn Android development with Kotlin.</string>
<string name="cart_calculator_name">Cart Calculator</string>
<string name="cart_calculator_description">Quickly total up your shopping cart.</string>
<string name="cleaner_android_name">Cleaner for Android</string>
<string name="cleaner_android_description">Free up space and optimize your device.</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/untranslatable_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@
<string name="error_loading_changelog">Error loading changelog</string>

<string name="ad_banner_unit_id">ca-app-pub-5294151573817700/3821250346</string>
<string name="package_ast_kotlin" translatable="false">com.d4rk.androidtutorials</string>
<string name="package_cart_calculator" translatable="false">com.d4rk.cartcalculator</string>
<string name="package_cleaner_android" translatable="false">com.d4rk.cleaner</string>
</resources>