Skip to content

Commit

Permalink
[#62] card rotation MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Aug 17, 2023
1 parent 6c0bfc1 commit cf7aa61
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import com.google.android.material.slider.Slider;
import java.io.File;
import java.util.concurrent.atomic.AtomicReference;
import phone.vishnu.quotes.R;
Expand All @@ -51,6 +52,7 @@ public class CustomiseFragment extends Fragment {
private TextView quoteTextView, authorTextView;
private CardView cardView;
private ImageView moveIV;
private Slider angleSlider;

public CustomiseFragment() {}

Expand Down Expand Up @@ -79,6 +81,7 @@ public View onCreateView(
cardView = inflate.findViewById(R.id.customiseCardView);

moveIV = inflate.findViewById(R.id.customiseMoveIV);
angleSlider = inflate.findViewById(R.id.customiseAngleSlider);

return inflate;
}
Expand Down Expand Up @@ -128,13 +131,17 @@ public void onViewCreated(

float cardX = sharedPreferenceHelper.getCardX();
float cardY = sharedPreferenceHelper.getCardY();
float cardRotation = sharedPreferenceHelper.getCardRotation();

if (cardX != -1)
cardView.setX(
(constraintLayout.getWidth() - offsetViewBounds.left) / cardX);
if (cardY != -1)
cardView.setY(
(constraintLayout.getHeight() - offsetViewBounds.top) / cardY);

if (cardRotation != -1) cardView.setRotation(cardRotation);

});

AtomicReference<Float> dX = new AtomicReference<>(cardView.getX());
Expand Down Expand Up @@ -185,5 +192,13 @@ public void onViewCreated(
}
return true;
});

angleSlider.addOnChangeListener(
(slider, value, fromUser) -> {
if (fromUser) {
cardView.setRotation(value);
sharedPreferenceHelper.setCardRotation(value);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

float cardX = sharedPreferenceHelper.getCardX();
float cardY = sharedPreferenceHelper.getCardY();
float cardRotation = sharedPreferenceHelper.getCardRotation();

if (cardX != -1)
cardView.setX(
(constraintLayout.getWidth() - offsetViewBounds.left) / cardX);
if (cardY != -1)
cardView.setY(
(constraintLayout.getHeight() - offsetViewBounds.top) / cardY);

if (cardRotation != -1) cardView.setRotation(cardRotation);
});

shareIcon.setOnClickListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private Bitmap getBitmap(Context context, Quote q) {
float fontSize = sharedPreferenceHelper.getFontSizePreference();
float cardX = sharedPreferenceHelper.getCardX();
float cardY = sharedPreferenceHelper.getCardY();
float cardRotation = sharedPreferenceHelper.getCardRotation();

ConstraintLayout constraintLayout = shareView.findViewById(R.id.shareConstraintLayout);

Expand Down Expand Up @@ -230,6 +231,8 @@ private Bitmap getBitmap(Context context, Quote q) {
? heightPixels - cardViewHeight - DP_8
: finalY); // bottom bound

if (cardRotation != -1) cardView.setRotation(cardRotation);

Bitmap bitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
shareView.draw(new Canvas(bitmap));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class SharedPreferenceHelper {
private final String CARD_X = "cardXFloat";
private final String CARD_Y = "cardYFloat";

private final String CARD_ROTATION = "cardRotationFloat";

private final SharedPreferences sharedPreferences;

public SharedPreferenceHelper(Context context) {
Expand Down Expand Up @@ -194,6 +196,14 @@ public float getCardY() {
return sharedPreferences.getFloat(CARD_Y, -1);
}

public void setCardRotation(float rotation) {
sharedPreferences.edit().putFloat(CARD_ROTATION, rotation).apply();
}

public float getCardRotation() {
return sharedPreferences.getFloat(CARD_ROTATION, -1);
}

public void setShareButtonAction(int action) {
sharedPreferences.edit().putInt(SHARE_BUTTON_ACTION, action).apply();
}
Expand Down Expand Up @@ -265,6 +275,7 @@ public void resetSharedPreferences() {
setFavActionReverse(false);
setCardX(-1);
setCardY(-1);
setCardRotation(-1);
}

public ArrayList<String> getFontListToBeRemoved() {
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/layout/fragment_customise.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,22 @@

</androidx.cardview.widget.CardView>

<com.google.android.material.slider.Slider
android:id="@+id/customiseAngleSlider"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="64dp"
android:padding="8dp"
android:stepSize="1"
android:theme="@style/Theme.MaterialComponents.DayNight"
android:value="0"
android:valueFrom="-90"
android:valueTo="90"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit cf7aa61

Please sign in to comment.