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
Expand Up @@ -34,6 +34,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
homeViewModel.getAnnouncementTitle().observe(getViewLifecycleOwner(), title -> binding.announcementTitle.setText(title));
homeViewModel.getAnnouncementSubtitle().observe(getViewLifecycleOwner(), subtitle -> binding.announcementSubtitle.setText(subtitle));
homeViewModel.getDailyTip().observe(getViewLifecycleOwner(), tip -> {
binding.tipText.setText(tip);
binding.shareTipButton.setOnClickListener(v -> shareTip(tip));
});
new FastScrollerBuilder(binding.scrollView)
.useMd2Style()
.build();
Expand All @@ -51,4 +55,11 @@ private void initializeAds() {
binding.smallBannerAd.loadAd(new AdRequest.Builder().build());
binding.largeBannerAd.loadAd(new AdRequest.Builder().build());
}

private void shareTip(String tip) {
android.content.Intent shareIntent = new android.content.Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, tip);
startActivity(android.content.Intent.createChooser(shareIntent, getString(com.d4rk.androidtutorials.java.R.string.share_using)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ 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<>();

public HomeViewModel(@NonNull Application application) {
super(application);
homeRepository = new HomeRepository(application);

announcementTitle.setValue(application.getString(R.string.announcement_title));
announcementSubtitle.setValue(application.getString(R.string.announcement_subtitle));
dailyTip.setValue(homeRepository.getDailyTip());
}

/**
Expand All @@ -41,6 +43,13 @@ public LiveData<String> getAnnouncementSubtitle() {
return announcementSubtitle;
}

/**
* Provides a LiveData for the tip of the day text.
*/
public LiveData<String> getDailyTip() {
return dailyTip;
}

/**
* Returns an Intent that opens the Google Play Store page for your app.
* The HomeFragment can startActivity(...) on it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import android.content.Intent;
import android.net.Uri;
import com.d4rk.androidtutorials.java.BuildConfig;
import com.d4rk.androidtutorials.java.R;

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

private final Context context;

public HomeRepository(Context context) {
context.getApplicationContext();
this.context = context.getApplicationContext();
}

/**
Expand All @@ -25,4 +28,14 @@ public Intent getPlayStoreIntent() {
intent.setPackage("com.android.vending");
return intent;
}

/**
* Returns a daily tip based on the current date.
*/
public String getDailyTip() {
String[] tips = context.getResources().getStringArray(R.array.daily_tips);
long daysSinceEpoch = System.currentTimeMillis() / (24L * 60 * 60 * 1000);
int index = (int) (daysSinceEpoch % tips.length);
return tips[index];
}
}
33 changes: 33 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,39 @@
app:adSize="MEDIUM_RECTANGLE"
app:adUnitId="@string/ad_banner_unit_id" />

<com.google.android.material.card.MaterialCardView
android:id="@+id/tip_card"
style="@style/Widget.Material3.CardView.Outlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginBottom="16dp">

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/tip_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/tip_of_the_day"
android:textAppearance="@style/TextAppearance.Material3.BodyMedium" />

<com.google.android.material.button.MaterialButton
android:id="@+id/share_tip_button"
style="@style/Widget.Material3.Button.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/share"
app:icon="@drawable/ic_share" />
</androidx.appcompat.widget.LinearLayoutCompat>
</com.google.android.material.card.MaterialCardView>

<com.airbnb.lottie.LottieAnimationView
android:id="@+id/learningAnimation"
android:layout_width="379dp"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,12 @@
<string name="ad_storage">Ad storage</string>
<string name="ad_user_data">Ad user data</string>
<string name="ad_personalization">Ad personalization</string>
<string name="tip_of_the_day">Tip of the Day</string>
<string-array name="daily_tips">
<item>Use ConstraintLayout to create responsive UIs.</item>
<item>Leverage ViewBinding for safer UI code.</item>
<item>Implement RecyclerView for smooth scrolling lists.</item>
<item>Use Material Design 3 components for a modern look.</item>
<item>Profile your app regularly to track performance.</item>
</string-array>
</resources>