From 50dfc57c61a01dc20a42129246b458bf90e26154 Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Tue, 4 May 2021 20:18:17 -0600 Subject: getting closer to implementing the recycler view --- .../example/wiimmterfaceandroid/MainActivity.java | 21 ++++-- .../wiimmterfaceandroid/RecentCodesAdapter.java | 10 ++- .../wiimmterfaceandroid/RecentCodesFragment.java | 2 +- .../wiimmterfaceandroid/WatchCodeFragment.java | 85 +++++++++++++++++++--- .../wiimmterfaceandroid/model/FriendCode.java | 5 +- .../viewmodel/FriendCodeViewModel.java | 11 +-- .../wiimmterfaceandroid/wiimmfi/RoomData.java | 2 +- .../res/layout/fragment_recent_friend_codes.xml | 31 -------- .../main/res/layout/friend_code_input_fragment.xml | 17 +++++ .../main/res/layout/recent_friend_codes_item.xml | 6 +- 10 files changed, 125 insertions(+), 65 deletions(-) delete mode 100644 app/src/main/res/layout/fragment_recent_friend_codes.xml 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 recentFCList; + ObservableArrayList 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 recentFCList; - AppDatabase database; - public WatchCodeFragment(AppDatabase database, List 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>() { + @Override + public void onChanged(ObservableList sender) { + getActivity().runOnUiThread(() -> { + adapter.notifyDataSetChanged(); + }); + } + + @Override + public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount) { + getActivity().runOnUiThread(() -> { + adapter.notifyItemRangeChanged(positionStart, itemCount); + }); + } + + @Override + public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) { + getActivity().runOnUiThread(() -> { + adapter.notifyItemRangeInserted(positionStart, itemCount); + }); + } + + @Override + public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount) { + getActivity().runOnUiThread(() -> { + adapter.notifyItemMoved(fromPosition, toPosition); + }); + } + + @Override + public void onItemRangeRemoved(ObservableList 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 players = new ArrayList<>(); public RoomData (ArrayList players, String playerLink, String friendCode) { - this.playerLink = getPlayerLink(); this.friendCode = friendCode; + this.playerLink = getPlayerLink(); Document doc = null; if (playerLink == null) { diff --git a/app/src/main/res/layout/fragment_recent_friend_codes.xml b/app/src/main/res/layout/fragment_recent_friend_codes.xml deleted file mode 100644 index e05ce4a..0000000 --- a/app/src/main/res/layout/fragment_recent_friend_codes.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/friend_code_input_fragment.xml b/app/src/main/res/layout/friend_code_input_fragment.xml index 4f3b9ea..a70e6df 100644 --- a/app/src/main/res/layout/friend_code_input_fragment.xml +++ b/app/src/main/res/layout/friend_code_input_fragment.xml @@ -44,4 +44,21 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/watch_button" /> + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/recent_friend_codes_item.xml b/app/src/main/res/layout/recent_friend_codes_item.xml index e56629a..7609cdc 100644 --- a/app/src/main/res/layout/recent_friend_codes_item.xml +++ b/app/src/main/res/layout/recent_friend_codes_item.xml @@ -9,10 +9,8 @@ android:id="@+id/recent_friend_code_button" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_margin="15dp" - android:backgroundTint="#B3B3B3" - android:text="0000-0000-0000" - android:textColor="#383838" + android:text="Button" + app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> \ No newline at end of file -- cgit v1.2.3