-
Notifications
You must be signed in to change notification settings - Fork 101
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
Review Task 2 #9
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pl.fracz.mcr.comment; | ||
|
||
import pl.fracz.mcr.preferences.ApplicationSettings; | ||
import pl.fracz.mcr.source.SourceFile; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class AbstractComment { | ||
private final SimpleDateFormat CREATION_TIME_FORMAT = new SimpleDateFormat("HH:mm dd.MM.yyyy"); | ||
|
||
private final Type type; | ||
|
||
private final String author; | ||
|
||
protected SourceFile sourceFile; | ||
|
||
private Date date; | ||
|
||
private int lineNumber; | ||
|
||
public AbstractComment(Type type, int lineNumber) { | ||
this.type = type; | ||
this.author = ApplicationSettings.getAuthor(); | ||
this.lineNumber = lineNumber; | ||
this.date = new Date(System.currentTimeMillis()); | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
} | ||
|
||
public Date getDate() { | ||
return date; | ||
} | ||
|
||
public String getDateFormatted() { | ||
return CREATION_TIME_FORMAT.format(date); | ||
} | ||
|
||
public int getLineNumber() { | ||
return lineNumber; | ||
} | ||
|
||
protected String getAuthor() { | ||
return author; | ||
} | ||
|
||
public static enum Type { | ||
TEXT, VOICE | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package pl.fracz.mcr.comment; | ||
|
||
public class CommentNotAddedException extends Exception { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package pl.fracz.mcr.comment; | ||
|
||
import pl.fracz.mcr.source.Line; | ||
|
||
import java.io.File; | ||
|
||
public class Comment extends AbstractComment { | ||
private String text; | ||
|
||
private File file; | ||
|
||
public Comment(AbstractComment.Type type, Line line) { | ||
super(type, line.get()); | ||
} | ||
|
||
public void setText(String text) { | ||
checkValidType(AbstractComment.Type.TEXT); | ||
this.text = text; | ||
} | ||
|
||
public String getText() { | ||
checkValidType(AbstractComment.Type.TEXT); | ||
return text; | ||
} | ||
|
||
public void setFile(File file) { | ||
checkValidType(AbstractComment.Type.VOICE); | ||
this.file = file; | ||
} | ||
|
||
public File getFile() { | ||
checkValidType(AbstractComment.Type.VOICE); | ||
return file; | ||
Comment on lines
+26
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checkValiType() powinna byc w innym miejscu, setter i getter powinny tylko ustawiac i pobierac wartosc zmiennej |
||
} | ||
|
||
private void checkValidType(AbstractComment.Type type) { | ||
if (type != this.getType()) { | ||
throw new IllegalArgumentException("The " + type + " comment is required to set this attribute"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package pl.fracz.mcr.source; | ||
|
||
/* | ||
2013-10-23, fracz, first implementation | ||
2013-10-30, fracz, added syntax highlighting | ||
2014-02-26, fracz, added ability to add voice comment | ||
*/ | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Context; | ||
import android.graphics.Color; | ||
import android.graphics.Typeface; | ||
import android.text.Html; | ||
import android.widget.LinearLayout; | ||
import android.widget.TextView; | ||
import pl.fracz.mcr.comment.AbstractComment; | ||
import pl.fracz.mcr.comment.Comment; | ||
import pl.fracz.mcr.comment.CommentNotAddedException; | ||
|
||
import java.io.File; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
/** | ||
* View that represents one line of code. | ||
*/ | ||
@SuppressLint("ViewConstructor") | ||
public class Line extends LinearLayout implements Serializable { | ||
private static final long serialVersionUID = 3076583280108678995L; | ||
private static final int TWO = 2; | ||
|
||
private final int _lineNumber; | ||
|
||
private final String _lineOfCode; | ||
|
||
// holds the line number | ||
private TextView lineNumberView; | ||
|
||
private TextView lineContent; | ||
|
||
private SourceFile sourceFile; | ||
|
||
private List<Comment> comments; | ||
|
||
public Line(Context context, SourceFile sourceFile, int lineNumber, | ||
String lineOfCode, boolean syntaxColor) { | ||
super(context); | ||
this.sourceFile = sourceFile; | ||
this._lineNumber = lineNumber; | ||
this._lineOfCode = lineOfCode; | ||
setOrientation(LinearLayout.HORIZONTAL); | ||
|
||
lineNumberView = new TextView(getContext()); | ||
lineNumberView.setText(String.format("%d.", lineNumber);); | ||
lineNumberView.setSingleLine(); | ||
lineNumberView.setWidth(30); | ||
addView(lineNumberView); | ||
|
||
TextView lineContent = new TextView(getContext()); | ||
addLineContent(syntaxColor); | ||
} | ||
|
||
public int get() { | ||
return _lineNumber; | ||
} | ||
|
||
/** | ||
* Adds a text comment. | ||
* | ||
* @param comment | ||
* @throws CommentNotAddedException | ||
*/ | ||
public void addTextComment(String comment) throws CommentNotAddedException { | ||
Comment textComment = new Comment(AbstractComment.Type.TEXT, this); | ||
textComment.setText(comment); | ||
comments.add(textComment); | ||
if (comments.size() > 0) { | ||
lineNumberView.setBackgroundColor(Color.parseColor("#008000")); | ||
} | ||
} | ||
|
||
/** | ||
* Adds a voice comment. | ||
* | ||
* @param recordedFile | ||
* @throws CommentNotAddedException | ||
*/ | ||
public void createVoiceComment(File recodedFile) throws CommentNotAddedException { | ||
Comment voiceComment = new Comment(AbstractComment.Type.VOICE, this); | ||
voiceComment.setFile(recodedFile); | ||
comments.add(voiceComment); | ||
if (comments.size() > 0) { | ||
lineNumberView.setBackgroundColor(Color.parseColor("#008000")); | ||
} | ||
} | ||
|
||
// public void addVideoComment(File videoFile) throws CommentNotAddedException { | ||
// } | ||
|
||
private void addLineContent(boolean syntaxColor){ | ||
if (!syntaxColor || !SyntaxHighlighter.canBeHighlighted(syntaxColor)) | ||
lineContent.setText(Html.fromHtml(lineOfCode)); | ||
else | ||
lineContent.setText(SyntaxHighlighter.highlight(Html.fromHtml(lineOfCode))); | ||
lineContent.setTypeface(Typeface.MONOSPACE); | ||
addView(lineContent); | ||
} | ||
|
||
public List<Comment> getComments(){ | ||
return this.comments; | ||
} | ||
|
||
public boolean hasConversation() { | ||
sourceFile.markConversation(this); | ||
return getComments().size() > TWO; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pl.fracz.mcr.source; | ||
|
||
import pl.fracz.mcr.comment.CommentNotAddedException; | ||
|
||
public class NoSelectedLineException extends CommentNotAddedException { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package pl.fracz.mcr.source; | ||
|
||
import android.content.Context; | ||
import android.graphics.Color; | ||
import android.view.View; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. przerwa pomidzy importami |
||
import pl.fracz.mcr.comment.CommentNotAddedException; | ||
import pl.fracz.mcr.preferences.ApplicationSettings; | ||
import pl.fracz.mcr.syntax.PrettifyHighlighter; | ||
import pl.fracz.mcr.syntax.SyntaxHighlighter; | ||
import pl.fracz.mcr.util.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.StringTokenizer; | ||
|
||
public class SourceFile { | ||
private static final SyntaxHighlighter SYNTAX_HIGHLIGHTER = new PrettifyHighlighter(); | ||
|
||
private static final Color SELECTED_LINE_COLOR = Color.parseColor("#444444"); | ||
|
||
private final View.OnClickListener lineHighlighter = view -> { | ||
if (selectedLine != null) { | ||
selectedLine.setBackgroundColor(Color.TRANSPARENT); | ||
} | ||
selectedLine = (Line) view; | ||
selectedLine.setBackgroundColor(SELECTED_LINE_COLOR); | ||
Comment on lines
+26
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. można by wydzielić do innej metody |
||
}; | ||
|
||
private final String sourceCode; | ||
|
||
private final String identifier; | ||
|
||
private final String language; | ||
|
||
private Line selectedLine; | ||
|
||
private String highlightedSourceCode; | ||
|
||
public SourceFile(String sourceCode, String language) { | ||
this.sourceCode = sourceCode; | ||
this.language = language; | ||
this.identifier = calculateSHA1SourceChecksum(); | ||
} | ||
|
||
private String calculateSHA1SourceChecksum() { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance("SHA1"); | ||
byte[] digest = md.digest(sourceCode.getBytes()); | ||
StringBuffer sb = new StringBuffer(); | ||
for (int i = 0; i < digest.length; i++) { | ||
sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1)); | ||
} | ||
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rowniez mozna wydzielic do prywatnej metody |
||
return sb.toString(); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
|
||
public Collection<Line> getLines(Context context) { | ||
StringTokenizer tokenizer = new StringTokenizer(getHighlightedSourceCode(), "\n"); | ||
Collection<Line> lines = new ArrayList<Line>(tokenizer.countTokens()); | ||
while (tokenizer.hasMoreTokens()) { | ||
Line line = new Line(context, this, lines.size() + 1, tokenizer.nextToken()); | ||
line.setOnClickListener(lineHighlighter); | ||
lines.add(line); | ||
} | ||
Comment on lines
+66
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. również wydzielić do osobnej metody |
||
return lines; | ||
} | ||
|
||
private String getHighlightedSourceCode() { | ||
if (highlightedSourceCode == null) { | ||
highlightedSourceCode = highlightSourceCode(); | ||
} | ||
return highlightedSourceCode; | ||
} | ||
|
||
private String highlightSourceCode() { | ||
String code = replaceTabs(); | ||
if (ApplicationSettings.highlightSources()) { | ||
return SYNTAX_HIGHLIGHTER.highlight(code, language); | ||
} else { | ||
return code; | ||
} | ||
Comment on lines
+83
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. zamienić na operator trinarny ? : |
||
} | ||
|
||
private String replaceTabs() { | ||
StringBuilder tabReplacement = new StringBuilder(); | ||
for (int i = 0; i < ApplicationSettings.getTabSize(); i++) { | ||
tabReplacement.append(" "); | ||
} | ||
return sourceCode.replace("\t", tabReplacement.toString()); | ||
} | ||
|
||
public String getIdentifier() { | ||
return identifier; | ||
} | ||
|
||
public void addTextComment(String comment) throws CommentNotAddedException { | ||
if (selectedLine == null) { | ||
throw new NoSelectedLineException(); | ||
} | ||
getSelectedLine().addTextComment(comment); | ||
} | ||
|
||
public void addVoiceComment(File recordedFile) throws CommentNotAddedException { | ||
if (selectedLine == null) { | ||
throw new NoSelectedLineException(); | ||
} | ||
getSelectedLine().createVoiceComment(recordedFile); | ||
} | ||
|
||
public Line getSelectedLine() { | ||
return selectedLine; | ||
} | ||
|
||
public void markConversation(Line line) { | ||
// Nothing to do, it's perfect. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty method |
||
} | ||
|
||
public static SourceFile createFromString(String sourceCode, String language) { | ||
return new SourceFile(sourceCode, language); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnscessary, just repeats constructor |
||
} | ||
|
||
/** | ||
* Creates source file based on a file reference. | ||
*/ | ||
public static SourceFile createFromFile(File sourceFile) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. createSourceFileFromFile or copySourceFile |
||
String sourceCode = FileUtils.read(sourceFile); | ||
return createFromString(sourceCode, FileUtils.getExtension(sourceFile.getName())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Abstract class is not abstract