|
| 1 | +package com.tcd.gamelauncher; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.drawable.Drawable; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.ImageView; |
| 10 | +import android.widget.TextView; |
| 11 | +import androidx.annotation.NonNull; |
| 12 | +import androidx.recyclerview.widget.RecyclerView; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class Game { |
| 16 | + String packageName; |
| 17 | + String title; |
| 18 | + transient Drawable icon; |
| 19 | + long totalTimePlayed; |
| 20 | + long lastStartTime; |
| 21 | + |
| 22 | + Game(String packageName, String title, Drawable icon) { |
| 23 | + this.packageName = packageName; |
| 24 | + this.title = title; |
| 25 | + this.icon = icon; |
| 26 | + this.totalTimePlayed = 0; |
| 27 | + this.lastStartTime = 0; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> { |
| 32 | + private final List<Game> gameList; |
| 33 | + private final Context context; |
| 34 | + private final GameLaunchCallback launchCallback; |
| 35 | + private final GameRemoveCallback removeCallback; |
| 36 | + |
| 37 | + interface GameLaunchCallback { |
| 38 | + void onLaunch(String packageName); |
| 39 | + } |
| 40 | + |
| 41 | + interface GameRemoveCallback { |
| 42 | + void onRemove(int position); |
| 43 | + } |
| 44 | + |
| 45 | + public GameAdapter( |
| 46 | + List<Game> gameList, |
| 47 | + Context context, |
| 48 | + GameLaunchCallback launchCallback, |
| 49 | + GameRemoveCallback removeCallback) { |
| 50 | + this.gameList = gameList; |
| 51 | + this.context = context; |
| 52 | + this.launchCallback = launchCallback; |
| 53 | + this.removeCallback = removeCallback; |
| 54 | + } |
| 55 | + |
| 56 | + @NonNull |
| 57 | + @Override |
| 58 | + public GameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 59 | + View view = LayoutInflater.from(context).inflate(R.layout.game_container, parent, false); |
| 60 | + return new GameViewHolder(view); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onBindViewHolder(@NonNull GameViewHolder holder, int position) { |
| 65 | + Game game = gameList.get(position); |
| 66 | + holder.gameTitle.setText(game.title); |
| 67 | + holder.gameIcon.setImageDrawable(game.icon); |
| 68 | + |
| 69 | + holder.openGame.setOnClickListener(v -> launchCallback.onLaunch(game.packageName)); |
| 70 | + |
| 71 | + holder.itemView.setOnClickListener( |
| 72 | + v -> { |
| 73 | + GameFragment gameFragment = new GameFragment(); |
| 74 | + Bundle args = new Bundle(); |
| 75 | + args.putString("PACKAGE_NAME", game.packageName); |
| 76 | + args.putString("GAME_TITLE", game.title); |
| 77 | + args.putLong("TOTAL_TIME_PLAYED", game.totalTimePlayed); |
| 78 | + gameFragment.setArguments(args); |
| 79 | + |
| 80 | + ((MainActivity) context) |
| 81 | + .getSupportFragmentManager() |
| 82 | + .beginTransaction() |
| 83 | + .setCustomAnimations( |
| 84 | + R.anim.enter_from_left, |
| 85 | + R.anim.exit_to_right, |
| 86 | + R.anim.enter_from_right, |
| 87 | + R.anim.exit_to_left) |
| 88 | + .replace(R.id.Framelay, gameFragment) |
| 89 | + .addToBackStack(null) |
| 90 | + .commit(); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public int getItemCount() { |
| 96 | + return gameList.size(); |
| 97 | + } |
| 98 | + |
| 99 | + static class GameViewHolder extends RecyclerView.ViewHolder { |
| 100 | + TextView gameTitle; |
| 101 | + ImageView gameIcon, openGame; |
| 102 | + |
| 103 | + GameViewHolder(@NonNull View itemView) { |
| 104 | + super(itemView); |
| 105 | + gameTitle = itemView.findViewById(R.id.Game_Title); |
| 106 | + gameIcon = itemView.findViewById(R.id.Game_Icon); |
| 107 | + openGame = itemView.findViewById(R.id.Open_Game); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments