Skip to content

Commit

Permalink
feat(categories): MVC to categories
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Del Pino <idelpino@teclib.com>
  • Loading branch information
Ivan Del Pino authored and ajsb85 committed Dec 11, 2018
1 parent f358e6a commit 6b73ab0
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 215 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright Teclib. All rights reserved.
*
* Flyve MDM is a mobile device management software.
*
* Flyve MDM is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* Flyve MDM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ------------------------------------------------------------------------------
* @author Ivan Del Pino
* @copyright Copyright Teclib. All rights reserved.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/flyve-mdm/android-inventory-agent
* @link https://flyve-mdm.com
* ------------------------------------------------------------------------------
*/

package org.flyve.inventory.agent.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import org.flyve.inventory.agent.R;
import org.flyve.inventory.agent.ui.ActivityDetailServer;

import java.util.ArrayList;


public class CategoriesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private ArrayList<String> data;
private Activity activity;

public CategoriesAdapter(ArrayList<String> data, Activity activity) {
this.data = data;
this.activity = activity;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
int resource = R.layout.list_item_categories;
View v = LayoutInflater.from(viewGroup.getContext()).inflate(resource, viewGroup, false);
return new DataViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((DataViewHolder) holder).bindData(data.get(position), position);
}

public class DataViewHolder extends RecyclerView.ViewHolder {
TextView title;
Button showCategory;
View viewSeparatorBottom;

DataViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
showCategory = itemView.findViewById(R.id.showCategory);
viewSeparatorBottom = itemView.findViewById(R.id.viewSeparatorBottom);
}

void bindData(final String model, int position) {
if (position % 2 == 1) {
itemView.setBackgroundColor(activity.getResources().getColor(R.color.white));
} else {
itemView.setBackgroundColor(activity.getResources().getColor(R.color.grayDarkList));
}

title.setText(model);

if ((data.size() -1) == position) {
viewSeparatorBottom.setVisibility(View.VISIBLE);
}

showCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ActivityDetailServer.class);
intent.putExtra("serverName", model);
activity.startActivity(intent);
}
});
}
}

@Override
public int getItemCount() {
return data.size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,25 @@

import android.content.Context;

import org.flyve.inventory.agent.schema.ServerSchema;

import java.util.ArrayList;

public interface Categories {

interface View {
void showError(String message);
void successful(String message);
void modelServer(ServerSchema model);
void showCategories(ArrayList<String> model);
}

interface Presenter {
// Views
void showError(String message);
void successful(String message);
void modelServer(ServerSchema model);
void showCategory(ArrayList<String> model);

// Models
void saveServer(ArrayList<String> message, Context applicationContext);
void deleteServer(String serverName, Context applicationContext);
void updateServer(ArrayList<String> serverInfo, String serverName, Context applicationContext);
void loadServer(String serverName, Context applicationContext);
void loadCategory(Context applicationContext);
}

interface Model {
void saveServer(ArrayList<String> message, Context applicationContext);
void deleteServer(String serverName, Context applicationContext);
void updateServer(ArrayList<String> serverInfo, String serverName, Context applicationContext);
void loadServer(String serverName, Context applicationContext);
void loadCategory(Context applicationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@

import android.content.Context;

import org.flyve.inventory.agent.schema.ServerSchema;
import org.flyve.inventory.InventoryTask;
import org.flyve.inventory.agent.utils.FlyveLog;
import org.flyve.inventory.agent.utils.LocalPreferences;
import org.json.JSONException;
import org.json.JSONObject;
import org.flyve.inventory.agent.utils.Helpers;
import org.flyve.inventory.agent.utils.Utils;

import java.util.ArrayList;

Expand All @@ -42,94 +41,24 @@ public class CategoriesModel implements Categories.Model {
}

@Override
public void saveServer(ArrayList<String> modelServer, Context context) {
LocalPreferences preferences = new LocalPreferences(context);
if (modelServer.size() >= 4) {
if (!"".equals(modelServer.get(0))) {
JSONObject jo = new JSONObject();
try {
jo.put("address", modelServer.get(0));
jo.put("tag", modelServer.get(1));
jo.put("login", modelServer.get(2));
jo.put("pass", modelServer.get(3));
ArrayList<String> serverArray = preferences.loadServerArray();
serverArray.add(modelServer.get(0));
preferences.saveServerArray(serverArray);
} catch (JSONException e) {
FlyveLog.e(e.getMessage());
presenter.showError("Error");
public void loadCategory(Context context) {
try {
final InventoryTask inventoryTask = new InventoryTask(context, Helpers.getAgentDescription(context), true);
inventoryTask.getJSON(new InventoryTask.OnTaskCompleted() {
@Override
public void onTaskSuccess(String s) {
ArrayList<String> model = Utils.loadJsonHeader(s);
presenter.showCategory(model);
}
preferences.saveJSONObject(modelServer.get(0), jo);
presenter.successful("Successful");
} else {
presenter.showError("Missing server");
}
} else {
presenter.showError("Error");
}
}

@Override
public void updateServer(ArrayList<String> modelServer, String serverName, Context context) {
LocalPreferences preferences = new LocalPreferences(context);
if (modelServer.size() >= 4) {
if (!"".equals(modelServer.get(0))) {
JSONObject jo = new JSONObject();
try {
jo.put("address", modelServer.get(0));
jo.put("tag", modelServer.get(1));
jo.put("login", modelServer.get(2));
jo.put("pass", modelServer.get(3));
ArrayList<String> serverArray = preferences.loadServerArray();
for (int i = 0; i < serverArray.size(); i++) {
if (serverArray.get(i).equals(serverName)) {
serverArray.set(i, modelServer.get(0));
preferences.saveServerArray(serverArray);
}
}
} catch (JSONException e) {
FlyveLog.e(e.getMessage());
presenter.showError("Error");
@Override
public void onTaskError(Throwable throwable) {
presenter.showError(throwable.getMessage());
}
preferences.deletePreferences(serverName);
preferences.saveJSONObject(modelServer.get(0), jo);
presenter.successful("Successful");
} else {
presenter.showError("Missing Server");
}
} else {
presenter.showError("Error");
}
}

@Override
public void loadServer(String serverName, Context context) {
LocalPreferences preferences = new LocalPreferences(context);
try {
JSONObject jo = preferences.loadJSONObject(serverName);
ServerSchema serverSchema = new ServerSchema();
serverSchema.setAddress(jo.getString("address"));
serverSchema.setTag(jo.getString("tag"));
serverSchema.setLogin(jo.getString("login"));
serverSchema.setPass(jo.getString("pass"));
presenter.modelServer(serverSchema);
} catch (JSONException e) {
});
} catch (Exception e) {
FlyveLog.e(e.getMessage());
presenter.showError("Error");
}
}

@Override
public void deleteServer(String serverName, Context context) {
if (serverName != null && !"".equals(serverName)) {
LocalPreferences preferences = new LocalPreferences(context);
preferences.deletePreferences(serverName);
ArrayList<String> serverArray = preferences.loadServerArray();
serverArray.remove(serverName);
preferences.saveServerArray(serverArray);
presenter.successful("Successful Delete");
} else {
presenter.showError("Error");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import android.content.Context;

import org.flyve.inventory.agent.schema.ServerSchema;

import java.util.ArrayList;

public class CategoriesPresenter implements Categories.Presenter {
Expand All @@ -47,37 +45,17 @@ public void showError(String message) {
}

@Override
public void successful(String message) {
public void showCategory(ArrayList<String> model) {
if (view != null) {
view.successful(message);
view.showCategories(model);
}
}

@Override
public void modelServer(ServerSchema model) {
if (view != null) {
view.modelServer(model);
public void loadCategory(Context applicationContext) {
if (model != null) {
model.loadCategory(applicationContext);
}
}

@Override
public void saveServer(ArrayList<String> message, Context applicationContext) {
model.saveServer(message, applicationContext);
}

@Override
public void deleteServer(String serverName, Context applicationContext) {
model.deleteServer(serverName, applicationContext);
}

@Override
public void updateServer(ArrayList<String> serverInfo, String serverName, Context applicationContext) {
model.updateServer(serverInfo, serverName, applicationContext);
}

@Override
public void loadServer(String serverName, Context applicationContext) {
model.loadServer(serverName, applicationContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@

import org.flyve.inventory.InventoryTask;
import org.flyve.inventory.agent.R;
import org.flyve.inventory.agent.utils.FlyveLog;
import org.flyve.inventory.agent.utils.Helpers;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import org.flyve.inventory.agent.utils.Utils;

public class ReportModel implements Report.Model {

private Report.Presenter presenter;

public ReportModel(Report.Presenter presenter) {
ReportModel(Report.Presenter presenter) {
this.presenter = presenter;
}

Expand All @@ -51,7 +46,7 @@ public void generateReport(final Activity activity) {
inventoryTask.getJSON(new InventoryTask.OnTaskCompleted() {
@Override
public void onTaskSuccess(String s) {
presenter.sendInventory(s, load(s));
presenter.sendInventory(s, Utils.loadJsonHeader(s));
inventoryTask.getXMLSyn();
}

Expand All @@ -62,32 +57,6 @@ public void onTaskError(Throwable throwable) {
});
}

private ArrayList<String> load(String jsonStr) {
ArrayList<String> data = new ArrayList<>();

try {
JSONObject json = new JSONObject(jsonStr);
JSONObject jsonrequest = json.getJSONObject("request");
JSONObject jsonContent = jsonrequest.getJSONObject("content");

Iterator<?> keys = jsonContent.keys();

while( keys.hasNext() ) {
String key = (String)keys.next();

if ( jsonContent.get(key) instanceof JSONArray) {
// add header
data.add(key.toUpperCase());
}
}
return data;
} catch (Exception ex) {
presenter.showError(ex.getMessage());
FlyveLog.e(ex.getMessage());
}
return data;
}

public void showDialogShare(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.dialog_share_title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void onCreate() {
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
intent = new Intent(TIMER_RECEIVER);
startForeground(1, new Notification());
}

@RequiresApi(api = Build.VERSION_CODES.O)
Expand Down
Loading

0 comments on commit 6b73ab0

Please sign in to comment.