Skip to content

Commit

Permalink
Pin on long press
Browse files Browse the repository at this point in the history
  • Loading branch information
m-i-n-a-r committed Jan 20, 2021
1 parent 3aa63a6 commit 44c27f7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ The main purpose of the app is provide a **random choice** in different ways. Th

## Features
- Every tab in the bottom navigation bar contains a type of random choice. The available types are:
- **Roulette** -> chooses between a specified number of options, entered from the user. Includes a list of recent options easy to select, delete or restore.
- **Roulette** -> chooses between a specified number of options, entered from the user. Includes a list of recent options easy to select, pin, delete or restore.
- **Coin** -> simply flips a coin and prints the result.
- **Dice** -> throws a dice and prints the result.
- **Magic Ball** -> provides randomly chosen answers to any question.
Expand Down
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Expand Up @@ -10,8 +10,8 @@
{
"type": "SINGLE",
"filters": [],
"versionCode": 29,
"versionName": "1.13",
"versionCode": 30,
"versionName": "1.14",
"outputFile": "app-release.apk"
}
]
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/minar/randomix/adapter/RecentAdapter.java
@@ -1,6 +1,8 @@
package com.minar.randomix.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -15,6 +17,8 @@

import java.util.List;

import static androidx.core.content.ContextCompat.getColor;

public class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.RecentHolder> {
private final List<List<String>> recentList;
private final LayoutInflater inflater;
Expand Down Expand Up @@ -63,6 +67,14 @@ public RecentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
@Override
public void onBindViewHolder(@NonNull RecentHolder holder, int position) {
List<String> current = recentList.get(position);
if (current.contains("HorribleWorkaroundToPin")) {
holder.optionList.setTypeface(null, Typeface.BOLD);
holder.optionList.setTextColor(getThemeAccentColor(holder.adapter.inflater.getContext()));
}
else {
holder.optionList.setTypeface(null, Typeface.NORMAL);
holder.optionList.setTextColor(getColor(holder.adapter.inflater.getContext(), R.color.goodGray));
}
holder.optionList.setText(RecentUtils.fromOptionList(current));
}

Expand All @@ -75,4 +87,11 @@ public int getItemCount() {
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}

// Get the accent color
public static int getThemeAccentColor (final Context context) {
final TypedValue value = new TypedValue ();
context.getTheme ().resolveAttribute (R.attr.colorAccent, value, true);
return value.data;
}
}
Expand Up @@ -48,7 +48,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
sp = PreferenceManager.getDefaultSharedPreferences(getContext());
String recent = sp.getString("recent", "");
Gson gson = new Gson();
Type type = new TypeToken<List<List<String>>>() {}.getType();
Type type = new TypeToken<List<List<String>>>() {
}.getType();
recentList = gson.fromJson(recent, type);
if (recentList == null) recentList = new ArrayList<>();

Expand Down Expand Up @@ -78,16 +79,17 @@ private void populateRecentLayout(RecyclerView recentListLayout, ConstraintLayou
if (recentList == null || recentList.isEmpty())
rouletteBottomSheet.findViewById(R.id.recentNoResult).setVisibility(View.VISIBLE);

// Case 2, populate the recycler
// Case 2, populate the recycler
else {
adapter = new RecentAdapter(getContext(), recentList);
recentListLayout.setLayoutManager(new LinearLayoutManager(requireContext()));
recentListLayout.setAdapter(adapter);
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position, List<String> optionList,View view) {
public void onItemClick(int position, List<String> optionList, View view) {
roulette.restoreOption(optionList);
}

@Override
public void onItemLongClick(int position, View view) {
// Pin this option list (i.e. avoid deletion)
Expand All @@ -100,6 +102,7 @@ public void onItemLongClick(int position, View view) {
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
// Remove swiped item from list and notify the RecyclerView
Expand All @@ -126,7 +129,17 @@ void deleteRecent(int index, Context context) {

// Pin the selected list, to change it's layout and avoid its automatic deletion
void pinRecent(int position, Context context) {
Gson gson = new Gson();
fetchRecentList(context);

// I know, it's awful, but it was the only way to avoid big changes and crashes
if (!recentList.get(position).contains("HorribleWorkaroundToPin"))
recentList.get(position).add("HorribleWorkaroundToPin");
else recentList.get(position).remove("HorribleWorkaroundToPin");

adapter.notifyItemChanged(position);
String json = gson.toJson(recentList);
sp.edit().putString("recent", json).apply();
}

// Update the stored value of the recent options
Expand All @@ -151,18 +164,32 @@ void restoreLatest(Context context) {
private void insertInRecent(List<String> newRecent) {
// Check if there's a duplicate, create a copy to avoid overwriting
List<String> values = new ArrayList<>(newRecent);
for (List<String> elem : recentList) {
if (newRecent.size() == elem.size()) {
for (List<String> recent : recentList) {
if (newRecent.size() == recent.size() || newRecent.size() == recent.size() - 1) {
newRecent = new ArrayList<>(newRecent);
elem = new ArrayList<>(elem);
recent = new ArrayList<>(recent);
Collections.sort(newRecent);
Collections.sort(elem);
if (newRecent.equals(elem)) return;

// Don't consider the pin entry
recent.remove("HorribleWorkaroundToPin");
Collections.sort(recent);
if (newRecent.equals(recent)) return;
}
}
// Keep 10 recent only, avoid deleting pinned recent
if (recentList.size() > 9) {
// Remove the first non-pinned element
for (List<String> recent : recentList) {
if (!recent.contains("HorribleWorkaroundToPin")) {
recentList.remove(recent);
recentList.add(values);
break;
}
}
// No element is removable, don't save
}
// Keep 10 recent only
recentList.add(values);
if (recentList.size() > 10) recentList.remove(0);
// Recent list not empty, save
else recentList.add(values);
}

// Fetch the recent list from share preferences or initialize it
Expand Down
@@ -1,5 +1,6 @@
package com.minar.randomix.utilities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -13,9 +14,13 @@ public static List<String> toOptionList(String formattedOptions) {
// Given a list of options, return the corresponding formatted string
public static String fromOptionList(List<String> optionList) {
StringBuilder result = new StringBuilder();
for (String option : optionList) {
// Remove the awful pinning workaround
List<String> copy = new ArrayList<String>(optionList);
copy.remove("HorribleWorkaroundToPin");

for (String option : copy) {
result.append(option);
if (optionList.size() > optionList.indexOf(option) + 1)
if (copy.size() > copy.indexOf(option) + 1)
result.append(" | ");
}
return result.toString();
Expand Down

0 comments on commit 44c27f7

Please sign in to comment.