Skip to content

Handle code card state #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
843 changes: 0 additions & 843 deletions app/src/main/java/pl/tkadziolka/snipmeandroid/Bridge.java

This file was deleted.

41 changes: 41 additions & 0 deletions app/src/main/java/pl/tkadziolka/snipmeandroid/bridge/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ private SnippetFilterType(final int index) {
}
}

public enum UserReaction {
NONE(0),
LIKE(1),
DISLIKE(2);

private int index;
private UserReaction(final int index) {
this.index = index;
}
}

public enum ModelState {
LOADING(0),
LOADED(1),
Expand Down Expand Up @@ -141,6 +152,12 @@ public void setOwner(@Nullable Owner setterArg) {
this.owner = setterArg;
}

private @Nullable Boolean isOwner;
public @Nullable Boolean getIsOwner() { return isOwner; }
public void setIsOwner(@Nullable Boolean setterArg) {
this.isOwner = setterArg;
}

private @Nullable String timeAgo;
public @Nullable String getTimeAgo() { return timeAgo; }
public void setTimeAgo(@Nullable String setterArg) {
Expand All @@ -153,6 +170,12 @@ public void setVoteResult(@Nullable Long setterArg) {
this.voteResult = setterArg;
}

private @Nullable UserReaction userReaction;
public @Nullable UserReaction getUserReaction() { return userReaction; }
public void setUserReaction(@Nullable UserReaction setterArg) {
this.userReaction = setterArg;
}

public static final class Builder {
private @Nullable String uuid;
public @NonNull Builder setUuid(@Nullable String setterArg) {
Expand All @@ -179,6 +202,11 @@ public static final class Builder {
this.owner = setterArg;
return this;
}
private @Nullable Boolean isOwner;
public @NonNull Builder setIsOwner(@Nullable Boolean setterArg) {
this.isOwner = setterArg;
return this;
}
private @Nullable String timeAgo;
public @NonNull Builder setTimeAgo(@Nullable String setterArg) {
this.timeAgo = setterArg;
Expand All @@ -189,15 +217,22 @@ public static final class Builder {
this.voteResult = setterArg;
return this;
}
private @Nullable UserReaction userReaction;
public @NonNull Builder setUserReaction(@Nullable UserReaction setterArg) {
this.userReaction = setterArg;
return this;
}
public @NonNull Snippet build() {
Snippet pigeonReturn = new Snippet();
pigeonReturn.setUuid(uuid);
pigeonReturn.setTitle(title);
pigeonReturn.setCode(code);
pigeonReturn.setLanguage(language);
pigeonReturn.setOwner(owner);
pigeonReturn.setIsOwner(isOwner);
pigeonReturn.setTimeAgo(timeAgo);
pigeonReturn.setVoteResult(voteResult);
pigeonReturn.setUserReaction(userReaction);
return pigeonReturn;
}
}
Expand All @@ -208,8 +243,10 @@ public static final class Builder {
toMapResult.put("code", (code == null) ? null : code.toMap());
toMapResult.put("language", (language == null) ? null : language.toMap());
toMapResult.put("owner", (owner == null) ? null : owner.toMap());
toMapResult.put("isOwner", isOwner);
toMapResult.put("timeAgo", timeAgo);
toMapResult.put("voteResult", voteResult);
toMapResult.put("userReaction", userReaction == null ? null : userReaction.index);
return toMapResult;
}
static @NonNull Snippet fromMap(@NonNull Map<String, Object> map) {
Expand All @@ -224,10 +261,14 @@ public static final class Builder {
pigeonResult.setLanguage((language == null) ? null : SnippetLanguage.fromMap((Map)language));
Object owner = map.get("owner");
pigeonResult.setOwner((owner == null) ? null : Owner.fromMap((Map)owner));
Object isOwner = map.get("isOwner");
pigeonResult.setIsOwner((Boolean)isOwner);
Object timeAgo = map.get("timeAgo");
pigeonResult.setTimeAgo((String)timeAgo);
Object voteResult = map.get("voteResult");
pigeonResult.setVoteResult((voteResult == null) ? null : ((voteResult instanceof Integer) ? (Integer)voteResult : (Long)voteResult));
Object userReaction = map.get("userReaction");
pigeonResult.setUserReaction(userReaction == null ? null : UserReaction.values()[(int)userReaction]);
return pigeonResult;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import android.text.Spanned
import android.text.format.DateUtils
import android.text.style.ForegroundColorSpan
import androidx.core.text.getSpans
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import org.koin.core.component.inject
import pl.tkadziolka.snipmeandroid.Bridge
import pl.tkadziolka.snipmeandroid.bridge.Bridge
import pl.tkadziolka.snipmeandroid.bridge.ModelPlugin
import pl.tkadziolka.snipmeandroid.domain.reaction.UserReaction
import pl.tkadziolka.snipmeandroid.domain.snippets.Owner
import pl.tkadziolka.snipmeandroid.domain.snippets.Snippet
import pl.tkadziolka.snipmeandroid.domain.snippets.SnippetCode
Expand Down Expand Up @@ -86,7 +86,9 @@ class MainModelPlugin : ModelPlugin<Bridge.MainModelBridge>(), Bridge.MainModelB
code = it.code.toModelSnippetCode()
language = it.language.toModelSnippetLanguage()
owner = it.owner.toModelOwner()
isOwner = it.isOwner
voteResult = (it.numberOfLikes - it.numberOfDislikes).toLong()
userReaction = it.userReaction.toModelUserReaction()
timeAgo = DateUtils.getRelativeTimeSpanString(
it.modifiedAt.time,
Date().time,
Expand Down Expand Up @@ -125,4 +127,11 @@ class MainModelPlugin : ModelPlugin<Bridge.MainModelBridge>(), Bridge.MainModelB
it.color = foregroundColor.toLong()
it
}

private fun UserReaction.toModelUserReaction(): Bridge.UserReaction =
when(this) {
UserReaction.LIKE -> Bridge.UserReaction.LIKE
UserReaction.DISLIKE -> Bridge.UserReaction.DISLIKE
else -> Bridge.UserReaction.NONE
}
}
8 changes: 8 additions & 0 deletions flutter_module/bridge/main_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class Snippet {
SnippetCode? code;
SnippetLanguage? language;
Owner? owner;
bool? isOwner;
String? timeAgo;
int? voteResult;
UserReaction? userReaction;
}

class SnippetCode {
Expand Down Expand Up @@ -87,6 +89,12 @@ class SnippetFilter {
SnippetFilterType? type;
}

enum UserReaction {
none,
like,
dislike
}

// State

enum ModelState { loading, loaded, error }
Expand Down
9 changes: 9 additions & 0 deletions flutter_module/lib/generated/assets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
///This file is automatically generated. DO NOT EDIT, all your changes would be lost.
class Assets {
Assets._();

static const String reactionDislike = 'assets/images/icons/reaction_dislike.png';
static const String reactionLike = 'assets/images/icons/reaction_like.png';
static const String reactionUndefined = 'assets/images/icons/reaction_undefined.png';

}
16 changes: 16 additions & 0 deletions flutter_module/lib/model/main_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ enum SnippetFilterType {
shared,
}

enum UserReaction {
none,
like,
dislike,
}

enum ModelState {
loading,
loaded,
Expand All @@ -80,17 +86,21 @@ class Snippet {
this.code,
this.language,
this.owner,
this.isOwner,
this.timeAgo,
this.voteResult,
this.userReaction,
});

String? uuid;
String? title;
SnippetCode? code;
SnippetLanguage? language;
Owner? owner;
bool? isOwner;
String? timeAgo;
int? voteResult;
UserReaction? userReaction;

Object encode() {
final Map<Object?, Object?> pigeonMap = <Object?, Object?>{};
Expand All @@ -99,8 +109,10 @@ class Snippet {
pigeonMap['code'] = code?.encode();
pigeonMap['language'] = language?.encode();
pigeonMap['owner'] = owner?.encode();
pigeonMap['isOwner'] = isOwner;
pigeonMap['timeAgo'] = timeAgo;
pigeonMap['voteResult'] = voteResult;
pigeonMap['userReaction'] = userReaction?.index;
return pigeonMap;
}

Expand All @@ -118,8 +130,12 @@ class Snippet {
owner: pigeonMap['owner'] != null
? Owner.decode(pigeonMap['owner']!)
: null,
isOwner: pigeonMap['isOwner'] as bool?,
timeAgo: pigeonMap['timeAgo'] as String?,
voteResult: pigeonMap['voteResult'] as int?,
userReaction: pigeonMap['userReaction'] != null
? UserReaction.values[pigeonMap['userReaction']! as int]
: null,
);
}
}
Expand Down
10 changes: 10 additions & 0 deletions flutter_module/lib/presentation/styles/text_styles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class TextStyles extends Text {
style: const TextStyle(color: Colors.grey),
);

const TextStyles.secondaryBold(this.text, {Key? key})
: super(
text,
key: key,
style: const TextStyle(
color: Colors.grey,
fontWeight: FontWeight.bold,
),
);

const TextStyles.label(this.text, {Key? key})
: super(
text,
Expand Down
75 changes: 75 additions & 0 deletions flutter_module/lib/presentation/widgets/snippet_details_bar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
import 'package:flutter_module/generated/assets.dart';
import 'package:flutter_module/model/main_model.dart';
import 'package:flutter_module/presentation/styles/dimens.dart';
import 'package:flutter_module/presentation/styles/surface_styles.dart';
import 'package:flutter_module/presentation/styles/text_styles.dart';

class SnippetDetailsBar extends StatelessWidget {
const SnippetDetailsBar({
Key? key,
required this.snippet,
}) : super(key: key);

final Snippet snippet;

@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextStyles.regular(snippet.language?.raw ?? "Unknown language"),
const SizedBox(height: Dimens.m),
snippet.isOwner == true
? TextStyles.secondaryBold(snippet.owner?.login ?? "")
: TextStyles.secondary(snippet.owner?.login ?? ""),
const SizedBox(height: Dimens.s),
TextStyles.helper(snippet.timeAgo ?? "")
],
),
),
_UserReactionIndicator(reaction: snippet.userReaction),
const SizedBox(width: Dimens.l),
SurfaceStyles.rateBox(
TextStyles.title(
_getVoteCountText(snippet.voteResult),
),
)
],
);
}

String _getVoteCountText(int? voteResult) {
const defaultValue = '+0';
if (voteResult == null) return defaultValue;
if (voteResult == 0) return defaultValue;
if (voteResult > 0) return '+$voteResult';
return '-$voteResult';
}
}

class _UserReactionIndicator extends StatelessWidget {
const _UserReactionIndicator({
Key? key,
this.reaction,
}) : super(key: key);

final UserReaction? reaction;

@override
Widget build(BuildContext context) {
if (reaction == UserReaction.like) {
return Image.asset(Assets.reactionLike);
}

if (reaction == UserReaction.dislike) {
return Image.asset(Assets.reactionDislike);
}

return Image.asset(Assets.reactionUndefined);
}
}
45 changes: 2 additions & 43 deletions flutter_module/lib/presentation/widgets/snippet_list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter_module/presentation/styles/dimens.dart';
import 'package:flutter_module/presentation/styles/surface_styles.dart';
import 'package:flutter_module/presentation/styles/text_styles.dart';
import 'package:flutter_module/presentation/widgets/code_text_view.dart';
import 'package:flutter_module/presentation/widgets/snippet_details_bar.dart';

class SnippetListTile extends HookWidget {
const SnippetListTile({
Expand Down Expand Up @@ -61,46 +62,4 @@ class SnippetListTile extends HookWidget {
),
);
}
}

class SnippetDetailsBar extends StatelessWidget {
const SnippetDetailsBar({
Key? key,
required this.snippet,
}) : super(key: key);

final Snippet snippet;

@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextStyles.regular(snippet.language?.raw ?? ""),
const SizedBox(height: Dimens.m),
TextStyles.secondary(snippet.owner?.login ?? ""),
const SizedBox(height: Dimens.s),
TextStyles.helper(snippet.timeAgo ?? "")
],
),
),
SurfaceStyles.rateBox(
TextStyles.title(
_getVoteCountText(snippet.voteResult),
),
)
],
);
}

String _getVoteCountText(int? voteResult) {
if (voteResult == null) return '-';
if (voteResult == 0) return '-';
if (voteResult > 0) return '+$voteResult';
return '-$voteResult';
}
}
}
Loading