Skip to content

Commit 4ed212c

Browse files
Merge pull request #8 from D4rK7355608/codex/implement-daily-tip-display-feature
Add daily tip card
2 parents 9fc1fec + 4f428e4 commit 4ed212c

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
3434
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
3535
homeViewModel.getAnnouncementTitle().observe(getViewLifecycleOwner(), title -> binding.announcementTitle.setText(title));
3636
homeViewModel.getAnnouncementSubtitle().observe(getViewLifecycleOwner(), subtitle -> binding.announcementSubtitle.setText(subtitle));
37+
homeViewModel.getDailyTip().observe(getViewLifecycleOwner(), tip -> {
38+
binding.tipText.setText(tip);
39+
binding.shareTipButton.setOnClickListener(v -> shareTip(tip));
40+
});
3741
new FastScrollerBuilder(binding.scrollView)
3842
.useMd2Style()
3943
.build();
@@ -51,4 +55,11 @@ private void initializeAds() {
5155
binding.smallBannerAd.loadAd(new AdRequest.Builder().build());
5256
binding.largeBannerAd.loadAd(new AdRequest.Builder().build());
5357
}
58+
59+
private void shareTip(String tip) {
60+
android.content.Intent shareIntent = new android.content.Intent(android.content.Intent.ACTION_SEND);
61+
shareIntent.setType("text/plain");
62+
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, tip);
63+
startActivity(android.content.Intent.createChooser(shareIntent, getString(com.d4rk.androidtutorials.java.R.string.share_using)));
64+
}
5465
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public class HomeViewModel extends AndroidViewModel {
1818

1919
private final MutableLiveData<String> announcementTitle = new MutableLiveData<>();
2020
private final MutableLiveData<String> announcementSubtitle = new MutableLiveData<>();
21+
private final MutableLiveData<String> dailyTip = new MutableLiveData<>();
2122

2223
public HomeViewModel(@NonNull Application application) {
2324
super(application);
2425
homeRepository = new HomeRepository(application);
2526

2627
announcementTitle.setValue(application.getString(R.string.announcement_title));
2728
announcementSubtitle.setValue(application.getString(R.string.announcement_subtitle));
29+
dailyTip.setValue(homeRepository.getDailyTip());
2830
}
2931

3032
/**
@@ -41,6 +43,13 @@ public LiveData<String> getAnnouncementSubtitle() {
4143
return announcementSubtitle;
4244
}
4345

46+
/**
47+
* Provides a LiveData for the tip of the day text.
48+
*/
49+
public LiveData<String> getDailyTip() {
50+
return dailyTip;
51+
}
52+
4453
/**
4554
* Returns an Intent that opens the Google Play Store page for your app.
4655
* The HomeFragment can startActivity(...) on it.

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
import android.content.Intent;
55
import android.net.Uri;
66
import com.d4rk.androidtutorials.java.BuildConfig;
7+
import com.d4rk.androidtutorials.java.R;
78

89
/**
910
* Repository for Home screen data/logic.
1011
* Here you can manage fetching or storing any data needed on the HomeFragment.
1112
*/
1213
public class HomeRepository {
1314

15+
private final Context context;
16+
1417
public HomeRepository(Context context) {
15-
context.getApplicationContext();
18+
this.context = context.getApplicationContext();
1619
}
1720

1821
/**
@@ -25,4 +28,14 @@ public Intent getPlayStoreIntent() {
2528
intent.setPackage("com.android.vending");
2629
return intent;
2730
}
31+
32+
/**
33+
* Returns a daily tip based on the current date.
34+
*/
35+
public String getDailyTip() {
36+
String[] tips = context.getResources().getStringArray(R.array.daily_tips);
37+
long daysSinceEpoch = System.currentTimeMillis() / (24L * 60 * 60 * 1000);
38+
int index = (int) (daysSinceEpoch % tips.length);
39+
return tips[index];
40+
}
2841
}

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

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

137+
<com.google.android.material.card.MaterialCardView
138+
android:id="@+id/tip_card"
139+
style="@style/Widget.Material3.CardView.Outlined"
140+
android:layout_width="match_parent"
141+
android:layout_height="wrap_content"
142+
android:layout_marginHorizontal="16dp"
143+
android:layout_marginBottom="16dp">
144+
145+
<androidx.appcompat.widget.LinearLayoutCompat
146+
android:layout_width="match_parent"
147+
android:layout_height="wrap_content"
148+
android:gravity="center_vertical"
149+
android:orientation="horizontal"
150+
android:padding="16dp">
151+
152+
<com.google.android.material.textview.MaterialTextView
153+
android:id="@+id/tip_text"
154+
android:layout_width="0dp"
155+
android:layout_height="wrap_content"
156+
android:layout_weight="1"
157+
android:text="@string/tip_of_the_day"
158+
android:textAppearance="@style/TextAppearance.Material3.BodyMedium" />
159+
160+
<com.google.android.material.button.MaterialButton
161+
android:id="@+id/share_tip_button"
162+
style="@style/Widget.Material3.Button.Icon"
163+
android:layout_width="wrap_content"
164+
android:layout_height="wrap_content"
165+
android:contentDescription="@string/share"
166+
app:icon="@drawable/ic_share" />
167+
</androidx.appcompat.widget.LinearLayoutCompat>
168+
</com.google.android.material.card.MaterialCardView>
169+
137170
<com.airbnb.lottie.LottieAnimationView
138171
android:id="@+id/learningAnimation"
139172
android:layout_width="379dp"

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,12 @@
396396
<string name="ad_storage">Ad storage</string>
397397
<string name="ad_user_data">Ad user data</string>
398398
<string name="ad_personalization">Ad personalization</string>
399+
<string name="tip_of_the_day">Tip of the Day</string>
400+
<string-array name="daily_tips">
401+
<item>Use ConstraintLayout to create responsive UIs.</item>
402+
<item>Leverage ViewBinding for safer UI code.</item>
403+
<item>Implement RecyclerView for smooth scrolling lists.</item>
404+
<item>Use Material Design 3 components for a modern look.</item>
405+
<item>Profile your app regularly to track performance.</item>
406+
</string-array>
399407
</resources>

0 commit comments

Comments
 (0)