Skip to content

Commit

Permalink
Remove context dependency from EventAdapter
Browse files Browse the repository at this point in the history
- Introduce IResourceManager to provide resource contract, move towards
  a more testable code base
- Add type safety in WebServer and Adapter

Signed-off-by: Arka Prava Basu <[email protected]>
  • Loading branch information
archie94 committed Sep 1, 2018
1 parent 108f388 commit 8161171
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
25 changes: 15 additions & 10 deletions src/main/java/org/havenapp/main/ListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

import org.havenapp.main.database.HavenEventDB;
import org.havenapp.main.model.Event;
import org.havenapp.main.resources.IResourceManager;
import org.havenapp.main.resources.ResourceManager;
import org.havenapp.main.service.SignalSender;
import org.havenapp.main.ui.EventActivity;
import org.havenapp.main.ui.EventAdapter;
Expand All @@ -66,6 +68,7 @@ public class ListActivity extends AppCompatActivity {
private EventAdapter adapter;
private List<Event> events = new ArrayList<>();
private PreferenceManager preferences;
private IResourceManager resourceManager;

private int modifyPos = -1;

Expand All @@ -80,6 +83,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_list);
Log.d("Main", "onCreate");

resourceManager = new ResourceManager(this);
preferences = new PreferenceManager(this.getApplicationContext());
recyclerView = findViewById(R.id.main_list);
fab = findViewById(R.id.fab);
Expand Down Expand Up @@ -153,7 +157,7 @@ public void onClick(View v) {
findViewById(R.id.empty_view).setVisibility(View.GONE);
}

adapter = new EventAdapter(ListActivity.this, events);
adapter = new EventAdapter(events, resourceManager);
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setAdapter(adapter);

Expand Down Expand Up @@ -193,8 +197,8 @@ public void run ()

HavenEventDB.getDatabase(ListActivity.this).getEventDAO().delete(event);

Snackbar.make(recyclerView, getString(R.string.event_deleted), Snackbar.LENGTH_SHORT)
.setAction(getString(R.string.undo), new View.OnClickListener() {
Snackbar.make(recyclerView, resourceManager.getString(R.string.event_deleted), Snackbar.LENGTH_SHORT)
.setAction(resourceManager.getString(R.string.undo), new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.removeCallbacks(runnableDelete);
Expand Down Expand Up @@ -238,11 +242,12 @@ protected void onRestoreInstanceState(Bundle savedInstanceState) {
protected void onResume() {
super.onResume();

resourceManager = new ResourceManager(this);
final long newCount = HavenEventDB.getDatabase(this).getEventDAO().count();

if (newCount > events.size()) {
events = HavenEventDB.getDatabase(this).getEventDAO().getAllEventDesc();
adapter = new EventAdapter(ListActivity.this, events);
adapter = new EventAdapter(events, resourceManager);
recyclerView.setAdapter(adapter);

adapter.SetOnItemClickListener(new EventAdapter.OnItemClickListener() {
Expand All @@ -263,7 +268,7 @@ public void onItemClick(View view, int position) {
events.add(0,event);
adapter.notifyItemInserted(0);
adapter.notifyDataSetChanged();
initialCount = newCount;
**/

Expand Down Expand Up @@ -344,8 +349,8 @@ public void run ()

handler.postDelayed(runnableDelete, 3000);

Snackbar.make(recyclerView, getString(R.string.events_deleted), Snackbar.LENGTH_SHORT)
.setAction(getString(R.string.undo), v -> {
Snackbar.make(recyclerView, resourceManager.getString(R.string.events_deleted), Snackbar.LENGTH_SHORT)
.setAction(resourceManager.getString(R.string.undo), v -> {
handler.removeCallbacks(runnableDelete);

for (Event event : removedEvents) {
Expand All @@ -367,7 +372,7 @@ private void showLicenses ()
.withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR)
.withAboutIconShown(true)
.withAboutVersionShown(true)
.withAboutAppName(getString(R.string.app_name))
.withAboutAppName(resourceManager.getString(R.string.app_name))
//start the activity
.start(this);
}
Expand All @@ -379,15 +384,15 @@ private void testNotifications ()
SignalSender sender = SignalSender.getInstance(this, preferences.getSignalUsername().trim());
ArrayList<String> recip = new ArrayList<>();
recip.add(preferences.getSmsNumber());
sender.sendMessage(recip, getString(R.string.signal_test_message), null);
sender.sendMessage(recip, resourceManager.getString(R.string.signal_test_message), null);
}
else if (!TextUtils.isEmpty(preferences.getSmsNumber())) {

SmsManager manager = SmsManager.getDefault();

StringTokenizer st = new StringTokenizer(preferences.getSmsNumber(),",");
while (st.hasMoreTokens())
manager.sendTextMessage(st.nextToken(), null, getString(R.string.signal_test_message), null, null);
manager.sendTextMessage(st.nextToken(), null, resourceManager.getString(R.string.signal_test_message), null, null);

}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/havenapp/main/dao/EventDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ interface EventDAO {

@Query("SELECT COUNT(*) FROM EVENT")
fun count() : Int
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/havenapp/main/resources/IResourceManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.havenapp.main.resources

/**
* A Resource Manager Contract.
* Created by Arka Prava Basu <[email protected]> on 1/9/18.
*/
interface IResourceManager {
fun getString(id: Int): String
}
15 changes: 15 additions & 0 deletions src/main/java/org/havenapp/main/resources/ResourceManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.havenapp.main.resources

import android.content.Context

/**
* An implementation of [IResourceManager] which requires [Context] for providing resources.
* <p>
* Created by Arka Prava Basu <[email protected]> on 1/9/18.
*/
class ResourceManager(private val context: Context): IResourceManager {

override fun getString(id: Int): String {
return context.getString(id)
}
}
5 changes: 4 additions & 1 deletion src/main/java/org/havenapp/main/service/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import fi.iki.elonen.NanoHTTPD;
Expand Down Expand Up @@ -96,14 +97,16 @@ else if (inSid == null || mSession == null || (inSid != null && (!safeEquals(inS
.findById(eventTriggerId);

try {
File fileMedia = new File(eventTrigger.getMPath());
File fileMedia = new File(Objects.requireNonNull(eventTrigger.getMPath()));
FileInputStream fis = new FileInputStream(fileMedia);
return newChunkedResponse(Response.Status.OK, getMimeType(eventTrigger), fis);

}
catch (IOException ioe)
{
Log.e(TAG,"unable to return media file",ioe);
} catch (NullPointerException npe) {
Log.e(TAG,"unable to return media file", npe);
}
}
else if (uri.getPath().startsWith("/feed"))
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/havenapp/main/ui/EventAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.havenapp.main.ui;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -9,6 +9,7 @@

import org.havenapp.main.R;
import org.havenapp.main.model.Event;
import org.havenapp.main.resources.IResourceManager;

import java.util.List;

Expand All @@ -18,31 +19,31 @@

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventVH> {

private Context context;
private List<Event> events;
private IResourceManager resourceManager;

private OnItemClickListener clickListener;

public EventAdapter(Context context, List<Event> events) {
this.context = context;
public EventAdapter(List<Event> events, @NonNull IResourceManager resourceManager) {
this.events = events;

this.resourceManager = resourceManager;
}


@Override
public EventVH onCreateViewHolder(ViewGroup parent, int viewType) {
public EventVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_event, parent, false);
return new EventVH(view);
}

@Override
public void onBindViewHolder(EventVH holder, int position) {
public void onBindViewHolder(@NonNull EventVH holder, int position) {

Event event = events.get(position);

String title = event.getMStartTime().toLocaleString();
String desc = event.getEventTriggers().size() + " " + context.getString(R.string.detection_events);
String desc = event.getEventTriggers().size() + " " +
resourceManager.getString(R.string.detection_events);

holder.title.setText(title);
holder.note.setText(desc);
Expand Down Expand Up @@ -80,4 +81,7 @@ public void SetOnItemClickListener(final OnItemClickListener itemClickListener)
this.clickListener = itemClickListener;
}

public void setEvents(List<Event> events) {
this.events = events;
}
}

0 comments on commit 8161171

Please sign in to comment.