diff options
author | Bryson Steck <steck.bryson@gmail.com> | 2021-05-04 20:18:17 -0600 |
---|---|---|
committer | Bryson Steck <steck.bryson@gmail.com> | 2021-05-04 20:18:17 -0600 |
commit | 50dfc57c61a01dc20a42129246b458bf90e26154 (patch) | |
tree | 4c9e38c1fb4130b566b9393b317b7c7c104a51ba /app/src/main/java/com/example/wiimmterfaceandroid | |
parent | 99643e5be84af7e8da217ed48597ceb3c9d3bc85 (diff) | |
download | wiimmfi-watcher-50dfc57c61a01dc20a42129246b458bf90e26154.tar wiimmfi-watcher-50dfc57c61a01dc20a42129246b458bf90e26154.tar.gz wiimmfi-watcher-50dfc57c61a01dc20a42129246b458bf90e26154.tar.bz2 |
getting closer to implementing the recycler view
Diffstat (limited to 'app/src/main/java/com/example/wiimmterfaceandroid')
7 files changed, 106 insertions, 30 deletions
diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java b/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java index 45c20da..71b25fa 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java @@ -3,6 +3,7 @@ package com.example.wiimmterfaceandroid; import androidx.appcompat.app.AppCompatActivity; +import androidx.databinding.ObservableArrayList; import androidx.fragment.app.FragmentContainerView; import androidx.room.Room; @@ -14,23 +15,33 @@ import com.example.wiimmterfaceandroid.model.FriendCode; import java.util.List; public class MainActivity extends AppCompatActivity { - List<FriendCode> recentFCList; + ObservableArrayList<FriendCode> recentFCList = new ObservableArrayList<>(); + AppDatabase database; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FragmentContainerView fcInput = findViewById(R.id.room_fragment); - AppDatabase database = Room.databaseBuilder(this, AppDatabase.class, "friend-codes-db").build(); - recentFCList = database.getFriendCodeDao().getAll(); + this.database = Room.databaseBuilder(this, AppDatabase.class, "friend-codes-db").build(); + new Thread(() -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + recentFCList.addAll(database.getFriendCodeDao().getAll()); + }).start(); + + setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() - .replace(R.id.friend_code_input_fragment, new WatchCodeFragment(database, recentFCList), null) + .replace(R.id.friend_code_input_fragment, new WatchCodeFragment(), null) .setReorderingAllowed(true) .commit(); getSupportFragmentManager().beginTransaction() - .replace(R.id.room_fragment, new RecentCodesFragment(database, recentFCList), null) + .replace(R.id.room_fragment, new RecentCodesFragment(), null) .setReorderingAllowed(true) .commit(); } diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java b/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java index 1aad0a5..7d278de 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java @@ -4,12 +4,15 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.databinding.ObservableArrayList; import androidx.recyclerview.widget.RecyclerView; import com.example.wiimmterfaceandroid.model.FriendCode; +import com.google.android.material.card.MaterialCardView; +import com.google.android.material.textview.MaterialTextView; import java.util.List; @@ -27,13 +30,13 @@ public class RecentCodesAdapter extends RecyclerView.Adapter<RecentCodesAdapter. @NonNull @Override - public RecentCodesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recent_friend_codes_item, parent, false); - return new RecentCodesAdapter.ViewHolder(view); + return new ViewHolder(view); } @Override - public void onBindViewHolder(@NonNull RecentCodesAdapter.ViewHolder holder, int position) { + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Button fcButton = holder.itemView.findViewById(R.id.recent_friend_code_button); FriendCode currentFC = entries.get(position); fcButton.setText(currentFC.friendCode); @@ -47,6 +50,7 @@ public class RecentCodesAdapter extends RecyclerView.Adapter<RecentCodesAdapter. public int getItemCount() { return entries.size(); } + class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(@NonNull View itemView) { super(itemView); diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java b/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java index c7554f9..bde507f 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java @@ -29,7 +29,7 @@ import androidx.recyclerview.widget.RecyclerView; public class RecentCodesFragment extends Fragment { public RecentCodesFragment() { - super(R.layout.fragment_recent_friend_codes); + } FriendCodeViewModel viewModel; diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java index 55c6a63..98ef40b 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java @@ -8,29 +8,90 @@ import android.widget.EditText; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.databinding.ObservableList; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; -import com.example.wiimmterfaceandroid.database.AppDatabase; import com.example.wiimmterfaceandroid.model.FriendCode; import com.example.wiimmterfaceandroid.viewmodel.FriendCodeViewModel; import com.example.wiimmterfaceandroid.wiimmfi.WiimmfiActivity; import com.google.android.material.textview.MaterialTextView; -import java.util.List; - public class WatchCodeFragment extends Fragment { - List<FriendCode> recentFCList; - AppDatabase database; - public WatchCodeFragment(AppDatabase database, List<FriendCode> recentFCList) { + public WatchCodeFragment() { super(R.layout.friend_code_input_fragment); - this.database = database; - this.recentFCList = recentFCList; + } + + public boolean isValidFriendCode(String friendCode) { + String[] friendCodeSplit = friendCode.split("-"); + boolean valid = false; + if (friendCodeSplit.length == 3) { + for (String friendCodePart : friendCodeSplit) { + valid = friendCodePart.length() == 4; + if (!valid) { + break; + } + } + } + return valid; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + FriendCodeViewModel viewModel = new ViewModelProvider(getActivity()).get(FriendCodeViewModel.class); + + RecentCodesAdapter adapter = new RecentCodesAdapter( + viewModel.getEntries(), + (entry) -> { + viewModel.setCurrentEntry(entry); + Intent intent = new Intent(view.getContext(), WiimmfiActivity.class); + intent.putExtra("friendCode", entry.friendCode); + startActivity(intent); + } + ); + viewModel.getEntries().addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList<FriendCode>>() { + @Override + public void onChanged(ObservableList<FriendCode> sender) { + getActivity().runOnUiThread(() -> { + adapter.notifyDataSetChanged(); + }); + } + + @Override + public void onItemRangeChanged(ObservableList<FriendCode> sender, int positionStart, int itemCount) { + getActivity().runOnUiThread(() -> { + adapter.notifyItemRangeChanged(positionStart, itemCount); + }); + } + + @Override + public void onItemRangeInserted(ObservableList<FriendCode> sender, int positionStart, int itemCount) { + getActivity().runOnUiThread(() -> { + adapter.notifyItemRangeInserted(positionStart, itemCount); + }); + } + + @Override + public void onItemRangeMoved(ObservableList<FriendCode> sender, int fromPosition, int toPosition, int itemCount) { + getActivity().runOnUiThread(() -> { + adapter.notifyItemMoved(fromPosition, toPosition); + }); + } + + @Override + public void onItemRangeRemoved(ObservableList<FriendCode> sender, int positionStart, int itemCount) { + getActivity().runOnUiThread(() -> { + adapter.notifyItemRangeRemoved(positionStart, itemCount); + }); + } + }); + RecyclerView recyclerView = view.findViewById(R.id.recent_friend_codes_recycler_view); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + recyclerView.setAdapter(adapter); Button watchButton = view.findViewById(R.id.watch_button); EditText friendCode = view.findViewById(R.id.friend_code_edit_text); MaterialTextView errorText = view.findViewById(R.id.error_text); @@ -38,19 +99,21 @@ public class WatchCodeFragment extends Fragment { @Override public void onClick(View v) { Intent intent = new Intent(view.getContext(), WiimmfiActivity.class); - FriendCodeViewModel friendCodeViewModel = new FriendCodeViewModel(friendCode.getText().toString()); - if (friendCodeViewModel.getFullFriendCode() == null) { + if (!isValidFriendCode(friendCode.getText().toString())) { errorText.setText("ERROR: Insert a friend code in the format XXXX-XXXX-XXXX"); friendCode.setText(""); } else { errorText.setText(""); + viewModel.saveFriendCode("", friendCode.getText().toString()); // FriendCodeObj friendCodeObj = new FriendCodeObj(); // friendCodeObj.friendCode = friendCode.getText().toString(); // database.getFriendCodeDao().insert(friendCodeObj); - intent.putExtra("friendCode", friendCodeViewModel.getFullFriendCode()); + intent.putExtra("friendCode", friendCode.getText().toString()); startActivity(intent); } } }); + + } } diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/model/FriendCode.java b/app/src/main/java/com/example/wiimmterfaceandroid/model/FriendCode.java index cb13138..3aad0a1 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/model/FriendCode.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/model/FriendCode.java @@ -9,10 +9,13 @@ import java.io.Serializable; @Entity public class FriendCode implements Serializable { + @PrimaryKey (autoGenerate = true) + public long id; + @ColumnInfo(name="name") public String name; - @PrimaryKey + @NonNull @ColumnInfo(name="friendCode") public String friendCode; diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java b/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java index cf31868..ac039ff 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java @@ -25,7 +25,7 @@ public class FriendCodeViewModel extends AndroidViewModel { public FriendCodeViewModel(Application app) { super(app); saving.setValue(false); - db = Room.databaseBuilder(app, AppDatabase.class, "fc-database").build(); + db = Room.databaseBuilder(app, AppDatabase.class, "friend-codes-db").build(); new Thread(() -> { try { Thread.sleep(1000); @@ -56,17 +56,12 @@ public class FriendCodeViewModel extends AndroidViewModel { saving.setValue(true); new Thread(() -> { if (currentEntry.getValue() != null) { - FriendCode current = currentEntry.getValue(); - current.name = name; - current.friendCode = friendCode; - db.getFriendCodeDao().update(current); - int index = entries.indexOf(current); - entries.set(index, current); + } else { FriendCode newEntry = new FriendCode(); newEntry.name = name; newEntry.friendCode = friendCode; - entries.add(newEntry); + db.getFriendCodeDao().insert(newEntry); } saving.postValue(false); diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java b/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java index 2fccffc..46f35d8 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java @@ -15,8 +15,8 @@ public class RoomData { ArrayList<Player> players = new ArrayList<>(); public RoomData (ArrayList<Player> players, String playerLink, String friendCode) { - this.playerLink = getPlayerLink(); this.friendCode = friendCode; + this.playerLink = getPlayerLink(); Document doc = null; if (playerLink == null) { |