Skip to content

Commit c7bb355

Browse files
Merge pull request #9 from D4rK7355608/codex/add-promotional-module-for-other-apps
Add developer app promotion module
2 parents 50ce574 + 0705370 commit c7bb355

File tree

8 files changed

+155
-0
lines changed

8 files changed

+155
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.d4rk.androidtutorials.java.data.model;
2+
3+
import androidx.annotation.DrawableRes;
4+
import androidx.annotation.StringRes;
5+
6+
public record PromotedApp(
7+
@DrawableRes int iconResId,
8+
@StringRes int nameResId,
9+
@StringRes int descriptionResId,
10+
String packageName
11+
) {}

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/home/HomeFragment.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
3838
binding.tipText.setText(tip);
3939
binding.shareTipButton.setOnClickListener(v -> shareTip(tip));
4040
});
41+
setupPromotions(LayoutInflater.from(requireContext()));
4142
new FastScrollerBuilder(binding.scrollView)
4243
.useMd2Style()
4344
.build();
@@ -62,4 +63,17 @@ private void shareTip(String tip) {
6263
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, tip);
6364
startActivity(android.content.Intent.createChooser(shareIntent, getString(com.d4rk.androidtutorials.java.R.string.share_using)));
6465
}
66+
67+
private void setupPromotions(LayoutInflater inflater) {
68+
ViewGroup container = binding.promotedAppsContainer;
69+
for (com.d4rk.androidtutorials.java.data.model.PromotedApp app : homeViewModel.getPromotedApps()) {
70+
com.d4rk.androidtutorials.java.databinding.PromotedAppItemBinding itemBinding =
71+
com.d4rk.androidtutorials.java.databinding.PromotedAppItemBinding.inflate(inflater, container, false);
72+
itemBinding.appIcon.setImageResource(app.iconResId());
73+
itemBinding.appName.setText(app.nameResId());
74+
itemBinding.appDescription.setText(app.descriptionResId());
75+
itemBinding.appButton.setOnClickListener(v -> startActivity(homeViewModel.getPromotedAppIntent(app.packageName())));
76+
container.addView(itemBinding.getRoot());
77+
}
78+
}
6579
}

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/home/HomeViewModel.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import android.app.Application;
44
import android.content.Intent;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import com.d4rk.androidtutorials.java.data.model.PromotedApp;
59

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

2328
public HomeViewModel(@NonNull Application application) {
2429
super(application);
@@ -27,6 +32,22 @@ public HomeViewModel(@NonNull Application application) {
2732
announcementTitle.setValue(application.getString(R.string.announcement_title));
2833
announcementSubtitle.setValue(application.getString(R.string.announcement_subtitle));
2934
dailyTip.setValue(homeRepository.getDailyTip());
35+
36+
promotedApps.add(new PromotedApp(
37+
R.mipmap.ic_shortcut_kotlin_edition,
38+
R.string.kotlin_edition_name,
39+
R.string.kotlin_edition_description,
40+
application.getString(R.string.package_ast_kotlin)));
41+
promotedApps.add(new PromotedApp(
42+
R.drawable.ic_shop,
43+
R.string.cart_calculator_name,
44+
R.string.cart_calculator_description,
45+
application.getString(R.string.package_cart_calculator)));
46+
promotedApps.add(new PromotedApp(
47+
R.drawable.ic_safety_check_tinted,
48+
R.string.cleaner_android_name,
49+
R.string.cleaner_android_description,
50+
application.getString(R.string.package_cleaner_android)));
3051
}
3152

3253
/**
@@ -57,4 +78,18 @@ public LiveData<String> getDailyTip() {
5778
public Intent getOpenPlayStoreIntent() {
5879
return homeRepository.getPlayStoreIntent();
5980
}
81+
82+
/**
83+
* List of apps to promote on the Home screen.
84+
*/
85+
public List<PromotedApp> getPromotedApps() {
86+
return promotedApps;
87+
}
88+
89+
/**
90+
* Builds an intent to open the Google Play listing for the provided package.
91+
*/
92+
public Intent getPromotedAppIntent(String packageName) {
93+
return homeRepository.getAppPlayStoreIntent(packageName);
94+
}
6095
}

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/home/repository/HomeRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public Intent getPlayStoreIntent() {
2929
return intent;
3030
}
3131

32+
/**
33+
* Returns an Intent that opens the Google Play Store page for the provided package.
34+
*/
35+
public Intent getAppPlayStoreIntent(String packageName) {
36+
String url = "https://play.google.com/store/apps/details?id=" + packageName;
37+
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
38+
intent.setPackage("com.android.vending");
39+
return intent;
40+
}
41+
3242
/**
3343
* Returns a daily tip based on the current date.
3444
*/

app/src/main/res/layout/fragment_home.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,35 @@
134134
app:adSize="MEDIUM_RECTANGLE"
135135
app:adUnitId="@string/ad_banner_unit_id" />
136136

137+
<androidx.appcompat.widget.LinearLayoutCompat
138+
android:id="@+id/other_apps_section"
139+
android:layout_width="match_parent"
140+
android:layout_height="wrap_content"
141+
android:orientation="vertical"
142+
android:layout_marginBottom="16dp"
143+
android:layout_marginStart="16dp"
144+
android:layout_marginEnd="16dp">
145+
146+
<com.google.android.material.textview.MaterialTextView
147+
android:id="@+id/other_apps_title"
148+
android:layout_width="match_parent"
149+
android:layout_height="wrap_content"
150+
android:text="@string/other_apps_title"
151+
android:textAppearance="@style/TextAppearance.Material3.TitleMedium" />
152+
153+
<HorizontalScrollView
154+
android:layout_width="match_parent"
155+
android:layout_height="wrap_content"
156+
android:scrollbars="none">
157+
158+
<androidx.appcompat.widget.LinearLayoutCompat
159+
android:id="@+id/promoted_apps_container"
160+
android:layout_width="wrap_content"
161+
android:layout_height="wrap_content"
162+
android:orientation="horizontal" />
163+
</HorizontalScrollView>
164+
</androidx.appcompat.widget.LinearLayoutCompat>
165+
137166
<com.google.android.material.card.MaterialCardView
138167
android:id="@+id/tip_card"
139168
style="@style/Widget.Material3.CardView.Outlined"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
style="@style/Widget.Material3.CardView.Elevated"
5+
android:layout_width="160dp"
6+
android:layout_height="wrap_content"
7+
android:layout_marginEnd="8dp">
8+
9+
<androidx.appcompat.widget.LinearLayoutCompat
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:orientation="vertical"
13+
android:padding="8dp">
14+
15+
<androidx.appcompat.widget.AppCompatImageView
16+
android:id="@+id/app_icon"
17+
android:layout_width="48dp"
18+
android:layout_height="48dp"
19+
android:layout_gravity="center_horizontal"
20+
android:contentDescription="@null"
21+
android:src="@drawable/ic_android" />
22+
23+
<com.google.android.material.textview.MaterialTextView
24+
android:id="@+id/app_name"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:layout_marginTop="8dp"
28+
android:text="App"
29+
android:textAppearance="@style/TextAppearance.Material3.BodyMedium" />
30+
31+
<com.google.android.material.textview.MaterialTextView
32+
android:id="@+id/app_description"
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:text="Description"
36+
android:textAppearance="@style/TextAppearance.Material3.BodySmall" />
37+
38+
<com.google.android.material.button.MaterialButton
39+
android:id="@+id/app_button"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:layout_marginTop="8dp"
43+
android:text="@string/get_on_google_play"
44+
app:icon="@drawable/ic_play_store_tinted" />
45+
</androidx.appcompat.widget.LinearLayoutCompat>
46+
</com.google.android.material.card.MaterialCardView>

app/src/main/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,11 @@
404404
<item>Use Material Design 3 components for a modern look.</item>
405405
<item>Profile your app regularly to track performance.</item>
406406
</string-array>
407+
<string name="other_apps_title">More apps by the developer</string>
408+
<string name="kotlin_edition_name">AST - Kotlin Edition</string>
409+
<string name="kotlin_edition_description">Learn Android development with Kotlin.</string>
410+
<string name="cart_calculator_name">Cart Calculator</string>
411+
<string name="cart_calculator_description">Quickly total up your shopping cart.</string>
412+
<string name="cleaner_android_name">Cleaner for Android</string>
413+
<string name="cleaner_android_description">Free up space and optimize your device.</string>
407414
</resources>

app/src/main/res/values/untranslatable_strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@
4444
<string name="error_loading_changelog">Error loading changelog</string>
4545

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

0 commit comments

Comments
 (0)