getting closer to implementing the recycler view
This commit is contained in:
parent
99643e5be8
commit
50dfc57c61
10 changed files with 125 additions and 65 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/recent_friend_codes_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="15dp"
|
||||
android:text="Recently Watched Friend Codes:"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recent_friend_codes_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView" />
|
||||
|
||||
<!-- <androidx.recyclerview.widget.RecyclerView-->
|
||||
<!-- android:id="@+id/recent_friend_codes_recycler_view"-->
|
||||
<!-- android:layout_width="match_parent"-->
|
||||
<!-- android:layout_height="0dp"-->
|
||||
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
|
||||
<!-- app:layout_constraintTop_toBottomOf="@+id/textView" />-->
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -44,4 +44,21 @@
|
|||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/watch_button" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView3"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Recently Watched Friend Codes:"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/error_text" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recent_friend_codes_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView3"
|
||||
tools:layout_editor_absoluteX="15dp" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -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" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Reference in a new issue